Skip to content

Commit 27c8f98

Browse files
committed
Merge branch 'main' of https://github.com/geode-sdk/cli into main
2 parents 1c69f38 + 0d7d5b5 commit 27c8f98

17 files changed

+1029
-992
lines changed

src/file.rs

+31-34
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
2-
use std::fs;
3-
use std::io;
4-
use std::path::PathBuf;
5-
6-
/// Return all files in directory and subdirectories
7-
pub fn read_dir_recursive(src: &PathBuf) -> Result<Vec<PathBuf>, io::Error> {
8-
let mut res = Vec::new();
9-
for item in fs::read_dir(src)? {
10-
let path = item?.path();
11-
if path.is_dir() {
12-
res.extend(read_dir_recursive(src)?);
13-
}
14-
else {
15-
res.push(path);
16-
}
17-
}
18-
Ok(res)
19-
}
20-
21-
pub fn copy_dir_recursive(src: &PathBuf, dest: &PathBuf) -> Result<(), io::Error> {
22-
fs::create_dir_all(dest)?;
23-
for item in fs::read_dir(src)? {
24-
let item_path = item?.path();
25-
let dest_path = dest.join(item_path.file_name().unwrap());
26-
if item_path.is_dir() {
27-
copy_dir_recursive(&item_path, &dest_path)?;
28-
}
29-
else {
30-
fs::copy(&item_path, &dest_path)?;
31-
}
32-
}
33-
Ok(())
34-
}
1+
use std::fs;
2+
use std::io;
3+
use std::path::PathBuf;
4+
5+
/// Return all files in directory and subdirectories
6+
pub fn read_dir_recursive(src: &PathBuf) -> Result<Vec<PathBuf>, io::Error> {
7+
let mut res = Vec::new();
8+
for item in fs::read_dir(src)? {
9+
let path = item?.path();
10+
if path.is_dir() {
11+
res.extend(read_dir_recursive(src)?);
12+
} else {
13+
res.push(path);
14+
}
15+
}
16+
Ok(res)
17+
}
18+
19+
pub fn copy_dir_recursive(src: &PathBuf, dest: &PathBuf) -> Result<(), io::Error> {
20+
fs::create_dir_all(dest)?;
21+
for item in fs::read_dir(src)? {
22+
let item_path = item?.path();
23+
let dest_path = dest.join(item_path.file_name().unwrap());
24+
if item_path.is_dir() {
25+
copy_dir_recursive(&item_path, &dest_path)?;
26+
} else {
27+
fs::copy(&item_path, &dest_path)?;
28+
}
29+
}
30+
Ok(())
31+
}

src/index.rs

+90-42
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
use clap::Subcommand;
2-
use semver::VersionReq;
3-
use zip::ZipArchive;
41
use crate::config::Config;
52
use crate::file::copy_dir_recursive;
63
use crate::util::logging::ask_value;
74
use crate::util::mod_file::{parse_mod_info, try_parse_mod_info};
8-
use crate::{done, info, warn, fatal, NiceUnwrap};
9-
use sha3::{Digest, Sha3_256};
10-
use serde::{Serialize, Deserialize};
5+
use crate::{done, fatal, info, warn, NiceUnwrap};
6+
use clap::Subcommand;
7+
use colored::Colorize;
8+
use reqwest::header::{AUTHORIZATION, USER_AGENT};
9+
use semver::VersionReq;
10+
use serde::{Deserialize, Serialize};
1111
use serde_json::json;
12-
use reqwest::header::{USER_AGENT, AUTHORIZATION};
12+
use sha3::{Digest, Sha3_256};
1313
use std::collections::HashSet;
1414
use std::fs;
15-
use std::path::{Path, PathBuf};
1615
use std::io;
17-
use colored::Colorize;
16+
use std::path::{Path, PathBuf};
17+
use zip::ZipArchive;
1818

