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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#![doc = include_str!("../README.md")]
#![doc(
test(attr(allow(unused_variables), deny(warnings))),
html_logo_url = "https://raw.githubusercontent.com/asaaki/wargo/main/.assets/logo-temp.png"
)]
#![cfg_attr(feature = "docs", feature(doc_cfg))]
#![forbid(unsafe_code)]
use cargo_metadata::{Message, MetadataCommand};
use filetime::{set_symlink_file_times, FileTime};
use globwalk::DirEntry;
use serde::Deserialize;
use std::{
env,
error::Error,
ffi::OsStr,
fs,
path::{Path, PathBuf},
process::{Command, Stdio},
result::Result,
vec,
};
mod check;
mod paths;
mod progress;
type GenericResult<T> = Result<T, Box<dyn Error>>;
pub type NullResult = GenericResult<()>;
const SKIPPABLES: [&str; 4] = ["wargo", "cargo-wsl", "cargo", "wsl"];
const HELP_TEXT: &str = r#"wargo
cargo's evil twin to work with projects in the twilight zone of WSL2
HELP TEXT WENT MISSING IN THE DARK …
Maybe you find more helpful information at:
https://github.com/asaaki/wargo
"#;
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct WargoConfig {
project_dir: Option<String>,
dest_base_dir: Option<String>,
use_mktemp: bool,
mktemp_rchars: u8,
#[serde(default = "default_true")]
ignore_git: bool,
#[serde(default = "default_true")]
ignore_target: bool,
clean: bool,
}
const fn default_true() -> bool {
true
}
pub fn run(_from: &str) -> NullResult {
check::wsl2_or_exit()?;
let args = parse_args();
if args.is_empty() || args[0] == "--help" {
println!("{}", HELP_TEXT);
return Ok(());
}
let workspace_root = MetadataCommand::new()
.exec()?
.workspace_root
.into_std_path_buf()
.canonicalize()?;
let wargo_config = get_wargo_config(&workspace_root)?;
let dest_dir = get_destination_dir(&wargo_config, &workspace_root);
let entries = collect_entries(&wargo_config, &workspace_root)?;
if wargo_config.clean && dest_dir.exists() {
fs::remove_dir_all(&dest_dir)?;
}
fs::create_dir_all(&dest_dir)?;
copy_files(entries, &workspace_root, &dest_dir)?;
let artifacts = exec_cargo_command(&dest_dir, &workspace_root, args)?;
copy_artifacts(&dest_dir, &workspace_root, artifacts)?;
Ok(())
}
fn copy_artifacts<P>(dest_dir: &P, workspace_root: &P, artifacts: Vec<PathBuf>) -> NullResult
where
P: AsRef<Path>,
{
if !artifacts.is_empty() {
for artifact in artifacts {
let rel_artifact = artifact.strip_prefix(&dest_dir)?;
let origin_location = &workspace_root.as_ref().join(rel_artifact);
if let Some(parent) = origin_location.parent() {
fs::create_dir_all(&parent)?;
fs::copy(artifact, origin_location)?;
eprintln!("Copied compile artifact to: {}", origin_location.display());
}
}
};
Ok(())
}
fn parse_args() -> Vec<String> {
if env::args().count() == 0 {
return Vec::new();
}
let args: Vec<String> = env::args()
.skip_while(|arg| match arg.split('/').last() {
Some(a) => SKIPPABLES.contains(&a),
None => false,
})
.collect();
args
}
fn collect_entries<P>(
wargo_config: &WargoConfig,
workspace_root: &P,
) -> GenericResult<Vec<DirEntry>>
where
P: AsRef<Path>,
{
let mut patterns = vec!["**"];
if wargo_config.ignore_git {
patterns.push("!.git")
}
if wargo_config.ignore_target {
patterns.push("!target")
}
let entries: Vec<DirEntry> =
globwalk::GlobWalkerBuilder::from_patterns(workspace_root, &patterns)
.contents_first(false)
.build()?
.into_iter()
.filter_map(Result::ok)
.collect();
Ok(entries)
}
fn copy_files<P>(entries: Vec<DirEntry>, workspace_root: &P, dest_dir: &P) -> NullResult
where
P: AsRef<Path>,
{
let bar = progress::bar(entries.len() as u64);
for entry in bar.wrap_iter(entries.iter()) {
let is_dir = entry.file_type().is_dir();
let src_path = entry.path();
let prj_path = src_path.strip_prefix(workspace_root)?;
let dst_path = &dest_dir.as_ref().to_path_buf().join(prj_path);
let metadata = entry.metadata()?;
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
if is_dir {
fs::create_dir_all(dst_path)?;
} else {
fs::copy(src_path, dst_path)?;
}
set_symlink_file_times(dst_path, atime, mtime)?;
}
bar.finish_with_message("Files copied");
Ok(())
}
fn exec_cargo_command<P>(
dest_dir: &P,
workspace_root: &P,
args: Vec<String>,
) -> GenericResult<Vec<PathBuf>>
where
P: AsRef<Path>,
{
let ws_rel_location = env::current_dir()?
.canonicalize()?
.strip_prefix(&workspace_root)?
.to_path_buf();
let exec_dest = dest_dir.as_ref().join(ws_rel_location).canonicalize()?;
let mut files: Vec<PathBuf> = Vec::new();
let mut cargo_args = args;
if let Some(arg) = cargo_args.first() {
if arg == "build" {
cargo_args.insert(1, "--message-format=json-render-diagnostics".into());
let mut cmd = Command::new("cargo")
.args(cargo_args)
.current_dir(&exec_dest)
.stdout(Stdio::piped())
.spawn()?;
let reader = std::io::BufReader::new(cmd.stdout.take().expect("no stdout captured"));
for message in Message::parse_stream(reader) {
if let Message::CompilerArtifact(artifact) = message.unwrap() {
if artifact.target.kind[0] == "bin" {
for filename in artifact.filenames {
files.push(filename.into_std_path_buf())
}
}
}
}
cmd.wait()?;
} else {
let mut cmd = Command::new("cargo")
.args(cargo_args)
.current_dir(&exec_dest)
.spawn()?;
cmd.wait()?;
}
};
Ok(files)
}
fn get_wargo_config<P>(workspace_root: &P) -> GenericResult<WargoConfig>
where
P: AsRef<Path>,
{
let wargo_config = workspace_root.as_ref().join("Wargo.toml");
let wargo_config = fs::read_to_string(wargo_config)?;
let wargo_config: WargoConfig = toml::from_str(&wargo_config)?;
Ok(wargo_config)
}
fn get_destination_dir<P>(wargo_config: &WargoConfig, workspace_root: &P) -> PathBuf
where
P: AsRef<Path>,
{
let project_dir = get_project_dir(wargo_config, &workspace_root);
let dest = if let Some(dir) = &wargo_config.dest_base_dir {
paths::untilde(dir)
} else {
let home = dirs::home_dir().unwrap();
home.join("tmp")
}
.join(project_dir);
paths::normalize_path(&dest)
}
fn get_project_dir<'a, P>(wargo_config: &'a WargoConfig, workspace_root: &'a P) -> &'a OsStr
where
P: AsRef<Path>,
{
let project_dir = if let Some(dir) = &wargo_config.project_dir {
OsStr::new(dir)
} else {
workspace_root.as_ref().iter().last().unwrap()
};
project_dir
}