Skip to content

Commit

Permalink
feat(xtask): First pass at 'cargo xtask unpublished'
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 25, 2023
1 parent 0b20bd9 commit c6f8ee9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ publish = false

[dependencies]
anyhow = "1.0.47"
cargo = { version = "0.71.0", path = "../.." }
cargo = { path = "../.." }
clap = "4.2.0"
env_logger = "0.10.0"
log = "0.4.17"
1 change: 1 addition & 0 deletions crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod unpublished;
mod xtask;

fn main() {
Expand Down
86 changes: 86 additions & 0 deletions crates/xtask/src/unpublished.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use cargo::core::registry::PackageRegistry;
use cargo::core::QueryKind;
use cargo::core::Registry;
use cargo::core::SourceId;
use cargo::util::command_prelude::*;

pub fn cli() -> clap::Command {
clap::Command::new("unpublished")
}

pub fn exec(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> cargo::CliResult {
let ws = args.workspace(config)?;
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() {
let name = member.name();
let current = member.version();
if member.publish() == &Some(vec![]) {
log::trace!("skipping {name}, `publish = false`");
continue;
}

let version_req = format!("<={current}");
let query = cargo::core::dependency::Dependency::parse(
name,
Some(&version_req),
source_id.clone(),
)?;
let possibilities = loop {
// Exact to avoid returning all for path/git
match registry.query_vec(&query, QueryKind::Exact) {
std::task::Poll::Ready(res) => {
break res?;
}
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()));
}
}
}

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}");
}
}

Ok(())
}
14 changes: 14 additions & 0 deletions crates/xtask/src/xtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use cargo::util::command_prelude::*;

pub fn cli() -> clap::Command {
clap::Command::new("xtask")
.subcommand_required(true)
.arg_required_else_help(true)
.arg(
opt(
"verbose",
Expand Down Expand Up @@ -29,11 +31,18 @@ pub fn cli() -> clap::Command {
.action(ArgAction::Append)
.global(true),
)
.subcommands([crate::unpublished::cli()])
}

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

match args.subcommand() {
Some(("unpublished", args)) => crate::unpublished::exec(args, config)?,
Some((name, _)) => unreachable!("clap enforces {name} to not exist"),
None => unreachable!("clap enforces a subcommand is present"),
}

Ok(())
}

Expand Down Expand Up @@ -67,3 +76,8 @@ fn config_configure(config: &mut Config, args: &ArgMatches) -> CliResult {
)?;
Ok(())
}

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

0 comments on commit c6f8ee9

Please sign in to comment.