Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modernize CI #197

Merged
merged 1 commit into from Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Expand Up @@ -6,4 +6,4 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
edition = "2018"

[dependencies]
xaction = "0.2.1"
xshell = "0.2.2"
143 changes: 82 additions & 61 deletions xtask/src/main.rs
@@ -1,95 +1,116 @@
use std::env;
#[cfg(test)]
mod tidy;

use xaction::{cargo_toml, cmd, cp, git, push_rustup_toolchain, rm_rf, section, Result};
use std::time::Instant;

const MSRV: &str = "1.36.0";
use xshell::{cmd, Shell};

fn main() {
if let Err(err) = try_main() {
eprintln!("error: {}", err);
std::process::exit(1)
}
}
const MSRV: &str = "1.36.0";

fn try_main() -> Result<()> {
let subcommand = std::env::args().nth(1);
match subcommand {
Some(it) if it == "ci" => (),
_ => {
print_usage();
Err("invalid arguments")?
}
}
fn main() -> xshell::Result<()> {
let sh = Shell::new()?;

let cargo_toml = cargo_toml()?;
let _e = push_toolchain(&sh, "stable")?;

{
let _s = section("TEST_STABLE");
let _t = push_rustup_toolchain("stable");
cmd!("cargo test --features unstable").run()?;
cmd!("cargo test --features unstable --release").run()?;

// Skip doctests, they need `std`
cmd!("cargo test --features unstable --no-default-features --test it").run()?;
let _s = section("BUILD");
cmd!(sh, "cargo test --workspace --no-run").run()?;
}

cmd!("cargo test --features 'unstable std parking_lot' --no-default-features").run()?;
cmd!("cargo test --features 'unstable std parking_lot' --no-default-features --release")
{
let _s = section("TEST");

for &release in &[None, Some("--release")] {
cmd!(sh, "cargo test --features unstable {release...}").run()?;
cmd!(
sh,
"cargo test --no-default-features --features unstable,std,parking_lot {release...}"
)
.run()?;
}

cmd!("cargo test --features 'unstable alloc' --no-default-features --test it").run()?;
cmd!("cargo test --features 'unstable std parking_lot alloc' --no-default-features")
.run()?;
// Skip doctests for no_std tests as those don't work
cmd!(sh, "cargo test --no-default-features --features unstable --test it").run()?;
cmd!(sh, "cargo test --no-default-features --features unstable,alloc --test it").run()?;
}

{
let _s = section("TEST_BETA");
let _t = push_rustup_toolchain("beta");
cmd!("cargo test --features unstable").run()?;
cmd!("cargo test --features unstable --release").run()?;
let _e = push_toolchain(&sh, "beta")?;
cmd!(sh, "cargo test --features unstable").run()?;
}

{
let _s = section("TEST_MSRV");
let _t = push_rustup_toolchain(MSRV);
cp("Cargo.lock.msrv", "Cargo.lock")?;
cmd!("cargo build").run()?;
let _e = push_toolchain(&sh, MSRV)?;
sh.copy_file("Cargo.lock.msrv", "Cargo.lock")?;
cmd!(sh, "cargo build").run()?;
}

{
let _s = section("TEST_MIRI");
rm_rf("./target")?;
let miri_nightly= cmd!(sh, "curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri").read()?;
let _e = push_toolchain(&sh, &format!("nightly-{}", miri_nightly))?;

let miri_nightly= cmd!("curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri").read()?;
let _t = push_rustup_toolchain(&format!("nightly-{}", miri_nightly));
sh.remove_path("./target")?;

cmd!("rustup component add miri").run()?;
cmd!("cargo miri setup").run()?;
cmd!("cargo miri test --features unstable").run()?;
cmd!(sh, "rustup component add miri").run()?;
cmd!(sh, "cargo miri setup").run()?;
cmd!(sh, "cargo miri test --features unstable").run()?;
}

let version = cargo_toml.version()?;
let tag = format!("v{}", version);

let dry_run =
env::var("CI").is_err() || git::has_tag(&tag)? || git::current_branch()? != "master";
xaction::set_dry_run(dry_run);

{
let _s = section("PUBLISH");
cargo_toml.publish()?;
git::tag(&tag)?;
git::push_tags()?;

let version = cmd!(sh, "cargo pkgid").read()?.rsplit_once('#').unwrap().1.to_string();
let tag = format!("v{version}");

let current_branch = cmd!(sh, "git branch --show-current").read()?;
let has_tag = cmd!(sh, "git tag --list").read()?.lines().any(|it| it.trim() == tag);
let dry_run = sh.var("CI").is_err() || has_tag || current_branch != "master";
eprintln!("Publishing{}!", if dry_run { " (dry run)" } else { "" });

let dry_run_arg = if dry_run { Some("--dry-run") } else { None };
cmd!(sh, "cargo publish {dry_run_arg...}").run()?;
if dry_run {
eprintln!("{}", cmd!(sh, "git tag {tag}"));
eprintln!("{}", cmd!(sh, "git push --tags"));
} else {
cmd!(sh, "git tag {tag}").run()?;
cmd!(sh, "git push --tags").run()?;
}
}
Ok(())
}

fn print_usage() {
eprintln!(
"\
Usage: cargo run -p xtask <SUBCOMMAND>
fn push_toolchain<'a>(
sh: &'a xshell::Shell,
toolchain: &str,
) -> xshell::Result<xshell::PushEnv<'a>> {
cmd!(sh, "rustup toolchain install {toolchain} --no-self-update").run()?;
let res = sh.push_env("RUSTUP_TOOLCHAIN", toolchain);
cmd!(sh, "rustc --version").run()?;
Ok(res)
}

fn section(name: &'static str) -> impl Drop {
println!("::group::{name}");
let start = Instant::now();
defer(move || {
let elapsed = start.elapsed();
eprintln!("{name}: {elapsed:.2?}");
println!("::endgroup::");
})
}

SUBCOMMANDS:
ci
"
)
fn defer<F: FnOnce()>(f: F) -> impl Drop {
struct D<F: FnOnce()>(Option<F>);
impl<F: FnOnce()> Drop for D<F> {
fn drop(&mut self) {
if let Some(f) = self.0.take() {
f()
}
}
}
D(Some(f))
}
7 changes: 7 additions & 0 deletions xtask/src/tidy.rs
@@ -0,0 +1,7 @@
use xshell::{cmd, Shell};

#[test]
fn test_formatting() {
let sh = Shell::new().unwrap();
cmd!(sh, "cargo fmt --all -- --check").run().unwrap()
}
6 changes: 0 additions & 6 deletions xtask/tests/tidy.rs

This file was deleted.