updt 3.0.0

Cross-platform update helper for npm, cargo, rustup, scoop, Homebrew, paru, flatpak, pacman, DNF, and pkg.
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
pub fn first_json_payload(output: &str) -> Option<&str> {
    let start = output.find('{')?;
    let bytes = output.as_bytes();
    let mut scanner = JsonPayloadScanner::default();

    for (idx, b) in bytes.iter().enumerate().skip(start) {
        if scanner.consume(*b) {
            return output.get(start..=idx);
        }
    }
    None
}

#[derive(Default)]
struct JsonPayloadScanner {
    depth: i32,
    in_string: bool,
    escaped: bool,
}

impl JsonPayloadScanner {
    fn consume(&mut self, byte: u8) -> bool {
        if self.in_string {
            self.consume_string_byte(byte);
            false
        } else {
            self.consume_json_byte(byte)
        }
    }

    fn consume_string_byte(&mut self, byte: u8) {
        if self.escaped {
            self.escaped = false;
            return;
        }
        match byte {
            b'\\' => self.escaped = true,
            b'"' => self.in_string = false,
            _ => {}
        }
    }

    fn consume_json_byte(&mut self, byte: u8) -> bool {
        match byte {
            b'"' => self.in_string = true,
            b'{' => self.depth += 1,
            b'}' => {
                self.depth -= 1;
                return self.depth == 0;
            }
            _ => {}
        }
        false
    }
}

pub fn first_token(line: &str) -> Option<String> {
    line.split_whitespace().next().map(ToOwned::to_owned)
}

pub fn parse_cargo_list(output: &str) -> Result<Vec<String>, ()> {
    let mut pkgs = Vec::new();
    for raw in output.lines() {
        if let Some(pkg) = parse_cargo_list_line(raw)? {
            pkgs.push(pkg);
        }
    }
    Ok(pkgs)
}

fn parse_cargo_list_line(raw: &str) -> Result<Option<String>, ()> {
    let line = raw.trim();
    if cargo_list_line_ignored(line) {
        return Ok(None);
    }
    let parts: Vec<&str> = line.split_whitespace().collect();
    parse_cargo_package_row(&parts)
}

fn cargo_list_line_ignored(line: &str) -> bool {
    line.is_empty()
        || line.starts_with("Polling registry ")
        || line.split_whitespace().collect::<Vec<_>>().as_slice()
            == ["Package", "Installed", "Latest", "Needs", "update"]
}

fn parse_cargo_package_row(parts: &[&str]) -> Result<Option<String>, ()> {
    if !cargo_package_row_shape(parts) {
        return Ok(None);
    }
    cargo_package_update(parts[0], parts[3])
}

fn cargo_package_row_shape(parts: &[&str]) -> bool {
    parts.len() == 4
        && parts[1].starts_with('v')
        && (parts[2].starts_with('v') || parts[2] == "N/A")
}

