Skip to content

Commit

Permalink
update toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Nov 6, 2024
1 parent cf4c41f commit 369d3e2
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 28 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ on:
permissions:
contents: read
jobs:
test:
name: Run tests
rust:
name: Rust Job
runs-on: ubuntu-latest
steps:
- name: Checkout code
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup rust
- name: Setup Rust Toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
- name: Run tests
- name: Run Cargo Test
run: RUSTFLAGS="-Awarnings" cargo test --all-features --workspace
timeout-minutes: 10
- name: Check Formatting
run: cargo fmt -- --check
- name: Run Clippy
run: cargo clippy -- -D warnings
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"cSpell.words": ["Gollum", "quicktype", "rustfmt"]
"cSpell.words": [
"Awarnings",
"Gollum",
"quicktype",
"rustfmt"
]
}
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion workspace/gh-workflow-gen/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use gh_workflow::toolchain::RustToolchain;

fn main() {
let toolchain = RustToolchain::default();
let toolchain = RustToolchain::default()
.workspace(true)
.fmt(true)
.clippy(true);
toolchain
.to_workflow()
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions workspace/gh-workflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
async-trait = "0.1.83"
convert_case = "0.6.0"
derive_more = { version = "1.0.0", features = ["from"] }
derive_setters = "0.1.6"
indexmap = {version = "2.6.0", features = ["serde"]}
Expand Down
49 changes: 36 additions & 13 deletions workspace/gh-workflow/src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::workflow::*;
/// A type-safe representation of the Rust toolchain.
/// Instead of writing the github action for Rust by hand, we can use this
/// struct to generate the github action.
#[derive(Default)]
#[derive(Default, Clone)]
pub enum Version {
#[default]
Stable,
Expand All @@ -27,30 +27,53 @@ impl ToString for Version {
}
}

#[derive(Setters, Default)]
#[derive(Setters, Default, Clone)]
#[setters(strip_option)]
pub struct RustToolchain {
version: Version,
fmt: bool,
clippy: bool,
// TODO: add more rust tool chain components
timeout: Option<Duration>,
workspace: bool,
}

impl RustToolchain {
pub fn to_workflow(&self) -> Result<Workflow> {
let job = Job::new("Run tests")
let mut job = Job::new("Rust Job")
.runs_on("ubuntu-latest")
.timeout(Duration::from_secs(10 * 60))
.add_step(Step::uses("actions", "checkout", 4).name("Checkout code"))
.add_step(Step::uses("actions", "checkout", 4).name("Checkout Code"))
.add_step(
Step::uses("actions-rust-lang", "setup-rust-toolchain", 1)
.name("Setup rust")
.with(("toolchain", "stable")),
)
.add_step(
Step::run("RUSTFLAGS=\"-Awarnings\" cargo test --all-features --workspace")
.name("Run tests"),
.name("Setup Rust Toolchain")
.with(("toolchain", self.version.clone())),
);

if let Some(timeout) = self.timeout {
job = job.timeout(timeout);
}

let mut cargo_test_args = vec!["--all-features"];

if self.workspace {
cargo_test_args.push("--workspace");
}

job = job.add_step(
Step::run(format!(
"RUSTFLAGS=\"-Awarnings\" cargo test {}",
cargo_test_args.join(" ")
))
.name("Run Cargo Test"),
);

if self.fmt {
job = job.add_step(Step::run("cargo fmt -- --check").name("Check formatting"));
}

if self.clippy {
job = job.add_step(Step::run("cargo clippy -- -D warnings").name("Run clippy"));
}

Workflow::new("CI")
.permissions(Permissions::read())
.on(vec![
Expand All @@ -64,6 +87,6 @@ impl RustToolchain {
],
),
])
.add_job("test", job)
.add_job("rust", job)
}
}
12 changes: 5 additions & 7 deletions workspace/gh-workflow/src/workflow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{path::Path, time::Duration};

use convert_case::{Case, Casing};
use derive_setters::Setters;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -86,10 +87,7 @@ pub struct EventAction {

impl Workflow {
pub fn new<T: ToString>(name: T) -> Self {
Self {
name: Some(name.to_string()),
..Default::default()
}
Self { name: Some(name.to_string()), ..Default::default() }
}
pub fn to_string(&self) -> Result<String> {
Ok(serde_yaml::to_string(self)?)
Expand Down Expand Up @@ -365,7 +363,7 @@ pub struct Step<T> {

impl<T> Step<T> {
pub fn name<S: ToString>(mut self, name: S) -> Self {
self.name = Some(name.to_string());
self.name = Some(name.to_string().to_case(Case::Title));
self
}
}
Expand Down Expand Up @@ -447,8 +445,8 @@ pub trait Apply<Value> {
fn apply(self, value: Value) -> Value;
}

impl<Ty, S: ToString> Apply<Step<Ty>> for (S, S) {
fn apply(self, mut step: Step<Ty>) -> Step<Ty> {
impl<S1: ToString, S2: ToString> Apply<Step<Use>> for (S1, S2) {
fn apply(self, mut step: Step<Use>) -> Step<Use> {
let mut index_map: IndexMap<String, Value> = step.with.unwrap_or_default();
index_map.insert(self.0.to_string(), Value::String(self.1.to_string()));
step.with = Some(index_map);
Expand Down

0 comments on commit 369d3e2

Please sign in to comment.