From 3779192aefe56b3c247f04486c3fb4d7879a8ed6 Mon Sep 17 00:00:00 2001 From: Mehul Date: Sun, 8 Dec 2024 23:11:13 -0500 Subject: [PATCH 1/2] feat: add benchmark to workflow --- crates/gh-workflow-tailcall/src/workflow.rs | 23 ++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/gh-workflow-tailcall/src/workflow.rs b/crates/gh-workflow-tailcall/src/workflow.rs index 4c72753..da3436e 100644 --- a/crates/gh-workflow-tailcall/src/workflow.rs +++ b/crates/gh-workflow-tailcall/src/workflow.rs @@ -21,11 +21,14 @@ pub struct Workflow { /// Name of the workflow. pub name: String, + + /// When enabled, a benchmark job is added to the workflow. + pub benchmarks: bool, } impl Default for Workflow { fn default() -> Self { - Self { auto_release: false, name: "CI".into() } + Self { auto_release: false, name: "CI".into(), benchmarks: false } } } @@ -61,7 +64,7 @@ impl From for GHWorkflow { let cond = is_main.and(is_push); // Jobs - let build = build_and_test(); + let build = build_and_test(&value); let mut workflow = GHWorkflow::new(value.name) .add_env(flags) .on(event) @@ -115,8 +118,8 @@ fn release_job(cond: &Context, build: &Job, permissions: &Permissions) -> .add_step(Release::default().command(Command::Release)) } -fn build_and_test() -> Job { - Job::new("Build and Test") +fn build_and_test(value: &Workflow) -> Job { + let mut job = Job::new("Build and Test") .permissions(Permissions::default().contents(Level::Read)) .add_step(Step::checkout()) .add_step( @@ -142,5 +145,15 @@ fn build_and_test() -> Job { .nightly() .args("--all-features --workspace -- -D warnings") .name("Cargo Clippy"), - ) + ); + + if value.benchmarks { + job = job.add_step( + Cargo::new("bench") + .args("--workspace") + .name("Cargo Bench"), + ); + } + + job } From 017b02ce15972060342e5ebbfd2bb94f80eb3e9d Mon Sep 17 00:00:00 2001 From: Mehul Date: Tue, 10 Dec 2024 02:21:56 -0500 Subject: [PATCH 2/2] Convert build_and_test to a method in Workflow --- crates/gh-workflow-tailcall/src/workflow.rs | 83 +++++++++++---------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/crates/gh-workflow-tailcall/src/workflow.rs b/crates/gh-workflow-tailcall/src/workflow.rs index da3436e..eac71dd 100644 --- a/crates/gh-workflow-tailcall/src/workflow.rs +++ b/crates/gh-workflow-tailcall/src/workflow.rs @@ -43,6 +43,47 @@ impl Workflow { pub fn to_github_workflow(&self) -> GHWorkflow { self.clone().into() } + + /// Creates the "Build and Test" job for the workflow. + pub fn build_and_test(&self) -> Job { + let mut job = Job::new("Build and Test") + .permissions(Permissions::default().contents(Level::Read)) + .add_step(Step::checkout()) + .add_step( + Toolchain::default() + .add_stable() + .add_nightly() + .add_clippy() + .add_fmt(), + ) + .add_step( + Cargo::new("test") + .args("--all-features --workspace") + .name("Cargo Test"), + ) + .add_step( + Cargo::new("fmt") + .nightly() + .args("--check") + .name("Cargo Fmt"), + ) + .add_step( + Cargo::new("clippy") + .nightly() + .args("--all-features --workspace -- -D warnings") + .name("Cargo Clippy"), + ); + + if self.benchmarks { + job = job.add_step( + Cargo::new("bench") + .args("--workspace") + .name("Cargo Bench"), + ); + } + + job + } } impl From for GHWorkflow { @@ -64,7 +105,7 @@ impl From for GHWorkflow { let cond = is_main.and(is_push); // Jobs - let build = build_and_test(&value); + let build = value.build_and_test(); let mut workflow = GHWorkflow::new(value.name) .add_env(flags) .on(event) @@ -117,43 +158,3 @@ fn release_job(cond: &Context, build: &Job, permissions: &Permissions) -> .add_step(Step::checkout()) .add_step(Release::default().command(Command::Release)) } - -fn build_and_test(value: &Workflow) -> Job { - let mut job = Job::new("Build and Test") - .permissions(Permissions::default().contents(Level::Read)) - .add_step(Step::checkout()) - .add_step( - Toolchain::default() - .add_stable() - .add_nightly() - .add_clippy() - .add_fmt(), - ) - .add_step( - Cargo::new("test") - .args("--all-features --workspace") - .name("Cargo Test"), - ) - .add_step( - Cargo::new("fmt") - .nightly() - .args("--check") - .name("Cargo Fmt"), - ) - .add_step( - Cargo::new("clippy") - .nightly() - .args("--all-features --workspace -- -D warnings") - .name("Cargo Clippy"), - ); - - if value.benchmarks { - job = job.add_step( - Cargo::new("bench") - .args("--workspace") - .name("Cargo Bench"), - ); - } - - job -}