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
use crate::Cache;
use anyhow::Result;
use std::path::PathBuf;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
pub struct Config {
cache: Option<Cache>,
verbose: bool,
choice: ColorChoice,
}
impl Config {
pub fn new() -> Config {
Config {
cache: None,
verbose: false,
choice: if atty::is(atty::Stream::Stderr) {
ColorChoice::Auto
} else {
ColorChoice::Never
},
}
}
pub fn load_cache(&mut self) -> Result<()> {
assert!(!self.cache.is_some());
self.cache = Some(Cache::new()?);
Ok(())
}
pub fn cache(&self) -> &Cache {
self.cache.as_ref().expect("cache not loaded yet")
}
pub fn is_verbose(&self) -> bool {
self.verbose
}
pub fn verbose(&self, f: impl FnOnce()) {
if self.verbose {
f();
}
}
pub fn set_verbose(&mut self, verbose: bool) {
self.verbose = verbose;
}
pub fn status(&self, name: &str, rest: &str) {
let mut shell = StandardStream::stderr(self.choice);
drop(shell.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true)));
eprint!("{:>12}", name);
drop(shell.reset());
eprintln!(" {}", rest);
}
pub fn print_error(&self, err: &anyhow::Error) {
if let Some(code) = crate::utils::normal_process_exit_code(err) {
std::process::exit(code);
}
let mut shell = StandardStream::stderr(self.choice);
drop(shell.set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_bold(true)));
eprint!("error");
drop(shell.reset());
eprintln!(": {}", err);
for cause in err.chain().skip(1) {
eprintln!("");
drop(shell.set_color(ColorSpec::new().set_bold(true)));
eprint!("Caused by");
drop(shell.reset());
eprintln!(":");
eprintln!(" {}", cause.to_string().replace("\n", "\n "));
}
}
pub fn info(&self, msg: &str) {
let mut shell = StandardStream::stderr(self.choice);
drop(shell.set_color(ColorSpec::new().set_fg(Some(Color::Cyan)).set_bold(true)));
eprint!("info");
drop(shell.reset());
eprintln!(": {}", msg);
}
fn get_tool(&self, tool: &str, version: Option<&str>) -> (PathBuf, PathBuf) {
let mut cache_path = self.cache().root().join(tool);
if let Some(v) = version {
cache_path.push(v);
cache_path.push(tool)
}
cache_path.set_extension(std::env::consts::EXE_EXTENSION);
if let Some(s) = std::env::var_os(tool.to_uppercase().replace("-", "_")) {
(s.into(), cache_path)
} else {
(cache_path.clone(), cache_path)
}
}
pub fn get_wasm_bindgen(&self, version: &str) -> (PathBuf, PathBuf) {
self.get_tool("wasm-bindgen", Some(version))
}
pub fn get_wasm_opt(&self) -> (PathBuf, PathBuf) {
self.get_tool("wasm-opt", None)
}
}