rusht 1.3.0

Shell commands written in Rust
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use ::std::cmp::min;
use ::std::collections::HashMap;
use ::std::collections::HashSet;
use ::std::path::PathBuf;
use std::cmp::max;
use std::env;

use ::itertools::Itertools;
use ::log::debug;
use ::log::warn;
use ::smallvec::SmallVec;

use crate::common::Dependent;
use crate::common::Task;
use crate::java::mvnw_args::TestMode;
use crate::java::newtype::{FullyQualifiedName, Profile};

#[derive(Debug, PartialEq, Eq, Default)]
pub struct MvnCmdConfig {
    /// Which files were changed. Might have been deleted.
    pub changed_files: HashSet<PathBuf>,
    /// Which modules to build. Empty means everything.
    pub modules: Option<Vec<String>>,
    pub no_build_deps: bool,
    pub tests: TestMode,
    pub lint: bool,
    pub checkstyle_version: String,
    pub verbose: bool,
    pub update: bool,
    pub clean: bool,
    pub phase_override: Option<String>,
    pub execs: Vec<FullyQualifiedName>,
    pub profiles: Vec<Profile>,
    pub threads: u32,
    pub max_memory_mb: u32,
    pub max_exec_memory_mb: u32,
    pub mvn_exe: PathBuf,
    pub mvn_arg: Vec<String>,
    pub mvn_defs: Vec<String>,
    pub java_home: PathBuf,
    pub cwd: PathBuf,
}

#[derive(Debug, Default)]
struct MvnTasks {
    version: Option<Task>,
    clean: Option<Task>,
    install_lint: Option<Task>,
    lint: Option<Task>,
    build: Option<Task>,
    test: Option<Task>,
    exes: SmallVec<[Task; 1]>,
}

impl MvnCmdConfig {
    /// Return commands that can be started concurrently and will wait for eachother.
    pub fn build_cmds(&self) -> Vec<Dependent> {
        self.collect_tasks().flatten()
    }

    fn collect_tasks(&self) -> MvnTasks {
        let single_cmd = self.modules.is_none() && self.profiles.is_empty();

        let mut tasks = MvnTasks::default();
        let mut args = vec![];
        if self.verbose {
            debug!("printing versions because of verbose mode");
            tasks.version = Some(self.make_mvn_task(vec!["--version".to_owned()]));
        }

        // Clean
        if self.clean {
            if !single_cmd {
                debug!("clean and build in separate commands, to clean everything while only building a subset (either because no --all or because of profiles)");
                let mut clean_args = vec!["clean".to_owned()];
                if !self.verbose {
                    clean_args.push("--quiet".to_owned());
                }
                tasks.clean = Some(self.make_mvn_task(clean_args));
            } else {
                debug!("clean and build in same command because of --all");
                args.push("clean".to_owned());
            }
        } else {
            debug!("not cleaning, incremental build");
        }

        // Lint
        if !self.lint {
            debug!("no lint requested, skipping checkstyle");
        } else if self.changed_files.is_empty() {
            debug!("no affected files, checkstyle lint was requested but will be skipped");
        } else {
            //TODO @mverleg: avoid doing this if all files are deleted
            if let Some(checkstyle_conf_pth) = self.get_checkstyle_conf_path() {
                debug!(
                    "linting enabled, found checkstyle config at: {}",
                    checkstyle_conf_pth.to_string_lossy()
                );
                let (task, checkstyle_jar_pth) =
                    ensure_checkstyle_jar_exists(&self.checkstyle_version);
                if let Some(task) = task {
                    tasks.install_lint = Some(task);
                }
                let mut lint_args = vec![
                    format!("-Xmx{}m", self.max_memory_mb),
                    "-jar".to_owned(),
                    checkstyle_jar_pth.to_str().unwrap().to_owned(),
                    "-c".to_owned(),
                    checkstyle_conf_pth.to_str().unwrap().to_owned(),
                ];
                lint_args.extend_from_slice(
                    &self
                        .changed_files
                        .iter()
                        .map(|af| {
                            af.to_str()
                                .expect("changed file path not unicode")
                                .to_owned()
                        })
                        .collect::<Vec<_>>(),
                );
                tasks.lint = Some(Task::new(
                    "java".to_owned(),
                    lint_args,
                    self.cwd.clone(),
                    None,
                ));
            }
        }

        // Determine maven stage
        let stage = if let Some(phase) = &self.phase_override {
            debug!("maven phase '{phase}' was explicitly requested");
            phase
        } else if self.tests.run_any() && single_cmd {
            debug!("maven verify because no install requested, there are tests (that might be ITs), and the tests don't run in a separate command");
            "integration-test"
        } else if self.tests == TestMode::NoBuild {
            debug!("maven compile because no install or tests requested");
            "compile"
        } else {
            debug!("maven test-compile because no install requested, and tests are run in a separate command");
            "test-compile"
            //TODO @mverleg: using package because goat does not compile well without it
            //"package"
        };
        args.push(stage.to_owned());
        if self.tests == TestMode::NoBuild {
            args.push("-Dmaven.test.skip=true".to_owned())
        }

        // Affected build modules
        if let Some(modules) = &self.modules {
            debug!(
                "building {} specific modules {} dependencies",
                modules.len(),
                if self.no_build_deps { "WITHOUT" } else  { "and their" }
            );
            if modules.is_empty() {
                panic!("no modules detected with -x nor specified with -p, and no --all")
            }
            for module in modules {
                args.push("--projects".to_owned());
                args.push(format!("{}", module));
            }
            if ! self.no_build_deps {
                args.push("--also-make".to_owned())
            }
        } else {
            debug!("building all modules");
        }

        // Modifier flags
        args.push(format!("--threads={}", self.threads));
        args.push(format!("-Dmaven.artifact.threads={}", max(8, 4 * self.threads)));
        if self.update {
            args.push("--update-snapshots".to_owned());
        } else {
            debug!("using offline mode because -U wasn't requested; try with -U if this fails");
            args.push("--offline".to_owned());
        }
        if !self.verbose {
            args.push("--quiet".to_owned());
        }

        // Default optimization flags
        self.add_opt_args(&mut args);

        // Tests
        match self.tests {
            TestMode::Files => {
                debug!("only running tests for changed files");
                unimplemented!("test mode files not implemented")
            }
            TestMode::Modules => {
                debug!("running tests for all modules that have changed files");
                unimplemented!("test mode modules not implemented")
            }
            TestMode::All => {
                debug!("running all tests");
            }
            TestMode::NoRun => {
                debug!("building tests but not running them");
                if single_cmd {
                    args.push("-DskipTests".to_owned());
                }
            }
            TestMode::NoBuild => {
                debug!("not building or running tests");
                if single_cmd {
                    args.push("-Dmaven.test.skip=true".to_owned());
                    args.push("-DskipTests".to_owned());
                }
            }
        }
        if !single_cmd || !self.tests.run_any() {
            debug!("skipping tests in build command, since tests are run in a separate command, or not at all");
            if ! args.contains(&"-DskipTests".to_owned()) {
                //TODO: to_owned is horrible but not impactful ^
                args.push("-DskipTests".to_owned());
            }
        }
        if self.tests.run_any() {
            if single_cmd {
                debug!("build and test in same command (all modules are built)");
                self.add_test_args(&mut args);
            } else {
                debug!("build and test in separate commands, to build recursively but test only specific modules");
                let mut test_args = vec!["test".to_owned()];
                self.add_opt_args(&mut test_args);
                self.add_test_args(&mut test_args);
                tasks.test = Some(self.make_mvn_task(test_args));
            }
        }

        tasks.build = Some(self.make_mvn_task(args));

        // Execute a class.
        for exec in &self.execs {
            let mut exe_args = vec![
                "exec:java".to_owned(),
                format!("-Dexec.mainClass=\"{}\"", exec),
            ];
            self.add_opt_args(&mut exe_args);
            tasks
                .exes
                .push(self.make_mvn_task_with_mem(exe_args, self.max_exec_memory_mb));
        }

        tasks
    }

