Skip to content

Commit

Permalink
Improve template file names
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Juuxel committed Jun 29, 2024
1 parent 403a9f5 commit f5d8157
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/app/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ 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<String> = Vec::new();
// Mod properties
context.put("PACKAGE_NAME", &app.package_name);
context.put("PACKAGE_DIR", &app.package_name.replace(".", "/"));
let mut mod_id: String = app.mod_id.clone();
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",
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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())));
Expand All @@ -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<FileData> = files
Expand All @@ -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 {
Expand Down
18 changes: 17 additions & 1 deletion src/templates/engine/filer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;
}

Expand All @@ -32,6 +33,7 @@ where
{
writer: &'a mut zip::ZipWriter<W>,
directories: HashSet<String>,
pub file_name: Option<String>,
}

impl<'a, W> ZipFiler<'a, W>
Expand All @@ -42,6 +44,7 @@ where
Self {
writer,
directories: HashSet::new(),
file_name: None,
}
}
}
Expand Down Expand Up @@ -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"))]
Expand Down Expand Up @@ -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.
}
}
}

Expand All @@ -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.
Expand All @@ -155,14 +167,18 @@ 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()?;
}

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;

Expand Down

0 comments on commit f5d8157

Please sign in to comment.