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
use std::cmp::Ordering;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;

use lazy_static::lazy_static;
use regex::Regex;
use semver::Version;
use walkdir::WalkDir;

// A `Component` keeps track of the rustc version associated with the component in question.
#[derive(Debug)]
struct Component {
    date_vers: Option<DateVersion>,
    path: PathBuf,
}

// A `DateVersion` allows you to sort first by the semantic version and date second if the versions
// are equal.
#[derive(Debug, Eq, PartialEq, PartialOrd)]
struct DateVersion {
    rustc_vers: Option<Version>,
    date: String,
}

impl Ord for DateVersion {
    fn cmp(&self, other: &DateVersion) -> Ordering {
        let vers_cmp = self.rustc_vers.cmp(&other.rustc_vers);
        if vers_cmp == Ordering::Equal {
            return self.date.cmp(&other.date);
        }
        vers_cmp
    }
}

impl DateVersion {
    fn new(rustc_vers: Option<Version>, date: String) -> DateVersion {
        DateVersion { rustc_vers, date }
    }
}

impl Component {
    fn new(date_vers: Option<DateVersion>, path: PathBuf) -> Component {
        Component { date_vers, path }
    }
}

// Return either the user's preferred $RUSTUP_HOME or the default location.
fn rustup_home() -> Option<PathBuf> {
    let mut p = PathBuf::new();
    if let Some(custom_path) = option_env!("RUSTUP_HOME") {
        p.push(custom_path);
        return Some(p);
    }

    let home = dirs::home_dir()?;
    p.push(home);
    p.push(".rustup");

    Some(p)
}

// Given the version string from rustc, attempt to parse the date.
fn parse_rustc_date(rustc_v: &[u8]) -> Option<DateVersion> {
    // This may not be the most ideal way to get the version.
    // It assumes that the output looks like:
    // rustc 1.32.0 (9fda7c223 2019-01-16)
    lazy_static! {
        static ref PATTERN: Regex = Regex::new(
            r"rustc (\d+.\d+.\d+(?:-[\.0-9a-z]+)?)(?: \([[:alnum:]]+ (\d{4}-\d{2}-\d{2})\))?"
        )
        .unwrap();
    }

    let version = str::from_utf8(rustc_v).unwrap_or_default();
    let captures = PATTERN.captures(version)?;
    let vers = Version::parse(captures.get(1).map_or("", |v| v.as_str())).ok();
    let date = String::from(captures.get(2).map_or("", |v| v.as_str()));

    Some(DateVersion::new(vers, date))
}

// Try and parse the version from the Rust compiler.
fn rustc_version(bin_path: &Path) -> Option<DateVersion> {
    match Command::new(bin_path).arg("-V").output() {
        Ok(o) => parse_rustc_date(&o.stdout),
        Err(_) => None,
    }
}

/// Given a Rust component name, search through all of the available toolchains
/// on the system to see if it is installed. It will return the path of the component that has
/// the latest version.
pub fn find_installed_component(name: &str) -> Option<PathBuf> {
    let mut components = Vec::new();
    let mut root = rustup_home()?;
    root.push("toolchains");

    // For Windows, we need to add an exe extension.
    let mut n = String::from(name);
    let name = if cfg!(windows) {
        n.push_str(".exe");
        n
    } else {
        n
    };

    for entry in WalkDir::new(root)
        .max_depth(3)
        .into_iter()
        .filter_map(|e| e.ok())
    {
        let parent = entry.path().parent()?;
        if parent.ends_with("bin") {
            let bin_name = entry.path().file_name()?;

            if bin_name == name.as_str() {
                // This assumes that we will always have a rustc in this same toolchain location.
                // I suppose a user could have a very custom build but I am not sure how much we
                // need to support.
                let mut rustc_path = PathBuf::from(parent);
                if cfg!(windows) {
                    rustc_path.push("rustc.exe");
                } else {
                    rustc_path.push("rustc");
                }
                components.push(Component::new(
                    rustc_version(&rustc_path),
                    PathBuf::from(&entry.path()),
                ));
            }
        }
    }

    // Sort by the rustc version leaving the maximal one at the end.
    components.sort_by(|a, b| a.date_vers.cmp(&b.date_vers));

    if let Some(c) = components.pop() {
        return Some(c.path);
    }

    None
}

#[cfg(test)]
mod test {
    use semver::Version;

    use super::{parse_rustc_date, DateVersion};

    #[test]
    fn test_parse_rustc_date() {
        let cases = vec![
            "".as_bytes(),
            "rustc not found".as_bytes(),
            "rustc 1.34.0-nightly (097c04cf4 2019-02-24)".as_bytes(),
            "rustc 1.34.0-beta.1 (744b374ab 2019-02-26)".as_bytes(),
            "rustc 1.35.0-dev".as_bytes(),
            "rustc 1.32.0 (9fda7c223 2019-01-16)".as_bytes(),
        ];
        let expected = vec![
            None,
            None,
            Some(DateVersion::new(
                Some(Version::parse("1.34.0-nightly").unwrap()),
                String::from("2019-02-24"),
            )),
            Some(DateVersion::new(
                Some(Version::parse("1.34.0-beta.1").unwrap()),
                String::from("2019-02-26"),
            )),
            Some(DateVersion::new(
                Some(Version::parse("1.35.0-dev").unwrap()),
                String::from(""),
            )),
            Some(DateVersion::new(
                Some(Version::parse("1.32.0").unwrap()),
                String::from("2019-01-16"),
            )),
        ];

        for (i, case) in cases.iter().enumerate() {
            assert_eq!(parse_rustc_date(case), expected[i]);
        }
    }

    #[test]
    fn test_version_parse_fail() {
        let v2 = Version::parse("1.1.0").unwrap();
        let d1 = DateVersion::new(None, String::from("2019-01-01"));
        let d2 = DateVersion::new(Some(v2), String::from("2019-01-01"));

        assert!(d2 > d1);
    }

    #[test]
    fn test_different_versions() {
        let v1 = Version::parse("1.2.3").unwrap();
        let v2 = Version::parse("1.1.0").unwrap();
        let d1 = DateVersion::new(Some(v1), String::from("2019-01-01"));
        let d2 = DateVersion::new(Some(v2), String::from("2019-01-01"));

        assert!(d2 < d1);
    }

    #[test]
    fn test_many_nightly_strings() {
        let v = Version::parse("1.0.0-nightly").unwrap();
        let mut versions = vec![
            DateVersion::new(Some(v.clone()), String::from("2019-02-20")),
            DateVersion::new(Some(v.clone()), String::from("2019-02-24")),
            DateVersion::new(Some(v.clone()), String::from("2019-01-10")),
        ];
        versions.sort();

        assert_eq!(
            versions.pop().unwrap(),
            DateVersion::new(Some(v.clone()), String::from("2019-02-24"))
        );
    }
}