diff --git a/framework/meta/src/cmd/info.rs b/framework/meta/src/cmd/info.rs index 2d36399474..6d51ccd39f 100644 --- a/framework/meta/src/cmd/info.rs +++ b/framework/meta/src/cmd/info.rs @@ -1,10 +1,11 @@ -use super::upgrade::print_tree_dir_metadata; use crate::{ cli::InfoArgs, folder_structure::{dir_pretty_print, RelevantDirectories}, version_history::LAST_UPGRADE_VERSION, }; +use super::print_util::print_tree_dir_metadata; + pub fn call_info(args: &InfoArgs) { let path = if let Some(some_path) = &args.path { some_path.as_str() diff --git a/framework/meta/src/cmd/print_util.rs b/framework/meta/src/cmd/print_util.rs index 7b3e0be9b8..e93099f348 100644 --- a/framework/meta/src/cmd/print_util.rs +++ b/framework/meta/src/cmd/print_util.rs @@ -1,6 +1,9 @@ use colored::Colorize; +use multiversx_sc_meta_lib::version::FrameworkVersion; use std::path::Path; +use crate::folder_structure::{DependencyReference, DirectoryType, RelevantDirectory}; + pub fn print_all_count(num_contract_crates: usize) { println!( "\n{}", @@ -24,3 +27,33 @@ pub fn print_all_command(meta_path: &Path, cargo_run_args: &[String]) { cargo_run_args.join(" "), ); } + +pub fn print_tree_dir_metadata(dir: &RelevantDirectory, last_version: &FrameworkVersion) { + match dir.dir_type { + DirectoryType::Contract => print!(" {}", "[contract]".blue()), + DirectoryType::Lib => print!(" {}", "[lib]".magenta()), + } + + match &dir.version { + DependencyReference::Version(version_req) => { + let version_string = format!("[{}]", version_req.semver); + if version_req.semver == *last_version { + print!(" {}", version_string.green()); + } else { + print!(" {}", version_string.red()); + }; + }, + DependencyReference::Git(git_reference) => { + let git_string = format!( + "[git: {} rev: {}]", + git_reference.git.truecolor(255, 127, 0), + git_reference.rev.truecolor(255, 127, 0) + ); + print!(" {}", git_string.truecolor(255, 198, 0)); + }, + DependencyReference::Path(path_buf) => { + let git_string = format!("[path: {}]", path_buf.truecolor(255, 127, 0)); + print!(" {}", git_string.truecolor(255, 198, 0)); + }, + } +} diff --git a/framework/meta/src/cmd/upgrade.rs b/framework/meta/src/cmd/upgrade.rs index ab14b75075..b4b9a28904 100644 --- a/framework/meta/src/cmd/upgrade.rs +++ b/framework/meta/src/cmd/upgrade.rs @@ -8,5 +8,4 @@ mod upgrade_print; mod upgrade_selector; mod upgrade_settings; -pub use upgrade_print::print_tree_dir_metadata; pub use upgrade_selector::upgrade_sc; diff --git a/framework/meta/src/cmd/upgrade/upgrade_print.rs b/framework/meta/src/cmd/upgrade/upgrade_print.rs index 51d66f9333..6c6360c14a 100644 --- a/framework/meta/src/cmd/upgrade/upgrade_print.rs +++ b/framework/meta/src/cmd/upgrade/upgrade_print.rs @@ -1,8 +1,5 @@ use crate::{ - folder_structure::{ - DirectoryType::{Contract, Lib}, - RelevantDirectory, - }, + folder_structure::{DependencyReference, RelevantDirectory}, version::FrameworkVersion, }; use colored::Colorize; @@ -82,20 +79,6 @@ pub fn print_postprocessing_after_39_1(path: &Path) { ); } -pub fn print_tree_dir_metadata(dir: &RelevantDirectory, last_version: &FrameworkVersion) { - match dir.dir_type { - Contract => print!(" {}", "[contract]".blue()), - Lib => print!(" {}", "[lib]".magenta()), - } - - let version_string = format!("[{}]", dir.version.semver); - if dir.version.semver == *last_version { - print!(" {}", version_string.green()); - } else { - print!(" {}", version_string.red()); - }; -} - pub fn print_cargo_dep_remove(path: &Path, dep_name: &str) { println!( "{}/dependencies/{}", @@ -113,15 +96,17 @@ pub fn print_cargo_dep_add(path: &Path, dep_name: &str) { } pub fn print_cargo_check(dir: &RelevantDirectory) { - println!( - "\n{}", - format!( - "Running cargo check after upgrading to version {} in {}\n", - dir.version.semver, - dir.path.display(), - ) - .purple() - ); + if let DependencyReference::Version(version_req) = &dir.version { + println!( + "\n{}", + format!( + "Running cargo check after upgrading to version {} in {}\n", + version_req.semver, + dir.path.display(), + ) + .purple() + ); + } } pub fn print_cargo_check_fail() { diff --git a/framework/meta/src/cmd/upgrade/upgrade_selector.rs b/framework/meta/src/cmd/upgrade/upgrade_selector.rs index 0f958b176e..5031339220 100644 --- a/framework/meta/src/cmd/upgrade/upgrade_selector.rs +++ b/framework/meta/src/cmd/upgrade/upgrade_selector.rs @@ -1,6 +1,6 @@ use crate::{ cli::UpgradeArgs, - cmd::upgrade::upgrade_settings::UpgradeSettings, + cmd::{print_util::print_tree_dir_metadata, upgrade::upgrade_settings::UpgradeSettings}, folder_structure::{dir_pretty_print, RelevantDirectories, RelevantDirectory}, version::FrameworkVersion, version_history::{versions_iter, CHECK_AFTER_UPGRADE_TO, LAST_UPGRADE_VERSION, VERSIONS}, diff --git a/framework/meta/src/folder_structure/relevant_directory.rs b/framework/meta/src/folder_structure/relevant_directory.rs index be0c4ce0ac..a42a7dd27a 100644 --- a/framework/meta/src/folder_structure/relevant_directory.rs +++ b/framework/meta/src/folder_structure/relevant_directory.rs @@ -6,7 +6,7 @@ use std::{ }; use toml::Value; -use super::version_req::VersionReq; +use super::{version_req::VersionReq, DependencyReference, GitReference}; /// Used for retrieving crate versions. pub const FRAMEWORK_CRATE_NAMES: &[&str] = &[ @@ -35,7 +35,7 @@ pub enum DirectoryType { #[derive(Debug, Clone)] pub struct RelevantDirectory { pub path: PathBuf, - pub version: VersionReq, + pub version: DependencyReference, pub upgrade_in_progress: Option<(FrameworkVersion, FrameworkVersion)>, pub dir_type: DirectoryType, } @@ -89,7 +89,7 @@ impl RelevantDirectories { pub fn count_for_version(&self, version: &FrameworkVersion) -> usize { self.0 .iter() - .filter(|dir| dir.version.semver == *version) + .filter(|dir| dir.version.is_framework_version(version)) .count() } @@ -99,13 +99,13 @@ impl RelevantDirectories { ) -> impl Iterator { self.0 .iter() - .filter(move |dir| dir.version.semver == *version) + .filter(move |dir| dir.version.is_framework_version(version)) } /// Marks all appropriate directories as ready for upgrade. pub fn start_upgrade(&mut self, from_version: FrameworkVersion, to_version: FrameworkVersion) { for dir in self.0.iter_mut() { - if dir.version.semver == from_version { + if dir.version.is_framework_version(&from_version) { dir.upgrade_in_progress = Some((from_version.clone(), to_version.clone())); } } @@ -116,7 +116,9 @@ impl RelevantDirectories { pub fn finish_upgrade(&mut self) { for dir in self.0.iter_mut() { if let Some((_, to_version)) = &dir.upgrade_in_progress { - dir.version.semver = to_version.clone(); + if let DependencyReference::Version(version_req) = &mut dir.version { + version_req.semver = to_version.clone(); + } dir.upgrade_in_progress = None; } } @@ -136,7 +138,7 @@ fn populate_directories(path: &Path, ignore: &[String], result: &mut Vec bool { } } -fn find_framework_version_string(cargo_toml_contents: &CargoTomlContents) -> Option { +fn find_framework_toml_dependency( + cargo_toml_contents: &CargoTomlContents, +) -> Option { for &crate_name in FRAMEWORK_CRATE_NAMES { - if let Some(old_base) = cargo_toml_contents.dependency(crate_name) { - if let Some(Value::String(s)) = old_base.get("version") { - return Some(s.clone()); + if let Some(dep_value) = cargo_toml_contents.dependency(crate_name) { + if let Some(Value::String(s)) = dep_value.get("path") { + return Some(DependencyReference::Path(s.clone())); + } + + if let Some(Value::String(git)) = dep_value.get("git") { + let rev = dep_value + .get("rev") + .and_then(|v| v.as_str()) + .unwrap_or_default(); + return Some(DependencyReference::Git(GitReference { + git: git.clone(), + rev: rev.to_owned(), + })); + } + + if let Some(Value::String(s)) = dep_value.get("version") { + return Some(DependencyReference::Version(VersionReq::from_string( + s.clone(), + ))); } } } @@ -200,10 +221,10 @@ impl RelevantDirectory { } } -fn find_framework_version(dir_path: &Path) -> Option { +fn find_framework_dependency(dir_path: &Path) -> Option { if let Some(cargo_toml_contents) = load_cargo_toml_contents(dir_path) { - if let Some(version) = find_framework_version_string(&cargo_toml_contents) { - return Some(VersionReq::from_string(version)); + if let Some(dep_ref) = find_framework_toml_dependency(&cargo_toml_contents) { + return Some(dep_ref); } } diff --git a/framework/meta/src/folder_structure/version_req.rs b/framework/meta/src/folder_structure/version_req.rs index bbfa752a12..8d7863920d 100644 --- a/framework/meta/src/folder_structure/version_req.rs +++ b/framework/meta/src/folder_structure/version_req.rs @@ -3,6 +3,24 @@ use crate::{ version_history::{find_version_by_str, LAST_VERSION}, }; +/// Models how a dependency is expressed in Cargo.toml. +#[derive(Debug, Clone)] +pub enum DependencyReference { + Version(VersionReq), + Git(GitReference), + Path(String), +} + +impl DependencyReference { + pub fn is_framework_version(&self, version: &FrameworkVersion) -> bool { + if let DependencyReference::Version(version_req) = self { + &version_req.semver == version + } else { + false + } + } +} + /// Crate version requirements, as expressed in Cargo.toml. A very crude version. /// /// TODO: replace with semver::VersionReq at some point. @@ -36,3 +54,12 @@ impl VersionReq { } } } + +/// A dependency reference to a git commit. We mostly use git commits when referencing git. +/// +/// TODO: add support for `branch` and `tag`. +#[derive(Debug, Clone)] +pub struct GitReference { + pub git: String, + pub rev: String, +}