Skip to content

Commit

Permalink
Turned nargo_cli into a lib+bin crate and made all commands public
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Oct 9, 2024
1 parent f6a7306 commit af19dfc
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 107 deletions.
6 changes: 4 additions & 2 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ workspace = true
[[bin]]
name = "nargo"
path = "src/main.rs"
required-features = ["build-binary"]

[build-dependencies]
build-data.workspace = true
Expand Down Expand Up @@ -64,8 +65,8 @@ notify-debouncer-full = "0.3.1"
termion = "3.0.0"

# Logs
tracing-subscriber.workspace = true
tracing-appender = "0.2.3"
tracing-appender = { version = "0.2.3", optional = true }
tracing-subscriber = { workspace = true, optional = true }

[target.'cfg(not(unix))'.dependencies]
tokio-util = { version = "0.7.8", features = ["compat"] }
Expand Down Expand Up @@ -97,3 +98,4 @@ harness = false

[features]
codegen-docs = ["dep:clap-markdown"]
build-binary = ["dep:tracing-subscriber", "dep:tracing-appender"]
10 changes: 5 additions & 5 deletions tooling/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ use super::NargoConfig;
/// Checks the constraint system for errors
#[derive(Debug, Clone, Args)]
#[clap(visible_alias = "c")]
pub(crate) struct CheckCommand {
pub struct CheckCommand {
/// The name of the package to check
#[clap(long, conflicts_with = "workspace")]
package: Option<CrateName>,
pub package: Option<CrateName>,

/// Check all packages in the workspace
#[clap(long, conflicts_with = "package")]
workspace: bool,
pub workspace: bool,

/// Force overwrite of existing files
#[clap(long = "overwrite")]
allow_overwrite: bool,
pub allow_overwrite: bool,

#[clap(flatten)]
compile_options: CompileOptions,
pub compile_options: CompileOptions,
}

pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> {
Expand Down
10 changes: 5 additions & 5 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ use rayon::prelude::*;

/// Compile the program and its secret execution trace into ACIR format
#[derive(Debug, Clone, Args)]
pub(crate) struct CompileCommand {
pub struct CompileCommand {
/// The name of the package to compile
#[clap(long, conflicts_with = "workspace")]
package: Option<CrateName>,
pub package: Option<CrateName>,

/// Compile all packages in the workspace.
#[clap(long, conflicts_with = "package")]
workspace: bool,
pub workspace: bool,

#[clap(flatten)]
compile_options: CompileOptions,
pub compile_options: CompileOptions,

/// Watch workspace and recompile on changes.
#[clap(long, hide = true)]
watch: bool,
pub watch: bool,
}

pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> {
Expand Down
16 changes: 8 additions & 8 deletions tooling/nargo_cli/src/cli/dap_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ use super::NargoConfig;
use noir_debugger::errors::{DapError, LoadError};

#[derive(Debug, Clone, Args)]
pub(crate) struct DapCommand {
pub struct DapCommand {
/// Override the expression width requested by the backend.
#[arg(long, value_parser = parse_expression_width, default_value = "4")]
expression_width: ExpressionWidth,
pub expression_width: ExpressionWidth,

#[clap(long)]
preflight_check: bool,
pub preflight_check: bool,

#[clap(long)]
preflight_project_folder: Option<String>,
pub preflight_project_folder: Option<String>,

#[clap(long)]
preflight_package: Option<String>,
pub preflight_package: Option<String>,

#[clap(long)]
preflight_prover_name: Option<String>,
pub preflight_prover_name: Option<String>,

#[clap(long)]
preflight_generate_acir: bool,
pub preflight_generate_acir: bool,

#[clap(long)]
preflight_skip_instrumentation: bool,
pub preflight_skip_instrumentation: bool,
}

fn parse_expression_width(input: &str) -> Result<ExpressionWidth, std::io::Error> {
Expand Down
14 changes: 7 additions & 7 deletions tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ use crate::errors::CliError;

/// Executes a circuit in debug mode
#[derive(Debug, Clone, Args)]
pub(crate) struct DebugCommand {
pub struct DebugCommand {
/// Write the execution witness to named file
witness_name: Option<String>,
pub witness_name: Option<String>,

/// The name of the toml file which contains the inputs for the prover
#[clap(long, short, default_value = PROVER_INPUT_FILE)]
prover_name: String,
pub prover_name: String,

/// The name of the package to execute
#[clap(long)]
package: Option<CrateName>,
pub package: Option<CrateName>,

#[clap(flatten)]
compile_options: CompileOptions,
pub compile_options: CompileOptions,

/// Force ACIR output (disabling instrumentation)
#[clap(long)]
acir_mode: bool,
pub acir_mode: bool,

/// Disable vars debug instrumentation (enabled by default)
#[clap(long)]
skip_instrumentation: Option<bool>,
pub skip_instrumentation: Option<bool>,
}

pub(crate) fn run(args: DebugCommand, config: NargoConfig) -> Result<(), CliError> {
Expand Down
14 changes: 7 additions & 7 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ use crate::errors::CliError;
/// Executes a circuit to calculate its return value
#[derive(Debug, Clone, Args)]
#[clap(visible_alias = "e")]
pub(crate) struct ExecuteCommand {
pub struct ExecuteCommand {
/// Write the execution witness to named file
///
/// Defaults to the name of the package being executed.
witness_name: Option<String>,
pub witness_name: Option<String>,

/// The name of the toml file which contains the inputs for the prover
#[clap(long, short, default_value = PROVER_INPUT_FILE)]
prover_name: String,
pub prover_name: String,

/// The name of the package to execute
#[clap(long, conflicts_with = "workspace")]
package: Option<CrateName>,
pub package: Option<CrateName>,

/// Execute all packages in the workspace
#[clap(long, conflicts_with = "package")]
workspace: bool,
pub workspace: bool,

#[clap(flatten)]
compile_options: CompileOptions,
pub compile_options: CompileOptions,

/// JSON RPC url to solve oracle calls
#[clap(long)]
oracle_resolver: Option<String>,
pub oracle_resolver: Option<String>,
}

pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> {
Expand Down
8 changes: 4 additions & 4 deletions tooling/nargo_cli/src/cli/export_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ use super::NargoConfig;

/// Exports functions marked with #[export] attribute
#[derive(Debug, Clone, Args)]
pub(crate) struct ExportCommand {
pub struct ExportCommand {
/// The name of the package to compile
#[clap(long, conflicts_with = "workspace")]
package: Option<CrateName>,
pub package: Option<CrateName>,

/// Compile all packages in the workspace
#[clap(long, conflicts_with = "package")]
workspace: bool,
pub workspace: bool,

#[clap(flatten)]
compile_options: CompileOptions,
pub compile_options: CompileOptions,
}

pub(crate) fn run(args: ExportCommand, config: NargoConfig) -> Result<(), CliError> {
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/fmt_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use super::NargoConfig;

/// Format the Noir files in a workspace
#[derive(Debug, Clone, Args)]
pub(crate) struct FormatCommand {
pub struct FormatCommand {
/// Run noirfmt in check mode
#[arg(long)]
check: bool,
pub check: bool,
}

pub(crate) fn run(args: FormatCommand, config: NargoConfig) -> Result<(), CliError> {
Expand Down
12 changes: 6 additions & 6 deletions tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ use super::{
/// 2. Counts the final number gates in the circuit used by a backend
#[derive(Debug, Clone, Args)]
#[clap(visible_alias = "i")]
pub(crate) struct InfoCommand {
pub struct InfoCommand {
/// The name of the package to detail
#[clap(long, conflicts_with = "workspace")]
package: Option<CrateName>,
pub package: Option<CrateName>,

/// Detail all packages in the workspace
#[clap(long, conflicts_with = "package")]
workspace: bool,
pub workspace: bool,

/// Output a JSON formatted report. Changes to this format are not currently considered breaking.
#[clap(long, hide = true)]
json: bool,
pub json: bool,

#[clap(long, hide = true)]
profile_info: bool,
pub profile_info: bool,

#[clap(flatten)]
compile_options: CompileOptions,
pub compile_options: CompileOptions,
}

pub(crate) fn run(args: InfoCommand, config: NargoConfig) -> Result<(), CliError> {
Expand Down
10 changes: 5 additions & 5 deletions tooling/nargo_cli/src/cli/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ use std::path::PathBuf;

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

/// Use a library template
#[arg(long, conflicts_with = "bin", conflicts_with = "contract")]
pub(crate) lib: bool,
pub lib: bool,

/// Use a binary template [default]
#[arg(long, conflicts_with = "lib", conflicts_with = "contract")]
pub(crate) bin: bool,
pub bin: bool,

/// Use a contract template
#[arg(long, conflicts_with = "lib", conflicts_with = "bin")]
pub(crate) contract: bool,
pub contract: bool,
}

const BIN_EXAMPLE: &str = include_str!("./noir_template_files/binary.nr");
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_cli/src/cli/lsp_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::errors::CliError;
///
/// VS Code Noir Language Support: https://marketplace.visualstudio.com/items?itemName=noir-lang.vscode-noir
#[derive(Debug, Clone, Args)]
pub(crate) struct LspCommand;
pub struct LspCommand;

pub(crate) fn run(_args: LspCommand, _config: NargoConfig) -> Result<(), CliError> {
use tokio::runtime::Builder;
Expand Down
60 changes: 32 additions & 28 deletions tooling/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ use color_eyre::eyre;

mod fs;

mod check_cmd;
mod compile_cmd;
mod dap_cmd;
mod debug_cmd;
mod execute_cmd;
mod export_cmd;
mod fmt_cmd;
mod info_cmd;
mod init_cmd;
mod lsp_cmd;
mod new_cmd;
mod test_cmd;
pub mod check_cmd;
pub mod compile_cmd;
pub mod dap_cmd;
pub mod debug_cmd;
pub mod execute_cmd;
pub mod export_cmd;
pub mod fmt_cmd;
pub mod info_cmd;
pub mod init_cmd;
pub mod lsp_cmd;
pub mod new_cmd;
pub mod test_cmd;

const GIT_HASH: &str = env!("GIT_COMMIT");
const IS_DIRTY: &str = env!("GIT_DIRTY");
Expand All @@ -35,25 +35,23 @@ static VERSION_STRING: &str = formatcp!(

#[derive(Parser, Debug)]
#[command(name="nargo", author, version=VERSION_STRING, about, long_about = None)]
struct NargoCli {
pub struct NargoCli {
#[command(subcommand)]
command: NargoCommand,
pub command: NargoCommand,

#[clap(flatten)]
config: NargoConfig,
pub config: NargoConfig,
}

#[non_exhaustive]
#[derive(Args, Clone, Debug)]
pub(crate) struct NargoConfig {
pub struct NargoConfig {
// REMINDER: Also change this flag in the LSP test lens if renamed
#[arg(long, hide = true, global = true, default_value = "./")]
program_dir: PathBuf,
pub program_dir: PathBuf,
}

#[non_exhaustive]
#[derive(Subcommand, Clone, Debug)]
enum NargoCommand {
pub enum NargoCommand {
Check(check_cmd::CheckCommand),
Fmt(fmt_cmd::FormatCommand),
#[command(alias = "build")]
Expand All @@ -71,9 +69,22 @@ enum NargoCommand {
Dap(dap_cmd::DapCommand),
}

/// Parse the command line arguments and execute the command.
#[cfg(not(feature = "codegen-docs"))]
pub fn start_cli() -> eyre::Result<()> {
run_cmd(NargoCli::parse())
}

#[cfg(feature = "codegen-docs")]
pub(crate) fn start_cli() -> eyre::Result<()> {
let NargoCli { command, mut config } = NargoCli::parse();
let markdown: String = clap_markdown::help_markdown::<NargoCli>();
println!("{markdown}");
Ok(())
}

/// Execute the CLI command.
pub fn run_cmd(cmd: NargoCli) -> eyre::Result<()> {
let NargoCli { command, mut config } = cmd;

// If the provided `program_dir` is relative, make it absolute by joining it to the current directory.
if !config.program_dir.is_absolute() {
Expand Down Expand Up @@ -105,10 +116,3 @@ pub(crate) fn start_cli() -> eyre::Result<()> {

Ok(())
}

#[cfg(feature = "codegen-docs")]
pub(crate) fn start_cli() -> eyre::Result<()> {
let markdown: String = clap_markdown::help_markdown::<NargoCli>();
println!("{markdown}");
Ok(())
}
Loading

0 comments on commit af19dfc

Please sign in to comment.