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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#![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 color_eyre::eyre::{Context, Result};
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},
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>,
ignore_git: Option<bool>,
include_git: Option<bool>,
ignore_target: Option<bool>,
include_target: Option<bool>,
clean: bool,
#[serde(skip)]
clean_git: bool,
}
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 mut wargo_config = get_wargo_config(&workspace_root)?;
let dest_dir = get_destination_dir(&wargo_config, &workspace_root);
let entries = collect_entries(&mut wargo_config, &workspace_root)?;
copy_files(entries, &wargo_config, &workspace_root, &dest_dir)?;
let artifacts = exec_cargo_command(&dest_dir, &workspace_root, args)?;
copy_artifacts(&dest_dir, &workspace_root, artifacts)?;
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 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: WargoConfig = if wargo_config.exists() {
let wargo_config = fs::read_to_string(wargo_config)?;
toml::from_str(&wargo_config)?
} else {
WargoConfig::default()
};
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
}
fn collect_entries<P>(
wargo_config: &mut WargoConfig,
workspace_root: &P,
) -> GenericResult<Vec<DirEntry>>
where
P: AsRef<Path>,
{
let mut patterns = vec!["**"];
if let Some(include_git) = wargo_config.include_git {
if !include_git {
patterns.push("!.git");
} else {
wargo_config.clean_git = true;
}
} else if let Some(ignore_git) = wargo_config.ignore_git {
if ignore_git {
patterns.push("!.git");
} else {
wargo_config.clean_git = true;
}
} else {
patterns.push("!.git");
}
if let Some(include_target) = wargo_config.include_target {
if !include_target {
patterns.push("!target");
}
} else if let Some(ignore_target) = wargo_config.ignore_target {
if ignore_target {
patterns.push("!target");
}
} else {
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>,
wargo_config: &WargoConfig,
workspace_root: &P,
dest_dir: &P,
) -> NullResult
where
P: AsRef<Path>,
{
if wargo_config.clean && dest_dir.as_ref().exists() {
fs::remove_dir_all(&dest_dir).context("dest_dir cleaning failed")?;
}
fs::create_dir_all(&dest_dir).context("dest_dir creation failed")?;
let git_dir = &dest_dir.as_ref().join(".git");
if wargo_config.clean_git && git_dir.exists() {
fs::remove_dir_all(&git_dir).context("dest_dir/.git cleaning failed")?;
}
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).context("Directory creation failed")?;
} else {
fs::copy(src_path, dst_path).with_context(|| {
format!(
"Copying failed: {} -> {}",
&src_path.display(),
&dst_path.display()
)
})?;
}
set_symlink_file_times(dst_path, atime, mtime).with_context(|| {
format!("Setting file timestamps failed for {}", &dst_path.display())
})?;
}
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 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(())
}