Skip to content

Commit

Permalink
Pass env as argument to post build script
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Aug 26, 2022
1 parent aa6ce5d commit 36189cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fmt;
use std::fmt::Display;

pub trait Runner<E> {
fn run(&self) -> Result<(), E>;
}
Expand All @@ -7,3 +10,12 @@ pub enum Env {
Dev,
Release,
}

impl Display for Env {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Env::Dev => write!(f, "dev"),
Env::Release => write!(f, "release"),
}
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn main() {

if let Some(script_name) = script {
let script_path = current_dir.join(script_name);
let script_runner = ScriptRunner::new(script_path);
let script_runner = ScriptRunner::new(script_path, &env);
script_runner.run().expect("Post build runner failed");
}
}
Expand All @@ -108,7 +108,7 @@ fn main() {
let post_build_runner = if let Some(script_name) = script {
let script_path = current_dir.join(script_name);
if script_path.exists() {
Some(ScriptRunner::new(script_path))
Some(ScriptRunner::new(script_path, &env))
} else {
eprintln!("Could not find script: {}", script_path.display());
None
Expand Down
11 changes: 8 additions & 3 deletions src/script_runner.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::build::Env;
use crate::build::Runner;
use crate::exec;
use std::fmt;
Expand All @@ -19,11 +20,15 @@ impl fmt::Display for Error {
#[derive(Debug, Clone)]
pub struct ScriptRunner {
script_path: PathBuf,
env: Env,
}

impl ScriptRunner {
pub fn new(script_path: PathBuf) -> Self {
Self { script_path }
pub fn new(script_path: PathBuf, env: &Env) -> Self {
Self {
script_path,
env: env.clone(),
}
}
}

Expand All @@ -32,7 +37,7 @@ impl Runner<Error> for ScriptRunner {
exec::run(&exec::Config {
work_dir: ".".into(),
cmd: self.script_path.to_string_lossy().into(),
args: vec![],
args: vec![self.env.to_string()],
})
.map_err(Error::Exec)?;

Expand Down

0 comments on commit 36189cf

Please sign in to comment.