diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5584db0..7a4b3f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ # ----------------------------- END WARNING ------------------------- # ------------------------------------------------------------------- -name: CI +name: Build and Test env: RUSTFLAGS: -Dwarnings on: diff --git a/README.md b/README.md index 7da524f..ed7c9e2 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,12 @@ Then you can start creating GitHub Actions in your [build.rs](https://github.com - Simply add a `build.rs` file to your project's root directory. - Add the following code to generate the GitHub Actions workflow: - ```rust +```rust use rust_gh_workflows::*; fn main() { - Workflow::new("CI") + // Create a workflow + let workflow = Workflow::new("CI") .permissions(Permissions::read()) .on(Event::default().push(Push::default().branch("main")) .add_job( @@ -48,9 +49,10 @@ Then you can start creating GitHub Actions in your [build.rs](https://github.com .add_step(Step::setup_rust().add_toolchain(Toolchain::Stable)) .add_step(Step::cargo("test", vec!["--all-features", "--workspace"])) ) - .unwrap() - .generate() .unwrap(); + + // Generate the ci.yml + workflow.generate().unwrap(); } ``` diff --git a/workspace/gh-workflow-gen/build.rs b/workspace/gh-workflow-gen/build.rs index 84fd2a0..03b61c3 100644 --- a/workspace/gh-workflow-gen/build.rs +++ b/workspace/gh-workflow-gen/build.rs @@ -1,39 +1,5 @@ use gh_workflow::*; fn main() { - let job_build = Job::new("Build and Test") - .add_step(Step::checkout()) - .add_step( - Step::setup_rust() - .toolchain_stable() - .toolchain_nightly() - .component_clippy() - .component_rustfmt(), - ) - // TODO: Improve type-safety and intellisense - .add_step(Step::cargo("test", vec!["--all-features", "--workspace"])) - .add_step(Step::cargo_nightly("fmt", vec!["--check"])) - .add_step(Step::cargo_nightly( - "clippy", - vec!["--all-features", "--workspace", "--", "-D warnings"], - )); - - let on_push = Event::push().branch("main"); - - let on_pull_request = Event::pull_request_target() - .open() - .synchronize() - .reopen() - .branch("main"); - - let rust_flags = RustFlags::deny("warnings"); - - Workflow::new("CI") - .env(rust_flags) - .permissions(Permissions::read()) - .on(on_push) - .on(on_pull_request) - .add_job("build", job_build) - .generate() - .unwrap(); + Workflow::setup_rust().generate().unwrap(); } diff --git a/workspace/gh-workflow/README.md b/workspace/gh-workflow/README.md index 7da524f..2c5f401 100644 --- a/workspace/gh-workflow/README.md +++ b/workspace/gh-workflow/README.md @@ -38,7 +38,8 @@ Then you can start creating GitHub Actions in your [build.rs](https://github.com use rust_gh_workflows::*; fn main() { - Workflow::new("CI") + // Create a workflow + let workflow = Workflow::new("CI") .permissions(Permissions::read()) .on(Event::default().push(Push::default().branch("main")) .add_job( @@ -48,9 +49,10 @@ Then you can start creating GitHub Actions in your [build.rs](https://github.com .add_step(Step::setup_rust().add_toolchain(Toolchain::Stable)) .add_step(Step::cargo("test", vec!["--all-features", "--workspace"])) ) - .unwrap() - .generate() .unwrap(); + + // Generate the ci.yml + workflow.generate().unwrap(); } ``` diff --git a/workspace/gh-workflow/src/toolchain.rs b/workspace/gh-workflow/src/toolchain.rs index a0fbff6..6f75f45 100644 --- a/workspace/gh-workflow/src/toolchain.rs +++ b/workspace/gh-workflow/src/toolchain.rs @@ -165,22 +165,22 @@ impl ToolchainStep { self } - pub fn toolchain_stable(mut self) -> Self { + pub fn with_stable_toolchain(mut self) -> Self { self.toolchain.push(Toolchain::Stable); self } - pub fn toolchain_nightly(mut self) -> Self { + pub fn with_nightly_toolchain(mut self) -> Self { self.toolchain.push(Toolchain::Nightly); self } - pub fn component_clippy(mut self) -> Self { + pub fn with_clippy(mut self) -> Self { self.components.push(Component::Clippy); self } - pub fn component_rustfmt(mut self) -> Self { + pub fn with_fmt(mut self) -> Self { self.components.push(Component::Rustfmt); self } diff --git a/workspace/gh-workflow/src/workflow.rs b/workspace/gh-workflow/src/workflow.rs index 732ab06..bec7f96 100644 --- a/workspace/gh-workflow/src/workflow.rs +++ b/workspace/gh-workflow/src/workflow.rs @@ -9,7 +9,7 @@ use serde_json::Value; use crate::error::Result; use crate::generate::Generate; -use crate::{EventValue, ToolchainStep}; +use crate::{Arch, Event, EventValue, RustFlags, ToolchainStep, Vendor}; #[derive(Debug, Default, Setters, Serialize, Deserialize, Clone, PartialEq, Eq)] #[serde(rename_all = "kebab-case")] @@ -79,6 +79,43 @@ impl Workflow { pub fn env>(self, env: T) -> Self { env.apply(self) } + + pub fn setup_rust() -> Self { + let build_job = Job::new("Build and Test") + .add_step(Step::checkout()) + .add_step( + Step::setup_rust() + .with_stable_toolchain() + .with_nightly_toolchain() + .with_clippy() + .with_fmt(), + ) + + // TODO: make it type-safe + .add_step(Step::cargo("test", vec!["--all-features", "--workspace"])) + .add_step(Step::cargo_nightly("fmt", vec!["--check"])) + .add_step(Step::cargo_nightly( + "clippy", + vec!["--all-features", "--workspace", "--", "-D warnings"], + )); + + let push_event = Event::push().branch("main"); + + let pr_event = Event::pull_request_target() + .open() + .synchronize() + .reopen() + .branch("main"); + + let rust_flags = RustFlags::deny("warnings"); + + Workflow::new("Build and Test") + .env(rust_flags) + .permissions(Permissions::read()) + .on(push_event) + .on(pr_event) + .add_job("build", build_job) + } } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]