Skip to content

Commit

Permalink
feat: Add 'cargo xtask' for custom tasks
Browse files Browse the repository at this point in the history
This commit implements the initial scaffolding of the 'cargo xtask'
pattern for the OmniBOR Rust workspace. In particular, it defines
logging, a basic CLI, a mechanism for pipelining and rollback of steps
in a process, and the rough outline of a "release" subcommand which
will in the future handle releasing new versions of crates in the
workspace.

Signed-off-by: Andrew Lilley Brinker <[email protected]>
  • Loading branch information
alilleybrinker committed Feb 23, 2024
1 parent b03f22c commit 4fd81ec
Show file tree
Hide file tree
Showing 10 changed files with 464 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[alias]
xtask = "run --package xtask --"
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
[workspace]

members = ["omnibor", "gitoid"]
members = ["omnibor", "gitoid", "xtask"]
resolver = "2"

[workspace.package]
edition = "2021"
license = "Apache-2.0"
license-file = "LICENSE"
homepage = "https://omnibor.io"

# Config for 'cargo dist'
[workspace.metadata.dist]
# The preferred cargo-dist version to use in CI (Cargo.toml SemVer syntax)
Expand Down
9 changes: 5 additions & 4 deletions gitoid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[package]
categories = ["cryptography", "development-tools"]
description = "Git Object Identifiers in Rust"
edition = "2021"
homepage = "https://omnibor.io/"
keywords = ["gitbom", "omnibor", "sbom", "gitoid"]
license = "Apache-2.0"
name = "gitoid"
readme = "../README.md"
readme = "README.md"
repository = "https://github.com/omnibor/omnibor-rs"
version = "0.5.1"

homepage.workspace = true
license.workspace = true
edition.workspace = true

[lib]
crate-type = ["rlib", "cdylib"]

Expand Down
7 changes: 4 additions & 3 deletions omnibor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[package]
categories = ["cryptography", "development-tools"]
description = "Reproducible software identity and fine-grained build dependency tracking."
edition = "2021"
homepage = "https://omnibor.io/"
keywords = ["gitbom", "omnibor", "sbom"]
license = "Apache-2.0"
name = "omnibor"
readme = "../README.md"
repository = "https://github.com/omnibor/omnibor-rs"
version = "0.4.0"

homepage.workspace = true
license.workspace = true
edition.workspace = true

[dependencies]

# Library dependencies
Expand Down
17 changes: 17 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "xtask"
description = "Helper tasks for the omnibor project workspace"
version = "0.1.0"
publish = false
readme = "README.md"

homepage.workspace = true
license.workspace = true
edition.workspace = true

[dependencies]
anyhow = "1.0.80"
clap = "4.5.1"
duct = "0.13.7"
env_logger = "0.11.2"
log = "0.4.20"
20 changes: 20 additions & 0 deletions xtask/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `xtask`

This is the `xtask` package for the OmniBOR Rust project. This implements
commonly-used project-wide commands for convenience.

## Design Goals

This crate has a few key design goals:

- __Fast compilation__: This tool will get recompiled whenever changes are
made to it, and we want to empower contributors to the OmniBOR project to
make changes to `xtask` when they encounter a new task for the project that
they want to automate. To make this editing appealing, the write-edit-run
loop needs to be fast, which means fast compilation.
- __Minimal dependencies__: Related to the above, the `xtask` crate should
have a minimal number of dependencies, and where possible those dependencies
should be configured with the minimum number of features.
- __Easy to use__: The commands exposed by `xtask` should have as simple an
interface, and be as automatic, as possible. Fewer flags, fewer required
arguments, etc.
92 changes: 92 additions & 0 deletions xtask/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use clap::{arg, builder::PossibleValue, value_parser, ArgMatches, Command, ValueEnum};
use std::fmt::{Display, Formatter, Result as FmtResult};

pub fn args() -> ArgMatches {
Command::new("xtask")
.about("Task runner for the OmniBOR Rust workspace")
.help_expected(true)
.subcommand(
Command::new("release")
.about("Release a new version of a workspace crate")
.arg(
arg!(-c --crate <CRATE>)
.required(true)
.value_parser(value_parser!(Crate))
.help("the crate to release"),
)
.arg(
arg!(-b --bump <BUMP>)
.required(true)
.value_parser(value_parser!(Bump))
.help("the version to bump"),
)
.arg(
arg!(--execute)
.required(false)
.value_parser(value_parser!(bool))
.help("not a dry run, actually execute the release")
),
)
.get_matches()
}

/// The crate to release; can be "gitoid" or "omnibor"
#[derive(Debug, Clone, Copy)]
pub enum Crate {
GitOid,
OmniBor,
}

impl Display for Crate {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Crate::GitOid => write!(f, "gitoid"),
Crate::OmniBor => write!(f, "omnibor"),
}
}
}

impl ValueEnum for Crate {
fn value_variants<'a>() -> &'a [Self] {
&[Crate::GitOid, Crate::OmniBor]
}

fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Crate::GitOid => PossibleValue::new("gitoid"),
Crate::OmniBor => PossibleValue::new("omnibor"),
})
}
}

/// The version to bump; can be "major", "minor", or "patch"
#[derive(Debug, Clone, Copy)]
pub enum Bump {
Major,
Minor,
Patch,
}

impl Display for Bump {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Bump::Major => write!(f, "major"),
Bump::Minor => write!(f, "minor"),
Bump::Patch => write!(f, "patch"),
}
}
}

impl ValueEnum for Bump {
fn value_variants<'a>() -> &'a [Self] {
&[Bump::Major, Bump::Minor, Bump::Patch]
}

fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Bump::Major => PossibleValue::new("major"),
Bump::Minor => PossibleValue::new("minor"),
Bump::Patch => PossibleValue::new("patch"),
})
}
}
17 changes: 17 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod cli;
mod pipeline;
mod release;

use anyhow::Result;
use env_logger::{Env, Builder as LoggerBuilder};

fn main() -> Result<()> {
LoggerBuilder::from_env(Env::default().default_filter_or("info")).init();

let args = cli::args();

match args.subcommand() {
Some(("release", args)) => release::run(args),
Some(_) | None => Ok(()),
}
}
Loading

0 comments on commit 4fd81ec

Please sign in to comment.