-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathmigrate.rs
90 lines (80 loc) · 2.87 KB
/
migrate.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
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
use std::fs;
use std::path::Path;
use crate::dirs::*;
use crate::file;
use eyre::Result;
use rayon::Scope;
pub fn run() {
rayon::scope(|s| {
task(s, || rename_plugin("nodejs", "node"));
task(s, || rename_plugin("golang", "go"));
task(s, migrate_trusted_configs);
task(s, migrate_tracked_configs);
task(s, || remove_deprecated_plugin("node", "rtx-nodejs"));
task(s, || remove_deprecated_plugin("go", "rtx-golang"));
task(s, || remove_deprecated_plugin("java", "rtx-java"));
task(s, || remove_deprecated_plugin("python", "rtx-python"));
task(s, || remove_deprecated_plugin("ruby", "rtx-ruby"));
});
}
fn task(s: &Scope, job: impl FnOnce() -> Result<()> + Send + 'static) {
s.spawn(|_| {
if let Err(err) = job() {
eprintln!("[WARN] migrate: {}", err);
}
});
}
fn move_subdirs(from: &Path, to: &Path) -> Result<()> {
if from.exists() {
eprintln!("migrating {} to {}", from.display(), to.display());
file::create_dir_all(to)?;
for f in from.read_dir()? {
let f = f?.file_name();
let from_file = from.join(&f);
let to_file = to.join(&f);
if !to_file.exists() {
eprintln!("moving {} to {}", from_file.display(), to_file.display());
file::rename(from_file, to_file)?;
}
}
file::remove_all(from)?;
}
Ok(())
}
fn rename_plugin(from: &str, to: &str) -> Result<()> {
move_subdirs(&INSTALLS.join(from), &INSTALLS.join(to))?;
move_subdirs(&PLUGINS.join(from), &PLUGINS.join(to))?;
Ok(())
}
fn migrate_tracked_configs() -> Result<()> {
move_dirs(&DATA.join("tracked_config_files"), &TRACKED_CONFIGS)?;
move_dirs(&DATA.join("tracked-config-files"), &TRACKED_CONFIGS)?;
Ok(())
}
fn migrate_trusted_configs() -> Result<()> {
move_dirs(&CACHE.join("trusted-configs"), &TRUSTED_CONFIGS)?;
move_dirs(&CONFIG.join("trusted-configs"), &TRUSTED_CONFIGS)?;
move_dirs(&DATA.join("trusted-configs"), &TRUSTED_CONFIGS)?;
Ok(())
}
fn move_dirs(from: &Path, to: &Path) -> Result<bool> {
if from.exists() && !to.exists() {
eprintln!("migrating {} to {}", from.display(), to.display());
file::create_dir_all(to.parent().unwrap())?;
file::rename(from, to)?;
Ok(true)
} else {
Ok(false)
}
}
fn remove_deprecated_plugin(name: &str, plugin_name: &str) -> Result<()> {
let plugin_root = PLUGINS.join(name);
let gitconfig = plugin_root.join(".git").join("config");
let gitconfig_body = fs::read_to_string(gitconfig).unwrap_or_default();
if !gitconfig_body.contains(&format!("github.com/mise-plugins/{plugin_name}")) {
return Ok(());
}
eprintln!("removing deprecated plugin {plugin_name}, will use core {name} plugin from now on");
file::remove_all(plugin_root)?;
Ok(())
}