-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.rs
More file actions
144 lines (120 loc) · 4.2 KB
/
config.rs
File metadata and controls
144 lines (120 loc) · 4.2 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::say;
use eyre::Result;
use fs_err as fs;
use std::path::{Path, PathBuf};
pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION");
pub(crate) const LONG_VERSION: &str = concat!(
env!("CARGO_PKG_VERSION"),
" (",
env!("VERGEN_GIT_SHA"),
" ",
env!("VERGEN_BUILD_TIMESTAMP"),
")"
);
pub(crate) const FOUNDRYUP_REPO: &str = "foundry-rs/foundryup";
#[derive(Debug)]
pub(crate) struct Config {
pub foundry_dir: PathBuf,
pub versions_dir: PathBuf,
pub bin_dir: PathBuf,
pub man_dir: PathBuf,
pub network: NetworkConfig,
}
impl Config {
pub(crate) fn new() -> Result<Self> {
let base_dir =
std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from).or_else(home::home_dir);
let base_dir = base_dir.ok_or_else(|| eyre::eyre!("could not determine home directory"))?;
let foundry_dir = std::env::var_os("FOUNDRY_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| base_dir.join(".foundry"));
let versions_dir = foundry_dir.join("versions");
let bin_dir = foundry_dir.join("bin");
let man_dir = foundry_dir.join("share/man/man1");
Ok(Self { foundry_dir, versions_dir, bin_dir, man_dir, network: NetworkConfig::FOUNDRY })
}
pub(crate) fn ensure_dirs(&self) -> Result<()> {
fs::create_dir_all(&self.versions_dir)?;
fs::create_dir_all(&self.bin_dir)?;
fs::create_dir_all(&self.man_dir)?;
Ok(())
}
pub(crate) fn migrate_legacy_versions(&self) -> Result<()> {
if !self.versions_dir.exists() {
return Ok(());
}
let default_repo = NetworkConfig::FOUNDRY.repo;
for entry in fs::read_dir(&self.versions_dir)? {
let entry = entry?;
let path = entry.path();
if !path.is_dir() {
continue;
}
let name = entry.file_name();
let name = name.to_string_lossy();
if name.contains('/') || self.is_owner_dir(&path) {
continue;
}
if self.is_legacy_version_dir(&path) {
let new_path = self.version_dir(default_repo, &name);
fs::create_dir_all(new_path.parent().unwrap())?;
say!("migrating legacy version '{name}' to {default_repo}/{name}");
fs::rename(&path, &new_path)?;
}
}
Ok(())
}
fn is_legacy_version_dir(&self, path: &Path) -> bool {
for bin in NetworkConfig::FOUNDRY.bins {
let bin_name = if cfg!(windows) { format!("{bin}.exe") } else { bin.to_string() };
if path.join(&bin_name).exists() {
return true;
}
}
false
}
fn is_owner_dir(&self, path: &Path) -> bool {
// Owner dirs have repo subdirs, which have version subdirs.
fn has_dir(path: &Path, mut f: impl FnMut(&Path) -> bool) -> bool {
fs::read_dir(path)
.into_iter()
.flatten()
.flatten()
.any(|entry| f(&entry.path()) && entry.metadata().is_ok_and(|m| m.is_dir()))
}
has_dir(path, |p| has_dir(p, |_| true))
}
pub(crate) fn version_dir(&self, repo: &str, version: &str) -> PathBuf {
self.versions_dir.join(repo).join(version)
}
pub(crate) fn bin_path(&self, name: &str) -> PathBuf {
let name = if cfg!(windows) && !name.ends_with(".exe") {
format!("{name}.exe")
} else {
name.to_string()
};
self.bin_dir.join(name)
}
pub(crate) fn repo_dir(&self, repo: &str) -> PathBuf {
self.foundry_dir.join(repo)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct NetworkConfig {
pub repo: &'static str,
pub bins: &'static [&'static str],
pub archive_prefix: &'static str,
pub default_version: &'static str,
pub display_name: &'static str,
pub has_attestation: bool,
}
impl NetworkConfig {
pub(crate) const FOUNDRY: Self = Self {
repo: "foundry-rs/foundry",
bins: &["forge", "cast", "anvil", "chisel"],
archive_prefix: "foundry",
default_version: "latest",
display_name: "foundry",
has_attestation: true,
};
}