Skip to content

Commit

Permalink
improve add tool
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-schott committed May 6, 2024
1 parent 4d03a94 commit ba4a434
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
15 changes: 4 additions & 11 deletions errors/src/errors/package/package_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,9 @@ create_messages!(
}

@backtraced
missing_on_chain_program_name {
args: (),
msg: "The name of the program to execute on-chain is missing.".to_string(),
help: Some("Either set `--local` to execute the local program on chain, or set `--program <PROGRAM>`.".to_string()),
}

@backtraced
conflicting_on_chain_program_name {
args: (first: impl Display, second: impl Display),
msg: format!("Conflicting program names given to execute on chain: `{first}` and `{second}`."),
help: Some("Either set `--local` to execute the local program on chain, or set `--program <PROGRAM>`.".to_string()),
invalid_file_name_dependency {
args: (name: impl Display),
msg: format!("The dependency program name `{name}` is invalid."),
help: Some("Aleo program names must only contain lower case letters, numbers and underscores.".to_string()),
}
);
12 changes: 10 additions & 2 deletions leo/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ pub struct CLI {
#[clap(subcommand)]
command: Commands,

#[clap(long, global = true, help = "Optional path to Leo program root folder")]
#[clap(long, global = true, help = "Path to Leo program root folder")]
path: Option<PathBuf>,

#[clap(long, global = true, help = "Optional path to aleo program registry.")]
#[clap(long, global = true, help = "Path to aleo program registry.")]
pub home: Option<PathBuf>,
}

Expand Down Expand Up @@ -401,6 +401,7 @@ function external_nested_function:
name: "nested_example_layer_0".to_string(),
local: None,
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(project_directory.clone()),
Expand Down Expand Up @@ -494,6 +495,7 @@ program child.aleo {
name: "parent".to_string(),
local: Some(parent_directory.clone()),
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(grandparent_directory.clone()),
Expand All @@ -508,6 +510,7 @@ program child.aleo {
name: "child".to_string(),
local: Some(child_directory.clone()),
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(grandparent_directory.clone()),
Expand All @@ -522,6 +525,7 @@ program child.aleo {
name: "child".to_string(),
local: Some(child_directory.clone()),
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(parent_directory.clone()),
Expand Down Expand Up @@ -645,6 +649,7 @@ program outer.aleo {
name: "inner_1".to_string(),
local: Some(inner_1_directory.clone()),
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(outer_directory.clone()),
Expand All @@ -659,6 +664,7 @@ program outer.aleo {
name: "inner_2".to_string(),
local: Some(inner_2_directory.clone()),
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(outer_directory.clone()),
Expand Down Expand Up @@ -812,6 +818,7 @@ program outer_2.aleo {
name: "inner_1".to_string(),
local: Some(inner_1_directory.clone()),
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(outer_directory.clone()),
Expand All @@ -826,6 +833,7 @@ program outer_2.aleo {
name: "inner_2".to_string(),
local: Some(inner_2_directory.clone()),
network: "testnet3".to_string(),
clear: false,
},
},
path: Some(outer_directory.clone()),
Expand Down
63 changes: 50 additions & 13 deletions leo/cli/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ use std::path::PathBuf;
#[derive(Parser, Debug)]
#[clap(name = "leo", author = "The Aleo Team <[email protected]>", version)]
pub struct Add {
#[clap(name = "NAME", help = "The dependency name")]
#[clap(name = "NAME", help = "The dependency name. Ex: `credits.aleo` or `credits`.")]
pub(crate) name: String,

#[clap(short = 'l', long, help = "Optional path to local dependency")]
#[clap(short = 'l', long, help = "Path to local dependency")]
pub(crate) local: Option<PathBuf>,

#[clap(short = 'n', long, help = "Optional name of the network to use", default_value = "testnet3")]
#[clap(short = 'n', long, help = "Name of the network to use", default_value = "testnet3")]
pub(crate) network: String,

#[clap(short = 'c', long, help = "Clear all previous dependencies.", default_value = "false")]
pub(crate) clear: bool,
}

impl Command for Add {
Expand All @@ -53,32 +56,61 @@ impl Command for Add {
let manifest: Manifest = serde_json::from_str(&program_data)
.map_err(|err| PackageError::failed_to_deserialize_manifest_file(path.to_str().unwrap(), err))?;

// Allow both `credits.aleo` and `credits` syntax
let name = match self.name {
name if name.ends_with(".aleo") => name,
name => format!("{}.aleo", name),
// Make sure the program name is valid.
// Allow both `credits.aleo` and `credits` syntax.
let name: String = match &self.name {
name if name.ends_with(".aleo") && is_valid_program_name(&name[0..self.name.len() - 5]) => name.clone(),
name if is_valid_program_name(name) => format!("{name}.aleo"),
name => return Err(PackageError::invalid_file_name_dependency(name).into()),
};

// Add dependency section to manifest if it doesn't exist
let mut dependencies = match manifest.dependencies() {
Some(ref dependencies) => dependencies
let mut dependencies = match (self.clear, manifest.dependencies()) {
(false, Some(ref dependencies)) => dependencies
.iter()
.filter_map(|dependency| {
// Overwrite old dependencies of the same name.
if dependency.name() == &name {
println!("{} already exists as a dependency. Overwriting.", name);
let msg = match (dependency.path(), dependency.network()) {
(Some(local_path), _) => {
format!("local dependency at path `{}`", local_path.to_str().unwrap().replace('\"', ""))
}
(_, Some(network)) => {
format!("network dependency from `{}`", network)
}
_ => "git dependency".to_string(),
};
tracing::warn!("⚠️ Program `{name}` already exists as a {msg}. Overwriting.");
None
} else if self.local.is_some() && &self.local == dependency.path() {
// Overwrite old dependencies at the same local path.
tracing::warn!(
"⚠️ Path `{}` already exists as the location for local dependency `{}`. Overwriting.",
self.local.clone().unwrap().to_str().unwrap().replace('\"', ""),
dependency.name()
);
None
} else {
Some(dependency.clone())
}
})
.collect(),
None => Vec::new(),
_ => Vec::new(),
};

// Add new dependency to manifest
dependencies.push(match self.local {
Some(local_path) => Dependency::new(name, Location::Local, None, Some(local_path)),
None => Dependency::new(name, Location::Network, Some(Network::from(&self.network)), None),
Some(local_path) => {
tracing::info!(
"✅ Added local dependency to program `{name}` at path `{}`.",
local_path.to_str().unwrap().replace('\"', "")
);
Dependency::new(name, Location::Local, None, Some(local_path))
}
None => {
tracing::info!("✅ Added network dependency to program `{name}` from network `{}`.", self.network);
Dependency::new(name, Location::Network, Some(Network::from(&self.network)), None)
}
});

// Update manifest
Expand All @@ -96,3 +128,8 @@ impl Command for Add {
Ok(())
}
}

/// Returns `true` if the String is a valid Aleo program name.
pub fn is_valid_program_name(name: &str) -> bool {
name.chars().all(|c| matches!(c, '0'..='9' | 'a'..='z' | '_'))
}
4 changes: 0 additions & 4 deletions leo/cli/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ fn compile_leo_file(
options: BuildOptions,
stubs: IndexMap<Symbol, Stub>,
) -> Result<()> {
// Construct the Leo file name with extension `foo.leo`.
let file_name =
file_path.file_name().and_then(|name| name.to_str()).ok_or_else(PackageError::failed_to_get_file_name)?;

// Construct program name from the program_id found in `package.json`.
let program_name = program_id.name().to_string();

Expand Down

0 comments on commit ba4a434

Please sign in to comment.