]> git.proxmox.com Git - rustc.git/blob - vendor/xshell/examples/ci.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / vendor / xshell / examples / ci.rs
1 //! This CI script for `xshell`.
2 //!
3 //! It also serves as a real-world example, yay bootstrap!
4 use std::{env, process, time::Instant};
5
6 use xshell::{cmd, Result, Shell};
7
8 fn main() {
9 if let Err(err) = try_main() {
10 eprintln!("{}", err);
11 process::exit(1);
12 }
13 }
14
15 fn try_main() -> Result<()> {
16 let sh = Shell::new()?;
17 if env::args().nth(1).as_deref() == Some("publish") {
18 publish(&sh)
19 } else {
20 test(&sh)
21 }
22 }
23
24 fn test(sh: &Shell) -> Result<()> {
25 // A good setup for CI is to compile & run in two steps, to get separate feedback for compile
26 // time and run time. However, because we are using the crate itself to run CI, if we are
27 // running this, we've already compiled a bunch of stuff. Originally we tried to `rm -rf
28 // .target`, but we also observed weird SIGKILL: 9 errors on mac. Perhaps its our self-removal?
29 // Let's scope it only to linux (windows won't work, bc one can not remove oneself there).
30 if cfg!(linux) {
31 sh.remove_path("./target")?;
32 }
33
34 {
35 let _s = Section::new("BUILD");
36 cmd!(sh, "cargo test --workspace --no-run").run()?;
37 }
38
39 {
40 let _s = Section::new("TEST");
41 cmd!(sh, "cargo test --workspace").run()?;
42 }
43 Ok(())
44 }
45
46 fn publish(sh: &Shell) -> Result<()> {
47 let _s = Section::new("PUBLISH");
48
49 let pkgid = cmd!(sh, "cargo pkgid").read()?;
50 let (_path, version) = pkgid.rsplit_once('#').unwrap();
51
52 let tag = format!("v{}", version);
53 let tags = cmd!(sh, "git tag --list").read()?;
54 let tag_exists = tags.split_ascii_whitespace().any(|it| it == &tag);
55
56 let current_branch = cmd!(sh, "git branch --show-current").read()?;
57
58 if current_branch == "master" && !tag_exists {
59 // Could also just use `CARGO_REGISTRY_TOKEN` environmental variable.
60 let token = sh.var("CRATES_IO_TOKEN").unwrap_or("DUMMY_TOKEN".to_string());
61 cmd!(sh, "git tag v{version}").run()?;
62 cmd!(sh, "cargo publish --token {token} --package xshell-macros").run()?;
63 cmd!(sh, "cargo publish --token {token} --package xshell").run()?;
64 cmd!(sh, "git push --tags").run()?;
65 }
66 Ok(())
67 }
68
69 struct Section {
70 name: &'static str,
71 start: Instant,
72 }
73
74 impl Section {
75 fn new(name: &'static str) -> Section {
76 println!("::group::{}", name);
77 let start = Instant::now();
78 Section { name, start }
79 }
80 }
81
82 impl Drop for Section {
83 fn drop(&mut self) {
84 println!("{}: {:.2?}", self.name, self.start.elapsed());
85 println!("::endgroup::");
86 }
87 }