Skip to content

Commit

Permalink
polish ci
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jul 11, 2023
1 parent e165543 commit a69a294
Showing 1 changed file with 13 additions and 38 deletions.
51 changes: 13 additions & 38 deletions examples/ci.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This CI script for `xshell`.
//!
//! It also serves as a real-world example, yay bootstrap!
use std::{env, process, thread, time::Duration, time::Instant};
use std::{env, process, time::Instant};

use xshell::{cmd, Result, Shell};

Expand All @@ -22,8 +22,12 @@ fn try_main() -> Result<()> {
}

fn test(sh: &Shell) -> Result<()> {
// Can't delete oneself on Windows.
if !cfg!(windows) {
// A good setup for CI is to compile & run in two steps, to get separate feedback for compile
// time and run time. However, because we are using the crate itself to run CI, if we are
// running this, we've already compiled a bunch of stuff. Originally we tried to `rm -rf
// .target`, but we also observed weird SIGKILL: 9 errors on mac. Perhaps its our self-removal?
// Let's scope it only to linux (windows won't work, bc one can not remove oneself there).
if cfg!(linux) {
sh.remove_path("./target")?;
}

Expand All @@ -41,21 +45,9 @@ fn test(sh: &Shell) -> Result<()> {

fn publish(sh: &Shell) -> Result<()> {
let _s = Section::new("PUBLISH");
let manifest = sh.read_file("./Cargo.toml")?;

let version = manifest
.lines()
.find_map(|line| {
let words = line.split_ascii_whitespace().collect::<Vec<_>>();
match words.as_slice() {
[n, "=", v, ..] if n.trim() == "version" => {
assert!(v.starts_with('"') && v.ends_with('"'));
return Some(&v[1..v.len() - 1]);
}
_ => None,
}
})
.unwrap();
let pkgid = cmd!(sh, "cargo pkgid").read()?;
let (_path, version) = pkgid.rsplit_once('#').unwrap();

let tag = format!("v{}", version);
let tags = cmd!(sh, "git tag --list").read()?;
Expand All @@ -64,28 +56,11 @@ fn publish(sh: &Shell) -> Result<()> {
let current_branch = cmd!(sh, "git branch --show-current").read()?;

if current_branch == "master" && !tag_exists {
cmd!(sh, "git tag v{version}").run()?;

// Could also just use `CARGO_REGISTRY_TOKEN` environmental variable.
let token = sh.var("CRATES_IO_TOKEN").unwrap_or("DUMMY_TOKEN".to_string());
{
let _p = sh.push_dir("xshell-macros");
cmd!(sh, "cargo publish --token {token}").run()?;
for _ in 0..100 {
thread::sleep(Duration::from_secs(3));
let err_msg =
cmd!(sh, "cargo install xshell-macros --version {version} --bin non-existing")
.ignore_status()
.read_stderr()?;

let not_found = err_msg.contains("could not find ");
let tried_installing = err_msg.contains("Installing");
assert!(not_found ^ tried_installing);
if tried_installing {
break;
}
}
}
cmd!(sh, "cargo publish --token {token}").run()?;
cmd!(sh, "git tag v{version}").run()?;
cmd!(sh, "cargo publish --token {token} --package xshell-macros").run()?;
cmd!(sh, "cargo publish --token {token} --package xshell").run()?;
cmd!(sh, "git push --tags").run()?;
}
Ok(())
Expand Down

0 comments on commit a69a294

Please sign in to comment.