Skip to content

Commit

Permalink
Merge pull request #102 from tailcallhq/feat-add-when
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath authored Dec 11, 2024
2 parents 07dca0e + 07a6892 commit 4513f45
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions crates/gh-workflow/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ pub struct Cargo {
pub toolchain: Option<Version>,

/// Arguments to be passed to the cargo command.
pub args: Option<String>,
#[setters(skip)]
pub args: Vec<String>,
}

impl Cargo {
/// Creates a new `Cargo` instance with the specified command.
pub fn new<T: ToString>(cmd: T) -> Cargo {
Cargo {
command: cmd.to_string(),
Expand All @@ -33,10 +35,38 @@ impl Cargo {
}
}

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

/// Sets the arguments for the cargo command. If arguments are already set,
/// it will be overwritten.
pub fn args<T: ToString>(mut self, args: T) -> Self {
self.args = vec![args.to_string()];
self
}

/// Adds additional arguments to the cargo command.
pub fn add_args<T: ToString>(mut self, args: T) -> Self {
self.args.extend(
args.to_string()
.split_whitespace()
.map(|s| s.to_string())
.collect::<Vec<_>>(),
);
self
}

/// Adds the arguments to the cargo command when a condition is met.
pub fn add_args_when<T: ToString>(self, when: bool, args: T) -> Self {
if when {
self.add_args(args)
} else {
self
}
}
}

impl From<Cargo> for Step<Run> {
Expand All @@ -49,11 +79,14 @@ impl From<Cargo> for Step<Run> {

command.push(value.command);

if let Some(args) = value.args {
if !args.is_empty() {
command.push(args);
}
}
// Extend the command with non-empty arguments
command.extend(
value
.args
.into_iter()
.map(|arg| arg.trim().to_string())
.filter(|arg| !arg.is_empty()),
);

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

Expand Down

0 comments on commit 4513f45

Please sign in to comment.