ecode_core/configs/
dirs_and_files.rs1use std::{
2 env, fs,
3 path::{Path, PathBuf},
4};
5
6use fs_extra::dir::{copy, CopyOptions};
7
8use super::Config;
9
10pub fn path_to_string(path: PathBuf) -> String {
11 path.to_str().unwrap_or("").to_string()
12}
13
14pub fn find_it<P>(exe_name: P) -> Option<PathBuf>
15where
16 P: AsRef<Path>,
17{
18 env::var_os("PATH").and_then(|paths| {
19 env::split_paths(&paths)
20 .filter_map(|dir| {
21 let full_path = dir.join(&exe_name);
22 if full_path.is_file() {
23 Some(full_path)
24 } else {
25 None
26 }
27 })
28 .next()
29 })
30}
31
32pub fn get_bin_or_cmd_name<'a>() -> &'a str {
33 if cfg!(target_os = "windows") {
34 "code.cmd"
35 } else {
36 "code"
37 }
38}
39
40pub fn get_home_dir() -> Result<PathBuf, String> {
41 let home_dir = dirs::home_dir();
42 match home_dir {
43 None => Err("Couldn't find user's home folder.".into()),
44 Some(data) => Ok(data),
45 }
46}
47
48pub fn create_or_get_ena_home_folder() -> Result<PathBuf, Box<dyn std::error::Error>> {
49 let mut home_folder = get_home_dir()?;
50
51 home_folder = home_folder.join(".ena-code");
52 let path_ena_code_folder = Path::new(&home_folder);
53 if !path_ena_code_folder.is_dir() {
54 fs::create_dir(&path_ena_code_folder)?;
55 }
56 Ok(home_folder)
57}
58
59pub fn get_profiles_folder_path() -> PathBuf {
60 let config = Config::get_config(false);
61 Path::new(&config.profiles_folder).to_path_buf()
62}
63
64pub fn get_profile_path(profile_name: &str) -> PathBuf {
65 get_profiles_folder_path().join(profile_name)
66}
67
68pub fn check_profile_exists(profile_name: &str) -> bool {
69 let ena_folder = get_profile_path(profile_name);
70
71 let path = Path::new(&ena_folder);
72
73 path.is_dir()
74}
75
76pub fn create_profile(profile_name: &str, profile_fonte: &str) {
77 let dir_destino = get_profile_path(profile_name);
78 let dir_origin = get_profile_path(profile_fonte);
79 let mut options = CopyOptions::new();
80 options.skip_exist = true;
81 options.overwrite = false;
82 options.copy_inside = true;
83
84 if let Err(why) = copy(&dir_origin, &dir_destino, &options) {
85 println!("Couldn't derive from the profile {}, initializating from a new.\n{{Origin: {:?}, Target: {:?}}}\n\nReason: {}", profile_fonte, dir_origin, dir_destino, why);
86 }
87}
88
89pub fn copy_profile(profile: &str, profile_origin: &str) {
90 if profile != profile_origin
91 && !check_profile_exists(profile)
92 && check_profile_exists(profile_origin)
93 {
94 create_profile(profile, profile_origin)
95 }
96}
97
98pub fn remove_caracteres(path: &str, config: &Config) -> String {
99 let mut string_path = path.to_string();
100 string_path.retain(|c| !r#"(),".;:'<>/\|?*"#.contains(c));
101
102 if string_path.is_empty() {
103 string_path = config.create_new_profile_from.clone();
104 }
105 string_path
106}
107
108pub fn config_folder(config: &Config, profile_path: &Path, profiles_base_folder: &Path) -> PathBuf {
109 if config.shared_profiles_configs {
110 let default_profile_folder = profiles_base_folder.join(&config.create_new_profile_from);
111 default_profile_folder.join("configs")
112 } else {
113 profile_path.join("configs")
114 }
115}