use crate::cargo_ops;
use crate::config::Config;
use crate::topology::Topology;
use crate::ui;
use crate::PublishArgs;
pub fn run(config: Config, args: &PublishArgs) {
if std::env::var("CARGO_REGISTRY_TOKEN").is_err() {
ui::error(
"CARGO_REGISTRY_TOKEN not set. Run:\n \
. .\\env.ps1\n \
The token should be in ~/.cargo/credentials.toml",
);
std::process::exit(1);
}
let topo = match Topology::compute(&config) {
Ok(t) => t,
Err(e) => {
ui::error(&format!("Topology error: {}", e));
std::process::exit(1);
}
};
ui::heading(&format!(
"Publishing {} crates to crates.io ({} levels)",
config.repos.len(),
topo.levels.len(),
));
let mut published = 0usize;
let mut skipped = 0usize;
for level in &topo.levels {
for &crate_idx in level {
let repo = &config.repos[crate_idx];
let version = match cargo_ops::read_version(&repo.abs_dir) {
Ok(v) => v,
Err(e) => {
ui::error(&format!("{} — {}", repo.name, e));
continue;
}
};
ui::info(&format!("{} v{} ...", repo.name, version));
let full_name = repo.name.to_string();
match cargo_ops::version_exists_on_crates_io(&full_name, &version) {
Ok(true) => {
ui::info(&format!(
" {} v{} already on crates.io — skipped",
repo.name, version
));
skipped += 1;
continue;
}
Ok(false) => {} Err(_) => {
ui::warn(" Could not check crates.io; proceeding...");
}
}
if args.dry_run {
ui::info(&format!(
" [dry-run] would publish {} v{}",
repo.name, version
));
published += 1;
continue;
}
match cargo_ops::publish(&repo.abs_dir, false) {
Ok(msg) => {
ui::success(&format!("{} v{} — published ✓", repo.name, version));
if !msg.is_empty() {
println!(" {}", msg);
}
published += 1;
}
Err(e) => {
ui::error(&format!("{} v{} — {}", repo.name, version, e));
ui::error("Stopping publish chain due to failure.");
std::process::exit(1);
}
}
}
}
ui::separator();
ui::summary(published, skipped, 0);
}