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
143
144
145
146
147
148
149
150
151
152
153
154
extern crate proc_macro;
use proc_macro_hack::proc_macro_hack;
use syn::parse_macro_input;
#[proc_macro_hack]
pub fn run_command_str(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
detail::run_command_str(parse_macro_input!(input))
.unwrap_or_else(|error| error.to_compile_error())
.into()
}
#[proc_macro_hack]
pub fn run_command(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
detail::run_command(parse_macro_input!(input))
.unwrap_or_else(|error| error.to_compile_error())
.into()
}
mod detail {
use std::process::Command;
use quote::quote;
use syn::{Error, Result};
use proc_macro2::Span;
pub fn run_command_str(input: ArgList) -> Result<proc_macro2::TokenStream> {
let args : Vec<_> = input.args.iter().map(|x| x.value()).collect();
let output = execute_command(Command::new(&args[0]).args(&args[1..]))?;
let output = strip_trailing_newline(output.stdout);
let output = std::str::from_utf8(&output).expect("invalid UTF-8 in command output");
Ok(quote!(#output))
}
pub fn run_command(input: ArgList) -> Result<proc_macro2::TokenStream> {
let args : Vec<_> = input.args.iter().map(|x| x.value()).collect();
let output = execute_command(Command::new(&args[0]).args(&args[1..]))?;
let output = strip_trailing_newline(output.stdout);
if output.is_empty() {
Ok(quote!(&[0u8; 0]))
} else {
Ok(quote!( &[ #(#output,)* ] ))
}
}
pub struct ArgList {
args : syn::punctuated::Punctuated<syn::LitStr, syn::token::Comma>,
}
impl syn::parse::Parse for ArgList {
fn parse(input: syn::parse::ParseStream) -> Result<Self> {
type Inner = syn::punctuated::Punctuated<syn::LitStr, syn::token::Comma>;
let args = Inner::parse_terminated(&input)?;
if args.is_empty() {
Err(Error::new(input.cursor().span(), "missing required argument: command"))
} else {
Ok(Self{args})
}
}
}
fn execute_command(command: &mut Command) -> Result<std::process::Output> {
let output = command.output().map_err(|error|
Error::new(Span::call_site(), format!("failed to execute command: {}", error))
)?;
verbose_command_error(output).map_err(|message|
Error::new(Span::call_site(), message)
)
}
fn verbose_command_error(output: std::process::Output) -> std::result::Result<std::process::Output, String>
{
if output.status.success() {
Ok(output)
} else if let Some(status) = output.status.code() {
let message = Some(strip_trailing_newline(output.stderr));
let message = message.filter(|m| m.len() > 0 && m.len() <= 500);
let message = message.filter(|m| m.iter().position(|c| c == &b'\n').is_none());
let message = message.and_then(|m| String::from_utf8(m).ok());
if let Some(message) = message {
Err(format!("external command exited with status {}: {}", status, message))
} else {
Err(format!("external command exited with status {}", status))
}
} else {
#[cfg(target_family = "unix")] {
use std::os::unix::process::ExitStatusExt;
if let Some(signal) = output.status.signal() {
Err(format!("external command killed by signal {}", signal))
} else {
Err(format!("external command failed, but did not exit and was not killed by a signal, this can only be a bug in std::process"))
}
}
#[cfg(not(target_family = "unix"))] {
Err(format!("external command killed by signal"))
}
}
}
fn strip_trailing_newline(mut input: Vec<u8>) -> Vec<u8> {
if input.len() > 0 && input[input.len() - 1] == b'\n' {
input.pop();
}
input
}
}