-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,39 @@ | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::api::{app::App, models::server::Server, utils::script::Shell}; | ||
|
||
impl App { | ||
pub fn get_args(&self, server: &Server) -> Vec<String> { | ||
let (prefix, suffix) = self.get_args_prefix_suffix(server); | ||
let exec = self.get_args_exec(server); | ||
|
||
vec![prefix, exec, suffix].concat() | ||
} | ||
|
||
pub fn get_args_exec(&self, server: &Server) -> Vec<String> { | ||
server.jar.as_ref().map(|s| s.get_exec_arguments()).unwrap_or_default() | ||
} | ||
|
||
pub fn get_args_prefix_suffix(&self, server: &Server) -> (Vec<String>, Vec<String>) { | ||
let mut prefix = vec![]; | ||
|
||
prefix.extend(server.launcher.jvm_args.split_whitespace().map(ToOwned::to_owned)); | ||
|
||
// TODO: -Xmx -Xms | ||
|
||
prefix.extend(server.launcher.preset_flags.get_flags()); | ||
|
||
if server.launcher.eula_args && server.jar.as_ref().is_some_and(|x| x.flavor().supports_eula_args()) { | ||
prefix.push(String::from("-Dcom.mojang.eula.agree=true")); | ||
} | ||
|
||
for (key, value) in &server.launcher.properties { | ||
let value = serde_json::to_string(value).unwrap(); | ||
|
||
prefix.push(format!("-D{key}={value}")); | ||
} | ||
|
||
let mut suffix = vec![]; | ||
|
||
if server.launcher.nogui && server.jar.as_ref().is_some_and(|x| x.flavor().supports_nogui()) { | ||
suffix.push(String::from("--nogui")); | ||
} | ||
|
||
suffix.extend(server.launcher.game_args.split_whitespace().map(ToOwned::to_owned)); | ||
|
||
(prefix, suffix) | ||
} | ||
|
||
pub fn get_script_lines_for(&self, shell: Shell, server: &Server) -> Vec<String> { | ||
fn get_script_lines_for(&self, shell: &Shell, server: &Server) -> Vec<String> { | ||
Check warning on line 8 in src/api/app/actions/script/mod.rs GitHub Actions / clippythis argument (1 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
Check warning on line 8 in src/api/app/actions/script/mod.rs GitHub Actions / clippyunused `self` argument
|
||
let mut lines = vec![]; | ||
if shell == Shell::Bat { | ||
|
||
if *shell == Shell::Bat { | ||
lines.push(format!("title {}", server.name)); | ||
} | ||
|
||
lines.extend(server.launcher.prelaunch.clone()); | ||
let mut args = server.get_arguments(); | ||
args.push(shell.script_args().to_owned()); | ||
lines.push(args.join(" ")); | ||
lines.extend(server.launcher.postlaunch.clone()); | ||
lines | ||
} | ||
|
||
lines.push(self.get_args(server).join(" ")); | ||
pub fn action_generate_script(&self, shell: Shell, server: &Server, base: &Path) -> Result<()> { | ||
let script = shell.generate_script(self.get_script_lines_for(&shell, server)); | ||
|
||
lines.extend(server.launcher.postlaunch.clone()); | ||
std::fs::write(base.join(format!("start.{}", shell.file_ext())), script)?; | ||
|
||
lines | ||
Ok(()) | ||
} | ||
|
||
pub async fn action_generate_script(&self) -> Result<()> { | ||
todo!() | ||
pub async fn action_generate_scripts(&self, base: &Path) -> Result<()> { | ||
if let Some((_, server)) = &*self.server.read().await { | ||
self.action_generate_script(Shell::Bat, server, base)?; | ||
self.action_generate_script(Shell::Sh, server, base)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
//! https://github.com/Manishearth/pathdiff | ||
Check warning on line 1 in src/api/utils/pathdiff.rs GitHub Actions / clippyyou should put bare URLs between `<`/`>` or make a proper Markdown link
|
||
|
||
use std::path::*; | ||
Check warning on line 3 in src/api/utils/pathdiff.rs GitHub Actions / clippyusage of wildcard import
|
||
|
||
use anyhow::{anyhow, Result}; | ||
|
||
pub trait DiffTo { | ||
fn diff_to<P>(&self, path: P) -> Option<PathBuf> | ||
where | ||
P: AsRef<Path>, | ||
Self: AsRef<Path>; | ||
|
||
fn try_diff_to<P>(&self, path: P) -> Result<PathBuf> | ||
Check warning on line 13 in src/api/utils/pathdiff.rs GitHub Actions / clippymethod `try_diff_to` is never used
|
||
where | ||
P: AsRef<Path>, | ||
Self: AsRef<Path>; | ||
} | ||
|
||
impl<B> DiffTo for B | ||
|
@@ -18,9 +27,18 @@ where | |
{ | ||
diff_paths(path, self) | ||
} | ||
} | ||
|
||
use std::path::*; | ||
fn try_diff_to<P>(&self, path: P) -> anyhow::Result<PathBuf> | ||
where | ||
P: AsRef<Path>, | ||
Self: AsRef<Path> | ||
{ | ||
diff_paths(&path, self) | ||
.ok_or(anyhow!("Can't diff paths | ||
Base: {} | ||
Path: {}", self.as_ref().display(), path.as_ref().display())) | ||
} | ||
} | ||
|
||
/// Construct a relative path from a provided base directory path to the provided path. | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::{io::Write, path::{Path, PathBuf}}; | ||
|
||
use anyhow::Result; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
pub fn try_find_toml_upwards<T: DeserializeOwned>(filename: &str) -> Result<Option<(PathBuf, T)>> { | ||
let mut path = std::env::current_dir()?; | ||
|
||
let found_path = loop { | ||
path.push(filename); | ||
|
||
if path.is_file() { | ||
break path; | ||
} | ||
|
||
if !(path.pop() && path.pop()) { | ||
return Ok(None); | ||
} | ||
}; | ||
|
||
read_toml(&found_path).map(|data| Some((found_path, data))) | ||
} | ||
|
||
pub fn read_toml<T: DeserializeOwned>(path: &Path) -> Result<T> { | ||
let data: T = toml::from_str(&std::fs::read_to_string(&path)?)?; | ||
Check failure on line 25 in src/api/utils/toml.rs GitHub Actions / clippythe borrowed expression implements the required traits
|
||
|
||
Ok(data) | ||
} | ||
|
||
pub fn write_toml<T: Serialize>(path: &Path, filename: &str, value: &T) -> Result<()> { | ||
Check warning on line 30 in src/api/utils/toml.rs GitHub Actions / clippyfunction `write_toml` is never used
|
||
std::fs::create_dir_all(path)?; | ||
|
||
let content = toml::to_string_pretty(value)?; | ||
|
||
let mut file = std::fs::File::open(path.join(filename))?; | ||
file.write_all(content.as_bytes())?; | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::{io::{Read, Seek, Write}, path::Path}; | ||
|
||
use anyhow::{anyhow, Context, Result}; | ||
use walkdir::WalkDir; | ||
use zip::{write::FileOptions, ZipArchive, ZipWriter}; | ||
|
||
use crate::api::app::APP_VERSION; | ||
|
||
use super::pathdiff::DiffTo; | ||
|
||
pub async fn unzip<T: Read + Seek>(reader: T, to: &Path, prefix: Option<String>) -> Result<()> { | ||
Check warning on line 11 in src/api/utils/zip.rs GitHub Actions / clippyfunction `unzip` is never used
|
||
let mut archive = ZipArchive::new(reader)?; | ||
|
||
let mut files = archive.file_names().map(ToOwned::to_owned).collect::<Vec<_>>(); | ||
|
||
if let Some(prefix) = prefix.map(|p| format!("{p}/")) { | ||
if files.iter().all(|f| f.starts_with(&prefix)) { | ||
files = files.into_iter() | ||
.map(|f| f.replacen(&prefix, "", 1)) | ||
.collect() | ||
Check warning on line 20 in src/api/utils/zip.rs GitHub Actions / clippyconsider adding a `;` to the last statement for consistent formatting
|
||
} | ||
} | ||
|
||
|
||
for filename in files { | ||
if filename.ends_with('/') { | ||
// directory | ||
continue; | ||
} | ||
|
||
let mut file = archive.by_name(&filename)?; | ||
let target_path = to.join(&filename); | ||
|
||
std::fs::create_dir_all(target_path.parent().unwrap())?; | ||
let mut target_file = std::fs::File::create(&target_path)?; | ||
|
||
std::io::copy(&mut file, &mut target_file)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
Check warning on line 41 in src/api/utils/zip.rs GitHub Actions / clippyunused `async` for function with no await statements
|
||
|
||
pub async fn zip<T: Write + Seek>(writer: T, folder: &Path) -> Result<()> { | ||
let mut archive = ZipWriter::new(writer); | ||
|
||
archive.set_comment(format!("generated by mcman/{APP_VERSION}")); | ||
|
||
for entry in WalkDir::new(folder) { | ||
let entry = entry.with_context(|| "WalkDir")?; | ||
|
||
let path = folder.try_diff_to(entry.path())?; | ||
let path = path.to_string_lossy(); | ||
|
||
if entry.file_type().is_dir() { | ||
archive.add_directory(path, FileOptions::default())?; | ||
continue; | ||
} | ||
|
||
archive.start_file(path, FileOptions::default())?; | ||
|
||
let mut file = std::fs::File::open(entry.path())?; | ||
std::io::copy(&mut file, &mut archive)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
Check warning on line 66 in src/api/utils/zip.rs GitHub Actions / clippyunused `async` for function with no await statements
|