Skip to content

Commit

Permalink
feat: support auto-fix fixes #55
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Dec 11, 2024
1 parent 4513f45 commit bf92cf2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ jobs:
components: clippy, rustfmt
- name: Cargo Test
run: cargo test --all-features --workspace
lint:
name: Lint
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Cargo Fmt
run: cargo +nightly fmt --check
run: cargo +nightly fmt --all --check
- name: Cargo Clippy
run: cargo +nightly clippy --all-features --workspace -- -D warnings
release:
needs:
- build
- lint
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
name: Release
runs-on: ubuntu-latest
Expand All @@ -72,6 +81,7 @@ jobs:
release-pr:
needs:
- build
- lint
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
name: Release PR
runs-on: ubuntu-latest
Expand Down
56 changes: 38 additions & 18 deletions crates/gh-workflow-tailcall/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ pub struct Workflow {

/// When enabled, a benchmark job is added to the workflow.
pub benchmarks: bool,

/// Auto-fixes the code after
pub auto_fix: bool,
}

impl Default for Workflow {
fn default() -> Self {
Self { auto_release: false, name: "CI".into(), benchmarks: false }
Self {
auto_release: false,
name: "CI".into(),
benchmarks: false,
auto_fix: false,
}
}
}

Expand Down Expand Up @@ -58,14 +66,15 @@ impl Workflow {
let cond = is_main.and(is_push);

// Jobs
let build = self.build_and_test();
let permissions = Permissions::default()
.pull_requests(Level::Write)
.packages(Level::Write)
.contents(Level::Write);

let release = Job::new("Release")
.cond(cond.clone())
.add_needs(build.clone())
.add_needs(self.test())
.add_needs(self.lint())
.add_env(Env::github())
.add_env(Env::new(
"CARGO_REGISTRY_TOKEN",
Expand All @@ -81,7 +90,8 @@ impl Workflow {
Concurrency::new(Expression::new("release-${{github.ref}}"))
.cancel_in_progress(false),
)
.add_needs(build.clone())
.add_needs(self.test())
.add_needs(self.lint())
.add_env(Env::github())
.add_env(Env::new(
"CARGO_REGISTRY_TOKEN",
Expand All @@ -94,13 +104,35 @@ impl Workflow {
GHWorkflow::new(self.name.clone())
.add_env(flags)
.on(event)
.add_job("build", build.clone())
.add_job("build", self.test().clone())
.add_job("lint", self.lint())
.add_job_when(self.auto_release, "release", release)
.add_job_when(self.auto_release, "release-pr", release_pr)
}

fn lint(&self) -> Job {
let cargo_fmt_fix = Cargo::new("fmt").nightly().args("--all").name("Cargo Fmt");

let cargo_fmt = cargo_fmt_fix.clone().add_args("--check");

let cargo_clippy = Cargo::new("clippy")
.nightly()
.args("--all-features --workspace -- -D warnings")
.name("Cargo Clippy");

let cargo_clippy_fmt = cargo_clippy.clone().add_args("--fix");

Job::new("Lint")
.permissions(Permissions::default().contents(Level::Read))
.add_step(Step::checkout())
.add_step_when(!self.auto_fix, cargo_fmt)
.add_step_when(self.auto_fix, cargo_fmt_fix)
.add_step_when(!self.auto_fix, cargo_clippy)
.add_step_when(self.auto_fix, cargo_clippy_fmt)
}

/// Creates the "Build and Test" job for the workflow.
fn build_and_test(&self) -> Job {
fn test(&self) -> Job {
Job::new("Build and Test")
.permissions(Permissions::default().contents(Level::Read))
.add_step(Step::checkout())
Expand All @@ -116,18 +148,6 @@ impl Workflow {
.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"),
)
.add_step_when(
self.benchmarks,
Cargo::new("bench").args("--workspace").name("Cargo Bench"),
Expand Down
2 changes: 1 addition & 1 deletion crates/gh-workflow/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use derive_setters::Setters;
use crate::toolchain::Version;
use crate::{Run, Step};

#[derive(Setters)]
#[derive(Clone, Setters)]
#[setters(strip_option, into)]
pub struct Cargo {
/// The command to be executed for eg: fmt, clippy, build, test, etc.
Expand Down

0 comments on commit bf92cf2

Please sign in to comment.