sagiegurari / cargo-make
1
//! # storage
2
//!
3
//! Provides storage related utilities such as config/cache directory locations.
4
//!
5

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

10
use crate::legacy;
11
use std::env;
12
use std::path::{Path, PathBuf};
13

14 2
fn get_legacy_directory() -> Option<PathBuf> {
15 2
    legacy::get_cargo_make_home()
16 2
}
17

18 8
pub(crate) fn get_storage_directory(
19
    os_directory: Option<PathBuf>,
20
    file_name: &str,
21
    migrate: bool,
22
) -> Option<PathBuf> {
23 8
    match env::var("CARGO_MAKE_HOME") {
24
        // if env is defined, it is taken as highest priority
25 8
        Ok(directory) => Some(PathBuf::from(directory)),
26
        _ => {
27 8
            match os_directory {
28 8
                Some(directory) => {
29 8
                    let home_directory = directory.join("cargo-make");
30

31 8
                    let file_path = Path::new(&directory).join(file_name);
32

33
                    // migrate old data to new directory
34 8
                    if !file_path.exists() && migrate {
35 8
                        legacy::migrate(home_directory.clone(), file_name);
36
                    }
37

38 8
                    Some(home_directory)
39 8
                }
40 4
                None => get_legacy_directory(), // in case no dir is defined for system, default to old approach
41
            }
42
        }
43
    }
44 8
}

Read our documentation on viewing source code .

Loading