From f5d81579ce99f04646d55261bdb98d785e3e14e2 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Sun, 30 Jun 2024 02:07:40 +0300 Subject: [PATCH] Improve template file names Now it contains the platforms, the game version and the mod id. Before: template.zip After: my_mod-1.21-fabric-neoforge-template.zip my_mod-1.18.2-forge-only-template.zip --- src/app/generator.rs | 18 ++++++++++++++++++ src/templates/engine/filer.rs | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/app/generator.rs b/src/app/generator.rs index 802d05d..93de0f2 100644 --- a/src/app/generator.rs +++ b/src/app/generator.rs @@ -16,6 +16,9 @@ use version_resolver::maven::{resolve_latest_version, resolve_matching_version, pub async fn generate(app: &super::GeneratorApp) -> Result<()> { let mut context = engine::Context::new(); + // This vec contains all dash-separated parts of the template file name. + // Example: my_mod-1.21-fabric-neoforge-template.zip + let mut file_name_parts: Vec = Vec::new(); // Mod properties context.put("PACKAGE_NAME", &app.package_name); context.put("PACKAGE_DIR", &app.package_name.replace(".", "/")); @@ -23,12 +26,14 @@ pub async fn generate(app: &super::GeneratorApp) -> Result<()> { if mod_id.is_empty() { mod_id = crate::mod_ids::to_mod_id(&app.mod_name); } + file_name_parts.push(mod_id.clone()); context.put("MOD_ID", mod_id); let escaped_name = escape_json_and_toml(&app.mod_name); context.put("MOD_NAME", escaped_name); // Game version-specific let game_version = app.game_version; + file_name_parts.push(game_version.version().to_owned()); context.put("MINECRAFT_VERSION", game_version.version()); context.put( "GRADLE_JAVA_VERSION", @@ -161,6 +166,11 @@ pub async fn generate(app: &super::GeneratorApp) -> Result<()> { platforms.push("quilt"); } + // Add all platforms to template file name. + for platform in &platforms { + file_name_parts.push((*platform).to_owned()); + } + let platforms = platforms.join(","); context.put("ARCHITECTURY_PLATFORMS", platforms); @@ -188,6 +198,7 @@ pub async fn generate(app: &super::GeneratorApp) -> Result<()> { files.push(Box::pin(neoforge_only::neoforge_mods_toml_files(client.clone()))); } context.maybe_put("NEOFORGE_YARN_PATCH_VERSION", versions.neoforge_yarn_patch); + file_name_parts.push("neoforge-only".to_owned()); } ProjectType::Forge => { files.push(Box::pin(forge_only::all_files(client.clone()))); @@ -197,9 +208,13 @@ pub async fn generate(app: &super::GeneratorApp) -> Result<()> { std::future::ready(Ok(version)), ))); } + file_name_parts.push("forge-only".to_owned()); } } + // Add final template suffix to file name + file_name_parts.push("template".to_owned()); + // Resolve versions let (files, variables) = join!(join_all(files), join_all(variables)); let files: Vec = files @@ -214,6 +229,9 @@ pub async fn generate(app: &super::GeneratorApp) -> Result<()> { } engine::filer::use_filer(|filer| { + let file_name = file_name_parts.join("-"); + filer.set_file_name(file_name); + for file_data in files { let path = engine::apply_variables(&context, file_data.path.as_str(), false); let content: Bytes = match &file_data.content { diff --git a/src/templates/engine/filer.rs b/src/templates/engine/filer.rs index f9dcd73..0a038b9 100644 --- a/src/templates/engine/filer.rs +++ b/src/templates/engine/filer.rs @@ -9,6 +9,7 @@ use std::io::{Cursor, Seek, Write}; use zip::write::FileOptions; pub trait Filer { + fn set_file_name(&mut self, file_name: String); fn save(&mut self, path: &str, content: &[u8], permissions: &FilePermissions) -> Result<()>; } @@ -32,6 +33,7 @@ where { writer: &'a mut zip::ZipWriter, directories: HashSet, + pub file_name: Option, } impl<'a, W> ZipFiler<'a, W> @@ -42,6 +44,7 @@ where Self { writer, directories: HashSet::new(), + file_name: None, } } } @@ -72,6 +75,10 @@ where self.writer.write_all(content).into_diagnostic()?; Ok(()) } + + fn set_file_name(&mut self, file_name: String) { + self.file_name = Some(file_name) + } } #[cfg(not(target_family = "wasm"))] @@ -139,6 +146,10 @@ mod native { update_permissions(&full_path, permissions)?; Ok(()) } + + fn set_file_name(&mut self, _file_name: String) { + // Don't do anything as the directory filer prompts for the output name. + } } } @@ -147,6 +158,7 @@ where F: FnOnce(&mut dyn Filer) -> Result<()>, { let mut cursor = Cursor::new(Vec::new()); + let mut file_name: String = "template.zip".to_owned(); // Create and use the zip writer and filer. // This is its own scope in order to drop the borrow to the cursor. @@ -155,6 +167,10 @@ where { let mut filer = ZipFiler::new(&mut writer); block(&mut filer)?; + + if let Some(custom_name) = filer.file_name { + file_name = custom_name + ".zip"; + } } writer.finish().into_diagnostic()?; } @@ -162,7 +178,7 @@ where let saved = AsyncFileDialog::new() .set_title("Choose where to save the template") .add_filter("Zip file", &["zip"]) - .set_file_name("template.zip") + .set_file_name(file_name) .save_file() .await;