From ac00d0c0e92450aad66a995acd5059a410430625 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Mon, 12 Aug 2024 12:53:11 -0500 Subject: [PATCH] add init command --- check/src/main.rs | 11 ++++++++++- check/src/new.rs | 39 +++++++++++++++++++++++++++++++++++++++ main/src/main.rs | 7 ++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/check/src/main.rs b/check/src/main.rs index 9f8912b..f3c3c75 100644 --- a/check/src/main.rs +++ b/check/src/main.rs @@ -34,7 +34,7 @@ struct Opts { #[derive(Parser, Debug, Clone)] enum Apis { - /// Create a new Rust project. + /// Create a new Stylus project. New { /// Project name. name: PathBuf, @@ -42,6 +42,12 @@ enum Apis { #[arg(long)] minimal: bool, }, + /// Initializes a Stylus project in the current directory. + Init { + /// Create a minimal contract. + #[arg(long)] + minimal: bool, + }, /// Export a Solidity ABI. ExportAbi { /// The output file (defaults to stdout). @@ -296,6 +302,9 @@ async fn main_impl(args: Opts) -> Result<()> { Apis::New { name, minimal } => { run!(new::new(&name, minimal), "failed to open new project"); } + Apis::Init { minimal } => { + run!(new::init(minimal), "failed to initialize project"); + } Apis::ExportAbi { json, output } => { run!(export_abi::export_abi(output, json), "failed to export abi"); } diff --git a/check/src/new.rs b/check/src/new.rs index 668df01..213dffa 100644 --- a/check/src/new.rs +++ b/check/src/new.rs @@ -29,3 +29,42 @@ pub fn new(name: &Path, minimal: bool) -> Result<()> { println!("{GREY}new project at: {}", path.to_string_lossy().mint()); Ok(()) } + +pub fn init(minimal: bool) -> Result<()> { + let current_dir = current_dir().wrap_err("no current dir")?; + let repo = if minimal { + GITHUB_TEMPLATE_REPO_MINIMAL + } else { + GITHUB_TEMPLATE_REPO + }; + + let output = sys::new_command("git") + .arg("clone") + .arg("--depth") + .arg("1") + .arg("--no-checkout") + .arg(repo) + .arg(".") + .output() + .wrap_err("git clone failed")?; + + if !output.status.success() { + bail!("git clone command failed"); + } + + let output = sys::new_command("git") + .arg("checkout") + .arg(".") + .output() + .wrap_err("git checkout failed")?; + + if !output.status.success() { + bail!("git checkout command failed"); + } + + println!( + "{GREY}initialized project in: {}", + current_dir.to_string_lossy().mint() + ); + Ok(()) +} diff --git a/main/src/main.rs b/main/src/main.rs index bd01fc8..ab9a655 100644 --- a/main/src/main.rs +++ b/main/src/main.rs @@ -28,8 +28,11 @@ struct Opts { #[derive(Parser, Debug, Clone)] enum Subcommands { #[command(alias = "n")] - /// Create a new Rust project. + /// Create a new Stylus project. New, + #[command(alias = "i")] + /// Initializes a Stylus project in the current directory. + Init, #[command(alias = "x")] /// Export a Solidity ABI. ExportAbi, @@ -69,6 +72,7 @@ const COMMANDS: &[Binary] = &[ name: "cargo-stylus-check", apis: &[ "new", + "init", "activate", "export-abi", "cache", @@ -76,6 +80,7 @@ const COMMANDS: &[Binary] = &[ "deploy", "verify", "a", + "i", "n", "x", "c",