Skip to content

Commit

Permalink
Add forge template to scarb new and scarb init under separate flag (
Browse files Browse the repository at this point in the history
#1308)

Closes  #1208

- Added `--snforge` flag that enables forge template for
   - `new` command
   - `init` command
   
  On forge side foundry-rs/starknet-foundry#2113
  • Loading branch information
Draggu authored May 21, 2024
1 parent 2d884e9 commit a5e4c11
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions scarb/src/bin/scarb/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ pub struct InitArgs {
/// Do not initialize a new Git repository.
#[arg(long)]
pub no_vcs: bool,

/// Use a Starknet Foundry Forge template.
#[arg(long)]
pub snforge: bool,
}

/// Arguments accepted by the `metadata` command.
Expand Down
1 change: 1 addition & 0 deletions scarb/src/bin/scarb/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn run(args: InitArgs, config: &Config) -> Result<()> {
} else {
VersionControl::Git
},
snforge: args.snforge,
},
config,
)?;
Expand Down
1 change: 1 addition & 0 deletions scarb/src/bin/scarb/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn run(args: NewArgs, config: &Config) -> Result<()> {
} else {
VersionControl::Git
},
snforge: args.init.snforge,
},
config,
)?;
Expand Down
28 changes: 27 additions & 1 deletion scarb/src/ops/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use itertools::Itertools;
use crate::core::{edition_variant, Config, PackageName};
use crate::internal::fsx;
use crate::internal::restricted_names;
use crate::subcommands::get_env_vars;
use crate::{ops, DEFAULT_SOURCE_PATH, DEFAULT_TARGET_DIR_NAME, MANIFEST_FILE_NAME};
use std::process::{Command, Stdio};

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum VersionControl {
Expand All @@ -20,6 +22,7 @@ pub struct InitOptions {
pub path: Utf8PathBuf,
pub name: Option<PackageName>,
pub vcs: VersionControl,
pub snforge: bool,
}

#[derive(Debug)]
Expand All @@ -46,6 +49,7 @@ pub fn new_package(opts: InitOptions, config: &Config) -> Result<NewResult> {
path: opts.path.clone(),
name: name.clone(),
version_control: opts.vcs,
snforge: opts.snforge,
},
config,
)
Expand All @@ -67,6 +71,7 @@ pub fn init_package(opts: InitOptions, config: &Config) -> Result<NewResult> {
path: opts.path,
name: name.clone(),
version_control: opts.vcs,
snforge: opts.snforge,
},
config,
)
Expand Down Expand Up @@ -113,20 +118,22 @@ struct MkOpts {
path: Utf8PathBuf,
name: PackageName,
version_control: VersionControl,
snforge: bool,
}

fn mk(
MkOpts {
path,
name,
version_control,
snforge,
}: MkOpts,
config: &Config,
) -> Result<()> {
// Create project directory in case we are called from `new` op.
fsx::create_dir_all(&path)?;

let canonical_path = fsx::canonicalize_utf8(&path).unwrap_or(path);
let canonical_path = fsx::canonicalize_utf8(&path).unwrap_or(path.clone());

init_vcs(&canonical_path, version_control)?;
write_vcs_ignore(&canonical_path, config, version_control)?;
Expand Down Expand Up @@ -193,6 +200,25 @@ fn mk(
"#})
}

if snforge {
init_snforge(name, path, config)?;
}

Ok(())
}

fn init_snforge(name: PackageName, target_dir: Utf8PathBuf, config: &Config) -> Result<()> {
let mut process = Command::new("snforge")
.arg("init")
.arg(name.as_str())
.current_dir(fsx::canonicalize_utf8(&target_dir)?)
.envs(get_env_vars(config, Some(target_dir))?)
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.spawn()?;

process.wait()?;

Ok(())
}

Expand Down

0 comments on commit a5e4c11

Please sign in to comment.