-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
nargo init
command (#1859)
- Loading branch information
1 parent
befe549
commit 2d87c87
Showing
3 changed files
with
65 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::{ | ||
constants::{PKG_FILE, SRC_DIR}, | ||
errors::CliError, | ||
}; | ||
|
||
use super::fs::{create_named_dir, write_to_file}; | ||
use super::{NargoConfig, CARGO_PKG_VERSION}; | ||
use acvm::Backend; | ||
use clap::Args; | ||
use const_format::formatcp; | ||
use std::path::PathBuf; | ||
|
||
/// Create a Noir project in the current directory. | ||
#[derive(Debug, Clone, Args)] | ||
pub(crate) struct InitCommand; | ||
|
||
const SETTINGS: &str = formatcp!( | ||
r#"[package] | ||
authors = [""] | ||
compiler_version = "{CARGO_PKG_VERSION}" | ||
[dependencies]"#, | ||
); | ||
|
||
const EXAMPLE: &str = r#"fn main(x : Field, y : pub Field) { | ||
assert(x != y); | ||
} | ||
#[test] | ||
fn test_main() { | ||
main(1, 2); | ||
// Uncomment to make test fail | ||
// main(1, 1); | ||
} | ||
"#; | ||
|
||
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, | ||
config: NargoConfig, | ||
) -> Result<(), CliError<B>> { | ||
initialize_project(config.program_dir); | ||
Ok(()) | ||
} | ||
|
||
/// Initializes a new Noir project in `package_dir`. | ||
pub(crate) fn initialize_project(package_dir: PathBuf) { | ||
let src_dir = package_dir.join(SRC_DIR); | ||
create_named_dir(&src_dir, "src"); | ||
|
||
write_to_file(SETTINGS.as_bytes(), &package_dir.join(PKG_FILE)); | ||
write_to_file(EXAMPLE.as_bytes(), &src_dir.join("main.nr")); | ||
println!("Project successfully created! Binary located at {}", package_dir.display()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters