mod.rs 2.24 KB
Newer Older
1
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
2
// This file is part of ink!.
3
//
4
// ink! is free software: you can redistribute it and/or modify
5 6 7 8
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// ink! is distributed in the hope that it will be useful,
10 11 12 13 14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
15
// along with ink!.  If not, see <http://www.gnu.org/licenses/>.
16

17 18 19 20 21 22 23 24 25 26
use std::{
    io::{
        self,
        Write,
    },
    path::PathBuf,
    process::Command,
};

mod abi;
27
mod build;
Andrew Jones's avatar
Andrew Jones committed
28
mod deploy;
29 30 31 32
mod error;
mod new;

pub(crate) use self::{
33
    abi::execute_generate_abi,
34
    build::execute_build,
Andrew Jones's avatar
Andrew Jones committed
35
    deploy::execute_deploy,
36 37 38 39 40 41
    error::{
        CommandError,
        Result,
    },
    new::execute_new,
};
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
fn exec_cargo(command: &str, args: &[&'static str], working_dir: Option<&PathBuf>) -> Result<()> {
    let mut cmd = Command::new("cargo");
    let mut is_nightly_cmd = Command::new("cargo");
    if let Some(dir) = working_dir {
        cmd.current_dir(dir);
        is_nightly_cmd.current_dir(dir);
    }

    let is_nightly_default = is_nightly_cmd
        .arg("--version")
        .output()
        .map_err(|_| ())
        .and_then(|o| String::from_utf8(o.stdout).map_err(|_| ()))
        .unwrap_or_default()
        .contains("-nightly");

    if !is_nightly_default {
        cmd.arg("+nightly");
    }

    let output = cmd
        .arg(command)
        .args(args)
        .output()?;

    if !output.status.success() {
        // Dump the output streams produced by cargo into the stdout/stderr.
        io::stdout().write_all(&output.stdout)?;
        io::stderr().write_all(&output.stderr)?;
        return Err(error::CommandError::BuildFailed)
    }

    Ok(())
}

78 79 80 81 82 83 84 85 86 87 88
#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use tempfile::TempDir;

    pub fn with_tmp_dir<F: FnOnce(&PathBuf)>(f: F) {
        let tmp_dir = TempDir::new().expect("temporary directory creation failed");

        f(&tmp_dir.into_path());
    }
}