sagiegurari / cargo-make
1
//! # legacy
2
//!
3
//! Support legacy features.
4
//!
5

6
#[cfg(test)]
7
#[path = "./legacy_test.rs"]
8
mod legacy_test;
9

10
use dirs;
11
use fsio;
12
use std::env;
13
use std::fs::copy;
14
use std::path::PathBuf;
15

16 8
fn get_legacy_cargo_make_home() -> Option<PathBuf> {
17 8
    match dirs::home_dir() {
18 8
        Some(directory) => Some(directory.join(".cargo-make")),
19 0
        None => None,
20
    }
21 6
}
22

23 2
pub(crate) fn get_cargo_make_home() -> Option<PathBuf> {
24 2
    match env::var("CARGO_MAKE_HOME") {
25 2
        Ok(directory) => Some(PathBuf::from(directory)),
26 2
        _ => get_legacy_cargo_make_home(),
27
    }
28 2
}
29

30 8
fn migrate_from_directory(
31
    target_directory: PathBuf,
32
    file: &str,
33
    legacy_directory: &PathBuf,
34
) -> bool {
35 8
    let legacy_file = legacy_directory.join(file);
36

37 8
    if legacy_file.exists() {
38 2
        let exists = if target_directory.exists() {
39 0
            true
40
        } else {
41 2
            match fsio::directory::create(&target_directory) {
42 2
                Ok(_) => true,
43 0
                _ => false,
44
            }
45 2
        };
46

47 2
        if exists {
48 2
            let target_file = target_directory.join(file);
49 2
            info!("Legacy cargo-make file: {:#?} exists, target directory: {:#?} exists, copy to: {:#?}", &legacy_file, &target_directory, &target_file);
50

51 2
            match copy(&legacy_file, &target_file) {
52 2
                Ok(_) => {
53 2
                    info!("Delete legacy cargo-make file: {:#?}", &legacy_file);
54 2
                    fsio::file::delete(&legacy_file).unwrap_or(());
55

56
                    // delete old directory (will only work if empty)
57 2
                    fsio::directory::delete(legacy_directory).unwrap_or(());
58

59 2
                    true
60
                }
61 0
                Err(error) => {
62 0
                    info!(
63 0
                        "Error while copying legacy file: {:#?} to: {:#?}, error: {:#?}",
64 0
                        &legacy_file, &target_file, &error
65
                    );
66 0
                    false
67 0
                }
68 0
            }
69 2
        } else {
70 0
            false
71
        }
72
    } else {
73 8
        true
74
    }
75 8
}
76

77 8
pub(crate) fn migrate(target_directory: PathBuf, file: &str) -> bool {
78 8
    debug!(
79 0
        "Legacy cargo-make target_directory: {:#?} file: {:#?} ",
80 0
        &target_directory, &file
81
    );
82

83 8
    match get_legacy_cargo_make_home() {
84 8
        Some(directory) => migrate_from_directory(target_directory, &file, &directory),
85 0
        None => true,
86
    }
87 8
}
88

89 8
pub(crate) fn show_deprecated_attriute_warning(old_attribute: &str, new_attribute: &str) {
90 8
    warn!(
91 0
        "[DEPRECATED] The attribute '{}' has been replaced with '{}'. Please update your makefile.",
92
        old_attribute, new_attribute
93
    );
94 8
}

Read our documentation on viewing source code .

Loading