    fn get_checkstyle_conf_path(&self) -> Option<PathBuf> {
        let pth = if let Ok(env_checkstyle_conf_pth) = env::var("CHECKSTYLE_CONF_PTH").map(PathBuf::from) {
            debug!("using checkstyle path from env: {}", &env_checkstyle_conf_pth.to_string_lossy());
            env_checkstyle_conf_pth
        } else {
            let mut checkstyle_conf_pth = self.cwd.clone();
            checkstyle_conf_pth.push("sputnik-rules");
            checkstyle_conf_pth.push("checkstyle.xml");
            checkstyle_conf_pth
        };
        if !pth.is_file() {
            warn!(
                "skipping checkstyle because config was not found at '{}'",
                pth.to_string_lossy());
            return None
        }
        Some(pth)
    }

    fn make_mvn_task_with_mem(&self, args: Vec<String>, memory_mb: u32) -> Task {
        let mut extra_env = HashMap::new();
        extra_env.insert(
            "MAVEN_OPTS".to_owned(),
            format!(
                "-XX:+UseG1GC -Xms{}m -Xmx{}m",
                min(256, memory_mb),
                memory_mb
            ),
        );
        self.make_task(self.mvn_exe.to_str().unwrap(), args, extra_env)
    }

    fn make_mvn_task(&self, args: Vec<String>) -> Task {
        self.make_mvn_task_with_mem(args, self.max_memory_mb)
    }

    fn make_task(
        &self,
        exe: impl Into<String>,
        mut args: Vec<String>,
        mut extra_env: HashMap<String, String>,
    ) -> Task {
        args.extend_from_slice(&self.mvn_arg);
        for def in &self.mvn_defs {
            args.push(format!("-D{def}"))
        }
        extra_env.insert(
            "JAVA_HOME".to_owned(),
            self.java_home.to_str().unwrap().to_owned(),
        );
        if !self.profiles.is_empty() {
            debug!("(de)activating {} maven profiles", self.profiles.len());
            args.push(format!(
                "--activate-profiles=\"{}\"",
                self.profiles.iter().join(",")
            ));
        }
        Task::new_with_env(exe.into(), args, self.cwd.to_owned(), None, extra_env)
    }

