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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::{env, str};

use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
use toml_edit;

use crate::dependency::Dependency;
use crate::errors::*;

use semver::{Version, VersionReq};

const MANIFEST_FILENAME: &str = "Cargo.toml";

/// A Cargo manifest
#[derive(Debug, Clone)]
pub struct Manifest {
    /// Manifest contents as TOML data
    pub data: toml_edit::Document,
}

/// If a manifest is specified, return that one, otherise perform a manifest search starting from
/// the current directory.
/// If a manifest is specified, return that one. If a path is specified, perform a manifest search
/// starting from there. If nothing is specified, start searching from the current directory
/// (`cwd`).
pub fn find(specified: &Option<PathBuf>) -> Result<PathBuf> {
    match *specified {
        Some(ref path)
            if fs::metadata(&path)
                .chain_err(|| "Failed to get cargo file metadata")?
                .is_file() =>
        {
            Ok(path.to_owned())
        }
        Some(ref path) => search(path),
        None => search(&env::current_dir().chain_err(|| "Failed to get current directory")?),
    }
}

/// Search for Cargo.toml in this directory and recursively up the tree until one is found.
fn search(dir: &Path) -> Result<PathBuf> {
    let manifest = dir.join(MANIFEST_FILENAME);

    if fs::metadata(&manifest).is_ok() {
        Ok(manifest)
    } else {
        dir.parent()
            .ok_or_else(|| ErrorKind::MissingManifest.into())
            .and_then(|dir| search(dir))
    }
}

fn merge_inline_table(old_dep: &mut toml_edit::Item, new: &toml_edit::Item) {
    for (k, v) in new
        .as_inline_table()
        .expect("expected an inline table")
        .iter()
    {
        old_dep[k] = toml_edit::value(v.clone());
    }
}

fn str_or_1_len_table(item: &toml_edit::Item) -> bool {
    item.is_str() || item.as_table_like().map(|t| t.len() == 1).unwrap_or(false)
}
/// Merge a new dependency into an old entry. See `Dependency::to_toml` for what the format of the
/// new dependency will be.
fn merge_dependencies(old_dep: &mut toml_edit::Item, new: &Dependency) {
    assert!(!old_dep.is_none());

    let new_toml = new.to_toml().1;

    if str_or_1_len_table(old_dep) {
        // The old dependency is just a version/git/path. We are safe to overwrite.
        *old_dep = new_toml;
    } else if old_dep.is_table_like() {
        for key in &["version", "path", "git"] {
            // remove this key/value pairs
            old_dep[key] = toml_edit::Item::None;
        }
        if let Some(name) = new_toml.as_str() {
            old_dep["version"] = toml_edit::value(name);
        } else {
            merge_inline_table(old_dep, &new_toml);
        }
    } else {
        unreachable!("Invalid old dependency type");
    }

    if let Some(t) = old_dep.as_inline_table_mut() {
        t.fmt()
    }
}

fn get_version(old_dep: &toml_edit::Item) -> Result<toml_edit::Item> {
    if str_or_1_len_table(old_dep) {
        Ok(old_dep.clone())
    } else if old_dep.is_table_like() {
        let version = old_dep["version"].clone();
        if version.is_none() {
            Err("Missing version field".into())
        } else {
            Ok(version)
        }
    } else {
        unreachable!("Invalid old dependency type")
    }
}

fn old_version_compatible(dependency: &Dependency, old_version: &str) -> Result<bool> {
    let old_version = VersionReq::parse(old_version).chain_err(|| {
        ErrorKind::ParseVersion(dependency.name.to_string(), old_version.to_string())
    })?;

    let current_version = match dependency.version() {
        Some(current_version) => current_version,
        None => return Ok(false),
    };

    let current_version = Version::parse(&current_version).chain_err(|| {
        ErrorKind::ParseVersion(dependency.name.to_string(), current_version.into())
    })?;

    Ok(old_version.matches(&current_version))
}

/// Print a message if the new dependency version is different from the old one.
fn print_upgrade_if_necessary(
    crate_name: &str,
    old_dep: &toml_edit::Item,
    new_version: &toml_edit::Item,
) -> Result<()> {
    let old_version = get_version(old_dep)?;

    if let (Some(old_version), Some(new_version)) = (old_version.as_str(), new_version.as_str()) {
        if old_version == new_version {
            return Ok(());
        }
        let bufwtr = BufferWriter::stdout(ColorChoice::Always);
        let mut buffer = bufwtr.buffer();
        buffer
            .set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true))
            .chain_err(|| "Failed to set output colour")?;
        write!(&mut buffer, "    Upgrading ").chain_err(|| "Failed to write upgrade message")?;
        buffer
            .set_color(&ColorSpec::new())
            .chain_err(|| "Failed to clear output colour")?;
        writeln!(
            &mut buffer,
            "{} v{} -> v{}",
            crate_name, old_version, new_version,
        )
        .chain_err(|| "Failed to write upgrade versions")?;
        bufwtr
            .print(&buffer)
            .chain_err(|| "Failed to print upgrade message")?;
    }
    Ok(())
}

impl Manifest {
    /// Look for a `Cargo.toml` file
    ///
    /// Starts at the given path an goes into its parent directories until the manifest file is
    /// found. If no path is given, the process's working directory is used as a starting point.
    pub fn find_file(path: &Option<PathBuf>) -> Result<File> {
        find(path).and_then(|path| {
            OpenOptions::new()
                .read(true)
                .write(true)
                .open(path)
                .chain_err(|| "Failed to find Cargo.toml")
        })
    }

    /// Open the `Cargo.toml` for a path (or the process' `cwd`)
    pub fn open(path: &Option<PathBuf>) -> Result<Manifest> {
        let mut file = Manifest::find_file(path)?;
        let mut data = String::new();
        file.read_to_string(&mut data)
            .chain_err(|| "Failed to read manifest contents")?;

        data.parse().chain_err(|| "Unable to parse Cargo.toml")
    }

    /// Get the specified table from the manifest.
    pub fn get_table<'a>(&'a mut self, table_path: &[String]) -> Result<&'a mut toml_edit::Item> {
        /// Descend into a manifest until the required table is found.
        fn descend<'a>(
            input: &'a mut toml_edit::Item,
            path: &[String],
        ) -> Result<&'a mut toml_edit::Item> {
            if let Some(segment) = path.get(0) {
                let value = input[&segment].or_insert(toml_edit::table());

                if value.is_table_like() {
                    descend(value, &path[1..])
                } else {
                    Err(ErrorKind::NonExistentTable(segment.clone()).into())
                }
            } else {
                Ok(input)
            }
        }

        descend(&mut self.data.root, table_path)
    }

    /// Get all sections in the manifest that exist and might contain dependencies.
    /// The returned items are always `Table` or `InlineTable`.
    pub fn get_sections(&self) -> Vec<(Vec<String>, toml_edit::Item)> {
        let mut sections = Vec::new();

        for dependency_type in &["dev-dependencies", "build-dependencies", "dependencies"] {
            // Dependencies can be in the three standard sections...
            if self.data[dependency_type].is_table_like() {
                sections.push((
                    vec![String::from(*dependency_type)],
                    self.data[dependency_type].clone(),
                ))
            }

            // ... and in `target.<target>.(build-/dev-)dependencies`.
            let target_sections = self
                .data
                .as_table()
                .get("target")
                .and_then(toml_edit::Item::as_table_like)
                .into_iter()
                .flat_map(toml_edit::TableLike::iter)
                .filter_map(|(target_name, target_table)| {
                    let dependency_table = &target_table[dependency_type];
                    dependency_table.as_table_like().map(|_| {
                        (
                            vec![
                                "target".to_string(),
                                target_name.to_string(),
                                String::from(*dependency_type),
                            ],
                            dependency_table.clone(),
                        )
                    })
                });

            sections.extend(target_sections);
        }

        sections
    }

    /// Overwrite a file with TOML data.
    pub fn write_to_file(&self, file: &mut File) -> Result<()> {
        if self.data["package"].is_none() && self.data["project"].is_none() {
            if !self.data["workspace"].is_none() {
                return Err(ErrorKind::UnexpectedRootManifest.into());
            } else {
                return Err(ErrorKind::InvalidManifest.into());
            }
        }

        let s = self.data.to_string_in_original_order();
        let new_contents_bytes = s.as_bytes();

        // We need to truncate the file, otherwise the new contents
        // will be mixed up with the old ones.
        file.set_len(new_contents_bytes.len() as u64)
            .chain_err(|| "Failed to truncate Cargo.toml")?;
        file.write_all(new_contents_bytes)
            .chain_err(|| "Failed to write updated Cargo.toml")
    }

    /// Add entry to a Cargo.toml.
    pub fn insert_into_table(&mut self, table_path: &[String], dep: &Dependency) -> Result<()> {
        let table = self.get_table(table_path)?;

        let existing_dep = Self::find_dep(table, &dep.name);
        if let Some((mut dep_name, dep_item)) = existing_dep {
            // update an existing entry

            // if the `dep` is renamed in the `add` command,
            // but was present before, then we need to remove
            // the old entry and insert a new one
            // as the key has changed, e.g. from
            // a = "0.1"
            // to
            // alias = { version = "0.2", package = "a" }
            if let Some(renamed) = dep.rename() {
                table[renamed] = dep_item.clone();
                table[&dep_name] = toml_edit::Item::None;
                dep_name = renamed.to_owned();
            } else if dep.name != dep_name {
                // if `dep` had been renamed in the manifest,
                // and is not rename in the `add` command,
                // we need to remove the old entry and insert a new one
                // e.g. from
                // alias = { version = "0.1", package = "a" }
                // to
                // a = "0.2"
                table[&dep_name] = toml_edit::Item::None;
                let (ref name, ref mut new_dependency) = dep.to_toml();
                table[name] = new_dependency.clone();
                dep_name = dep.name.to_owned();
            }
            merge_dependencies(&mut table[dep_name], dep);
            if let Some(t) = table.as_inline_table_mut() {
                t.fmt()
            }
        } else {
            // insert a new entry
            let (ref name, ref mut new_dependency) = dep.to_toml();
            table[name] = new_dependency.clone();
        }
        Ok(())
    }

    /// Update an entry in Cargo.toml.
    pub fn update_table_entry(
        &mut self,
        table_path: &[String],
        dep: &Dependency,
        dry_run: bool,
    ) -> Result<()> {
        self.update_table_named_entry(table_path, dep.name_in_manifest(), dep, dry_run)
    }

    /// Update an entry with a specified name in Cargo.toml.
    pub fn update_table_named_entry(
        &mut self,
        table_path: &[String],
        item_name: &str,
        dep: &Dependency,
        dry_run: bool,
    ) -> Result<()> {
        let table = self.get_table(table_path)?;
        let new_dep = dep.to_toml().1;

        // If (and only if) there is an old entry, merge the new one in.
        if !table[item_name].is_none() {
            if let Err(e) = print_upgrade_if_necessary(&dep.name, &table[item_name], &new_dep) {
                eprintln!("Error while displaying upgrade message, {}", e);
            }
            if !dry_run {
                merge_dependencies(&mut table[item_name], dep);
                if let Some(t) = table.as_inline_table_mut() {
                    t.fmt()
                }
            }
        }

        Ok(())
    }

    /// Remove entry from a Cargo.toml.
    ///
    /// # Examples
    ///
    /// ```
    ///   use cargo_edit::{Dependency, Manifest};
    ///   use toml_edit;
    ///
    ///   let mut manifest = Manifest { data: toml_edit::Document::new() };
    ///   let dep = Dependency::new("cargo-edit").set_version("0.1.0");
    ///   let _ = manifest.insert_into_table(&vec!["dependencies".to_owned()], &dep);
    ///   assert!(manifest.remove_from_table("dependencies", &dep.name).is_ok());
    ///   assert!(manifest.remove_from_table("dependencies", &dep.name).is_err());
    ///   assert!(manifest.data["dependencies"].is_none());
    /// ```
    pub fn remove_from_table(&mut self, table: &str, name: &str) -> Result<()> {
        if !self.data[table].is_table_like() {
            return Err(ErrorKind::NonExistentTable(table.into()).into());
        } else {
            {
                let dep = &mut self.data[table][name];
                if dep.is_none() {
                    return Err(ErrorKind::NonExistentDependency(name.into(), table.into()).into());
                }
                // remove the dependency
                *dep = toml_edit::Item::None;
            }

            // remove table if empty
            if self.data[table].as_table_like().unwrap().is_empty() {
                self.data[table] = toml_edit::Item::None;
            }
        }
        Ok(())
    }

    /// Add multiple dependencies to manifest
    pub fn add_deps(&mut self, table: &[String], deps: &[Dependency]) -> Result<()> {
        deps.iter()
            .map(|dep| self.insert_into_table(table, dep))
            .collect::<Result<Vec<_>>>()?;

        Ok(())
    }

    /// Find a dependency by name (matching on package name for renamed deps)
    pub fn find_dep<'a>(
        table: &'a mut toml_edit::Item,
        dep_name: &'a str,
    ) -> Option<(String, &'a toml_edit::Item)> {
        table
            .as_table_like()
            .unwrap()
            .iter()
            .find(|&item| match item {
                (name, _) if name == dep_name => true,
                (_alias, toml_edit::Item::Table(table_dep))
                    if table_dep.contains_key("package") =>
                {
                    table_dep.get("package").unwrap().as_str() == Some(dep_name)
                }
                (_alias, toml_edit::Item::Value(toml_edit::Value::InlineTable(inline_dep)))
                    if inline_dep.contains_key("package") =>
                {
                    inline_dep.get("package").unwrap().as_str() == Some(dep_name)
                }
                _ => false,
            })
            .map(|dep| (dep.0.into(), dep.1))
    }
}

impl str::FromStr for Manifest {
    type Err = Error;

    /// Read manifest data from string
    fn from_str(input: &str) -> ::std::result::Result<Self, Self::Err> {
        let d: toml_edit::Document = input.parse().chain_err(|| "Manifest not valid TOML")?;

        Ok(Manifest { data: d })
    }
}

/// A Cargo manifest that is available locally.
#[derive(Debug)]
pub struct LocalManifest {
    /// Path to the manifest
    pub path: PathBuf,
    /// Manifest contents
    manifest: Manifest,
}

impl Deref for LocalManifest {
    type Target = Manifest;

    fn deref(&self) -> &Manifest {
        &self.manifest
    }
}

impl LocalManifest {
    /// Construct a `LocalManifest`. If no path is provided, make an educated guess as to which one
    /// the user means.
    pub fn find(path: &Option<PathBuf>) -> Result<Self> {
        let path = find(path)?;
        Self::try_new(&path)
    }

    /// Construct the `LocalManifest` corresponding to the `Path` provided.
    pub fn try_new(path: &Path) -> Result<Self> {
        let path = path.to_path_buf();
        Ok(LocalManifest {
            manifest: Manifest::open(&Some(path.clone()))?,
            path,
        })
    }

    /// Get the `File` corresponding to this manifest.
    fn get_file(&self) -> Result<File> {
        Manifest::find_file(&Some(self.path.clone()))
    }

    /// Instruct this manifest to upgrade a single dependency. If this manifest does not have that
    /// dependency, it does nothing.
    pub fn upgrade(
        &mut self,
        dependency: &Dependency,
        dry_run: bool,
        skip_compatible: bool,
    ) -> Result<()> {
        for (table_path, table) in self.get_sections() {
            let table_like = table.as_table_like().expect("Unexpected non-table");
            for (name, toml_item) in table_like.iter() {
                let dep_name = toml_item
                    .as_table_like()
                    .and_then(|t| t.get("package").and_then(|p| p.as_str()))
                    .unwrap_or(name);
                if dep_name == dependency.name {
                    if skip_compatible {
                        if let Some(old_version) = get_version(toml_item)?.as_str() {
                            if old_version_compatible(dependency, old_version)? {
                                continue;
                            }
                        }
                    }
                    self.manifest.update_table_named_entry(
                        &table_path,
                        &name,
                        dependency,
                        dry_run,
                    )?;
                }
            }
        }

        let mut file = self.get_file()?;
        self.write_to_file(&mut file)
            .chain_err(|| "Failed to write new manifest contents")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dependency::Dependency;
    use toml_edit;

    #[test]
    fn add_remove_dependency() {
        let mut manifest = Manifest {
            data: toml_edit::Document::new(),
        };
        let clone = manifest.clone();
        let dep = Dependency::new("cargo-edit").set_version("0.1.0");
        let _ = manifest.insert_into_table(&["dependencies".to_owned()], &dep);
        assert!(manifest
            .remove_from_table("dependencies", &dep.name)
            .is_ok());
        assert_eq!(manifest.data.to_string(), clone.data.to_string());
    }

    #[test]
    fn update_dependency() {
        let mut manifest = Manifest {
            data: toml_edit::Document::new(),
        };
        let dep = Dependency::new("cargo-edit").set_version("0.1.0");
        manifest
            .insert_into_table(&["dependencies".to_owned()], &dep)
            .unwrap();

        let new_dep = Dependency::new("cargo-edit").set_version("0.2.0");
        manifest
            .update_table_entry(&["dependencies".to_owned()], &new_dep, false)
            .unwrap();
    }

    #[test]
    fn update_wrong_dependency() {
        let mut manifest = Manifest {
            data: toml_edit::Document::new(),
        };
        let dep = Dependency::new("cargo-edit").set_version("0.1.0");
        manifest
            .insert_into_table(&["dependencies".to_owned()], &dep)
            .unwrap();
        let original = manifest.clone();

        let new_dep = Dependency::new("wrong-dep").set_version("0.2.0");
        manifest
            .update_table_entry(&["dependencies".to_owned()], &new_dep, false)
            .unwrap();

        assert_eq!(manifest.data.to_string(), original.data.to_string());
    }

    #[test]
    fn remove_dependency_no_section() {
        let mut manifest = Manifest {
            data: toml_edit::Document::new(),
        };
        let dep = Dependency::new("cargo-edit").set_version("0.1.0");
        assert!(manifest
            .remove_from_table("dependencies", &dep.name)
            .is_err());
    }

    #[test]
    fn remove_dependency_non_existent() {
        let mut manifest = Manifest {
            data: toml_edit::Document::new(),
        };
        let dep = Dependency::new("cargo-edit").set_version("0.1.0");
        let other_dep = Dependency::new("other-dep").set_version("0.1.0");
        let _ = manifest.insert_into_table(&["dependencies".to_owned()], &other_dep);
        assert!(manifest
            .remove_from_table("dependencies", &dep.name)
            .is_err());
    }

    #[test]
    fn old_version_is_compatible() -> Result<()> {
        let with_version = Dependency::new("foo").set_version("2.3.4");
        assert!(!old_version_compatible(&with_version, "1")?);
        assert!(old_version_compatible(&with_version, "2")?);
        assert!(!old_version_compatible(&with_version, "3")?);
        Ok(())
    }

    #[test]
    fn old_incompatible_with_missing_new_version() -> Result<()> {
        let no_version = Dependency::new("foo");
        assert!(!old_version_compatible(&no_version, "1")?);
        assert!(!old_version_compatible(&no_version, "2")?);
        Ok(())
    }

    #[test]
    fn old_incompatible_with_invalid() {
        let bad_version = Dependency::new("foo").set_version("CAKE CAKE");
        let good_version = Dependency::new("foo").set_version("1.2.3");
        assert!(old_version_compatible(&bad_version, "1").is_err());
        assert!(old_version_compatible(&good_version, "CAKE CAKE").is_err());
    }
}