Skip to content
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

Merged
merged 9 commits into from Mar 8, 2022
Merged

Conversation

JoshuaBatty
Copy link
Member

@JoshuaBatty JoshuaBatty commented Feb 24, 2022

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, and wallet. 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 the counter example we might also want more complex examples like Uniswap which is why I'm including the sway-swap example for now. I can imagine a Token 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.

Create a new Forc project

USAGE:
    forc init [OPTIONS] <project-name>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -t, --template <template>    Initialize a new project from a template.
                                 
                                 Option 1: Create a new project using supported examples from FuelLabs
                                 example templates: 
                                  - counter
                                  - wallet
                                  - sway-swap

ARGS:
    <project-name>    The name of your project

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 an authors field.

I'll also add some documentation to the sway book outlining this feature once this PR finally lands.

@JoshuaBatty JoshuaBatty self-assigned this Feb 24, 2022
@JoshuaBatty JoshuaBatty linked an issue Feb 24, 2022 that may be closed by this pull request
@JoshuaBatty JoshuaBatty marked this pull request as draft February 24, 2022 01:52
@JoshuaBatty JoshuaBatty added enhancement New feature or request forc labels Feb 24, 2022
Copy link
Contributor

@adlerjohn adlerjohn left a 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?

@JoshuaBatty
Copy link
Member Author

Can you rebase this branch against the branch of #822, so we can easily see the diff?

I've removed the changes staged in #822 so you can more easily read the diff now.

@JoshuaBatty JoshuaBatty force-pushed the josh/forc_template_init branch 3 times, most recently from a823946 to 95375e3 Compare March 2, 2022 01:20
@JoshuaBatty JoshuaBatty marked this pull request as ready for review March 2, 2022 01:30
@JoshuaBatty
Copy link
Member Author

Ok, I think this is ready for review.

I've removed the sway-swap and wallet templates for now and have just left in the counter template. We can look to add more official templates such as token etc.. when we have them ready to go.

Copy link
Contributor

@adlerjohn adlerjohn left a 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?

@JoshuaBatty
Copy link
Member Author

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 cargo run --bin forc -- init --template /home/josh/rust/sway/forc/test_project my_sway_project

The counter template actually calls into the counter example hosted in the examples folder on the sway GitHub repo and pulls from there, so no chance of any conflicts. But I see your point, there probably isn't a need to support local paths. I'll remove that option now.

@adlerjohn
Copy link
Contributor

it's working from me on linux when doing cargo run --bin forc -- init --template /home/josh/rust/sway/forc/test_project my_sway_project

Oh, I was using a relative path. E.g. test_project or ./test_project. Having to put in an absolute path would be kind of a PITA, but you're removing this feature anyways so all's well that ends well.

@@ -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)?;
Copy link
Contributor

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 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep sounds good!

Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

@adlerjohn adlerjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge conflicts

Copy link
Contributor

@sezna sezna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoshuaBatty
Copy link
Member Author

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 counter. We can add more in as they become available and we can discuss later if we want to more generally support git as a source of templates in a future issue.

sezna
sezna previously approved these changes Mar 8, 2022
mitchmindtree
mitchmindtree previously approved these changes Mar 8, 2022
@@ -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.
Copy link
Contributor

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.

Copy link
Member Author

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)?;
Copy link
Contributor

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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice spot!

@JoshuaBatty JoshuaBatty dismissed stale reviews from mitchmindtree and sezna via 288dd56 March 8, 2022 00:30
@JoshuaBatty JoshuaBatty merged commit 15f20d5 into master Mar 8, 2022
@JoshuaBatty JoshuaBatty deleted the josh/forc_template_init branch March 8, 2022 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request forc
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Forc init with example
4 participants