1919
#[derive(Subcommand, Debug)]
2020
#[clap(rename_all = "kebab-case")]
2121
pub enum Index {
2222
/// Create a new entry to be used in the index
2323
New {
2424
/// Output folder of entry
25-
output: PathBuf
25+
output: PathBuf,
2626
},
2727

2828
/// Install a mod from the index to the current profile
@@ -58,38 +58,50 @@ pub struct Entry {
5858

5959
pub fn update_index(config: &Config) {
6060
let index_dir = config.get_current_profile().index_dir();
61-
61+
6262
let target_index_dir = index_dir.join("geode-sdk_mods");
6363
// note to loader devs: never change the format pretty please
6464
let checksum = index_dir.join("geode-sdk_mods.checksum");
6565
let current_sha = fs::read_to_string(&checksum).unwrap_or_default();
6666

6767
let client = reqwest::blocking::Client::new();
6868

69-
let response = client.get("https://api.github.com/repos/geode-sdk/mods/commits/main")
69+
let response = client
70+
.get("https://api.github.com/repos/geode-sdk/mods/commits/main")
7071
.header("Accept", "application/vnd.github.sha")
7172
.header("If-None-Match", format!("\"{}\"", current_sha))
7273
.header(USER_AGENT, "GeodeCli")
73-
.header(AUTHORIZATION, std::env::var("GITHUB_TOKEN").map_or("".into(), |token| format!("Bearer {token}")))
74+
.header(
75+
AUTHORIZATION,
76+
std::env::var("GITHUB_TOKEN").map_or("".into(), |token| format!("Bearer {token}")),
77+
)
7478
.send()
7579
.nice_unwrap("Unable to fetch index version");
7680

7781
if response.status() == 304 {
7882
done!("Index is up-to-date");
7983
return;
8084
}
81-
assert!(response.status() == 200, "Version check received status code {}", response.status());
82-
let latest_sha = response.text().nice_unwrap("Unable to decode index version");
85+
assert!(
86+
response.status() == 200,
87+
"Version check received status code {}",
88+
response.status()
89+
);
90+
let latest_sha = response
91+
.text()
92+
.nice_unwrap("Unable to decode index version");
8393

8494
let mut zip_data = io::Cursor::new(Vec::new());
8595

86-
client.get("https://github.com/geode-sdk/mods/zipball/main")
87-
.send().nice_unwrap("Unable to download index")
88-
.copy_to(&mut zip_data).nice_unwrap("Unable to write to index");
96+
client
97+
.get("https://github.com/geode-sdk/mods/zipball/main")
98+
.send()
99+
.nice_unwrap("Unable to download index")
100+
.copy_to(&mut zip_data)
101+
.nice_unwrap("Unable to write to index");
89102

90103
let mut zip_archive = ZipArchive::new(zip_data).nice_unwrap("Unable to decode index zip");
91104

92-
93105
let before_items = if target_index_dir.join("mods-v2").exists() {
94106
let mut items = fs::read_dir(target_index_dir.join("mods-v2"))
95107
.unwrap()
@@ -108,16 +120,21 @@ pub fn update_index(config: &Config) {
108120
fs::remove_dir_all(&extract_dir).nice_unwrap("Unable to prepare new index");
109121
}
110122
fs::create_dir(&extract_dir).unwrap();
111-
zip_archive.extract(&extract_dir).nice_unwrap("Unable to extract new index");
123+
zip_archive
124+
.extract(&extract_dir)
125+
.nice_unwrap("Unable to extract new index");
112126

113-
114-
let new_root_dir = fs::read_dir(&extract_dir).unwrap().next().unwrap().unwrap().path();
115-
copy_dir_recursive(&new_root_dir, &target_index_dir)
116-
.nice_unwrap("Unable to copy new index");
127+
let new_root_dir = fs::read_dir(&extract_dir)
128+
.unwrap()
129+
.next()
130+
.unwrap()
131+
.unwrap()
132+
.path();
133+
copy_dir_recursive(&new_root_dir, &target_index_dir).nice_unwrap("Unable to copy new index");
117134

118135
// we don't care if temp dir removal fails
119136
drop(fs::remove_dir_all(extract_dir));
120-
137+
121138
let mut after_items = fs::read_dir(target_index_dir.join("mods-v2"))
122139
.unwrap()
123140
.map(|x| x.unwrap().path())
@@ -130,13 +147,21 @@ pub fn update_index(config: &Config) {
130147

131148
for i in &before_items {
132149
if !after_items.contains(i) {
133-
println!(" {} {}", "-".red(), i.file_name().unwrap().to_str().unwrap());
150+
println!(
151+
" {} {}",
152+
"-".red(),
153+
i.file_name().unwrap().to_str().unwrap()
154+
);
134155
}
135156
}
136157

137158
for i in &after_items {
138159
if !before_items.contains(i) {
139-
println!(" {} {}", "+".green(), i.file_name().unwrap().to_str().unwrap());
160+
println!(
161+
" {} {}",
162+
"+".green(),
163+
i.file_name().unwrap().to_str().unwrap()
164+
);
140165
}
141166
}
142167
}
@@ -147,18 +172,30 @@ pub fn update_index(config: &Config) {
147172
}
148173

149174
pub fn index_mods_dir(config: &Config) -> PathBuf {
150-
config.get_current_profile().index_dir().join("geode-sdk_mods").join("mods-v2")
175+
config
176+
.get_current_profile()
177+
.index_dir()
178+
.join("geode-sdk_mods")
179+
.join("mods-v2")
151180
}
152181

153182
pub fn get_entry(config: &Config, id: &String, version: &VersionReq) -> Option<Entry> {
154-
for dir in index_mods_dir(config).read_dir().nice_unwrap("Unable to read index") {
183+
for dir in index_mods_dir(config)
184+
.read_dir()
185+
.nice_unwrap("Unable to read index")
186+
{
155187
let path = dir.unwrap().path();
156-
let Ok(mod_info) = try_parse_mod_info(&path) else { continue; };
188+
let Ok(mod_info) = try_parse_mod_info(&path) else {
189+
continue;
190+
};
157191
if &mod_info.id == id && version.matches(&mod_info.version) {
158-
return Some(serde_json::from_str(
159-
&fs::read_to_string(path.join("entry.json"))
160-
.nice_unwrap("Unable to read index entry")
161-
).nice_unwrap("Unable to parse index entry"));
192+
return Some(
193+
serde_json::from_str(
194+
&fs::read_to_string(path.join("entry.json"))
195+
.nice_unwrap("Unable to read index entry"),
196+
)
197+
.nice_unwrap("Unable to parse index entry"),
198+
);
162199
}
163200
}
164201
None
@@ -167,7 +204,7 @@ pub fn get_entry(config: &Config, id: &String, version: &VersionReq) -> Option<E
167204
pub fn install_mod(config: &Config, id: &String, version: &VersionReq) -> PathBuf {
168205
let entry = get_entry(config, id, version)
169206
.nice_unwrap(format!("Unable to find '{id}' version '{version}'"));
170-
207+
171208
let plat = if cfg!(windows) {
172209
"windows"
173210
} else if cfg!(target_os = "macos") {
@@ -179,15 +216,18 @@ pub fn install_mod(config: &Config, id: &String, version: &VersionReq) -> PathBu
179216
if !entry.platforms.contains(plat) {
180217
fatal!("Mod '{}' is not available on '{}'", id, plat);
181218
}
182-
219+
183220
info!("Installing mod '{}' version '{}'", id, version);
184221

185222
let bytes = reqwest::blocking::get(entry.r#mod.download)
186223
.nice_unwrap("Unable to download mod")
187224
.bytes()
188225
.nice_unwrap("Unable to download mod");
189-
190-
let dest = config.get_current_profile().mods_dir().join(format!("{id}.geode"));
226+
227+
let dest = config
228+
.get_current_profile()
229+
.mods_dir()
230+
.join(format!("{id}.geode"));
191231

192232
let mut hasher = Sha3_256::new();
193233
hasher.update(&bytes);
@@ -214,10 +254,17 @@ fn create_index_json(path: &Path) {
214254

215255
let response = reqwest::blocking::get(&url).nice_unwrap("Unable to access .geode file at URL");
216256

217-
let file_name = reqwest::Url::parse(&url).unwrap()
257+
let file_name = reqwest::Url::parse(&url)
258+
.unwrap()
218259
.path_segments()
219260
.and_then(|segments| segments.last())
220-
.and_then(|name| if name.is_empty() { None } else { Some(name.to_string()) })
261+
.and_then(|name| {
262+
if name.is_empty() {
263+
None
264+
} else {
265+
Some(name.to_string())
266+
}
267+
})
221268
.unwrap_or_else(|| ask_value("Filename", None, true));
222269

223270
let file_contents = response
@@ -254,7 +301,8 @@ fn create_index_json(path: &Path) {
254301
std::fs::write(
255302
path.join("index.json"),
256303
String::from_utf8(ser.into_inner()).unwrap(),
257-
).nice_unwrap("Unable to write to project");
304+
)
305+
.nice_unwrap("Unable to write to project");
258306
}
259307

260308
fn create_entry(out_path: &Path) {
@@ -303,6 +351,6 @@ pub fn subcommand(config: &mut Config, cmd: Index) {
303351
update_index(config);
304352
install_mod(config, &id, &version.unwrap_or(VersionReq::STAR));
305353
done!("Mod installed");
306-
},
354+
}
307355
}
308356
}

0 commit comments

Comments
 (0)