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: add nargo backend ls and nargo backend use command to switch between backends #2552

Merged
merged 7 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
32 changes: 21 additions & 11 deletions crates/acvm_backend_barretenberg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ mod cli;
mod proof_system;
mod smart_contract;

const BACKENDS_DIR: &str = ".nargo/backends";

pub fn backends_directory() -> PathBuf {
let home_directory = dirs::home_dir().unwrap();
home_directory.join(BACKENDS_DIR)
}

#[cfg(test)]
fn get_bb() -> Backend {
let bb = Backend::new();
let bb = Backend::default();
crate::assert_binary_exists(&bb);
bb
}
Expand All @@ -24,21 +31,24 @@ fn assert_binary_exists(backend: &Backend) -> PathBuf {
binary_path
}

#[derive(Debug, Default)]
pub struct Backend {}
#[derive(Debug)]
pub struct Backend {
name: String,
}

impl Default for Backend {
fn default() -> Self {
Self { name: "acvm-backend-barretenberg".to_string() }
}
}

impl Backend {
pub fn new() -> Backend {
Backend::default()
pub fn new(name: String) -> Backend {
Backend { name }
}

fn backend_directory(&self) -> PathBuf {
const BACKENDS_DIR: &str = ".nargo/backends";
const BACKEND_NAME: &str = "acvm-backend-barretenberg";

let home_directory = dirs::home_dir().unwrap();

home_directory.join(BACKENDS_DIR).join(BACKEND_NAME)
backends_directory().join(&self.name)
}

fn binary_path(&self) -> PathBuf {
Expand Down
23 changes: 23 additions & 0 deletions crates/nargo_cli/src/backends.rs
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
use std::path::PathBuf;

use acvm_backend_barretenberg::backends_directory;
pub(crate) use acvm_backend_barretenberg::Backend;

fn active_backend_file_path() -> PathBuf {
backends_directory().join(".selected_backend")
}

pub(crate) fn set_active_backend(backend_name: &str) {
std::fs::write(active_backend_file_path(), backend_name.as_bytes()).unwrap();
}

pub(crate) fn get_active_backend() -> String {
let active_backend_file = active_backend_file_path();

if !active_backend_file.is_file() {
let barretenberg = "acvm-backend-barretenberg";
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
set_active_backend(barretenberg);
return barretenberg.to_string();
}

std::fs::read_to_string(active_backend_file).unwrap()
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
}
32 changes: 32 additions & 0 deletions crates/nargo_cli/src/cli/backend_cmd/ls_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use acvm_backend_barretenberg::backends_directory;
use clap::Args;

use crate::errors::CliError;

/// Prints the list of available backends
#[derive(Debug, Clone, Args)]
pub(crate) struct LsCommand;

pub(crate) fn run(_args: LsCommand) -> Result<(), CliError> {
for backend in get_available_backends() {
println!("{}", backend);
}

Ok(())
}

pub(super) fn get_available_backends() -> Vec<String> {
let backend_directory_contents = std::fs::read_dir(backends_directory()).unwrap();

backend_directory_contents
.into_iter()
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.is_dir() {
path.file_name().map(|name| name.to_string_lossy().to_string())
} else {
None
}
})
.collect()
}
31 changes: 31 additions & 0 deletions crates/nargo_cli/src/cli/backend_cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use clap::{Args, Subcommand};

use crate::errors::CliError;

mod ls_cmd;
mod use_cmd;

#[non_exhaustive]
#[derive(Args, Clone, Debug)]
pub(crate) struct BackendCommand {
#[command(subcommand)]
command: BackendCommands,
}

#[non_exhaustive]
#[derive(Subcommand, Clone, Debug)]
pub(crate) enum BackendCommands {
Ls(ls_cmd::LsCommand),
Use(use_cmd::UseCommand),
}

pub(crate) fn run(cmd: BackendCommand) -> Result<(), CliError> {
let BackendCommand { command } = cmd;

match command {
BackendCommands::Ls(args) => ls_cmd::run(args),
BackendCommands::Use(args) => use_cmd::run(args),
}?;

Ok(())
}
21 changes: 21 additions & 0 deletions crates/nargo_cli/src/cli/backend_cmd/use_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use clap::Args;

use crate::{backends::set_active_backend, errors::CliError};

use super::ls_cmd::get_available_backends;

/// Select the currently active backend
#[derive(Debug, Clone, Args)]
pub(crate) struct UseCommand {
backend: String,
}

pub(crate) fn run(args: UseCommand) -> Result<(), CliError> {
let backends = get_available_backends();

assert!(backends.contains(&args.backend), "backend doesn't exist");

set_active_backend(&args.backend);

Ok(())
}
17 changes: 15 additions & 2 deletions crates/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use std::path::PathBuf;

use color_eyre::eyre;

use crate::backends::get_active_backend;

mod fs;

mod backend_cmd;
mod check_cmd;
mod codegen_verifier_cmd;
mod compile_cmd;
Expand Down Expand Up @@ -47,6 +50,8 @@ pub(crate) struct NargoConfig {
#[non_exhaustive]
#[derive(Subcommand, Clone, Debug)]
enum NargoCommand {
#[command(hide = true)] // Hidden while dynamic backends feature is being built out.
Backend(backend_cmd::BackendCommand),
Check(check_cmd::CheckCommand),
CodegenVerifier(codegen_verifier_cmd::CodegenVerifierCommand),
#[command(alias = "build")]
Expand All @@ -70,11 +75,18 @@ pub(crate) fn start_cli() -> eyre::Result<()> {
}

// Search through parent directories to find package root if necessary.
if !matches!(command, NargoCommand::New(_) | NargoCommand::Init(_) | NargoCommand::Lsp(_)) {
if !matches!(
command,
NargoCommand::New(_)
| NargoCommand::Init(_)
| NargoCommand::Lsp(_)
| NargoCommand::Backend(_)
) {
config.program_dir = find_package_root(&config.program_dir)?;
}

let backend = crate::backends::Backend::default();
let active_backend = get_active_backend();
let backend = crate::backends::Backend::new(active_backend);

match command {
NargoCommand::New(args) => new_cmd::run(&backend, args, config),
Expand All @@ -87,6 +99,7 @@ pub(crate) fn start_cli() -> eyre::Result<()> {
NargoCommand::Test(args) => test_cmd::run(&backend, args, config),
NargoCommand::Info(args) => info_cmd::run(&backend, args, config),
NargoCommand::CodegenVerifier(args) => codegen_verifier_cmd::run(&backend, args, config),
NargoCommand::Backend(args) => backend_cmd::run(args),
NargoCommand::Lsp(args) => lsp_cmd::run(&backend, args, config),
}?;

Expand Down
Loading