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

xtask-unpublished: output a markdown table #12085

Merged
merged 3 commits into from
May 5, 2023
Merged
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
1 change: 1 addition & 0 deletions benches/capture/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Tool for capturing a real-world workspace for benchmarking."
publish = false
epage marked this conversation as resolved.
Show resolved Hide resolved

[dependencies]
cargo_metadata.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/cargo-test-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ homepage = "https://github.com/rust-lang/cargo"
repository = "https://github.com/rust-lang/cargo"
documentation = "https://github.com/rust-lang/cargo"
description = "Helper proc-macro for Cargo's testsuite."
publish = false

[lib]
proc-macro = true
1 change: 1 addition & 0 deletions crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "cargo-test-support"
version = "0.1.0"
license = "MIT OR Apache-2.0"
edition = "2021"
publish = false

[lib]
doctest = false
Expand Down
123 changes: 80 additions & 43 deletions crates/xtask-unpublished/src/xtask.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::collections::HashSet;

use cargo::core::registry::PackageRegistry;
use cargo::core::QueryKind;
use cargo::core::Registry;
use cargo::core::SourceId;
use cargo::ops::Packages;
use cargo::util::command_prelude::*;

pub fn cli() -> clap::Command {
clap::Command::new("xtask-unpublished")
.arg_package_spec_simple("Package to inspect the published status")
.arg(
opt(
"verbose",
Expand Down Expand Up @@ -76,14 +80,24 @@ fn config_configure(config: &mut Config, args: &ArgMatches) -> CliResult {

fn unpublished(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> cargo::CliResult {
let ws = args.workspace(config)?;

let members_to_inspect: HashSet<_> = {
let pkgs = args.packages_from_flags()?;
if let Packages::Packages(_) = pkgs {
HashSet::from_iter(pkgs.get_packages(&ws)?)
} else {
HashSet::from_iter(ws.members())
}
};

let mut results = Vec::new();
{
let mut registry = PackageRegistry::new(config)?;
let _lock = config.acquire_package_cache_lock()?;
registry.lock_patches();
let source_id = SourceId::crates_io(config)?;

for member in ws.members() {
for member in members_to_inspect {
let name = member.name();
let current = member.version();
if member.publish() == &Some(vec![]) {
Expand All @@ -92,11 +106,8 @@ fn unpublished(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> car
}

let version_req = format!("<={current}");
let query = cargo::core::dependency::Dependency::parse(
name,
Some(&version_req),
source_id.clone(),
)?;
let query =
cargo::core::dependency::Dependency::parse(name, Some(&version_req), source_id)?;
let possibilities = loop {
// Exact to avoid returning all for path/git
match registry.query_vec(&query, QueryKind::Exact) {
Expand All @@ -106,51 +117,77 @@ fn unpublished(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> car
std::task::Poll::Pending => registry.block_until_ready()?,
}
};
if let Some(last) = possibilities.iter().map(|s| s.version()).max() {
if last != current {
results.push((
name.to_string(),
Some(last.to_string()),
current.to_string(),
));
} else {
log::trace!("{name} {current} is published");
}
} else {
results.push((name.to_string(), None, current.to_string()));
}
let (last, published) = possibilities
.iter()
.map(|s| s.version())
.max()
.map(|last| (last.to_string(), last == current))
.unwrap_or(("-".to_string(), false));

results.push(vec![
name.to_string(),
last,
current.to_string(),
if published { "yes" } else { "no" }.to_string(),
]);
}
}
results.sort();

if !results.is_empty() {
results.insert(
0,
(
"name".to_owned(),
Some("published".to_owned()),
"current".to_owned(),
),
);
results.insert(
1,
(
"====".to_owned(),
Some("=========".to_owned()),
"=======".to_owned(),
),
);
}
for (name, last, current) in results {
if let Some(last) = last {
println!("{name} {last} {current}");
} else {
println!("{name} - {current}");
}
if results.is_empty() {
return Ok(());
}

results.insert(
0,
vec![
"name".to_owned(),
"crates.io".to_owned(),
"local".to_owned(),
"published?".to_owned(),
],
);

output_table(results);

Ok(())
}

/// Outputs a markdown table like this.
///
/// ```text
/// | name | crates.io | local | published? |
/// |------------------|-----------|--------|------------|
/// | cargo | 0.70.1 | 0.72.0 | no |
/// | cargo-platform | 0.1.2 | 0.1.2 | yes |
/// | cargo-util | - | 0.2.4 | no |
/// | crates-io | 0.36.0 | 0.36.0 | yes |
/// | home | - | 0.5.6 | no |
/// ```
fn output_table(table: Vec<Vec<String>>) {
let header = table.first().unwrap();
let paddings = table.iter().fold(vec![0; header.len()], |mut widths, row| {
for (width, field) in widths.iter_mut().zip(row) {
*width = usize::max(*width, field.len());
}
widths
});

let print = |row: &[_]| {
for (field, pad) in row.iter().zip(&paddings) {
print!("| {field:pad$} ");
}
println!("|");
};

print(header);

paddings.iter().for_each(|fill| print!("|-{:-<fill$}-", ""));
println!("|");

table.iter().skip(1).for_each(|r| print(r));
}

#[test]
fn verify_cli() {
cli().debug_assert();
Expand Down