Skip to main content

vivacity_core/
platform.rs

1//! What a platform check reports (`PlatformFailure`, the shared
2//! `--ignore-platform-req` matching) and the per-platform switches
3//! (`parallel_io`, `cache_dir`). The probe of the local PHP and the check
4//! of a lock against it live in `vivacity_resolver::platform`
5//! (`probe`, `platform_packages`, `check_install`), the port of
6//! `PlatformRepository` that `install` and `update` both use.
7
8use std::path::PathBuf;
9
10#[derive(Debug, PartialEq, Eq)]
11pub struct PlatformFailure {
12    /// "php", "ext-mbstring", ...
13    pub requirement: String,
14    pub constraint: String,
15    /// Requesting package (None = the lock's platform section).
16    pub required_by: Option<String>,
17    pub reason: FailureReason,
18}
19
20#[derive(Debug, PartialEq, Eq)]
21pub enum FailureReason {
22    Missing,
23    Mismatch { installed: String },
24    Unsupported,
25}
26
27/// Whether parallel file I/O pays on this machine: yes where I/O latency
28/// dominates (Linux: ext4/WSL2 measured 2x on a wiped vendor/ and on a
29/// cold classmap scan), no where the page cache is the bottleneck (APFS:
30/// parallel reads 1.3-4x slower, DECISIONS.md M5). `VIVACITY_PARALLEL_IO`
31/// (`0`/`1`) overrides the default.
32pub fn parallel_io() -> bool {
33    match std::env::var("VIVACITY_PARALLEL_IO") {
34        Ok(v) => v != "0" && !v.is_empty(),
35        Err(_) => cfg!(target_os = "linux"),
36    }
37}
38
39pub fn cache_dir() -> PathBuf {
40    if let Ok(d) = std::env::var("VIVACITY_CACHE_DIR") {
41        return PathBuf::from(d);
42    }
43    #[cfg(windows)]
44    {
45        if let Ok(l) = std::env::var("LOCALAPPDATA") {
46            if !l.is_empty() {
47                return PathBuf::from(l).join("vivacity");
48            }
49        }
50    }
51    if let Ok(xdg) = std::env::var("XDG_CACHE_HOME") {
52        return PathBuf::from(xdg).join("vivacity");
53    }
54    let home = std::env::var("HOME")
55        .or_else(|_| std::env::var("USERPROFILE"))
56        .unwrap_or_else(|_| ".".to_owned());
57    if cfg!(target_os = "macos") {
58        PathBuf::from(home).join("Library/Caches/vivacity")
59    } else {
60        PathBuf::from(home).join(".cache/vivacity")
61    }
62}
63
64/// `--ignore-platform-req` patterns (`*`, a name, `ext-*`, a trailing `+`).
65pub fn is_ignored(requirement: &str, ignored: &[String]) -> bool {
66    ignored.iter().any(|pat| {
67        let pat = pat.strip_suffix('+').unwrap_or(pat);
68        pat == "*"
69            || pat == requirement
70            || pat
71                .strip_suffix('*')
72                .is_some_and(|prefix| requirement.starts_with(prefix))
73    })
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn ignore_patterns() {
82        let pats = |v: &[&str]| v.iter().map(|s| (*s).to_owned()).collect::<Vec<_>>();
83        assert!(is_ignored("php", &pats(&["*"])));
84        assert!(is_ignored("ext-gd", &pats(&["ext-*"])));
85        assert!(!is_ignored("php", &pats(&["ext-*"])));
86        assert!(is_ignored("ext-gd", &pats(&["ext-gd+"])));
87        assert!(!is_ignored("ext-intl", &pats(&["ext-gd"])));
88    }
89}