Skip to content

Commit c6f8ee9

Browse files
committed
feat(xtask): First pass at 'cargo xtask unpublished'
1 parent 0b20bd9 commit c6f8ee9

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/xtask/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ publish = false
66

77
[dependencies]
88
anyhow = "1.0.47"
9-
cargo = { version = "0.71.0", path = "../.." }
9+
cargo = { path = "../.." }
1010
clap = "4.2.0"
1111
env_logger = "0.10.0"
12+
log = "0.4.17"

crates/xtask/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod unpublished;
12
mod xtask;
23

34
fn main() {

crates/xtask/src/unpublished.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use cargo::core::registry::PackageRegistry;
2+
use cargo::core::QueryKind;
3+
use cargo::core::Registry;
4+
use cargo::core::SourceId;
5+
use cargo::util::command_prelude::*;
6+
7+
pub fn cli() -> clap::Command {
8+
clap::Command::new("unpublished")
9+
}
10+
11+
pub fn exec(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> cargo::CliResult {
12+
let ws = args.workspace(config)?;
13+
let mut results = Vec::new();
14+
{
15+
let mut registry = PackageRegistry::new(config)?;
16+
let _lock = config.acquire_package_cache_lock()?;
17+
registry.lock_patches();
18+
let source_id = SourceId::crates_io(config)?;
19+
20+
for member in ws.members() {
21+
let name = member.name();
22+
let current = member.version();
23+
if member.publish() == &Some(vec![]) {
24+
log::trace!("skipping {name}, `publish = false`");
25+
continue;
26+
}
27+
28+
let version_req = format!("<={current}");
29+
let query = cargo::core::dependency::Dependency::parse(
30+
name,
31+
Some(&version_req),
32+
source_id.clone(),
33+
)?;
34+
let possibilities = loop {
35+
// Exact to avoid returning all for path/git
36+
match registry.query_vec(&query, QueryKind::Exact) {
37+
std::task::Poll::Ready(res) => {
38+
break res?;
39+
}
40+
std::task::Poll::Pending => registry.block_until_ready()?,
41+
}
42+
};
43+
if let Some(last) = possibilities.iter().map(|s| s.version()).max() {
44+
if last != current {
45+
results.push((
46+
name.to_string(),
47+
Some(last.to_string()),
48+
current.to_string(),
49+
));
50+
} else {
51+
log::trace!("{name} {current} is published");
52+
}
53+
} else {
54+
results.push((name.to_string(), None, current.to_string()));
55+
}
56+
}
57+
}
58+
59+
if !results.is_empty() {
60+
results.insert(
61+
0,
62+
(
63+
"name".to_owned(),
64+
Some("published".to_owned()),
65+
"current".to_owned(),
66+
),
67+
);
68+
results.insert(
69+
1,
70+
(
71+
"====".to_owned(),
72+
Some("=========".to_owned()),
73+
"=======".to_owned(),
74+
),
75+
);
76+
}
77+
for (name, last, current) in results {
78+
if let Some(last) = last {
79+
println!("{name} {last} {current}");
80+
} else {
81+
println!("{name} - {current}");
82+
}
83+
}
84+
85+
Ok(())
86+
}

crates/xtask/src/xtask.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use cargo::util::command_prelude::*;
22

33
pub fn cli() -> clap::Command {
44
clap::Command::new("xtask")
5+
.subcommand_required(true)
6+
.arg_required_else_help(true)
57
.arg(
68
opt(
79
"verbose",
@@ -29,11 +31,18 @@ pub fn cli() -> clap::Command {
2931
.action(ArgAction::Append)
3032
.global(true),
3133
)
34+
.subcommands([crate::unpublished::cli()])
3235
}
3336

3437
pub fn exec(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> cargo::CliResult {
3538
config_configure(config, args)?;
3639

40+
match args.subcommand() {
41+
Some(("unpublished", args)) => crate::unpublished::exec(args, config)?,
42+
Some((name, _)) => unreachable!("clap enforces {name} to not exist"),
43+
None => unreachable!("clap enforces a subcommand is present"),
44+
}
45+
3746
Ok(())
3847
}
3948

@@ -67,3 +76,8 @@ fn config_configure(config: &mut Config, args: &ArgMatches) -> CliResult {
6776
)?;
6877
Ok(())
6978
}
79+
80+
#[test]
81+
fn verify_cli() {
82+
cli().debug_assert();
83+
}

0 commit comments

Comments
 (0)