util/
ext.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2015 - 2016, Alberto Corona <ac@albertocorona.com>
// All rights reserved. This file is part of yabs, distributed under the BSD
// 3-Clause license. For full terms please see the LICENSE file.

extern crate toml;

use error::{YabsError, YabsErrorKind};
use std::env;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Child, Command};

pub struct Job {
    process: Child,
    command: String,
}

impl Job {
    pub fn new(raw: (String, Child)) -> Job {
        Job {
            command: raw.0,
            process: raw.1,
        }
    }

    pub fn command(&self) -> String {
        self.command.clone()
    }

    pub fn yield_self(&mut self) -> Result<(), YabsError> {
        let status = self.process.wait()?;
        if !status.success() {
            if let Some(ref mut stderr) = self.process.stderr {
                let mut buffer = String::new();
                stderr.read_to_string(&mut buffer)?;
                info!("{}", buffer);
            }
            bail!(YabsErrorKind::Command(self.command(), status.code().unwrap_or(1)));
        }
        Ok(())
    }
}

pub fn parse_toml_file<T: AsRef<Path> + Clone>(file: T) -> Result<String, YabsError> {
    let mut buff = String::new();
    let mut file = File::open(&file)?;
    file.read_to_string(&mut buff)?;
    Ok(buff)
}

pub fn get_assumed_filename() -> Option<String> {
    if let Ok(current_dir) = env::current_dir() {
        if let Some(file_stem) = current_dir.file_stem() {
            let mut file_name = file_stem.to_string_lossy().into_owned();
            file_name.push_str(".toml");
            return Some(file_name);
        }
    }
    None
}

pub fn get_assumed_filename_for_dir(dir: &PathBuf) -> Option<PathBuf> {
    if let Some(file_stem) = dir.file_stem() {
        return Some(PathBuf::from(file_stem.to_string_lossy().into_owned() + ".toml"));
    }
    None
}

pub fn run_cmd(cmd: &str) -> Result<(), YabsError> {
    let command = Command::new("sh").arg("-c").arg(&cmd).spawn()?.wait_with_output()?;
    println!("{}", &cmd);
    if !command.status.success() {
        print!("{}", String::from_utf8(command.stderr)?);
        bail!(YabsErrorKind::Command(cmd.to_owned(), command.status.code().unwrap_or(1)));
    }
    print!("{}", String::from_utf8(command.stdout)?);
    Ok(())
}

pub fn spawn_cmd(cmd: &str) -> Result<Child, YabsError> {
    Ok(Command::new("sh").arg("-c").arg(&cmd).spawn()?)
}

pub trait PrependEach<T> {
    fn prepend_each(&self, pre: &str) -> Vec<String>;
}

// self.include.prepend_each("-I");
impl PrependEach<String> for Vec<String> {
    fn prepend_each(&self, pre: &str) -> Vec<String> {
        let mut clone = self.clone();
        for each in &mut clone {
            *each = pre.to_owned() + each;
        }
        clone
    }
}