1 //! This CI script for `xshell`.
3 //! It also serves as a real-world example, yay bootstrap!
4 use std
::{env, process, time::Instant}
;
6 use xshell
::{cmd, Result, Shell}
;
9 if let Err(err
) = try_main() {
15 fn try_main() -> Result
<()> {
16 let sh
= Shell
::new()?
;
17 if env
::args().nth(1).as_deref() == Some("publish") {
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).
31 sh
.remove_path("./target")?
;
35 let _s
= Section
::new("BUILD");
36 cmd
!(sh
, "cargo test --workspace --no-run").run()?
;
40 let _s
= Section
::new("TEST");
41 cmd
!(sh
, "cargo test --workspace").run()?
;
46 fn publish(sh
: &Shell
) -> Result
<()> {
47 let _s
= Section
::new("PUBLISH");
49 let pkgid
= cmd
!(sh
, "cargo pkgid").read()?
;
50 let (_path
, version
) = pkgid
.rsplit_once('
#').unwrap();
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
);
56 let current_branch
= cmd
!(sh
, "git branch --show-current").read()?
;
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()?
;
75 fn new(name
: &'
static str) -> Section
{
76 println
!("::group::{}", name
);
77 let start
= Instant
::now();
78 Section { name, start }
82 impl Drop
for Section
{
84 println
!("{}: {:.2?}", self.name
, self.start
.elapsed());
85 println
!("::endgroup::");