/
cargo.rs
142 lines (116 loc) · 3.56 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use failure::Fail;
use serde::Deserialize;
use serde_json::Error as JsonError;
use std::io;
use std::process::{Command, Output, Stdio};
use std::string::FromUtf8Error;
use crate::cli;
#[derive(Debug, Fail)]
pub enum SubcommandError {
#[fail(display = "Command failed with I/O error")]
IoError(#[cause] io::Error),
#[fail(display = "Command failed with exit code: {:?}", _0)]
CommandError(Option<i32>),
#[fail(display = "Command failed with I/O error")]
Utf8Error(#[cause] FromUtf8Error),
#[fail(display = "JSON error: {}", _1)]
JsonError(#[cause] JsonError, String),
}
impl From<io::Error> for SubcommandError {
fn from(e: io::Error) -> Self {
SubcommandError::IoError(e)
}
}
impl From<FromUtf8Error> for SubcommandError {
fn from(e: FromUtf8Error) -> Self {
SubcommandError::Utf8Error(e)
}
}
trait Runner {
fn run(&mut self, verbose: bool) -> io::Result<Output>;
}
impl Runner for Command {
fn run(&mut self, verbose: bool) -> io::Result<Output> {
if verbose {
eprintln!("+ {:?}", self);
}
self.output()
}
}
#[derive(Deserialize, Debug)]
crate struct CargoArtifact {
crate executable: String,
crate target: CargoArtifactTarget,
}
#[derive(Deserialize, Debug)]
crate struct CargoArtifactTarget {
crate name: String,
}
#[derive(Deserialize, Debug)]
struct CargoMessage {
message: CargoMessageMessage,
}
#[derive(Deserialize, Debug)]
struct CargoMessageMessage {
rendered: String,
}
crate fn run(args: &cli::BuildArgs) -> Result<CargoArtifact, SubcommandError> {
let verbose = args.verbose();
// Add --release flag if necessary
let build_args = {
let release_flag = "--release".to_owned();
let mut args = args.rest.clone();
if !args.contains(&release_flag) {
args.push(release_flag);
}
args
};
let output = Command::new("cargo")
.arg("xbuild")
.arg("--message-format=json")
.arg(format!("--target={}", args.target))
.args(build_args)
.stderr(Stdio::inherit())
.run(verbose)?;
let json = String::from_utf8(output.stdout)?;
if output.status.success() {
// Successful build
parse_artifact(&json)
} else {
// Failed build
let (_artifacts, errors) = split_output(&json);
print_messages(errors)?;
Err(SubcommandError::CommandError(output.status.code()))
}
}
fn split_output(json: &str) -> (Vec<&str>, Vec<&str>) {
json.trim()
.split('\n')
.filter(|x| {
!x.is_empty()
&& !x.starts_with('#')
&& x.find("] cargo:").is_none()
&& x.find(r#""reason":"build-script-executed""#).is_none()
})
.partition(|x| x.find(r#""reason":"compiler-artifact""#).is_some())
}
fn parse_artifact(json: &str) -> Result<CargoArtifact, SubcommandError> {
// Warnings need to be handled separately
let (artifacts, warnings) = split_output(json);
print_messages(warnings)?;
// Return build artifact
let json = *artifacts.last().expect("Expected artifact JSON");
serde_json::from_str(json).map_err(|e| SubcommandError::JsonError(e, json.into()))
}
fn print_messages<'a, T>(messages: T) -> Result<(), SubcommandError>
where
T: IntoIterator<Item = &'a str>,
{
for s in messages {
let message: CargoMessage =
serde_json::from_str(s).map_err(|e| SubcommandError::JsonError(e, s.into()))?;
// TODO: Add highlighting
eprintln!("{}", message.message.rendered);
}
Ok(())
}