Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Sep 5, 2023
1 parent 46cb714 commit 1057aec
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Args {
let mut cargo_args = vec![];
let mut subcommand: Option<String> = None;

let mut manifest_path = None;
let mut manifest_path: Option<String> = None;
let mut color = None;

let mut package = vec![];
Expand Down
3 changes: 2 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl Context {

// if `--remove-dev-deps` flag is off, restore manifest file.
let restore = restore::Manager::new(!args.remove_dev_deps);
let metadata = Metadata::new(&args, &cargo, cargo_version, &restore)?;
let metadata =
Metadata::new(args.manifest_path.as_deref(), &cargo, cargo_version, &restore)?;
if metadata.cargo_version < 41 && args.include_deps_features {
bail!("--include-deps-features requires Cargo 1.41 or later");
}
Expand Down
24 changes: 8 additions & 16 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};
use std::{collections::BTreeMap, path::Path};

use anyhow::{bail, format_err, Context as _, Result};

use crate::{
context::Context,
fs,
metadata::{self, Metadata},
term,
};
use crate::{context::Context, fs, metadata::Metadata, term};

type ParseResult<T> = Result<T, &'static str>;

Expand Down Expand Up @@ -115,7 +107,7 @@ pub(crate) fn with(cx: &Context, f: impl FnOnce() -> Result<()>) -> Result<()> {
let mut private_crates = vec![];
for id in &cx.metadata.workspace_members {
let package = cx.packages(id);
let manifest_path = &package.manifest_path;
let manifest_path = &*package.manifest_path;
let is_root = manifest_path == root_manifest;
if is_root {
root_id = Some(id);
Expand Down Expand Up @@ -171,13 +163,13 @@ pub(crate) fn with(cx: &Context, f: impl FnOnce() -> Result<()>) -> Result<()> {
if term::verbose() {
info!("removing private crates from {}", manifest_path.display());
}
remove_private_crates(&mut doc, &cx.metadata, &private_crates)?;
remove_private_crates(&mut doc, workspace_root, &private_crates)?;
}
restore_handles.push(cx.restore.register(orig, manifest_path));
fs::write(manifest_path, doc.to_string())?;
}
if restore_lockfile {
let lockfile = &cx.metadata.workspace_root.join("Cargo.lock");
let lockfile = &workspace_root.join("Cargo.lock");
if lockfile.exists() {
restore_handles.push(cx.restore.register(fs::read_to_string(lockfile)?, lockfile));
}
Expand Down Expand Up @@ -210,8 +202,8 @@ fn remove_dev_deps(doc: &mut toml_edit::Document) {

fn remove_private_crates(
doc: &mut toml_edit::Document,
metadata: &metadata::Metadata,
private_crates: &[&PathBuf],
workspace_root: &Path,
private_crates: &[&Path],
) -> Result<()> {
let table = doc.as_table_mut();
if let Some(workspace) = table.get_mut("workspace").and_then(toml_edit::Item::as_table_like_mut)
Expand All @@ -221,7 +213,7 @@ fn remove_private_crates(
let mut i = 0;
while i < members.len() {
if let Some(member) = members.get(i).and_then(toml_edit::Value::as_str) {
let manifest_path = metadata.workspace_root.join(member).join("Cargo.toml");
let manifest_path = workspace_root.join(member).join("Cargo.toml");
if private_crates
.iter()
.find_map(|p| {
Expand Down
15 changes: 7 additions & 8 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
use anyhow::{format_err, Context as _, Result};
use serde_json::{Map, Value};

use crate::{cargo, cli::Args, fs, restore, term};
use crate::{cargo, fs, restore, term};

type Object = Map<String, Value>;
type ParseResult<T> = Result<T, &'static str>;
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(crate) struct Metadata {

impl Metadata {
pub(crate) fn new(
args: &Args,
manifest_path: Option<&str>,
cargo: &OsStr,
mut cargo_version: u32,
restore: &restore::Manager,
Expand All @@ -57,7 +57,7 @@ impl Metadata {
let mut cmd;
let json = if stable_cargo_version > cargo_version {
cmd = cmd!(cargo, "metadata", "--format-version=1", "--no-deps");
if let Some(manifest_path) = &args.manifest_path {
if let Some(manifest_path) = manifest_path {
cmd.arg("--manifest-path");
cmd.arg(manifest_path);
}
Expand All @@ -67,7 +67,7 @@ impl Metadata {
Path::new(no_deps["workspace_root"].as_str().unwrap()).join("Cargo.lock");
if !lockfile.exists() {
let mut cmd = cmd!(cargo, "generate-lockfile");
if let Some(manifest_path) = &args.manifest_path {
if let Some(manifest_path) = manifest_path {
cmd.arg("--manifest-path");
cmd.arg(manifest_path);
}
Expand All @@ -79,7 +79,7 @@ impl Metadata {
// a dependency that requires newer cargo features, `cargo metadata`
// with older cargo may fail.
cmd = cmd!("cargo", "+stable", "metadata", "--format-version=1");
if let Some(manifest_path) = &args.manifest_path {
if let Some(manifest_path) = manifest_path {
cmd.arg("--manifest-path");
cmd.arg(manifest_path);
}
Expand All @@ -94,7 +94,7 @@ impl Metadata {
Err(_e) => {
// If failed, try again with the version of cargo we will actually use.
cmd = cmd!(cargo, "metadata", "--format-version=1");
if let Some(manifest_path) = &args.manifest_path {
if let Some(manifest_path) = manifest_path {
cmd.arg("--manifest-path");
cmd.arg(manifest_path);
}
Expand All @@ -103,7 +103,7 @@ impl Metadata {
}
} else {
cmd = cmd!(cargo, "metadata", "--format-version=1");
if let Some(manifest_path) = &args.manifest_path {
if let Some(manifest_path) = manifest_path {
cmd.arg("--manifest-path");
cmd.arg(manifest_path);
}
Expand Down Expand Up @@ -252,7 +252,6 @@ pub(crate) struct Package {
/// The minimum supported Rust version of this package.
///
/// This is always `None` if running with a version of Cargo older than 1.58.
#[allow(dead_code)]
pub(crate) rust_version: Option<String>,
}

Expand Down
6 changes: 5 additions & 1 deletion src/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ pub(crate) fn version_range(
let start = match split.next().unwrap() {
"" => {
let mut rust_version = None;
// TODO: we should only check members we actually run command.
for id in cx.workspace_members() {
if cx.ignore_private && cx.is_private(id) {
continue;
}
let v = cx.rust_version(id);
if v.is_none() || v == rust_version {
// no-op
Expand All @@ -53,7 +57,7 @@ pub(crate) fn version_range(
}
match rust_version {
Some(v) => v.parse()?,
None => bail!("no rust-version field in Cargo.toml is specified"),
None => bail!("no rust-version field in any Cargo.toml is specified"),
}
}
s => s.parse()?,
Expand Down

0 comments on commit 1057aec

Please sign in to comment.