Skip to content

Commit

Permalink
feat: add Workflow::setup_rust() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Nov 8, 2024
1 parent f74da3d commit 5a3f184
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# ----------------------------- END WARNING -------------------------
# -------------------------------------------------------------------

name: CI
name: Build and Test
env:
RUSTFLAGS: -Dwarnings
on:
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
}
```

Expand Down
36 changes: 1 addition & 35 deletions workspace/gh-workflow-gen/build.rs
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 5 additions & 3 deletions workspace/gh-workflow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
}
```

Expand Down
8 changes: 4 additions & 4 deletions workspace/gh-workflow/src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
39 changes: 38 additions & 1 deletion workspace/gh-workflow/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Check failure on line 12 in workspace/gh-workflow/src/workflow.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused imports: `Arch` and `Vendor`

#[derive(Debug, Default, Setters, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -79,6 +79,43 @@ impl Workflow {
pub fn env<T: SetEnv<Self>>(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)]
Expand Down

0 comments on commit 5a3f184

Please sign in to comment.