|
| 1 | +use std::{ |
| 2 | + env, fs, |
| 3 | + process::{self, Command, ExitStatus, Stdio}, |
| 4 | + time::Instant, |
| 5 | +}; |
| 6 | + |
| 7 | +type Error = Box<dyn std::error::Error>; |
| 8 | +type Result<T> = std::result::Result<T, Error>; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + if let Err(err) = try_main() { |
| 12 | + eprintln!("{}", err); |
| 13 | + process::exit(1); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +fn try_main() -> Result<()> { |
| 18 | + let cwd = env::current_dir()?; |
| 19 | + let cargo_toml = cwd.join("Cargo.toml"); |
| 20 | + assert!( |
| 21 | + cargo_toml.exists(), |
| 22 | + "Cargo.toml not found, cwd: {}", |
| 23 | + cwd.display() |
| 24 | + ); |
| 25 | + |
| 26 | + { |
| 27 | + let _s = Section::new("BUILD"); |
| 28 | + shell("cargo test --workspace --no-run")?; |
| 29 | + } |
| 30 | + |
| 31 | + { |
| 32 | + let _s = Section::new("TEST"); |
| 33 | + shell("cargo test --workspace")?; |
| 34 | + } |
| 35 | + |
| 36 | + let current_branch = shell_output("git branch --show-current")?; |
| 37 | + if ¤t_branch == "master" { |
| 38 | + let _s = Section::new("PUBLISH"); |
| 39 | + let manifest = fs::read_to_string(&cargo_toml)?; |
| 40 | + let version = get_field(&manifest, "version")?; |
| 41 | + let tag = format!("v{}", version); |
| 42 | + let tags = shell_output("git tag --list")?; |
| 43 | + |
| 44 | + if !tags.contains(&tag) { |
| 45 | + let token = env::var("CRATES_IO_TOKEN").unwrap(); |
| 46 | + shell(&format!("git tag v{}", version))?; |
| 47 | + shell(&format!("cargo publish --token {}", token))?; |
| 48 | + shell("git push --tags")?; |
| 49 | + } |
| 50 | + } |
| 51 | + Ok(()) |
| 52 | +} |
| 53 | + |
| 54 | +fn get_field<'a>(text: &'a str, name: &str) -> Result<&'a str> { |
| 55 | + for line in text.lines() { |
| 56 | + let words = line.split_ascii_whitespace().collect::<Vec<_>>(); |
| 57 | + match words.as_slice() { |
| 58 | + [n, "=", v, ..] if n.trim() == name => { |
| 59 | + assert!(v.starts_with('"') && v.ends_with('"')); |
| 60 | + return Ok(&v[1..v.len() - 1]); |
| 61 | + } |
| 62 | + _ => (), |
| 63 | + } |
| 64 | + } |
| 65 | + Err(format!("can't find `{}` in\n----\n{}\n----\n", name, text))? |
| 66 | +} |
| 67 | + |
| 68 | +fn shell(cmd: &str) -> Result<()> { |
| 69 | + let status = command(cmd).status()?; |
| 70 | + check_status(status) |
| 71 | +} |
| 72 | + |
| 73 | +fn shell_output(cmd: &str) -> Result<String> { |
| 74 | + let output = command(cmd).stderr(Stdio::inherit()).output()?; |
| 75 | + check_status(output.status)?; |
| 76 | + let res = String::from_utf8(output.stdout)?; |
| 77 | + Ok(res.trim().to_string()) |
| 78 | +} |
| 79 | + |
| 80 | +fn command(cmd: &str) -> Command { |
| 81 | + eprintln!("> {}", cmd); |
| 82 | + let words = cmd.split_ascii_whitespace().collect::<Vec<_>>(); |
| 83 | + let (cmd, args) = words.split_first().unwrap(); |
| 84 | + let mut res = Command::new(cmd); |
| 85 | + res.args(args); |
| 86 | + res |
| 87 | +} |
| 88 | + |
| 89 | +fn check_status(status: ExitStatus) -> Result<()> { |
| 90 | + if !status.success() { |
| 91 | + Err(format!("$status: {}", status))?; |
| 92 | + } |
| 93 | + Ok(()) |
| 94 | +} |
| 95 | + |
| 96 | +struct Section { |
| 97 | + name: &'static str, |
| 98 | + start: Instant, |
| 99 | +} |
| 100 | + |
| 101 | +impl Section { |
| 102 | + fn new(name: &'static str) -> Section { |
| 103 | + println!("::group::{}", name); |
| 104 | + let start = Instant::now(); |
| 105 | + Section { name, start } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl Drop for Section { |
| 110 | + fn drop(&mut self) { |
| 111 | + eprintln!("{}: {:.2?}", self.name, self.start.elapsed()); |
| 112 | + println!("::endgroup::"); |
| 113 | + } |
| 114 | +} |
0 commit comments