-
Notifications
You must be signed in to change notification settings - Fork 661
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27916 from AleoHQ/fix/leo-add
[Feat] Better `leo add` and `leo build` UX
- Loading branch information
Showing
20 changed files
with
1,244 additions
and
312 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
|
@@ -18,18 +18,21 @@ use super::*; | |
use leo_retriever::{Dependency, Location, Manifest, Network}; | ||
use std::path::PathBuf; | ||
|
||
/// Clean outputs folder command | ||
/// Add a new on-chain or local dependency to the current package. | ||
#[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 { | ||
|
@@ -47,41 +50,74 @@ impl Command for Add { | |
fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> { | ||
let path = context.dir()?; | ||
|
||
// Deserialize the manifest | ||
// Deserialize the manifest. | ||
let program_data: String = std::fs::read_to_string(path.join("program.json")) | ||
.map_err(|err| PackageError::failed_to_read_file(path.to_str().unwrap(), err))?; | ||
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") | ||
&& Package::<CurrentNetwork>::is_aleo_name_valid(&name[0..self.name.len() - 5]) => | ||
{ | ||
name.clone() | ||
} | ||
name if Package::<CurrentNetwork>::is_aleo_name_valid(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 | ||
// Add dependency section to manifest if it doesn't exist. | ||
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 | ||
// Add new dependency to the 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 | ||
// Update the manifest file. | ||
let new_manifest = Manifest::new( | ||
manifest.program(), | ||
manifest.version(), | ||
|
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
Oops, something went wrong.