Skip to content

Commit 4089e74

Browse files
authored
Update publish script (#1325)
* Handle `curl` errors better by printing them * Add a user-agent to identify this script
1 parent ea0777f commit 4089e74

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

ci/publish.rs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::collections::HashMap;
1111
use std::env;
1212
use std::fs;
1313
use std::path::{Path, PathBuf};
14-
use std::process::{Command, Stdio};
14+
use std::process::{Command, Output, Stdio};
1515
use std::thread;
1616
use std::time::Duration;
1717

@@ -310,19 +310,17 @@ fn publish(krate: &Crate) -> bool {
310310

311311
// First make sure the crate isn't already published at this version. This
312312
// script may be re-run and there's no need to re-attempt previous work.
313-
let output = Command::new("curl")
314-
.arg(&format!("https://crates.io/api/v1/crates/{}", krate.name))
315-
.output()
316-
.expect("failed to invoke `curl`");
317-
if output.status.success()
318-
&& String::from_utf8_lossy(&output.stdout)
319-
.contains(&format!("\"newest_version\":\"{}\"", krate.version))
320-
{
321-
println!(
322-
"skip publish {} because {} is latest version",
323-
krate.name, krate.version,
324-
);
325-
return true;
313+
match curl(&format!("https://crates.io/api/v1/crates/{}", krate.name)) {
314+
Some(output) => {
315+
if output.contains(&format!("\"newest_version\":\"{}\"", krate.version)) {
316+
println!(
317+
"skip publish {} because {} is latest version",
318+
krate.name, krate.version,
319+
);
320+
return true;
321+
}
322+
}
323+
None => return false,
326324
}
327325

328326
let status = Command::new("cargo")
@@ -339,21 +337,20 @@ fn publish(krate: &Crate) -> bool {
339337
// After we've published then make sure that the `wasmtime-publish` group is
340338
// added to this crate for future publications. If it's already present
341339
// though we can skip the `cargo owner` modification.
342-
let output = Command::new("curl")
343-
.arg(&format!(
344-
"https://crates.io/api/v1/crates/{}/owners",
345-
krate.name
346-
))
347-
.output()
348-
.expect("failed to invoke `curl`");
349-
if output.status.success()
350-
&& String::from_utf8_lossy(&output.stdout).contains("wasmtime-publish")
351-
{
352-
println!(
353-
"wasmtime-publish already listed as an owner of {}",
354-
krate.name
355-
);
356-
return true;
340+
match curl(&format!(
341+
"https://crates.io/api/v1/crates/{}/owners",
342+
krate.name
343+
)) {
344+
Some(output) => {
345+
if output.contains("wasmtime-publish") {
346+
println!(
347+
"wasmtime-publish already listed as an owner of {}",
348+
krate.name
349+
);
350+
return true;
351+
}
352+
}
353+
None => return false,
357354
}
358355

359356
// Note that the status is ignored here. This fails most of the time because
@@ -376,6 +373,21 @@ fn publish(krate: &Crate) -> bool {
376373
true
377374
}
378375

376+
fn curl(url: &str) -> Option<String> {
377+
let output = cmd_output(
378+
Command::new("curl")
379+
.arg("--user-agent")
380+
.arg("bytecodealliance/wit-bindgen auto-publish script")
381+
.arg(url),
382+
);
383+
if !output.status.success() {
384+
println!("failed to curl: {}", output.status);
385+
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
386+
return None;
387+
}
388+
Some(String::from_utf8_lossy(&output.stdout).into())
389+
}
390+
379391
// Verify the current tree is publish-able to crates.io. The intention here is
380392
// that we'll run `cargo package` on everything which verifies the build as-if
381393
// it were published to crates.io. This requires using an incrementally-built
@@ -430,3 +442,11 @@ fn verify(crates: &[Crate]) {
430442
.unwrap();
431443
}
432444
}
445+
446+
fn cmd_output(cmd: &mut Command) -> Output {
447+
eprintln!("Running: `{:?}`", cmd);
448+
match cmd.output() {
449+
Ok(o) => o,
450+
Err(e) => panic!("Failed to run `{:?}`: {}", cmd, e),
451+
}
452+
}

0 commit comments

Comments
 (0)