-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.rs
51 lines (42 loc) · 1.31 KB
/
build.rs
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
use std::env;
use std::fs;
use std::path::Path;
use lazy_static::lazy_static;
static CONFIG_FILENAME: &str = "config.toml";
lazy_static! {
static ref PARENT_PATH: String = {
if cfg!(windows) {
format!("{}/sisterm/", env::var("LOCALAPPDATA").unwrap())
} else {
format!("{}/.config/sisterm/", env::var("HOME").unwrap())
}
};
}
fn main() {
// Generate configuration file
match generate() {
Ok(None) => (), // Already exists configuration file
Ok(Some(path)) => println!("Generated config file --> {}", path),
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
},
}
}
// Generate configration file
// If windows => %USERPROFILE%/AppData/Local/sisterm/config.toml
// other => $HOME/.config/sisterm/config.toml
// Returns the path to that configuration file
fn generate() -> Result<Option<String>, std::io::Error> {
// Path to the config.toml
let config_file_path = format!("{}{}", *PARENT_PATH, CONFIG_FILENAME);
// Check for file existence
if Path::new(&config_file_path).exists() {
return Ok(None);
}
// Create directory
fs::create_dir_all(&*PARENT_PATH)?;
// Copies the contents
fs::copy(CONFIG_FILENAME, &config_file_path)?;
Ok(Some(config_file_path))
}