Skip to content

Instantly share code, notes, and snippets.

@qtfkwk
Last active December 11, 2022 22:39
Show Gist options
  • Save qtfkwk/0b74b36133c87b9cca19804e05a9ffca to your computer and use it in GitHub Desktop.
Save qtfkwk/0b74b36133c87b9cca19804e05a9ffca to your computer and use it in GitHub Desktop.
Rust cargo-make Makefile.toml

Purpose

Use cargo-make a little bit more like make.

Instructions

  • Install cargo-make: cargo install cargo-make
  • Place Makefile.toml in your Rust project
  • Run cargo make or makers to run the default task fast
  • Optional: create a shell alias: alias m="makers", now build by running m instead of cargo make or makers
  • Optional: install dependencies for the watch task: cargo install b3sum; apt install inotify-tools

Tasks

Meta tasks

  • fast: Default task; runs build, test, clippy, and doc tasks
  • all: Runs check, fast tasks
  • full: Runs all, bench tasks
  • rebuild: Runs clean, dep, all tasks
  • check: Runs outdated, audit tasks

Regular tasks

  • update: Update dependencies
  • outdated: Check for outdated dependencies
  • audit: Check for known vulnerabilities
  • prebuild: Generate binary executable needed for readme and build tasks
  • readme: Generate README.md from README.template.md
  • watch: Watch for writes to Cargo.toml, README.template.md, or src, and if any have changed, clear the screen and run cargo test and cargo make (*nix sh only)
  • build: Build the project
  • test: Run unit and doc tests
  • test-verbose: Run unit and doc tests verbosely
  • bench: Run benchmarks
  • clippy: Run clippy lints
  • doc: Build documentation
  • clean: Delete generated files that should not be committed to version control
  • serve: Serve the directory via miniserve
[tasks.default]
alias = "fast"
[tasks.fast]
dependencies = ["build", "test", "clippy", "doc"]
[tasks.all]
dependencies = ["check", "fast"]
[tasks.full]
dependencies = ["all", "bench"]
[tasks.rebuild]
dependencies = ["clean", "dep", "all"]
[tasks.check]
alias = "my-check"
[tasks.my-check]
dependencies = ["outdated", "audit"]
[tasks.update]
script = '''
cargo update
'''
[tasks.outdated]
alias = "my-outdated"
[tasks.my-outdated]
script = '''
cargo outdated --exit-code 1
'''
[tasks.audit]
alias = "my-audit"
[tasks.my-audit]
script = '''
cargo audit
'''
[tasks.prebuild]
script = '''
cargo build --release --color=always
'''
[tasks.readme]
dependencies = ["prebuild"]
script_runner = "@rust"
script = '''
use std::io::prelude::*;
use std::path::PathBuf;
fn main() {
// Generate README.md from README.template.md
let template = PathBuf::from("README.template.md");
if !template.exists() {
std::fs::File::create(&template).unwrap();
}
let mut i = std::fs::File::open(&template).unwrap();
let mut o = std::fs::File::create("README.md").unwrap();
let mut data = String::new();
i.read_to_string(&mut data).unwrap();
for line in data.lines() {
writeln!(o, "{line}").unwrap();
if let Some(command) = line.strip_prefix("$ ") {
writeln!(o, "{}", pipe(&format!("target/release/{command}")).trim()).unwrap();
}
}
}
fn pipe<T: AsRef<str>>(command: T) -> String {
let (program, args) = split(command);
let child = std::process::Command::new(&program).args(&args).output().unwrap();
std::str::from_utf8(&child.stdout).unwrap().to_string()
}
fn split<T: AsRef<str>>(command: T) -> (String, Vec<String>) {
let mut s = command.as_ref().split(' ');
let program = s.next().unwrap().to_string();
let args = s.map(|x| x.to_string()).collect::<Vec<String>>();
(program, args)
}
'''
[tasks.build]
alias = "my-build"
[tasks.my-build]
dependencies = ["readme"]
script = '''
cargo build --release --color=always
'''
[tasks.test]
alias = "my-test"
[tasks.my-test]
script = '''
cargo test --release --color=always
'''
[tasks.test-verbose]
alias = "my-test-verbose"
[tasks.my-test-verbose]
script = '''
cargo test --release --color=always -- --nocapture --test-threads=1 2>&1 |less -R
'''
[tasks.watch]
script = '''
#!/bin/sh
ARGS="Cargo.toml README.template.md src"
run() { echo "\`\`\`\n\$ $@"; $@; e=$?; echo "\`\`\`\n"; return $e; }
sum() { find ${ARGS} -type f |sort |xargs b3sum; }
build() { run date; RUST_BACKTRACE=1 run cargo test && run cargo make; }
echo; clear; sum >.watch1; build; while true; do inotifywait -qqr ${ARGS}; sum >.watch2;
if ! diff .watch1 .watch2; then clear; build; fi; mv .watch2 .watch1; done
'''
[tasks.bench]
alias = "my-bench"
[tasks.my-bench]
script = '''
cargo bench
'''
[tasks.clippy]
alias = "my-clippy"
[tasks.my-clippy]
script = '''
cargo clippy --release --color=always -- -D clippy::all
'''
[tasks.doc]
script = '''
cargo doc --color=always --no-deps
'''
[tasks.clean]
alias = "my-clean"
[tasks.my-clean]
script = '''
cargo clean
rm -rf .watch1 .watch2
'''
[tasks.serve]
script = '''
miniserve -vp 8080 .
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment