refactor: extract java and maven helpers to net module
This commit is contained in:
1 parent
9df5e9a78a
commit
cc97c174c1
6 files changed
+49
-59
No files matched your search
+11
-1
@@ -112,7 +112,17 @@ impl InstanceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let installer = crate::instance::loader::get_installer(loader);
|
let installer = crate::instance::loader::get_installer(loader);
|
||||||
let effective_loader_version = loader_version.unwrap_or("latest");
|
let effective_loader_version = match loader_version {
|
||||||
|
Some(v) => v,
|
||||||
|
None if loader == ModLoader::Vanilla => "vanilla",
|
||||||
|
None => {
|
||||||
|
tracing::error!("No loader version provided for {} loader", loader);
|
||||||
|
return Err(InstanceError::InvalidName(format!(
|
||||||
|
"A loader version is required for {}",
|
||||||
|
loader
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
match installer
|
match installer
|
||||||
.install(&self.client, game_version, effective_loader_version, &instance_dir)
|
.install(&self.client, game_version, effective_loader_version, &instance_dir)
|
||||||
.await
|
.await
|
||||||
|
|||||||
+6
-19
@@ -34,20 +34,7 @@ pub struct FabricLibrary {
|
|||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `net.fabricmc:fabric-loader:0.16.0` → `net/fabricmc/fabric-loader/0.16.0/fabric-loader-0.16.0.jar`
|
|
||||||
fn maven_coord_to_path(coord: &str) -> Option<String> {
|
|
||||||
let parts: Vec<&str> = coord.split(':').collect();
|
|
||||||
match parts.as_slice() {
|
|
||||||
[group, artifact, version] => {
|
|
||||||
let group_path = group.replace('.', "/");
|
|
||||||
Some(format!(
|
|
||||||
"{}/{}/{}/{}-{}.jar",
|
|
||||||
group_path, artifact, version, artifact, version
|
|
||||||
))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_fabric_versions(
|
pub async fn fetch_fabric_versions(
|
||||||
client: &HttpClient,
|
client: &HttpClient,
|
||||||
@@ -129,7 +116,7 @@ pub async fn download_fabric_libraries(
|
|||||||
let libraries_dir = instance_dir.join(".minecraft").join("libraries");
|
let libraries_dir = instance_dir.join(".minecraft").join("libraries");
|
||||||
|
|
||||||
for lib in &profile.libraries {
|
for lib in &profile.libraries {
|
||||||
let maven_path = match maven_coord_to_path(&lib.name) {
|
let maven_path = match crate::net::maven_coord_to_path(&lib.name) {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
None => {
|
None => {
|
||||||
tracing::error!(
|
tracing::error!(
|
||||||
@@ -197,14 +184,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_maven_coord_to_path() {
|
fn test_maven_coord_to_path() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
maven_coord_to_path("net.fabricmc:fabric-loader:0.16.0"),
|
crate::net::maven_coord_to_path("net.fabricmc:fabric-loader:0.16.0"),
|
||||||
Some("net/fabricmc/fabric-loader/0.16.0/fabric-loader-0.16.0.jar".to_string())
|
Some("net/fabricmc/fabric-loader/0.16.0/fabric-loader-0.16.0.jar".to_string())
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
maven_coord_to_path("org.ow2.asm:asm:9.6"),
|
crate::net::maven_coord_to_path("org.ow2.asm:asm:9.6"),
|
||||||
Some("org/ow2/asm/asm/9.6/asm-9.6.jar".to_string())
|
Some("org/ow2/asm/asm/9.6/asm-9.6.jar".to_string())
|
||||||
);
|
);
|
||||||
assert_eq!(maven_coord_to_path("invalid"), None);
|
assert_eq!(crate::net::maven_coord_to_path("invalid"), None);
|
||||||
assert_eq!(maven_coord_to_path("only:two"), None);
|
assert_eq!(crate::net::maven_coord_to_path("only:two"), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-12
@@ -141,18 +141,7 @@ pub async fn run_forge_installer(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Detect the path to a Java runtime.
|
|
||||||
///
|
|
||||||
/// Checks `JAVA_HOME` first, then falls back to `"java"` assuming it is on `PATH`.
|
|
||||||
pub fn detect_java_path() -> String {
|
|
||||||
if let Ok(java_home) = std::env::var("JAVA_HOME") {
|
|
||||||
let path = std::path::Path::new(&java_home).join("bin").join("java");
|
|
||||||
if path.exists() {
|
|
||||||
return path.to_string_lossy().to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"java".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ pub enum NetError {
|
|||||||
StatusError { status: u16, url: String },
|
StatusError { status: u16, url: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct HttpClient {
|
pub struct HttpClient {
|
||||||
inner: Client,
|
inner: Client,
|
||||||
}
|
}
|
||||||
@@ -114,3 +115,27 @@ pub async fn download_file(
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn detect_java_path() -> String {
|
||||||
|
if let Ok(java_home) = std::env::var("JAVA_HOME") {
|
||||||
|
let path = std::path::Path::new(&java_home).join("bin").join("java");
|
||||||
|
if path.exists() {
|
||||||
|
return path.to_string_lossy().to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"java".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn maven_coord_to_path(coord: &str) -> Option<String> {
|
||||||
|
let parts: Vec<&str> = coord.split(':').collect();
|
||||||
|
match parts.as_slice() {
|
||||||
|
[group, artifact, version] => {
|
||||||
|
let group_path = group.replace('.', "/");
|
||||||
|
Some(format!(
|
||||||
|
"{}/{}/{}/{}-{}.jar",
|
||||||
|
group_path, artifact, version, artifact, version
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-9
@@ -152,15 +152,7 @@ pub async fn run_neoforge_installer(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn detect_java_path() -> String {
|
|
||||||
if let Ok(java_home) = std::env::var("JAVA_HOME") {
|
|
||||||
let path = std::path::Path::new(&java_home).join("bin").join("java");
|
|
||||||
if path.exists() {
|
|
||||||
return path.to_string_lossy().to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"java".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
+5
-18
@@ -31,20 +31,7 @@ pub struct QuiltLibrary {
|
|||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `org.quiltmc:quilt-loader:0.20.0` → `org/quiltmc/quilt-loader/0.20.0/quilt-loader-0.20.0.jar`
|
|
||||||
fn maven_coord_to_path(coord: &str) -> Option<String> {
|
|
||||||
let parts: Vec<&str> = coord.split(':').collect();
|
|
||||||
match parts.as_slice() {
|
|
||||||
[group, artifact, version] => {
|
|
||||||
let group_path = group.replace('.', "/");
|
|
||||||
Some(format!(
|
|
||||||
"{}/{}/{}/{}-{}.jar",
|
|
||||||
group_path, artifact, version, artifact, version
|
|
||||||
))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_quilt_versions(
|
pub async fn fetch_quilt_versions(
|
||||||
client: &HttpClient,
|
client: &HttpClient,
|
||||||
@@ -126,7 +113,7 @@ pub async fn download_quilt_libraries(
|
|||||||
let libraries_dir = instance_dir.join(".minecraft").join("libraries");
|
let libraries_dir = instance_dir.join(".minecraft").join("libraries");
|
||||||
|
|
||||||
for lib in &profile.libraries {
|
for lib in &profile.libraries {
|
||||||
let maven_path = match maven_coord_to_path(&lib.name) {
|
let maven_path = match crate::net::maven_coord_to_path(&lib.name) {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
None => {
|
None => {
|
||||||
tracing::error!(
|
tracing::error!(
|
||||||
@@ -187,10 +174,10 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_maven_coord_to_path() {
|
fn test_maven_coord_to_path() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
maven_coord_to_path("org.quiltmc:quilt-loader:0.20.0"),
|
crate::net::maven_coord_to_path("org.quiltmc:quilt-loader:0.20.0"),
|
||||||
Some("org/quiltmc/quilt-loader/0.20.0/quilt-loader-0.20.0.jar".to_string())
|
Some("org/quiltmc/quilt-loader/0.20.0/quilt-loader-0.20.0.jar".to_string())
|
||||||
);
|
);
|
||||||
assert_eq!(maven_coord_to_path("invalid"), None);
|
assert_eq!(crate::net::maven_coord_to_path("invalid"), None);
|
||||||
assert_eq!(maven_coord_to_path("only:two"), None);
|
assert_eq!(crate::net::maven_coord_to_path("only:two"), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user