use std::ffi::OsStr;
use std::process;
use crate::cargo::Cargo;
use crate::cargo::CURRENT_TARGET;
use crate::error::CargoResult;
use crate::msg::CommandMessages;
use crate::run::CargoRun;
#[cfg(feature = "test_unstable")]
use crate::test::CargoTest;
pub struct CargoBuild {
cmd: process::Command,
bin: bool,
example: bool,
}
impl CargoBuild {
pub fn new() -> Self {
Cargo::new().build()
}
pub(crate) fn with_command(cmd: process::Command) -> Self {
Self {
cmd,
bin: false,
example: false,
}
}
pub fn package<S: AsRef<OsStr>>(self, name: S) -> Self {
self.arg("--package").arg(name)
}
pub fn bins(mut self) -> Self {
self.bin = true;
self.arg("--bins")
}
pub fn bin<S: AsRef<OsStr>>(mut self, name: S) -> Self {
self.bin = true;
self.arg("--bin").arg(name)
}
pub fn examples(mut self) -> Self {
self.example = true;
self.arg("--examples")
}
pub fn example<S: AsRef<OsStr>>(mut self, name: S) -> Self {
self.example = true;
self.arg("--example").arg(name)
}
pub fn tests(self) -> Self {
self.arg("--tests")
}
pub fn test<S: AsRef<OsStr>>(self, name: S) -> Self {
self.arg("--test").arg(name)
}
pub fn manifest_path<S: AsRef<OsStr>>(self, path: S) -> Self {
self.arg("--manifest-path").arg(path)
}
pub fn release(self) -> Self {
self.arg("--release")
}
pub fn env<K, V>(mut self, key: K, val: V) -> Self
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.cmd.env(key, val);
self
}
pub fn env_remove<K>(mut self, key: K) -> Self
where
K: AsRef<OsStr>,
{
self.cmd.env_remove(key);
self
}
#[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<OsStr>>(self, triplet: S) -> Self {
self.arg("--target").arg(triplet)
}
pub fn current_target(self) -> Self {
self.target(CURRENT_TARGET)
}
pub fn target_dir<S: AsRef<OsStr>>(self, dir: S) -> Self {
self.arg("--target-dir").arg(dir)
}
pub fn all_features(self) -> Self {
self.arg("--all-features")
}
pub fn no_default_features(self) -> Self {
self.arg("--no-default-features")
}
pub fn features<S: AsRef<OsStr>>(self, features: S) -> Self {
self.arg("--features").arg(features)
}
pub fn arg<S: AsRef<OsStr>>(mut self, arg: S) -> Self {
self.cmd.arg(arg);
self
}
pub fn args<I: IntoIterator<Item = S>, S: AsRef<OsStr>>(mut self, args: I) -> Self {
self.cmd.args(args);
self
}
pub fn exec(self) -> CargoResult<CommandMessages> {
CommandMessages::with_command(self.cmd)
}
pub fn run(self) -> CargoResult<CargoRun> {
let msgs = CommandMessages::with_command(self.cmd)?;
CargoRun::from_message(msgs, self.bin, self.example)
}
#[cfg(feature = "test_unstable")]
pub fn run_tests(self) -> CargoResult<impl Iterator<Item = CargoResult<CargoTest>>> {
let msgs = CommandMessages::with_command(self.cmd)?;
Ok(CargoTest::with_messages(msgs))
}
}
impl Default for CargoBuild {
fn default() -> Self {
Self::new()
}
}