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
//! Main functions doing actual work.
//!
//! Use `installed_main_repo_packages()` to list the installed packages,
//! then use `intersect_packages()` to confirm which ones should be updated,
//! poll the packages' latest versions by calling `MainRepoPackage::pull_version` on them,
//! continue with doing whatever you wish.


use std::path::{PathBuf, Path};
use semver::Version as Semver;
use std::fs::{self, File};
use std::io::Read;
use regex::Regex;
use toml;
use json;

mod config;

pub use self::config::*;


lazy_static! {
    static ref PACKAGE_RGX: Regex = Regex::new(r"([^\s]+) ([^\s]+) \(([^+\s]+)+\+([^\s]+)\)").unwrap();
}


/// A representation of a package from the main [`crates.io`](https://crates.io) repository.
///
/// The newest version of a package is pulled from [`crates.io`](https://crates.io) via `pull_version()`.
///
/// The `parse()` function parses the format used in `$HOME/.cargo/.crates.toml`.
///
/// # Examples
///
/// ```
/// # extern crate cargo_update;
/// # extern crate semver;
/// # use cargo_update::ops::{MainRepoPackage, get_index_path};
/// # use semver::Version as Semver;
/// # use std::fs::{self, File};
/// # use std::io::Write;
/// # use std::env;
/// # fn main() {
/// # let mut cargo_dir = env::temp_dir();
/// # cargo_dir.push("cargo_update-doctest");
/// # let _ = fs::create_dir(&cargo_dir);
/// # cargo_dir.push("MainRepoPackage-0");
/// # let _ = fs::create_dir(&cargo_dir);
/// # File::create(["registry", "index", "github.com-1ecc6299db9ec823", "ra", "ce"].into_iter()
/// #     .fold(cargo_dir.clone(), |pb, chunk| {
/// #         let _ = fs::create_dir(pb.join(chunk));
/// #         pb.join(chunk)
/// #     }).join("racer"))
/// # .unwrap().write_all(br#"{"vers": "1.2.10", "yanked": false}"#).unwrap();
/// let registry = get_index_path(&cargo_dir);
/// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
/// let mut package = MainRepoPackage::parse(package_s).unwrap();
/// assert_eq!(package,
///            MainRepoPackage {
///                name: "racer".to_string(),
///                version: Some(Semver::parse("1.2.10").unwrap()),
///                newest_version: None,
///            });
///
/// package.pull_version(&registry);
/// assert!(package.newest_version.is_some());
/// # }
/// ```
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct MainRepoPackage {
    /// The package's name.
    ///
    /// Go to `https://crates.io/crates/{name}` to get the crate info.
    pub name: String,
    /// The package's locally installed version.
    pub version: Option<Semver>,
    /// The latest version of the package vailable at the main [`crates.io`](https://crates.io) repository.
    ///
    /// `None` by default, acquire via `MainRepoPackage::pull_version()`.
    pub newest_version: Option<Semver>,
}

impl MainRepoPackage {
    /// Try to decypher a package descriptor into a `MainRepoPackage`.
    ///
    /// Will return `None` if:
    ///
    ///   * the given package descriptor is invalid, or
    ///   * the package descriptor is not from the main [`crates.io`](https://crates.io) registry.
    ///
    /// In the returned instance, `newest_version` is always `None`, get it via `MainRepoPackage::pull_version()`.
    ///
    /// # Examples
    ///
    /// Main repository packages:
    ///
    /// ```
    /// # extern crate cargo_update;
    /// # extern crate semver;
    /// # use cargo_update::ops::MainRepoPackage;
    /// # use semver::Version as Semver;
    /// # fn main() {
    /// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
    /// assert_eq!(MainRepoPackage::parse(package_s).unwrap(),
    ///            MainRepoPackage {
    ///                name: "racer".to_string(),
    ///                version: Some(Semver::parse("1.2.10").unwrap()),
    ///                newest_version: None,
    ///            });
    ///
    /// let package_s = "cargo-outdated 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)";
    /// assert_eq!(MainRepoPackage::parse(package_s).unwrap(),
    ///            MainRepoPackage {
    ///                name: "cargo-outdated".to_string(),
    ///                version: Some(Semver::parse("0.2.0").unwrap()),
    ///                newest_version: None,
    ///            });
    /// # }
    /// ```
    ///
    /// Git repository:
    ///
    /// ```
    /// # use cargo_update::ops::MainRepoPackage;
    /// let package_s = "treesize 0.2.1 (git+https://github.com/melak47/treesize-rs#v0.2.1)";
    /// assert!(MainRepoPackage::parse(package_s).is_none());
    /// ```
    pub fn parse(what: &str) -> Option<MainRepoPackage> {
        PACKAGE_RGX.captures(what).and_then(|c| if c.get(3).unwrap().as_str() == "registry" {
            Some(MainRepoPackage {
                name: c.get(1).unwrap().as_str().to_string(),
                version: Some(Semver::parse(c.get(2).unwrap().as_str()).unwrap()),
                newest_version: None,
            })
        } else {
            None
        })
    }

    /// Download the version list for this crate off the main [`crates.io`](https://crates.io) registry.
    ///
    /// # Examples
    ///
    /// ```
    /// # use cargo_update::ops::{MainRepoPackage, get_index_path};
    /// # use std::fs::{self, File};
    /// # use std::io::Write;
    /// # use std::env;
    /// # let mut cargo_dir = env::temp_dir();
    /// # cargo_dir.push("cargo_update-doctest");
    /// # let _ = fs::create_dir(&cargo_dir);
    /// # cargo_dir.push("MainRepoPackage-pull_version-0");
    /// # let _ = fs::create_dir(&cargo_dir);
    /// # File::create(["registry", "index", "github.com-1ecc6299db9ec823", "ra", "ce"].into_iter()
    /// #     .fold(cargo_dir.clone(), |pb, chunk| {
    /// #         let _ = fs::create_dir(pb.join(chunk));
    /// #         pb.join(chunk)
    /// #     }).join("racer"))
    /// # .unwrap().write_all(br#"{"vers": "1.2.10", "yanked": false}"#).unwrap();
    /// let registry = get_index_path(&cargo_dir);
    /// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
    /// let mut package = MainRepoPackage::parse(package_s).unwrap();
    /// package.pull_version(&registry);
    /// assert!(package.newest_version.is_some());
    /// ```
    pub fn pull_version(&mut self, registry: &Path) {
        let vers = crate_versions(&find_package_data(&self.name, registry).unwrap());
        self.newest_version = vers.into_iter().max();
    }

    /// Check whether this package needs to be installed
    ///
    /// # Examples
    ///
    /// ```
    /// # extern crate cargo_update;
    /// # extern crate semver;
    /// # use cargo_update::ops::MainRepoPackage;
    /// # use semver::Version as Semver;
    /// # fn main() {
    /// assert!(MainRepoPackage {
    ///             name: "racer".to_string(),
    ///             version: Some(Semver::parse("1.7.2").unwrap()),
    ///             newest_version: Some(Semver::parse("2.0.6").unwrap()),
    ///         }.needs_update());
    /// assert!(MainRepoPackage {
    ///             name: "racer".to_string(),
    ///             version: None,
    ///             newest_version: Some(Semver::parse("2.0.6").unwrap()),
    ///         }.needs_update());
    /// assert!(!MainRepoPackage {
    ///             name: "racer".to_string(),
    ///             version: Some(Semver::parse("2.0.6").unwrap()),
    ///             newest_version: Some(Semver::parse("2.0.6").unwrap()),
    ///         }.needs_update());
    /// # }
    /// ```
    pub fn needs_update(&self) -> bool {
        self.version.is_none() || *self.version.as_ref().unwrap() < *self.newest_version.as_ref().unwrap()
    }
}


/// [Follow `install.root`](https://github.com/nabijaczleweli/cargo-update/issues/23) in the `config` file
/// parallel to the specified crates file up to the final one.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::resolve_crates_file;
/// # use std::env::temp_dir;
/// # let crates_file = temp_dir().join(".crates.toml");
/// let crates_file = resolve_crates_file(crates_file);
/// # let _ = crates_file;
/// ```
pub fn resolve_crates_file(crates_file: PathBuf) -> PathBuf {
    let config_file = crates_file.with_file_name("config");
    if config_file.exists() {
        let mut crates = String::new();
        File::open(&config_file).unwrap().read_to_string(&mut crates).unwrap();

        if let Some(idir) = toml::from_str::<toml::Value>(&crates)
            .unwrap()
            .get("install")
            .and_then(|t| t.as_table())
            .and_then(|t| t.get("root"))
            .and_then(|t| t.as_str()) {
            return resolve_crates_file(Path::new(idir).join(".crates.toml"));
        }
    }
    crates_file
}

/// List the installed packages at the specified location that originate
/// from the main [`crates.io`](https://crates.io) registry.
///
/// If the `.crates.toml` file doesn't exist an empty vector is returned.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::installed_main_repo_packages;
/// # use std::env::temp_dir;
/// # let cargo_dir = temp_dir().join(".crates.toml");
/// let packages = installed_main_repo_packages(&cargo_dir);
/// for package in &packages {
///     println!("{} v{}", package.name, package.version.as_ref().unwrap());
/// }
/// ```
pub fn installed_main_repo_packages(crates_file: &Path) -> Vec<MainRepoPackage> {
    if crates_file.exists() {
        let mut crates = String::new();
        File::open(crates_file).unwrap().read_to_string(&mut crates).unwrap();

        toml::from_str::<toml::Value>(&crates).unwrap()["v1"].as_table().unwrap().keys().flat_map(|s| MainRepoPackage::parse(s)).collect()
    } else {
        Vec::new()
    }
}

