cargo-drone 0.4.3

A cargo subcommand for Drone.
Documentation
//! Managing a cargo project found in current directory.

use config;
use failure::{err_msg, Error};
use std::env;
use std::process::{self, Command};

/// Builds the crate and aborts on failure.
pub fn build(config: &config::Root, release: bool) -> Result<(), Error> {
  let mut command = Command::new("xargo");
  command.arg("build");
  if release {
    command.arg("--release");
  }
  command.arg("--target").arg(&config.target);
  let status = command.spawn()?.wait()?;
  if !status.success() {
    process::exit(status.code().unwrap_or(1));
  }
  Ok(())
}

/// Returns a path to the crate's compiled binary.
pub fn binary_path(
  config: &config::Root,
  release: bool,
) -> Result<String, Error> {
  Ok(format!(
    "target/{}/{}/{}",
    config.target,
    if release { "release" } else { "debug" },
    crate_name()?,
  ))
}

/// Returns a crate name.
pub fn crate_name() -> Result<String, Error> {
  Ok(
    env::current_dir()?
      .file_name()
      .ok_or_else(|| err_msg("Current directory is invalid"))?
      .to_str()
      .ok_or_else(|| err_msg("Current directory path is not valid UTF-8"))?
      .to_string(),
  )
}