Skip to content

Commit

Permalink
sc-meta - support for git branch & tag
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 21, 2024
1 parent c33cda1 commit 3925406
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 47 deletions.
26 changes: 2 additions & 24 deletions framework/meta-lib/src/cargo_toml/cargo_toml_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,8 @@ impl CargoTomlContents {

/// Interprets the dependency value and organizes values in a struct.
pub fn dependency_raw_value(&self, crate_name: &str) -> Option<DependencyRawValue> {
if let Some(dep_value) = self.dependency(crate_name) {
match dep_value {
Value::String(version) => Some(DependencyRawValue::from_version(version)),
Value::Table(table) => {
let mut result = DependencyRawValue::default();
if let Some(Value::String(version)) = dep_value.get("version") {
result.version = Some(version.to_owned());
}
if let Some(Value::String(path)) = table.get("path") {
result.path = Some(path.to_owned());
}
if let Some(Value::String(git)) = table.get("git") {
result.git = Some(git.to_owned());
}
if let Some(Value::String(rev)) = table.get("rev") {
result.rev = Some(rev.to_owned());
}
Some(result)
},
_ => panic!("Unsupported dependency value"),
}
} else {
None
}
self.dependency(crate_name)
.map(DependencyRawValue::parse_toml_value)
}

pub fn insert_dependency_raw_value(&mut self, crate_name: &str, raw_value: DependencyRawValue) {
Expand Down
45 changes: 38 additions & 7 deletions framework/meta-lib/src/cargo_toml/cargo_toml_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@ use crate::version::FrameworkVersion;
use super::{DependencyRawValue, 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, PartialEq, Eq)]
pub struct GitCommitReference {
pub git: String,
pub rev: String,
}

/// A dependency reference to a git branch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitBranchReference {
pub git: String,
pub branch: String,
}

/// A dependency reference to a git tag.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitTagReference {
pub git: String,
pub tag: String,
}

/// Models how a dependency is expressed in Cargo.toml.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DependencyReference {
Version(VersionReq),
GitCommit(GitCommitReference),
GitBranch(GitBranchReference),
GitTag(GitTagReference),
Path(String),
Unsupported,
}
Expand All @@ -39,11 +53,28 @@ impl DependencyRawValue {
}

if let Some(git) = self.git {
let rev = self.rev.unwrap_or_default();
return DependencyReference::GitCommit(GitCommitReference {
git: git.clone(),
rev: rev.to_owned(),
});
if let Some(rev) = self.rev {
return DependencyReference::GitCommit(GitCommitReference {
git: git.clone(),
rev: rev.to_owned(),
});
}

if let Some(branch) = self.branch {
return DependencyReference::GitBranch(GitBranchReference {
git: git.clone(),
branch,
});
}

if let Some(tag) = self.tag {
return DependencyReference::GitTag(GitTagReference {
git: git.clone(),
tag: tag.to_owned(),
});
}

return DependencyReference::Unsupported;
}

// explicit version = "..."
Expand Down
16 changes: 16 additions & 0 deletions framework/meta-lib/src/cargo_toml/cargo_toml_deps_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub struct DependencyRawValue {
pub version: Option<String>,
pub git: Option<String>,
pub rev: Option<String>,
pub branch: Option<String>,
pub tag: Option<String>,
pub path: Option<String>,
}

Expand Down Expand Up @@ -31,6 +33,12 @@ impl DependencyRawValue {
if let Some(toml::Value::String(rev)) = table.get("rev") {
result.rev = Some(rev.to_owned());
}
if let Some(toml::Value::String(branch)) = table.get("branch") {
result.branch = Some(branch.to_owned());
}
if let Some(toml::Value::String(tag)) = table.get("tag") {
result.tag = Some(tag.to_owned());
}
result
},
_ => panic!("Unsupported dependency value"),
Expand All @@ -52,6 +60,14 @@ impl DependencyRawValue {
table.insert("rev".to_string(), toml::Value::String(rev));
}

if let Some(branch) = self.branch {
table.insert("branch".to_string(), toml::Value::String(branch));
}

if let Some(tag) = self.tag {
table.insert("tag".to_string(), toml::Value::String(tag));
}

if let Some(path) = self.path {
table.insert("path".to_string(), toml::Value::String(path));
}
Expand Down
3 changes: 1 addition & 2 deletions framework/meta-lib/src/contract/meta_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ members = [\".\"]
profile: ContractVariantProfile::default(),
framework_dependency: DependencyRawValue {
version: Some("x.y.z".to_owned()),
git: None,
rev: None,
path: Option::Some("../../../framework/base".to_owned()),
..Default::default()
},
contract_features: Vec::<String>::new(),
contract_default_features: None,
Expand Down
16 changes: 16 additions & 0 deletions framework/meta/src/cmd/print_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ pub fn print_tree_dir_metadata(dir: &RelevantDirectory, last_version: &Framework
);
print!(" {}", git_string.truecolor(255, 198, 0));
},
DependencyReference::GitBranch(git_reference) => {
let git_string = format!(
"[git: {} branch: {}]",
git_reference.git.truecolor(255, 127, 0),
git_reference.branch.truecolor(255, 127, 0)
);
print!(" {}", git_string.truecolor(255, 198, 0));
},
DependencyReference::GitTag(git_reference) => {
let git_string = format!(
"[git: {} rev: {}]",
git_reference.git.truecolor(255, 127, 0),
git_reference.tag.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));
Expand Down
18 changes: 4 additions & 14 deletions framework/meta/src/folder_structure/relevant_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,6 @@ fn can_continue_recursion(dir_entry: &DirEntry, blacklist: &[String]) -> bool {
}
}

fn find_framework_toml_dependency(
cargo_toml_contents: &CargoTomlContents,
) -> Option<DependencyReference> {
for &crate_name in FRAMEWORK_CRATE_NAMES {
if let Some(dep_raw) = cargo_toml_contents.dependency_raw_value(crate_name) {
return Some(dep_raw.interpret());
}
}

None
}

fn load_cargo_toml_contents(dir_path: &Path) -> Option<CargoTomlContents> {
let cargo_toml_path = dir_path.join(CARGO_TOML_FILE_NAME);
if cargo_toml_path.is_file() {
Expand All @@ -203,8 +191,10 @@ impl RelevantDirectory {

fn find_framework_dependency(dir_path: &Path) -> Option<DependencyReference> {
if let Some(cargo_toml_contents) = load_cargo_toml_contents(dir_path) {
if let Some(dep_ref) = find_framework_toml_dependency(&cargo_toml_contents) {
return Some(dep_ref);
for &crate_name in FRAMEWORK_CRATE_NAMES {
if let Some(dep_raw) = cargo_toml_contents.dependency_raw_value(crate_name) {
return Some(dep_raw.interpret());
}
}
}

Expand Down

0 comments on commit 3925406

Please sign in to comment.