forked from cross-rs/cross
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcargo.rs
83 lines (70 loc) · 1.67 KB
/
cargo.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
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
use std::{env, fs};
use errors::*;
use extensions::CommandExt;
#[derive(Clone, Copy)]
pub enum Subcommand {
Build,
Check,
Other,
Run,
Rustc,
Test,
Deb,
}
impl Subcommand {
pub fn needs_docker(&self) -> bool {
match *self {
Subcommand::Other => false,
_ => true,
}
}
pub fn needs_interpreter(&self) -> bool {
match *self {
Subcommand::Run | Subcommand::Test => true,
_ => false,
}
}
}
impl<'a> From<&'a str> for Subcommand {
fn from(s: &str) -> Subcommand {
match s {
"build" => Subcommand::Build,
"check" => Subcommand::Check,
"run" => Subcommand::Run,
"rustc" => Subcommand::Rustc,
"test" => Subcommand::Test,
"deb" => Subcommand::Deb,
_ => Subcommand::Other,
}
}
}
pub struct Root {
path: PathBuf,
}
impl Root {
pub fn path(&self) -> &Path {
&self.path
}
}
/// Cargo project root
pub fn root() -> Result<Option<Root>> {
let cd = env::current_dir().chain_err(|| "couldn't get current directory")?;
let mut dir = &*cd;
loop {
let toml = dir.join("Cargo.toml");
if fs::metadata(&toml).is_ok() {
return Ok(Some(Root { path: dir.to_owned() }));
}
match dir.parent() {
Some(p) => dir = p,
None => break,
}
}
Ok(None)
}
/// Pass-through mode
pub fn run(args: &[String], verbose: bool) -> Result<ExitStatus> {
Command::new("cargo").args(args).run_and_get_status(verbose)
}