-
Notifications
You must be signed in to change notification settings - Fork 0
Cargo stage options --json & --strict #91
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
a931659
bitflags
MusicalNinjaDad f0265b9
use flags in commands
MusicalNinjaDad 2daff2b
fix deadlock
MusicalNinjaDad 8e961ac
store flags in Spawned
MusicalNinjaDad f83b59f
simplify FromIterator<Spawned> for Exit<()>
MusicalNinjaDad 7ed33a6
output my stuff as json
MusicalNinjaDad e70d63c
for all cmds
MusicalNinjaDad b54c464
use serde_json
MusicalNinjaDad 662b159
fix payload
MusicalNinjaDad 4288d34
try only parsing lines which start with a {
MusicalNinjaDad c3aa963
and store json with errors too
MusicalNinjaDad 3037846
actually output the json on error
MusicalNinjaDad 55533a4
fix tests
MusicalNinjaDad 5a33158
don't push null jsons
MusicalNinjaDad 18382b2
flatten json nesting
MusicalNinjaDad 1ec95c5
rm json example output
MusicalNinjaDad 4dc1959
unbreak test
MusicalNinjaDad 8ab30f6
mod exit_with_json
MusicalNinjaDad f089764
commands as dir
MusicalNinjaDad 61ab187
move commands to own files
MusicalNinjaDad 36dc5c4
Cmd & Spawned -> commands
MusicalNinjaDad ca178fc
cli -> cli
MusicalNinjaDad 772ea29
From<io::Error> for Exit<T>
MusicalNinjaDad 593d660
fmt
MusicalNinjaDad 4bdb4a6
readable impl From<Cmd> for Exit<WithJson<()>>
MusicalNinjaDad 3cdca68
early return
MusicalNinjaDad f90531d
ewrly return with correct output on err
MusicalNinjaDad 7204952
fmt
MusicalNinjaDad 5fbecfa
v bump
MusicalNinjaDad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| use std::fmt::Debug; | ||
|
|
||
| use bitflags::bitflags; | ||
| use clap::{Parser, Subcommand}; | ||
| use clap_cargo::style::CLAP_STYLING as CARGO_STYLING; | ||
|
|
||
| #[derive(Parser)] | ||
| #[command(name = "cargo")] | ||
| #[command(bin_name = "cargo")] | ||
| #[command(styles = CARGO_STYLING)] | ||
| pub enum CargoCmd { | ||
| #[command(subcommand)] | ||
| Ninja(NinjaCommand), | ||
| } | ||
|
|
||
| #[derive(Subcommand)] | ||
| #[command(version)] | ||
| pub enum NinjaCommand { | ||
| /// fmt, lint & test then stage everything in git if all is good | ||
| Stage { | ||
| /// add --deny warnings to clippy invocations | ||
| #[arg(long)] | ||
| strict: bool, | ||
| /// output in json format | ||
| #[arg(long)] | ||
| json: bool, | ||
| }, | ||
| /// build (optionally with zigbuild for a given glibc version) | ||
| Build { | ||
| /// build for a specific glibc version (WSL-Ubuntu is 2.35) | ||
| #[arg(short, long)] | ||
| glibc: Option<String>, | ||
| /// build a release build (default is cargo's default profile, usually debug) | ||
| #[arg(short, long)] | ||
| release: bool, | ||
| /// build for a given target | ||
| #[arg(long)] | ||
| target: Option<String>, | ||
| }, | ||
| } | ||
|
|
||
| bitflags! { | ||
| #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub struct CheckFlags: u32 { | ||
| const STRICT = 0b00000001; | ||
| const JSON = 0b00000010; | ||
| } | ||
| } | ||
|
|
||
| impl From<&NinjaCommand> for CheckFlags { | ||
| fn from(xtask: &NinjaCommand) -> Self { | ||
| match xtask { | ||
| NinjaCommand::Stage { strict, json } => { | ||
| let mut flags = Self::default(); | ||
| flags.set(Self::STRICT, *strict); | ||
| flags.set(Self::JSON, *json); | ||
| flags | ||
| } | ||
| NinjaCommand::Build { .. } => CheckFlags::default(), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| use std::{ | ||
| fmt::Debug, | ||
| io, | ||
| process::{Child, Output}, | ||
| }; | ||
|
|
||
| use serde_json::{Value, json}; | ||
|
|
||
| mod stage; | ||
| pub use stage::*; | ||
|
|
||
| mod build; | ||
| pub use build::*; | ||
|
|
||
| use crate::{CheckFlags, Exit, WithJson}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Cmd { | ||
| pub name: &'static str, | ||
| pub result: Result<Output, io::Error>, | ||
| pub flags: CheckFlags, | ||
| } | ||
|
|
||
| pub trait CmdExt { | ||
| fn into_cmd(self, name: &'static str, checkflags: Option<CheckFlags>) -> Cmd; | ||
| } | ||
|
|
||
| impl CmdExt for Result<Output, io::Error> { | ||
| fn into_cmd(self, name: &'static str, checkflags: Option<CheckFlags>) -> Cmd { | ||
| Cmd { | ||
| name, | ||
| result: self, | ||
| flags: checkflags.unwrap_or_default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<Cmd> for Exit<WithJson<()>> { | ||
| fn from(cmd: Cmd) -> Self { | ||
| let Cmd { | ||
| name: task, | ||
| result: did_it_spawn, | ||
| flags, | ||
| } = cmd; | ||
|
|
||
| let output = match did_it_spawn { | ||
| Ok(output) => output, | ||
| Err(err_spawning) => { | ||
| let json = flags.contains(CheckFlags::JSON).then(|| { | ||
| json!({ | ||
| "task": task, | ||
| "status": "failed to spawn", | ||
| "error": &err_spawning.to_string(), | ||
| }) | ||
| }); | ||
| let msg = format!("{task} failed: {err_spawning}"); | ||
| return match json { | ||
| Some(json) => Exit::IO(WithJson { | ||
| value: String::new(), | ||
| json: Some(json), | ||
| }), | ||
| None => Self::IO(WithJson { | ||
| value: msg, | ||
| json: None, | ||
| }), | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| let status = output.status; | ||
| let json = flags.contains(CheckFlags::JSON).then(|| { | ||
| let payload = String::from_utf8_lossy(&output.stdout) | ||
| .lines() | ||
| .filter(|line| line.starts_with("{")) | ||
| .map(serde_json::from_str::<Value>) | ||
| .map(|json| json.unwrap_or_else(|err| json!({"unparsable": &err.to_string()}))) | ||
| .collect::<Value>(); | ||
| json!({ | ||
| "task": task, | ||
| "status": &status.to_string(), | ||
| "payload": payload, | ||
| }) | ||
| }); | ||
| let stdout = String::from_utf8_lossy(&output.stdout); | ||
| let stderr = String::from_utf8_lossy(&output.stderr); | ||
|
|
||
| match (status.success(), json) { | ||
| (true, Some(json)) => Exit::Ok(WithJson { | ||
| value: (), | ||
| json: Some(json), | ||
| }), | ||
| (true, None) => { | ||
| println!("{task}: OK"); | ||
| Self::Ok(WithJson { | ||
| value: (), | ||
| json: None, | ||
| }) | ||
| } | ||
| (false, Some(json)) => Exit::Error(WithJson { | ||
| value: String::new(), | ||
| json: Some(json), | ||
| }), | ||
| (false, None) => Self::Error(WithJson { | ||
| value: format!( | ||
| "====== {task} exited with {status} ======\n-- stdout: --\n{stdout}\n\n-- stderr: --\n{stderr}", | ||
| status = output.status | ||
| ), | ||
| json: None, | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Spawned { | ||
| pub name: &'static str, | ||
| pub child: Result<Child, io::Error>, | ||
| pub flags: CheckFlags, | ||
| } | ||
|
|
||
| impl Spawned { | ||
| pub fn wait(self) -> Cmd { | ||
| match self.child { | ||
| Ok(child) => child | ||
| .wait_with_output() | ||
| .into_cmd(self.name, Some(self.flags)), | ||
| Err(e) => Cmd { | ||
| name: self.name, | ||
| result: Err(e), | ||
| flags: self.flags, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub trait SpawnedExt { | ||
| fn into_spawned(self, name: &'static str, flags: Option<CheckFlags>) -> Spawned; | ||
| } | ||
|
|
||
| impl SpawnedExt for Result<Child, io::Error> { | ||
| fn into_spawned(self, name: &'static str, flags: Option<CheckFlags>) -> Spawned { | ||
| Spawned { | ||
| name, | ||
| child: self, | ||
| flags: flags.unwrap_or_default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FromIterator<Spawned> for Exit<WithJson<()>> { | ||
| fn from_iter<I: IntoIterator<Item = Spawned>>(spawns: I) -> Self { | ||
| spawns.into_iter().map(Exit::from).collect() | ||
| } | ||
| } | ||
|
|
||
| impl From<Spawned> for Exit<WithJson<()>> { | ||
| fn from(spawn: Spawned) -> Self { | ||
| spawn.wait().into() | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.