-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forc init project from template #836
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rebase this branch against the branch of #822, so we can easily see the diff?
9cae1e0
to
bc6af45
Compare
a823946
to
95375e3
Compare
Ok, I think this is ready for review. I've removed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to run this with a local path, I get
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "Not a valid local path"', forc/src/ops/forc_init.rs:68:30
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
I'm actually not sure there's much need for a local path anyways? And how would you distinguish between a local counter
directory and the counter
example?
Hmm not sure why the path isn't working, it's working from me on linux when doing The |
Oh, I was using a relative path. E.g. |
@@ -41,3 +106,212 @@ pub(crate) fn init_new_project(project_name: String) -> Result<(), Box<dyn std:: | |||
|
|||
Ok(()) | |||
} | |||
|
|||
pub(crate) fn init_from_git_template(project_name: String, example_url: &Url) -> Result<()> { | |||
let git = parse_github_link(example_url)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh we'll probably want to switch to using git2
for this now that #825 is in! I can help with this Monday if you're up for it @JoshuaBatty ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep sounds good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we will wait for that to take this one in then? Going to request changes to prevent it from being taken in by somebody
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll still want to change this to use git2
, as currently forc
has quite a few deps that are related purely to this and forc_explorer
's github fetching, but I can address this in a future PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge conflicts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
01e3e3f
to
918a85a
Compare
b884cf5
to
017fd01
Compare
I've removed the ability for a GitHub URL supplied by the user to be a template source. As it stands, only supported templates provided by FuelLabs can be used, as this stage, just |
forc/src/cli/commands/init.rs
Outdated
@@ -2,13 +2,22 @@ use crate::ops::forc_init; | |||
use anyhow::Result; | |||
use clap::Parser; | |||
|
|||
static TEMPLATE_HELP: &str = r#"Initialize a new project from a template. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should maybe be const
(see some discussion on const vs static here), but it's not important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, good spot i'll add that now.
@@ -41,3 +106,212 @@ pub(crate) fn init_new_project(project_name: String) -> Result<(), Box<dyn std:: | |||
|
|||
Ok(()) | |||
} | |||
|
|||
pub(crate) fn init_from_git_template(project_name: String, example_url: &Url) -> Result<()> { | |||
let git = parse_github_link(example_url)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll still want to change this to use git2
, as currently forc
has quite a few deps that are related purely to this and forc_explorer
's github fetching, but I can address this in a future PR.
@@ -494,23 +494,23 @@ fn tmp_git_repo_dir(name: &str, repo: &Url) -> PathBuf { | |||
} | |||
|
|||
/// Clones the package git repo into a temporary directory and applies the given function. | |||
fn with_tmp_git_repo<F, O>(name: &str, source: &SourceGit, f: F) -> Result<O> | |||
fn with_tmp_git_repo<F, O>(name: &str, source: &Url, f: F) -> Result<O> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice spot!
This PR allows a user to create a new
sway
project from an existing template project.There are three ways a user can create a new project from,
1: FuelLabs example
Currently, I've included 3 examples from FuelLabs that can be used as a starting point for a new
sway
project,counter
sway-swap
, andwallet
. We might not actually want these to be the 3 examples but I'm including them so we can get an idea for how this option might work. @SilentCicero suggested in #530 that along with thecounter
example we might also want more complex examples likeUniswap
which is why I'm including thesway-swap
example for now. I can imagine aToken
template would also be useful as well.Creating a new project using the
counter
example as a template currently looks like:forc init --template counter my_sway_project
2: GitHub URL
This allows you to paste a GitHub URL of a
sway
project. It works with links where the project is at either the root level or a subdirectory within the repository. For example, a project within a subdirectory looks like:forc init --template https://github.com/JoshuaBatty/fuel_test_project/tree/main/hello_world_test my_sway_project
3: Local Path
Finally, if you would like to use a
sway
project on your local machine as a template, you can pass in the absolute path into the--template
argument, for example:forc init --template /home/josh/rust/templates/token_example my_sway_project
CLI Help Output
Running
forc init --help
now displays the following.Currently, this PR includes the commits of #822. We will need to land that PR first before this can be merged as the
Forc.toml
parsing expects that there is anauthors
field.I'll also add some documentation to the
sway
book outlining this feature once this PR finally lands.