fn cargo_package_update(name: &str, needs_update: &str) -> Result<Option<String>, ()> {
    match needs_update {
        "Yes" => Ok(Some(name.to_string())),
        "No" => Ok(None),
        _ => Err(()),
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScoopExportApp {
    pub name: String,
    pub version: String,
    pub source: String,
    pub info: String,
}

impl ScoopExportApp {
    #[cfg(test)]
    pub fn new(name: &str, is_global: bool) -> Self {
        Self {
            name: name.to_string(),
            version: String::new(),
            source: String::new(),
            info: if is_global {
                "Global install".to_string()
            } else {
                String::new()
            },
        }
    }

    pub fn is_global(&self) -> bool {
        self.info.contains("Global install")
    }

    pub fn is_held(&self) -> bool {
        self.info.contains("Held package")
    }
}

pub fn parse_scoop_export_output(output: &str) -> Result<Vec<ScoopExportApp>, ()> {
    let json_text = first_json_payload(output).unwrap_or(output);
    let root = serde_json::from_str::<serde_json::Value>(json_text).map_err(|_| ())?;
    let apps = root.get("apps").and_then(|v| v.as_array()).ok_or(())?;
    let mut items = Vec::with_capacity(apps.len());
    for item in apps {
        let name = item
            .get("Name")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        if name.is_empty() {
            continue;
        }
        let version = item
            .get("Version")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        let source = item
            .get("Source")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        let info = item
            .get("Info")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        items.push(ScoopExportApp {
            name: name.to_string(),
            version: version.to_string(),
            source: source.to_string(),
            info: info.to_string(),
        });
    }
    Ok(items)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScoopBlockedProcessInfo {
    pub app_name: Option<String>,
    pub details: Vec<String>,
}

pub fn parse_scoop_blocked_process_output(output: &str) -> Option<ScoopBlockedProcessInfo> {
    const HEADER_MARKER: &str = "The following instances of \"";
    const HEADER_SUFFIX: &str = "\" are still running. Close them and try again.";
    const SKIP_MARKER: &str = "Running process detected, skip updating.";

    let cleaned = strip_ansi_control_sequences(output);
    let lines = cleaned.lines().map(str::trim_end).collect::<Vec<_>>();
    let mut header_index = None;
    let mut skip_index = None;

    for (idx, line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if header_index.is_none()
            && trimmed.contains(HEADER_MARKER)
            && trimmed.contains(HEADER_SUFFIX)
        {
            header_index = Some(idx);
        }
        if skip_index.is_none() && trimmed.contains(SKIP_MARKER) {
            skip_index = Some(idx);
        }
    }

    if header_index.is_none() && skip_index.is_none() {
        return None;
    }

    let app_name = header_index.and_then(|idx| {
        let line = lines[idx].trim();
        let start = line.find(HEADER_MARKER)? + HEADER_MARKER.len();
        let rest = line.get(start..)?;
        let end = rest.find(HEADER_SUFFIX)?;
        Some(rest[..end].to_string())
    });

    let details = if let Some(start) = header_index {
        let end = skip_index.unwrap_or(lines.len());
        lines[start..end]
            .iter()
            .map(|line| line.trim_end())
            .filter(|line| !line.trim().is_empty())
            .map(ToOwned::to_owned)
            .collect::<Vec<_>>()
    } else {
        vec![SKIP_MARKER.to_string()]
    };

    Some(ScoopBlockedProcessInfo { app_name, details })
}

pub fn strip_ansi_control_sequences(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut chars = text.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '\u{1b}' {
            if chars.peek().is_some_and(|next| *next == '[') {
                let _ = chars.next();
                for c in chars.by_ref() {
                    if c.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
            continue;
        }
        out.push(ch);
    }
    out
}

#[cfg(test)]
mod tests {
    use super::{
        ScoopBlockedProcessInfo, ScoopExportApp, parse_scoop_blocked_process_output,
        parse_scoop_export_output,
    };

    #[test]
    fn creates_scoop_export_app() {
        let app = ScoopExportApp::new("git", true);
        assert_eq!(app.name, "git");
        assert!(app.is_global());
        assert!(!app.is_held());
    }

    #[test]
    fn parses_scoop_export_apps() {
        let output = r#"{
            "apps": [
                {
                    "Name": "7zip",
                    "Version": "26.03",
                    "Source": "main",
                    "Info": ""
                },
                {
                    "Name": "git",
                    "Version": "2.54.0",
                    "Source": "main",
                    "Info": "Global install"
                },
                {
                    "Name": "curl",
                    "Version": "8.0.0",
                    "Source": "main",
                    "Info": "Held package"
                }
            ]
        }"#;

        let apps = parse_scoop_export_output(output).unwrap();
        assert_eq!(apps.len(), 3);
        assert_eq!(apps[0].name, "7zip");
        assert!(!apps[0].is_global());
        assert!(!apps[0].is_held());

        assert_eq!(apps[1].name, "git");
        assert!(apps[1].is_global());
        assert!(!apps[1].is_held());

        assert_eq!(apps[2].name, "curl");
        assert!(!apps[2].is_global());
        assert!(apps[2].is_held());
    }

    #[test]
    fn parses_scoop_export_apps_with_leading_noise() {
        let output = r#"WARN  Scoop bucket(s) out of date.
{
    "apps": [
        {
            "Name": "nodejs",
            "Version": "22.0.0"
        }
    ]
}"#;
        let apps = parse_scoop_export_output(output).unwrap();
        assert_eq!(apps.len(), 1);
        assert_eq!(apps[0].name, "nodejs");
        assert_eq!(apps[0].version, "22.0.0");
        assert!(!apps[0].is_global());
        assert!(!apps[0].is_held());
    }

    #[test]
    fn parses_scoop_blocked_process_output() {
        let output = "\
ERROR The following instances of \"git\" are still running. Close them and try again.\n\
\n\
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName\n\
-------  ------    -----      -----     ------     --  -- -----------\n\
    123      10    10000      20000       0.10   1234   1 git\n\
\n\
Running process detected, skip updating.\n";

        assert_eq!(
            parse_scoop_blocked_process_output(output),
            Some(ScoopBlockedProcessInfo {
                app_name: Some("git".to_string()),
                details: vec![
                    "ERROR The following instances of \"git\" are still running. Close them and try again.".to_string(),
                    "Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName".to_string(),
                    "-------  ------    -----      -----     ------     --  -- -----------".to_string(),
                    "123      10    10000      20000       0.10   1234   1 git".to_string(),
                ],
            })
        );
    }

    #[test]
    fn ignores_normal_scoop_update_output_for_blocked_process_detection() {
        let output = "\
Updating 'git' (2.54.0 -> 2.55.0)\n\
Downloading new version\n\
'git' (2.55.0) was installed successfully!\n";

        assert_eq!(parse_scoop_blocked_process_output(output), None);
    }

    #[test]
    fn ignores_unrelated_failures_for_blocked_process_detection() {
        let output = "ERROR hash check failed\nInstallation aborted.\n";

        assert_eq!(parse_scoop_blocked_process_output(output), None);
    }

    #[test]
    fn parses_cargo_list_output() {
        let output = "\
    Updating registry `https://github.com/rust-lang/crates.io-index`
    Updating git repository `https://github.com/nabijaczleweli/cargo-update`
warning: some warning message about crate locks
Package         Installed  Latest    Needs update
cargo-binstall  v1.19.1    v1.20.0   Yes
cargo-deny      v0.19.8    v0.19.9   Yes
cbindgen        v0.29.3    v0.29.4   Yes
charasay        v3.3.0     v3.3.1    Yes
updt            v0.2.0     v0.2.1    Yes
bindgen-cli     v0.72.1    v0.72.1   No
bluz            v0.3.1     v0.3.1    No
cargo-about     v0.9.0     v0.9.0    No
cargo-edit      v0.13.11   v0.13.11  No
cargo-expand    v1.0.122   v1.0.122  No
cargo-nextest   v0.9.137   v0.9.137  No
cargo-outdated  v0.19.0    v0.19.0   No
cargo-update    v20.0.2    v20.0.2   No
kaleidoscope    v0.2.0     N/A       No
mmisay          v0.1.0     v0.1.0    No
names           v0.14.0    v0.14.0   No
rusty-rain      v0.5.0     v0.5.0    No
tlrc            v1.13.1    v1.13.1   No
";
        let expected = vec![
            "cargo-binstall".to_string(),
            "cargo-deny".to_string(),
            "cbindgen".to_string(),
            "charasay".to_string(),
            "updt".to_string(),
        ];
        assert_eq!(super::parse_cargo_list(output), Ok(expected));
    }
}