Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cargo): remove dependence on install-update #796

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ merge = "~0.1"
regex-split = "~0.1"
notify-rust = "~4.10"
wildmatch = "2.3.0"
serde_json = "1.0.117"

[package.metadata.generate-rpm]
assets = [{ source = "target/release/topgrade", dest = "/usr/bin/topgrade" }]
Expand Down
77 changes: 76 additions & 1 deletion src/steps/generic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(unused_imports)]

use std::fs::File;
use std::path::PathBuf;
use std::process::Command;
use std::{env, path::Path};
Expand All @@ -8,7 +9,8 @@ use std::{fs, io::Write};
use color_eyre::eyre::eyre;
use color_eyre::eyre::Context;
use color_eyre::eyre::Result;
use semver::Version;
use semver::{Version, VersionReq};
use serde_json::Value;
use tempfile::tempfile_in;
use tracing::{debug, error};

Expand Down Expand Up @@ -54,6 +56,22 @@ pub fn run_cargo_update(ctx: &ExecutionContext) -> Result<()> {
return Err(SkipStep(format!("{} exists but empty", &toml_file.display())).into());
}

let required = VersionReq::parse(">=1.77.0").unwrap();

let version = Command::new("cargo").arg("--version").output_checked_utf8()?.stdout;

let version = version
.split_whitespace()
.nth(1)
.ok_or(SkipStep(format!("cargo --version output is malformed: {version}")))?;
debug!("{version}");

let version = Version::parse(version)?;

if required.matches(&version) {
return manual_cargo_updates(cargo_dir.join(".crates2.json").require()?);
}

print_separator("Cargo");
let cargo_update = require("cargo-install-update")
.ok()
Expand Down Expand Up @@ -90,6 +108,63 @@ pub fn run_cargo_update(ctx: &ExecutionContext) -> Result<()> {
Ok(())
}

fn manual_cargo_updates(crates_json: PathBuf) -> Result<()> {
let file = File::open(crates_json)?;
let value: Value = serde_json::from_reader(file)?;

let crate_map = value
.get("installs")
.ok_or(SkipStep("Improper .crates2.json".to_string()))?;

let mut install_command = Command::new("cargo");
install_command.arg("install");

let mut feature_packages = Vec::new();

match crate_map {
Value::Object(object) => {
for (package, meta) in object {
let to_install = package.contains("registry");
let package = package.split_whitespace().next().unwrap();
if to_install
&& !meta["no_default_features"].as_bool().unwrap()
&& !meta["all_features"].as_bool().unwrap()
&& meta["features"].as_array().unwrap().is_empty()
{
install_command.arg(package);
} else if to_install {
feature_packages.push((
package,
meta["all_features"].as_bool().unwrap(),
meta["no_default_features"].as_bool().unwrap(),
meta["features"].as_array().unwrap(),
));
}
}
}
_ => {
return Err(SkipStep("Improper .crates2.json".to_string()).into());
}
}

for (package, all_features, no_default, feature) in feature_packages {
let feature: Vec<_> = feature.iter().map(|feat| feat.as_str().unwrap()).collect();
let mut command = Command::new("cargo");
command.args(["install", package]);
if all_features {
command.arg("--all-features");
}
if no_default {
command.arg("--no-default-features");
}
command.arg("--features");
command.args(feature);

command.status_checked()?;
}
install_command.status_checked()
}

pub fn run_flutter_upgrade(ctx: &ExecutionContext) -> Result<()> {
let flutter = require("flutter")?;

Expand Down