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

feat!: Allow specifying new package name with --name flag #2144

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (Defaults to current directory name)
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
#[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 (Defaults to 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(())
}