Skip to main content

lean_ctx_patterns/
maven.rs

1// SPDX-License-Identifier: Apache-2.0
2
3macro_rules! static_regex {
4    ($pattern:expr_2021) => {{
5        static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
6        RE.get_or_init(|| {
7            regex::Regex::new($pattern).expect(concat!("BUG: invalid static regex: ", $pattern))
8        })
9    }};
10}
11
12fn maven_download_re() -> &'static regex::Regex {
13    static_regex!(r"(?i)\[INFO\]\s+(Downloading|Downloaded)\s+")
14}
15
16fn maven_progress_re() -> &'static regex::Regex {
17    static_regex!(r"\[INFO\].*kB\s+\|")
18}
19
20fn gradle_download_re() -> &'static regex::Regex {
21    static_regex!(r"(?i)^(Downloading|Download)\s+https?://")
22}
23
24fn gradle_progress_re() -> &'static regex::Regex {
25    static_regex!(r"^[<>=\s]+$|^[0-9]+%\s+EXECUTING")
26}
27
28fn tests_run_re() -> &'static regex::Regex {
29    static_regex!(r"Tests run:\s*\d+")
30}
31
32fn is_maven_noise(line: &str) -> bool {
33    let t = line.trim_start();
34    if maven_download_re().is_match(t) {
35        return true;
36    }
37    if maven_progress_re().is_match(t) {
38        return true;
39    }
40    if t.contains("Progress (") && t.contains("):") && t.contains('%') {
41        return true;
42    }
43    false
44}
45
46fn is_gradle_noise(line: &str) -> bool {
47    let t = line.trim();
48    if gradle_download_re().is_match(t) {
49        return true;
50    }
51    if gradle_progress_re().is_match(t) {
52        return true;
53    }
54    let tl = t.to_ascii_lowercase();
55    tl.starts_with("consider enabling configuration cache")
56        || tl.contains("deprecated gradle features were used")
57        || tl.starts_with("you can use '--warning-mode")
58}
59
60fn is_maven_or_gradle_command(command: &str) -> bool {
61    let c = command.trim();
62    let cl = c.to_ascii_lowercase();
63    cl.starts_with("mvn ")
64        || cl.starts_with("./mvnw ")
65        || cl.starts_with("mvnw ")
66        || cl.starts_with("gradle ")
67        || cl.starts_with("./gradlew ")
68        || cl.starts_with("gradlew ")
69}
70
71fn is_gradle_command(command: &str) -> bool {
72    let cl = command.trim().to_ascii_lowercase();
73    cl.starts_with("gradle ") || cl.starts_with("./gradlew ") || cl.starts_with("gradlew ")
74}
75
76pub fn compress(command: &str, output: &str) -> Option<String> {
77    if !is_maven_or_gradle_command(command) {
78        return None;
79    }
80    if is_gradle_command(command) {
81        Some(compress_gradle(output))
82    } else {
83        Some(compress_maven(output))
84    }
85}
86
87fn compress_maven(output: &str) -> String {
88    let mut kept = Vec::new();
89
90    for line in output.lines() {
91        let t = line.trim_end();
92        if t.trim().is_empty() {
93            continue;
94        }
95        if is_maven_noise(t) {
96            continue;
97        }
98
99        let tl = t.to_ascii_lowercase();
100        if tl.contains("[error]")
101            || tl.contains("[fatal]")
102            || tl.contains("build failure")
103            || tl.contains("build success")
104            || tl.contains("failure!")
105            || tl.contains("tests run:")
106            || tl.contains("failures:")
107            || tl.contains("errors:")
108            || tl.contains("skipped:")
109            || tests_run_re().is_match(t)
110        {
111            kept.push(t.trim().to_string());
112            continue;
113        }
114
115        if tl.contains("[warning]") {
116            kept.push(t.trim().to_string());
117        }
118    }
119
120    if kept.is_empty() {
121        "mvn (no build/test lines kept)".to_string()
122    } else {
123        kept.join("\n")
124    }
125}
126
127fn compress_gradle(output: &str) -> String {
128    let mut kept = Vec::new();
129    let mut task_lines = Vec::new();
130
131    for line in output.lines() {
132        let t = line.trim_end();
133        if t.trim().is_empty() {
134            continue;
135        }
136        if is_gradle_noise(t) {
137            continue;
138        }
139
140        let tl = t.to_ascii_lowercase();
141        if tl.starts_with("> task ") {
142            if tl.contains("failed")
143                || tl.contains("failure")
144                || tl.contains("skipped")
145                || tl.contains("no-source")
146            {
147                task_lines.push(t.trim().to_string());
148            }
149            continue;
150        }
151
152        if tl.contains("actionable tasks:") {
153            kept.push(t.trim().to_string());
154            continue;
155        }
156
157        if tl.contains("build successful")
158            || tl.contains("build failed")
159            || tl.starts_with("failure:")
160            || tl.contains("what went wrong:")
161            || tl.contains("execution failed for task")
162            || tl.contains("error:")
163            || tl.contains("exception")
164            || tl.contains("tests completed:")
165            || (tl.contains("test ") && (tl.contains("failed") || tl.contains("passed")))
166            || tl.contains("there were failing tests")
167        {
168            kept.push(t.trim().to_string());
169        }
170    }
171
172    if !task_lines.is_empty() {
173        kept.push("tasks:\n".to_string() + &task_lines.join("\n"));
174    }
175
176    if kept.is_empty() {
177        "gradle (no summary kept)".to_string()
178    } else {
179        kept.join("\n")
180    }
181}