Skip to content

Commit

Permalink
Update transpose command (#76)
Browse files Browse the repository at this point in the history
* Update transpose

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Add chagnelog

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* Update UI tests

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

* fmt

Signed-off-by: Oliver Tale-Yazdi <[email protected]>

---------

Signed-off-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
ggwpez authored Feb 6, 2024
1 parent 9117537 commit 261f404
Show file tree
Hide file tree
Showing 15 changed files with 1,973 additions and 155 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.1.0] - 2024-02-06

### Changed
- Make `transpose` smarter and add tests for `dependency lift-to-workspace`.

## [1.0.2] - 2024-02-02

### Fixed
Expand Down
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zepter"
version = "1.0.2"
version = "1.1.0"
edition = "2021"
authors = [ "Oliver Tale-Yazdi" ]
description = "Analyze, Fix and Format features in your Rust workspace."
Expand All @@ -26,14 +26,14 @@ colour = { version = "0.7.0", optional = true }
criterion = { version = "0.5", optional = true }
env_logger = { version = "0.10.2", features = [ "auto-color", "humantime" ], optional = true }
histo = "1.0.0"
itertools = "0.12.1"
itertools = "0.12.0"
log = { version = "0.4.20", optional = true }
semver = "1"
serde = "1.0.196"
serde_json = "1.0.113"
serde_yaml = "0.9.31"
tempfile = { version = "3.9.0", optional = true }
toml_edit = "0.21.1"
toml_edit = "0.21.0"
tracing = { version = "0.1.40", optional = true }

[dev-dependencies]
Expand Down
62 changes: 54 additions & 8 deletions src/autofix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,12 @@ impl AutoFixer {
if let Some(as_str) = dep.as_str() {
cargo_metadata::semver::VersionReq::parse(as_str).expect("Is semver");
let mut table = InlineTable::new();
table.remove("default-features"); // We also remove it to get the order right.

table.insert("workspace", Value::Boolean(Formatted::new(true)));
if let Some(default_feats) = default_feats {
table.insert("default-features", Value::Boolean(Formatted::new(default_feats)));
} else {
table.remove("default-features");
}

table.set_dotted(false);
Expand All @@ -490,11 +491,12 @@ impl AutoFixer {
return Err("'git' or 'path' dependency are currently not supported".into())
}
as_table.remove("version");
as_table.remove("default-features"); // We also remove it to get the order right.

as_table.insert("workspace", Value::Boolean(Formatted::new(true)));
if let Some(default_feats) = default_feats {
as_table.insert("default-features", Value::Boolean(Formatted::new(default_feats)));
} else {
as_table.remove("default-features");
}
} else {
unreachable!("Unknown kind of dependency: {:?}", dep);
Expand All @@ -507,10 +509,21 @@ impl AutoFixer {
dep: &Dependency,
default_feats: bool,
) -> Result<(), String> {
self.add_workspace_dep_inner(&dep.name, &dep.req.to_string(), default_feats)
}

pub(crate) fn add_workspace_dep_inner(
&mut self,
dep_name: &str,
dep_version: &str,
default_feats: bool,
) -> Result<(), String> {
// The carrot is implicit in cargo.
let version_str = dep_version.to_string().trim_start_matches('^').to_string();
let doc: &mut Document = self.doc.as_mut().unwrap();

if !doc.contains_table("workspace") {
return Err("No workspace table".into())
return Err("No workspace entry found".into())
}
let workspace = doc["workspace"].as_table_mut().unwrap();

Expand All @@ -519,16 +532,45 @@ impl AutoFixer {
}

let deps = workspace["dependencies"].as_table_mut().unwrap();
let mut t = InlineTable::new();

if deps.contains_key(&dep.name) {
return Err("Dependency already exists in the workspace".into())
if let Some(found) = deps.get(dep_name) {
if let Some(found) = found.as_inline_table() {
if let Some(version) = found.get("version") {
if remove_carrot(version.as_str().unwrap()) != version_str {
return Err(format!(
"Dependency '{}' already exists in the workspace with a different 'version' field: '{}' vs '{}'",
dep_name,
version.as_str().unwrap(),
dep_version
))
}
}

if let Some(default) = found.get("default-features") {
if default.as_bool().unwrap() != default_feats {
return Err(format!(
"Dependency '{}' already exists in the workspace with a different 'default-features' fields: '{}' vs '{}'",
dep_name,
default.as_bool().unwrap(),
default_feats
))
}
}

// We checked that:
// - There is either no version or its compatible
// - There is either no default-features or its compatible
t = found.clone();
} else {
return Err(format!("Dependency '{}' already exists in the workspace but could not validate its compatibility", dep_name))
}
}

let mut t = InlineTable::new();
t.insert("version", Value::String(Formatted::new(dep.req.to_string())));
t.insert("version", Value::String(Formatted::new(version_str)));
t.insert("default-features", Value::Boolean(Formatted::new(default_feats)));

deps.insert(&dep.name, Item::Value(Value::InlineTable(t)));
deps.insert(dep_name, Item::Value(Value::InlineTable(t)));

Ok(())
}
Expand Down Expand Up @@ -576,3 +618,7 @@ impl ToString for AutoFixer {
self.doc.as_ref().unwrap().to_string()
}
}

fn remove_carrot(version: &str) -> &str {
version.strip_prefix('^').unwrap_or(version)
}
4 changes: 1 addition & 3 deletions src/cmd/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,7 @@ pub fn build_feature_dag(meta: &Metadata, pkgs: &[Package]) -> Dag<CrateAndFeatu
.dependencies
.iter()
.find(|d| d.rename.as_ref().unwrap_or(&d.name) == &dep)
.unwrap_or_else(|| {
panic!("Could not resolve dep {} of {}", dep, pkg.id.to_string())
});
.unwrap_or_else(|| panic!("Could not resolve dep {} of {}", dep, pkg.id));

let dep_id = match resolve_dep(pkg, dep, meta) {
None => {
Expand Down
33 changes: 26 additions & 7 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,36 @@ pub enum FixHint {
}

impl Command {
pub fn run(&self) {
pub fn run(&self) -> Result<(), String> {
self.global.setup_logging();

match self.subcommand.as_ref() {
Some(SubCommand::Trace(cmd)) => cmd.run(&self.global),
Some(SubCommand::Lint(cmd)) => cmd.run(&self.global),
Some(SubCommand::Format(cmd)) => cmd.run(&self.global),
Some(SubCommand::Run(cmd)) => cmd.run(&self.global),
Some(SubCommand::Transpose(cmd)) => cmd.run(&self.global),
Some(SubCommand::Debug(cmd)) => cmd.run(&self.global),
None => run::RunCmd::default().run(&self.global),

Some(SubCommand::Trace(cmd)) => {
cmd.run(&self.global);
Ok(())
},
Some(SubCommand::Lint(cmd)) => {
cmd.run(&self.global);
Ok(())
},
Some(SubCommand::Format(cmd)) => {
cmd.run(&self.global);
Ok(())
},
Some(SubCommand::Run(cmd)) => {
cmd.run(&self.global);
Ok(())
},
Some(SubCommand::Debug(cmd)) => {
cmd.run(&self.global);
Ok(())
},
None => {
run::RunCmd::default().run(&self.global);
Ok(())
},
}
}
}
Expand Down
Loading

0 comments on commit 261f404

Please sign in to comment.