From c06eb8d8c1a7248cd149146a2a5066b40cec2ee8 Mon Sep 17 00:00:00 2001 From: Lucas Z Date: Thu, 17 Feb 2022 11:33:58 -0300 Subject: [PATCH] workdir support --- src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ad918e5..f59c786 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,6 +109,7 @@ impl Default for BuildMode { pub struct Build { files: Vec, env: HashMap, + work_dir: Option, out_dir: Option, buildmode: BuildMode, compiler: PathBuf, @@ -166,6 +167,7 @@ impl Build { Self { files: Vec::new(), env: HashMap::new(), + work_dir: None, out_dir: None, buildmode: BuildMode::CArchive, compiler: PathBuf::from("go"), @@ -214,6 +216,16 @@ impl Build { self } + /// Configures the working directory where the `go build` command is going + /// to run + pub fn work_dir>( + &mut self, + work_dir: P, + ) -> &mut Build { + self.work_dir = Some(work_dir.as_ref().to_owned()); + self + } + /// Configures the build mode. See `go help buildmode for more details. /// /// Build mode `c-archive` is used by default. @@ -284,10 +296,14 @@ impl Build { })?; let mut command = process::Command::new(&self.compiler); + if let Some(work_dir) = self.work_dir.as_ref() { + let full_work_dir = env::current_dir().unwrap().join(work_dir); + command.current_dir(full_work_dir); + } command.arg("build"); command.args(&["-buildmode", &self.buildmode.to_string()]); command.args(&["-o", &out.display().to_string()]); - command.args(self.files.iter()); + command.args(self.files.iter().map(|p| env::current_dir().unwrap().join(p))); command.env("CGO_ENABLED", "1"); command.env("CC", ccompiler.path());