Skip to content

Commit

Permalink
feat!: Allow specifying new package name with --name flag (#2144)
Browse files Browse the repository at this point in the history
* feat: Allow specifying new package name with `--name` flag

* chore: update message for option default value

* chore: update to match format used in `nargo prove`
  • Loading branch information
TomAFrench authored Aug 4, 2023
1 parent ccba78e commit e932599
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
17 changes: 12 additions & 5 deletions crates/nargo_cli/src/cli/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use std::path::PathBuf;

/// Create a Noir project in the current directory.
#[derive(Debug, Clone, Args)]
pub(crate) struct InitCommand;
pub(crate) struct InitCommand {
/// Name of the package [default: current directory name]
#[clap(long)]
name: Option<String>,
}

const EXAMPLE: &str = r#"fn main(x : Field, y : pub Field) {
assert(x != y);
Expand All @@ -27,17 +31,20 @@ fn test_main() {
pub(crate) fn run<B: Backend>(
// Backend is currently unused, but we might want to use it to inform the "new" template in the future
_backend: &B,
_args: InitCommand,
args: InitCommand,
config: NargoConfig,
) -> Result<(), CliError<B>> {
initialize_project(config.program_dir);
let package_name = args
.name
.unwrap_or_else(|| config.program_dir.file_name().unwrap().to_str().unwrap().to_owned());

initialize_project(config.program_dir, &package_name);
Ok(())
}

/// Initializes a new Noir project in `package_dir`.
pub(crate) fn initialize_project(package_dir: PathBuf) {
pub(crate) fn initialize_project(package_dir: PathBuf, package_name: &str) {
// TODO: Should this reject if we have non-Unicode filepaths?
let package_name = package_dir.file_name().expect("Expected a filename").to_string_lossy();
let src_dir = package_dir.join(SRC_DIR);
create_named_dir(&src_dir, "src");

Expand Down
14 changes: 9 additions & 5 deletions crates/nargo_cli/src/cli/new_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use std::path::PathBuf;
/// Create a Noir project in a new directory.
#[derive(Debug, Clone, Args)]
pub(crate) struct NewCommand {
/// Name of the package
package_name: String,
/// The path to save the new project
path: Option<PathBuf>,
path: PathBuf,

/// Name of the package [default: package directory name]
#[clap(long)]
name: Option<String>,
}

pub(crate) fn run<B: Backend>(
Expand All @@ -20,12 +22,14 @@ pub(crate) fn run<B: Backend>(
args: NewCommand,
config: NargoConfig,
) -> Result<(), CliError<B>> {
let package_dir = config.program_dir.join(args.package_name);
let package_dir = config.program_dir.join(&args.path);

if package_dir.exists() {
return Err(CliError::DestinationAlreadyExists(package_dir));
}

initialize_project(package_dir);
let package_name =
args.name.unwrap_or_else(|| args.path.file_name().unwrap().to_str().unwrap().to_owned());
initialize_project(package_dir, &package_name);
Ok(())
}

0 comments on commit e932599

Please sign in to comment.