Skip to content

Commit

Permalink
Merge pull request #1815 from multiversx/relevant-rev
Browse files Browse the repository at this point in the history
sc-meta support for framework deps as path or git
  • Loading branch information
andrei-marinica authored Oct 9, 2024
2 parents 4d3f6d6 + 30b8e2e commit 764a944
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 44 deletions.
3 changes: 2 additions & 1 deletion framework/meta/src/cmd/info.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
33 changes: 33 additions & 0 deletions framework/meta/src/cmd/print_util.rs
Original file line number Diff line number Diff line change
@@ -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{}",
Expand All @@ -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));
},
}
}
1 change: 0 additions & 1 deletion framework/meta/src/cmd/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
39 changes: 12 additions & 27 deletions framework/meta/src/cmd/upgrade/upgrade_print.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::{
folder_structure::{
DirectoryType::{Contract, Lib},
RelevantDirectory,
},
folder_structure::{DependencyReference, RelevantDirectory},
version::FrameworkVersion,
};
use colored::Colorize;
Expand Down Expand Up @@ -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/{}",
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion framework/meta/src/cmd/upgrade/upgrade_selector.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
49 changes: 35 additions & 14 deletions framework/meta/src/folder_structure/relevant_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = &[
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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()
}

Expand All @@ -99,13 +99,13 @@ impl RelevantDirectories {
) -> impl Iterator<Item = &RelevantDirectory> {
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()));
}
}
Expand All @@ -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;
}
}
Expand All @@ -136,7 +138,7 @@ fn populate_directories(path: &Path, ignore: &[String], result: &mut Vec<Relevan
}
}

if let Some(version) = find_framework_version(path) {
if let Some(version) = find_framework_dependency(path) {
let dir_type = if is_contract {
DirectoryType::Contract
} else {
Expand Down Expand Up @@ -172,11 +174,30 @@ fn can_continue_recursion(dir_entry: &DirEntry, blacklist: &[String]) -> bool {
}
}

fn find_framework_version_string(cargo_toml_contents: &CargoTomlContents) -> Option<String> {
fn find_framework_toml_dependency(
cargo_toml_contents: &CargoTomlContents,
) -> Option<DependencyReference> {
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(),
)));
}
}
}
Expand All @@ -200,10 +221,10 @@ impl RelevantDirectory {
}
}

fn find_framework_version(dir_path: &Path) -> Option<VersionReq> {
fn find_framework_dependency(dir_path: &Path) -> Option<DependencyReference> {
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);
}
}

Expand Down
27 changes: 27 additions & 0 deletions framework/meta/src/folder_structure/version_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}

0 comments on commit 764a944

Please sign in to comment.