Skip to content

Commit ecda55e

Browse files
YgorSouzaemilk
authored andcommitted
Add xtask crate (emilk#4293)
Replaces only the cargo_deny.sh script for now. Can be expanded over time to replace the other shell and python scripts, so only Rust is needed to work with the repository. Closes <emilk#2887> Closes <emilk#4373> --------- Co-authored-by: Emil Ernerfeldt <[email protected]>
1 parent 0fed59d commit ecda55e

File tree

9 files changed

+176
-19
lines changed

9 files changed

+176
-19
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
# we don't use `[build]` because of rust analyzer's build cache invalidation https://github.com/emilk/eframe_template/issues/93
55
[target.wasm32-unknown-unknown]
66
rustflags = ["--cfg=web_sys_unstable_apis"]
7+
8+
[alias]
9+
xtask = "run --quiet --package xtask --"

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ members = [
1414
"crates/epaint",
1515

1616
"examples/*",
17+
18+
"xtask",
1719
]
1820

1921
[workspace.package]

scripts/cargo_deny.sh

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
11
#!/usr/bin/env bash
22

3-
set -eu
4-
script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
5-
cd "$script_path/.."
6-
set -x
7-
8-
cargo install --quiet cargo-deny
9-
10-
cargo deny --all-features --log-level error --target aarch64-apple-darwin check
11-
cargo deny --all-features --log-level error --target aarch64-linux-android check
12-
cargo deny --all-features --log-level error --target i686-pc-windows-gnu check
13-
cargo deny --all-features --log-level error --target i686-pc-windows-msvc check
14-
cargo deny --all-features --log-level error --target i686-unknown-linux-gnu check
15-
cargo deny --all-features --log-level error --target wasm32-unknown-unknown check
16-
cargo deny --all-features --log-level error --target x86_64-apple-darwin check
17-
cargo deny --all-features --log-level error --target x86_64-pc-windows-gnu check
18-
cargo deny --all-features --log-level error --target x86_64-pc-windows-msvc check
19-
cargo deny --all-features --log-level error --target x86_64-unknown-linux-gnu check
20-
cargo deny --all-features --log-level error --target x86_64-unknown-linux-musl check
21-
cargo deny --all-features --log-level error --target x86_64-unknown-redox check
3+
cargo xtask deny

xtask/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "xtask"
3+
edition.workspace = true
4+
license.workspace = true
5+
rust-version.workspace = true
6+
version.workspace = true
7+
publish = false
8+
9+
[dependencies]

xtask/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## xtask - Task automation
2+
3+
This crate is meant to automate common tasks on the repository. It serves as a
4+
replacement for shell scripts that is more portable across host operating
5+
systems (namely Windows) and hopefully also easier to work with for
6+
contributors who are already familiar with Rust (and not necessarily with shell
7+
scripting).
8+
9+
The executable can be invoked via the subcommand `cargo xtask`, thanks to an
10+
alias defined in `.cargo/config.toml`.
11+
12+
For more information, see <https://github.com/matklad/cargo-xtask>.

xtask/src/deny.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Run `cargo deny`
2+
//!
3+
//! Also installs the subcommand if it is not already installed.
4+
5+
use std::process::Command;
6+
7+
use super::DynError;
8+
9+
pub fn deny(args: &[&str]) -> Result<(), DynError> {
10+
if !args.is_empty() {
11+
return Err(format!("Invalid arguments: {args:?}").into());
12+
}
13+
install_cargo_deny()?;
14+
let targets = [
15+
"aarch64-apple-darwin",
16+
"aarch64-linux-android",
17+
"i686-pc-windows-gnu",
18+
"i686-pc-windows-msvc",
19+
"i686-unknown-linux-gnu",
20+
"wasm32-unknown-unknown",
21+
"x86_64-apple-darwin",
22+
"x86_64-pc-windows-gnu",
23+
"x86_64-pc-windows-msvc",
24+
"x86_64-unknown-linux-gnu",
25+
"x86_64-unknown-linux-musl",
26+
"x86_64-unknown-redox",
27+
];
28+
for target in targets {
29+
let mut cmd = Command::new("cargo");
30+
cmd.args([
31+
"deny",
32+
"--all-features",
33+
"--log-level",
34+
"error",
35+
"--target",
36+
target,
37+
"check",
38+
]);
39+
super::utils::print_cmd(&cmd);
40+
let status = cmd.status()?;
41+
if !status.success() {
42+
return Err(status.to_string().into());
43+
}
44+
}
45+
Ok(())
46+
}
47+
48+
fn install_cargo_deny() -> Result<(), DynError> {
49+
let already_installed = Command::new("cargo")
50+
.args(["deny", "--version"])
51+
.output()
52+
.is_ok_and(|out| out.status.success());
53+
if already_installed {
54+
return Ok(());
55+
}
56+
let mut cmd = Command::new("cargo");
57+
cmd.args(["+stable", "install", "--quiet", "--locked", "cargo-deny"]);
58+
let reason = "install cargo-deny";
59+
super::utils::ask_to_run(cmd, true, reason)
60+
}

xtask/src/main.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![allow(clippy::print_stdout)]
2+
#![allow(clippy::print_stderr)]
3+
#![allow(clippy::exit)]
4+
5+
mod deny;
6+
pub(crate) mod utils;
7+
8+
type DynError = Box<dyn std::error::Error>;
9+
10+
fn main() {
11+
if let Err(e) = try_main() {
12+
eprintln!("{e}");
13+
std::process::exit(-1);
14+
}
15+
}
16+
17+
fn try_main() -> Result<(), DynError> {
18+
let arg_strings: Vec<_> = std::env::args().skip(1).collect();
19+
let args: Vec<_> = arg_strings.iter().map(String::as_str).collect();
20+
21+
match args.as_slice() {
22+
&[] | &["-h"] | &["--help"] => print_help(),
23+
&["deny", ..] => deny::deny(&args[1..])?,
24+
c => Err(format!("Invalid arguments {c:?}"))?,
25+
}
26+
Ok(())
27+
}
28+
29+
fn print_help() {
30+
let help = "
31+
xtask help
32+
33+
Subcommands
34+
deny: Run cargo-deny for all targets
35+
36+
Options
37+
-h, --help: print help and exit
38+
";
39+
println!("{help}");
40+
}

xtask/src/utils.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::{
2+
env,
3+
io::{self, Write as _},
4+
process::Command,
5+
};
6+
7+
use super::DynError;
8+
9+
/// Print the command and its arguments as if the user had typed them
10+
pub fn print_cmd(cmd: &Command) {
11+
print!("{} ", cmd.get_program().to_string_lossy());
12+
for arg in cmd.get_args() {
13+
print!("{} ", arg.to_string_lossy());
14+
}
15+
println!();
16+
}
17+
18+
/// Prompt user before running a command
19+
///
20+
/// Adapted from [miri](https://github.com/rust-lang/miri/blob/dba35d2be72f4b78343d1a0f0b4737306f310672/cargo-miri/src/util.rs#L181-L204)
21+
pub fn ask_to_run(mut cmd: Command, ask: bool, reason: &str) -> Result<(), DynError> {
22+
// Disable interactive prompts in CI (GitHub Actions, Travis, AppVeyor, etc).
23+
// Azure doesn't set `CI` though (nothing to see here, just Microsoft being Microsoft),
24+
// so we also check their `TF_BUILD`.
25+
let is_ci = env::var_os("CI").is_some() || env::var_os("TF_BUILD").is_some();
26+
if ask && !is_ci {
27+
let mut buf = String::new();
28+
print!("The script is going to run: \n\n`{cmd:?}`\n\n To {reason}.\nProceed? [Y/n] ",);
29+
io::stdout().flush().unwrap();
30+
io::stdin().read_line(&mut buf).unwrap();
31+
match buf.trim().to_lowercase().as_ref() {
32+
"" | "y" | "yes" => {}
33+
"n" | "no" => return Err("Aborting as per your request".into()),
34+
a => return Err(format!("Invalid answer `{a}`").into()),
35+
};
36+
} else {
37+
eprintln!("Running `{cmd:?}` to {reason}.");
38+
}
39+
40+
let status = cmd.status()?;
41+
if !status.success() {
42+
return Err(format!("failed to {reason}: {status}").into());
43+
}
44+
Ok(())
45+
}

0 commit comments

Comments
 (0)