Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: many many changes #22

Merged
merged 34 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b658403
feat: introduce `CargoCommand` enum
ssddOnTop Nov 7, 2024
751cdfd
fmt
ssddOnTop Nov 7, 2024
50e672a
rename cargo command to cargo
tusharmath Nov 8, 2024
b464c65
add new proposal for cargo commands
tusharmath Nov 8, 2024
603aca4
Merge branch 'main' into feat/cargo-cmd
ssddOnTop Nov 8, 2024
2e2eaad
merge
ssddOnTop Nov 8, 2024
6c8ad62
Merge branch 'main' into feat/cargo-cmd
ssddOnTop Nov 8, 2024
8bc7e7e
make cargo commands typesafe using `Cargo` struct
ssddOnTop Nov 8, 2024
c178f9a
add cargo mod
tusharmath Nov 9, 2024
7b1de39
Merge branch 'main' into feat/cargo-cmd
tusharmath Nov 9, 2024
ae02a3d
drop the setter trait implementations
tusharmath Nov 9, 2024
215d3b9
add combine for event
tusharmath Nov 9, 2024
c4a4cf8
add a typed version of jobs
tusharmath Nov 9, 2024
7e04aca
remove custom into setters
tusharmath Nov 9, 2024
4fd5947
Merge branch 'main' into feat/cargo-cmd
tusharmath Nov 9, 2024
4d0376a
doc: update README
tusharmath Nov 9, 2024
26bbda7
update README
tusharmath Nov 9, 2024
749be54
rename file
tusharmath Nov 9, 2024
d673e96
rename file
tusharmath Nov 9, 2024
64401cf
rename types
tusharmath Nov 9, 2024
4c022e5
rename methods of Toolchain
tusharmath Nov 9, 2024
b78800c
add into in all setters
tusharmath Nov 9, 2024
477a6de
fix: simplify From for RunsOn
tusharmath Nov 9, 2024
59328c0
update names for workflows
tusharmath Nov 10, 2024
d949289
drop setup_rust
tusharmath Nov 10, 2024
5c8874b
chore: drop toolchain package
tusharmath Nov 10, 2024
2b983bf
chore: move toolchain to a separate module
tusharmath Nov 10, 2024
5cfd707
rename types in cargo.rs
tusharmath Nov 10, 2024
4067936
update setters
tusharmath Nov 10, 2024
1f3f4fb
refactor: Step and StepValue
tusharmath Nov 10, 2024
ef95a77
lint fixes
tusharmath Nov 10, 2024
863f4c6
rename event_value to Event
tusharmath Nov 10, 2024
410f6da
Update job generation
tusharmath Nov 10, 2024
11c26d9
Merge branch 'main' into feat/cargo-cmd
tusharmath Nov 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 5 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,13 @@ To use **Rust GitHub Workflows** in your project, add it to your `Cargo.toml`:
gh-workflow = "0.1"
```

Then you can start creating GitHub Actions in your [build.rs](https://github.com/tailcallhq/rust-gh-workflows/blob/main/workspace/gh-workflow-gen/build.rs).

## 👷 Usage

- Simply add a `build.rs` file to your project's root directory.
- Add the following code to generate the GitHub Actions workflow:

```rust
use rust_gh_workflows::*;

fn main() {
// Create a workflow
let workflow = Workflow::new("CI")
.permissions(Permissions::read())
.on(Event::default().push(Push::default().branch("main"))
.add_job(
"build",
Job::new("Build and Test")
.add_step(Step::checkout())
.add_step(Step::setup_rust().add_toolchain(Toolchain::Stable))
.add_step(Step::cargo("test", vec!["--all-features", "--workspace"]))
)
.unwrap();

// Generate the ci.yml
workflow.generate().unwrap();
}
```
or via CLI

To view a fully functional example, check out the [build.rs](https://github.com/tailcallhq/rust-gh-workflows/blob/main/workspace/gh-workflow-gen/build.rs) of this project.

- Run `cargo build` to generate the GitHub Actions workflow.

**Workspace**

- The `workspace` directory contains the `gh-workflow-gen` crate, which generates the workflow.

## 🛠️ Roadmap
```bash
cargo add --build gh-workflow
```

- [ ] Support for Automated Cargo Releases
- [ ] Improve Type Safety of Nightly Builds
- [ ] Add Rust Docs for the API
Then you can start creating GitHub Actions in your [build.rs](https://github.com/tailcallhq/rust-gh-workflows/blob/main/workspace/gh-workflow-gen/build.rs).

## 💡 Why Rust?

Expand Down
48 changes: 45 additions & 3 deletions workspace/gh-workflow-gen/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
use gh_workflow::generate::Generate;
use gh_workflow::*;
use toolchain::Toolchain;

fn main() {
Generate::new(Workflow::setup_rust())
.name("ci.yml")
let job = Job::new("Build and Test")
.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"),
);

let event = Event::default()
.push(Push::default().add_branch("main"))
.pull_request_target(
PullRequestTarget::default()
.open()
.synchronize()
.reopen()
.add_branch("main"),
);

let flags = RustFlags::deny("warnings");

Workflow::new("Build and Test")
.env(flags)
.permissions(Permissions::read())
.on(event)
.add_job("build", job)
.generate()
.unwrap();
}
5 changes: 3 additions & 2 deletions workspace/gh-workflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ edition = "2021"

description = "A type-safe GitHub Actions workflow generator"
license = "Apache-2.0"
documentation = "https://github.com/tailcallhq/rust-gh-workflows"
documentation = "https://docs.rs/gh-workflow"
homepage = "https://github.com/tailcallhq/rust-gh-workflows"
repository = "https://github.com/tailcallhq/rust-gh-workflows"

[dependencies]
async-trait = "0.1.83"
derive_more = { version = "1.0.0", features = ["from"] }
derive_more = { version = "1.0.0", features = ["from", "deref", "deref_mut"] }
derive_setters = "0.1.6"
indexmap = { version = "2.6.0", features = ["serde"] }
merge = "0.1.0"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = { version = "1.0.128" }
serde_yaml = "0.9.34"
strum_macros = "0.26.4"

[dev-dependencies]
insta = "1.40.0"
Expand Down
70 changes: 70 additions & 0 deletions workspace/gh-workflow/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use derive_setters::Setters;

use crate::toolchain::Version;
use crate::StepValue;

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

/// The unique identifier of the Step.
pub id: Option<String>,

/// Name of the Step
pub name: Option<String>,

/// Toolchain to be used for example `+nightly`.
pub toolchain: Option<Version>,

/// Arguments to be passed to the cargo command.
pub args: Option<String>,
}

impl Cargo {
pub fn new<T: ToString>(cmd: T) -> Cargo {
Cargo {
command: cmd.to_string(),
id: Default::default(),
name: Default::default(),
toolchain: Default::default(),
args: Default::default(),
}
}

pub fn nightly(mut self) -> Self {
self.toolchain = Some(Version::Nightly);
self
}
}

impl From<Cargo> for StepValue {
fn from(value: Cargo) -> Self {
let mut command = vec!["cargo".to_string()];

if let Some(toolchain) = value.toolchain {
command.push(format!("+{}", toolchain));
}

command.push(value.command);

if let Some(args) = value.args {
if !args.is_empty() {
command.push(args);
}
}

let mut step = StepValue::run(command.join(" "));

if let Some(id) = value.id {
step = step.id(id);
}

if let Some(name) = value.name {
step = step.name(name);
}

step
}
}
Loading