/// Filter out the installed packages not specified to be updated.
///
/// List installed packages with `installed_main_repo_packages()`.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::{MainRepoPackage, intersect_packages};
/// # fn installed_main_repo_packages(_: &()) {}
/// # let cargo_dir = ();
/// # let packages_to_update = ["racer".to_string(), "cargo-outdated".to_string()];
/// let mut installed_packages = installed_main_repo_packages(&cargo_dir);
/// # let mut installed_packages =
/// #     vec![MainRepoPackage::parse("cargo-outdated 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)").unwrap(),
/// #          MainRepoPackage::parse("racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)").unwrap(),
/// #          MainRepoPackage::parse("rustfmt 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)").unwrap()];
/// installed_packages = intersect_packages(installed_packages, &packages_to_update, false);
/// # assert_eq!(&installed_packages,
/// #   &[MainRepoPackage::parse("cargo-outdated 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)").unwrap(),
/// #     MainRepoPackage::parse("racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)").unwrap()]);
/// ```
pub fn intersect_packages(installed: Vec<MainRepoPackage>, to_update: &[String], allow_installs: bool) -> Vec<MainRepoPackage> {
    installed.iter()
        .filter(|p| to_update.contains(&p.name))
        .cloned()
        .chain(to_update.iter().filter(|p| allow_installs && installed.iter().find(|i| i.name == **p).is_none()).map(|p| {
            MainRepoPackage {
                name: p.clone(),
                version: None,
                newest_version: None,
            }
        }))
        .collect()
}

/// Parse the raw crate descriptor from the repository into a collection of `Semver`s.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::crate_versions;
/// # use std::path::PathBuf;
/// # let desc_path = PathBuf::from("test-data/checksums-versions.json");
/// let versions = crate_versions(&desc_path);
///
/// println!("Released versions of checksums:");
/// for ver in &versions {
///     println!("  {}", ver);
/// }
/// ```
pub fn crate_versions(package_desc: &Path) -> Vec<Semver> {
    let mut buf = String::new();
    File::open(package_desc).unwrap().read_to_string(&mut buf).unwrap();

    buf.lines()
        .map(|p| json::parse(p).unwrap())
        .filter(|j| !j["yanked"].as_bool().unwrap())
        .map(|j| Semver::parse(j["vers"].as_str().unwrap()).unwrap())
        .collect()
}

/// Get the location of the latest registry index in the specified cargo directory.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::get_index_path;
/// # use std::env::temp_dir;
/// # use std::fs;
/// # let mut cargo_dir = temp_dir();
/// # let _ = fs::create_dir(&cargo_dir);
/// # cargo_dir.push("cargo_update-doctest");
/// # let _ = fs::create_dir(&cargo_dir);
/// # cargo_dir.push("get_index_path-0");
/// # let _ = fs::create_dir(&cargo_dir);
/// # let idx_dir = cargo_dir.join("registry").join("index").join("github.com-1ecc6299db9ec823");
/// # let _ = fs::create_dir_all(&idx_dir);
/// let index = get_index_path(&cargo_dir);
/// // Use find_package_data() to look for packages
/// # assert_eq!(index, idx_dir);
/// ```
pub fn get_index_path(cargo_dir: &Path) -> PathBuf {
    fs::read_dir(cargo_dir.join("registry").join("index"))
        .unwrap()
        .map(Result::unwrap)
        .filter(|i| i.file_type().unwrap().is_dir())
        .max_by_key(|i| i.metadata().unwrap().modified().unwrap())
        .unwrap()
        .path()
}

/// Find a package in the cargo index.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::find_package_data;
/// # use std::fs::{self, File};
/// # use std::env::temp_dir;
/// # let mut index_dir = temp_dir();
/// # let _ = fs::create_dir(&index_dir);
/// # index_dir.push("cargo_update-doctest");
/// # let _ = fs::create_dir(&index_dir);
/// # index_dir.push("find_package_data-0");
/// # let _ = fs::create_dir(&index_dir);
/// # let _ = fs::create_dir_all(index_dir.join("ca").join("rg"));
/// # File::create(index_dir.join("ca").join("rg").join("cargo")).unwrap();
/// # let cargo =
/// find_package_data("cargo", &index_dir);
/// # assert_eq!(cargo, Some(index_dir.join("ca").join("rg").join("cargo")));
/// ```
pub fn find_package_data(cratename: &str, index_dir: &Path) -> Option<PathBuf> {
    let maybepath = |pb: PathBuf| if pb.exists() { Some(pb) } else { None };

    match cratename.len() {
        0 => panic!("0-length cratename"),
        1 | 2 => maybepath(index_dir.join(cratename.len().to_string())).and_then(|pb| maybepath(pb.join(cratename))),
        3 => {
            maybepath(index_dir.join("3"))
                .and_then(|pb| maybepath(pb.join(&cratename[0..1])))
                .and_then(|pb| maybepath(pb.join(cratename)))
        }
        _ => {
            maybepath(index_dir.join(&cratename[0..2]))
                .and_then(|pb| maybepath(pb.join(&cratename[2..4])))
                .and_then(|pb| maybepath(pb.join(cratename)))
        }
    }
}