    fn add_opt_args(&self, args: &mut Vec<String>) {
        args.push("-Djava.net.preferIPv4Stack=true".to_owned());
        args.push("-Dmanagedversions.skip=true".to_owned());
        args.push("-Dmanagedversions.failOnError=false".to_owned());
        args.push("-Denforcer.skip=true".to_owned());
        args.push("-Ddatabase.skip=true".to_owned());
        args.push("-Dmaven.javadoc.skip=true".to_owned());
    }

    fn add_test_args(&self, args: &mut Vec<String>) {
        args.push("-DskipITs".to_owned());
        args.push("-Dsurefire.printSummary=false".to_owned());
        args.push("-DfailIfNoTests=false".to_owned());
        args.push("-Dparallel=all".to_owned());
        args.push("-DperCoreThreadCount=false".to_owned());
        args.push(format!(
            "-DthreadCount={}",
            if self.threads > 1 {
                4 * self.threads
            } else {
                1
            }
        ));
    }
}

fn ensure_checkstyle_jar_exists(version: &str) -> (Option<Task>, PathBuf) {
    let cache_dir = dirs::cache_dir().expect("failed to find cache directory");
    let mut checkstyle_jar_pth = cache_dir.clone();
    checkstyle_jar_pth.push(format!("checkstyle-{}.jar", version));
    if checkstyle_jar_pth.is_file() {
        debug!(
            "found checkstyle jar at: {}",
            checkstyle_jar_pth.to_string_lossy()
        );
        return (None, checkstyle_jar_pth);
    }
    let task = Task::new(
        "curl".to_owned(),
        vec![
            "-L".to_owned(),
            format!("https://github.com/checkstyle/checkstyle/releases/download/checkstyle-{version}/checkstyle-{version}-all.jar"),
            "--silent".to_owned(),
            "--show-error".to_owned(),
            "--fail".to_owned(),
            "--output".to_owned(),
            checkstyle_jar_pth.to_str().unwrap().to_owned(),
        ],
        cache_dir,
        None
    );
    debug!(
        "creating task to download checkstyle jar: {}",
        task.as_str()
    );
    (Some(task), checkstyle_jar_pth)
}

impl MvnTasks {
    fn flatten(self) -> Vec<Dependent> {
        let MvnTasks {
            version,
            clean,
            install_lint,
            lint,
            build,
            test,
            exes,
        } = self;
        let version = Dependent::new_optional("version", version);
        let mut clean = Dependent::new_optional("clean", clean);
        clean.depends_on(&version);
        let mut install_lint = Dependent::new_optional("install_lint", install_lint);
        install_lint.depends_on(&version);
        let mut lint = Dependent::new_optional("lint", lint);
        lint.depends_on(&install_lint);
        let mut build = Dependent::new_named("build", build.expect("build task must always exist"));
        build.depends_on(&lint); // linter sometimes fails on @Immutables if build is running
        build.depends_on(&clean);
        let mut test = Dependent::new_optional("test", test);
        test.depends_on(&build);
        let exes = exes
            .into_iter()
            .map(|ex| {
                let mut dep = Dependent::new_named("version", ex);
                dep.depends_on(&build);
                dep
            })
            .collect::<Vec<_>>();
        let mut tasks = vec![version, clean, install_lint, lint, build, test];
        tasks.extend(exes);
        tasks
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_all() {
        let conf = MvnCmdConfig {
            changed_files: vec![
                PathBuf::from("test/file.java"),
            ].into_iter().collect::<HashSet<_>>(),
            modules: None,
            tests: TestMode::NoBuild,
            ..MvnCmdConfig::default()
        };
        let cmds = get_tasks(conf.build_cmds());
        assert_eq!(cmds.len(), 1);
        let args = args_to_set(&cmds[0]);
        assert!(!args.contains("test-compile"));
        assert!(!args.contains("--also-make"));
        assert!(args.contains("--quiet"));
        assert!(args.contains("--offline"));
    }

    #[test]
    #[ignore]
    fn build_test_subset() {
        //TODO @mverleg:
        let conf = MvnCmdConfig {
            //TODO @mverleg: infer
            changed_files: vec![
                PathBuf::from("src/ClsA.java"),
                PathBuf::from("test/ClsB.java"),
            ].into_iter().collect::<HashSet<_>>(),
            modules: None,
            tests: TestMode::Files,
            ..MvnCmdConfig::default()
        };
        let cmds = get_tasks(conf.build_cmds());
        dbg!(&cmds);  //TODO @mverleg: TEMPORARY! REMOVE THIS!
        assert_eq!(cmds.len(), 1);
        let args = args_to_set(&cmds[0]);
        assert!(args.contains("--also-make"));
    }

    fn get_tasks(deps: Vec<Dependent>) -> Vec<Task> {
        deps.into_iter()
            .flat_map(|d| d.take_task().into_iter())
            .collect()
    }

    fn args_to_set(cmds: &Task) -> HashSet<String> {
        cmds.args.iter()
            .map(|a| a.to_owned())
            .collect::<HashSet<_>>()
    }
}