forked from assert-rs/assert_cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This switches us from using `cargo run` to `cargo build`, reading where the binary is placed, and callin that instead. Fixes assert-rs#95 because the user changing the `CWD` doesn't impact `cargo build`. Fixes assert-rs#79 because there is no `cargo` output when capturing the user's stdout/stderr. Fixes assert-rs#51 because the user changing the environment doesn't impact `cargo build`. This is a step towards working around assert-rs#100 because we can allow a user to cache the result of `cargo build`.
- Loading branch information
Showing
6 changed files
with
256 additions
and
104 deletions.
There are no files selected for viewing
This file contains 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 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,16 @@ | ||
use std::env; | ||
use std::fs; | ||
use std::io::Write; | ||
use std::path; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
|
||
// env::ARCH doesn't include full triple, and AFAIK there isn't a nicer way of getting the full triple | ||
// (see lib.rs for the rest of this hack) | ||
let out = path::PathBuf::from(env::var_os("OUT_DIR").expect("run within cargo")) | ||
.join("current_target.txt"); | ||
let default_target = env::var("TARGET").expect("run as cargo build script"); | ||
let mut file = fs::File::create(out).unwrap(); | ||
file.write_all(default_target.as_bytes()).unwrap(); | ||
} |
This file contains 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 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,115 @@ | ||
use std::ffi; | ||
use std::process; | ||
use std::str; | ||
use std::vec; | ||
|
||
use failure; | ||
use serde; | ||
use serde_json; | ||
|
||
const CURRENT_TARGET: &str = include_str!(concat!(env!("OUT_DIR"), "/current_target.txt")); | ||
|
||
#[derive(Debug)] | ||
pub struct Cargo { | ||
cmd: process::Command, | ||
} | ||
|
||
impl Cargo { | ||
pub fn new() -> Self { | ||
Self { | ||
cmd: process::Command::new("cargo"), | ||
} | ||
} | ||
|
||
pub fn arg<S: AsRef<ffi::OsStr>>(mut self, arg: S) -> Self { | ||
self.cmd.arg(arg); | ||
self | ||
} | ||
|
||
pub fn build(mut self) -> CargoBuild { | ||
self.cmd.arg("build").arg("--message-format=json"); | ||
CargoBuild { cmd: self.cmd } | ||
} | ||
} | ||
|
||
pub struct CargoBuild { | ||
cmd: process::Command, | ||
} | ||
|
||
impl CargoBuild { | ||
pub fn new() -> Self { | ||
Cargo::new().build() | ||
} | ||
|
||
pub fn quiet(self) -> Self { | ||
self.arg("--quiet") | ||
} | ||
|
||
pub fn bin<S: AsRef<ffi::OsStr>>(self, name: S) -> Self { | ||
self.arg("--bin").arg(name) | ||
} | ||
|
||
pub fn example<S: AsRef<ffi::OsStr>>(self, name: S) -> Self { | ||
self.arg("--example").arg(name) | ||
} | ||
|
||
pub fn release(self) -> Self { | ||
self.arg("--release") | ||
} | ||
|
||
#[cfg(debug_assertions)] | ||
pub fn current_release(self) -> Self { | ||
self | ||
} | ||
|
||
#[cfg(not(debug_assertions))] | ||
pub fn current_release(self) -> Self { | ||
self.release() | ||
} | ||
|
||
pub fn target<S: AsRef<ffi::OsStr>>(self, triplet: S) -> Self { | ||
self.arg("--target").arg(triplet) | ||
} | ||
|
||
pub fn current_taget(self) -> Self { | ||
self.target(CURRENT_TARGET) | ||
} | ||
|
||
pub fn arg<S: AsRef<ffi::OsStr>>(mut self, arg: S) -> Self { | ||
self.cmd.arg(arg); | ||
self | ||
} | ||
|
||
pub fn exec(mut self) -> Result<MessageItr, failure::Error> { | ||
let output = self.cmd.output()?; | ||
if !output.status.success() { | ||
bail!("{}", String::from_utf8_lossy(&output.stderr)); | ||
} | ||
|
||
let messages: Vec<Message> = str::from_utf8(&output.stdout) | ||
.expect("json to be UTF-8") | ||
.split('\n') | ||
.map(|s| Message { | ||
content: s.to_owned(), | ||
}) | ||
.collect(); | ||
|
||
Ok(messages.into_iter()) | ||
} | ||
} | ||
|
||
pub type MessageItr = vec::IntoIter<Message>; | ||
|
||
pub struct Message { | ||
content: String, | ||
} | ||
|
||
impl Message { | ||
pub fn convert<'a, T>(&'a self) -> Result<T, failure::Error> | ||
where | ||
T: serde::Deserialize<'a>, | ||
{ | ||
let data = serde_json::from_str(self.content.as_str())?; | ||
Ok(data) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.