Skip to content

Commit 27504f1

Browse files
committed
add basic files
1 parent 5cbaf38 commit 27504f1

15 files changed

+253
-172
lines changed

.rustfmt.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
unstable_features = true
2+
struct_lit_width = 60
3+
imports_granularity = "Module"
4+
group_imports = "StdExternalCrate"
5+
wrap_comments = true
6+
comment_width = 80

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workspace/gh-workflow-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ tokio = { version = "1.40.0", features = ["full"] }
1414

1515
[dev-dependencies]
1616
insta = "1.40.0"
17+
pretty_assertions = "1.4.1"

workspace/gh-workflow-rs/src/extensions.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: GitHub Actions Demo
2+
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
3+
on: [push]
4+
jobs:
5+
Explore-GitHub-Actions:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
9+
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
10+
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
11+
- name: Check out repository code
12+
uses: actions/checkout@v4
13+
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
14+
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
15+
- name: List files in the repository
16+
run: |
17+
ls ${{ github.workspace }}
18+
- run: echo "🍏 This job's status is ${{ job.status }}."

workspace/gh-workflow-rs/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
pub mod error;
2-
mod extensions;
3-
mod runtime;
4-
pub(crate) mod schema;
52
mod toolchain;
6-
mod worker;
7-
pub use schema::*;
3+
pub(crate) mod workflow;
4+
pub use workflow::*;

workspace/gh-workflow-rs/src/runtime.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
source: workspace/gh-workflow-rs/src/toolchain.rs
3+
expression: yml
4+
---
5+
name: CI
6+
on:
7+
- event: push
8+
branches:
9+
- main
10+
- event: pull-request
11+
branches:
12+
- main
13+
jobs:
14+
build:
15+
name: Setup Rust Toolchain
16+
runs-on:
17+
- linux
18+
steps:
19+
- uses: actions/checkout@v2
20+
- uses: actions-rs/toolchain@v1
21+
with:
22+
toolchain: stable
23+
components: rustfmt, clippy
24+
override: 'true'
25+
- uses: actions-rs/toolchain@v1
26+
with:
27+
command: check
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: workspace/gh-workflow-rs/src/toolchain.rs
3+
assertion_line: 74
4+
expression: yml
5+
---
6+
name: CI
7+
run-name: null
8+
on:
9+
- event: push
10+
branches:
11+
- main
12+
jobs:
13+
build:
14+
name: Setup Rust Toolchain
15+
runs-on:
16+
- linux
17+
steps:
18+
- uses: actions/checkout@v2
19+
- uses: actions-rs/toolchain@v1
20+
with:
21+
override: 'true'
22+
toolchain: stable
23+
components: rustfmt, clippy
24+
- uses: actions-rs/toolchain@v1
25+
with:
26+
command: check
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: workspace/gh-workflow-rs/src/worker.rs
3+
assertion_line: 106
4+
expression: generated
5+
---
6+
name: Test Workflow
7+
run-name: null
8+
on:
9+
- event: push
10+
branches:
11+
- main
12+
jobs:
13+
build:
14+
runs-on:
15+
- windows
16+
steps:
17+
- run: echo Hello World
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: workspace/gh-workflow-rs/src/worker.rs
3+
assertion_line: 98
4+
expression: generated
5+
---
6+
name: Test Workflow
7+
run-name: null
8+
on:
9+
- event: push
10+
branches:
11+
- main
12+
jobs:
13+
build:
14+
runs-on:
15+
- windows
16+
steps:
17+
- run: echo Hello World

workspace/gh-workflow-rs/src/toolchain.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ use std::collections::HashMap;
22

33
use derive_setters::Setters;
44

5-
use crate::schema::*;
5+
use crate::workflow::*;
66

7+
///
8+
/// A type-safe representation of the Rust toolchain.
9+
/// Instead of writing the github action for Rust by hand, we can use this struct to generate the github action.
10+
#[derive(Default)]
711
pub enum Version {
12+
#[default]
813
Stable,
914
Beta,
1015
Nightly,
@@ -19,11 +24,11 @@ impl Version {
1924
}
2025
}
2126

22-
#[derive(Setters)]
27+
#[derive(Setters, Default)]
2328
pub struct RustToolchain {
2429
version: Version,
25-
rust_fmt: bool,
26-
rust_clippy: bool,
30+
fmt: bool,
31+
clippy: bool,
2732
// TODO: add more rust tool chain components
2833
}
2934

@@ -38,7 +43,10 @@ impl RustToolchain {
3843
.uses("actions-rs/toolchain@v1".to_string())
3944
.with(HashMap::from([
4045
("toolchain".into(), self.version.to_string()),
41-
("components".into(), "rustfmt".into()),
46+
(
47+
"components".into(),
48+
vec!["rustfmt", "clippy"].join(", ").into(),
49+
),
4250
("override".into(), "true".into()),
4351
])),
4452
Step::default()
@@ -52,3 +60,20 @@ impl RustToolchain {
5260
}
5361
}
5462
}
63+
64+
#[cfg(test)]
65+
mod tests {
66+
use super::*;
67+
use insta::assert_snapshot;
68+
69+
#[test]
70+
fn test_to_job() {
71+
let toolchain = RustToolchain::default();
72+
let job = toolchain.to_job();
73+
let workflow = Workflow::default()
74+
.add_job("build".to_string(), job)
75+
.unwrap();
76+
let yml = serde_yaml::to_string(&workflow).unwrap();
77+
assert_snapshot!(yml);
78+
}
79+
}

0 commit comments

Comments
 (0)