Skip to content

Commit

Permalink
feat: improve space when rebuilding (#56)
Browse files Browse the repository at this point in the history
- no longer unzips the generated.zip file to the OUT_DIR when a single
  file can easily be read from the zip file
- this prevents multiple unzips in the `target` directory when the
  build is run multiple times
  • Loading branch information
andrewgazelka authored Mar 28, 2024
1 parent aff4b72 commit f7cec4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 60 deletions.
39 changes: 12 additions & 27 deletions generator-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,46 @@ mod schema;
use std::{
env,
fs::File,
io::Write,
io::{Read, Write},
path::{Path, PathBuf},
};

use anyhow::{ensure, Context};
use anyhow::Context;

use crate::schema::MinecraftData;

// java -DbundlerMainClass=net.minecraft.data.Main -jar minecraft_server.jar

pub struct GeneratorConfig {
pub input: PathBuf,
pub struct GeneratorConfig<T> {
pub registries: T,
pub output: Option<PathBuf>,
}

impl GeneratorConfig {
impl<T: Read> GeneratorConfig<T> {
#[must_use]
pub const fn new(input: PathBuf) -> Self {
pub const fn new(registries: T) -> Self {
Self {
input,
registries,
output: None,
}
}

pub fn build(&self) -> anyhow::Result<()> {
let generated = self.generate()?;

pub fn build(self) -> anyhow::Result<()> {
let output = match &self.output {
Some(path) => path.clone(),
None => Path::new(&env::var("OUT_DIR")?).join("generator-output.rs"),
};

let generated = self.generate()?;

let mut file = File::create(output)?;
file.write_all(generated.as_bytes())?;

Ok(())
}

fn generate(&self) -> anyhow::Result<String> {
// ensure input exists
ensure!(
self.input.exists(),
"input path {:?} does not exist",
self.input.display()
);

let reports = self.input.join("reports");
ensure!(reports.exists(), "reports directory does not exist");

let regitry = reports.join("registries.json");
ensure!(regitry.exists(), "registries.json does not exist");

let s = std::fs::read_to_string(regitry)?;

let data: MinecraftData = serde_json::from_str(&s)?;
fn generate(self) -> anyhow::Result<String> {
let data: MinecraftData = serde_json::from_reader(self.registries)?;

let result = data.entity_type.to_token_stream("EntityType")?;

Expand Down
41 changes: 8 additions & 33 deletions generator/build.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,22 @@
use std::{fs::File, io, path::Path};
use std::{fs::File, path::Path};

use zip::ZipArchive;

fn unzip_file_to_location(file_path: &Path, destination: &Path) -> io::Result<()> {
let file = File::open(file_path)?;
let mut archive = ZipArchive::new(file)?;

for i in 0..archive.len() {
let mut file = archive.by_index(i)?;

// todo: mangled name good?
let outpath = destination.join(file.mangled_name());

if (*file.name()).ends_with('/') {
std::fs::create_dir_all(&outpath)?;
} else {
if let Some(p) = outpath.parent() {
if !p.exists() {
std::fs::create_dir_all(p)?;
}
}
let mut outfile = File::create(&outpath)?;
io::copy(&mut file, &mut outfile)?;
}
}

Ok(())
}

fn main() {
// step 1 unzip generated.zip to OUT_DIR
let out_dir = std::env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir);

let root = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let root = Path::new(&root);

let generated_zip = root.join("generated.zip");

unzip_file_to_location(&generated_zip, out_dir).unwrap();
let file = File::open(generated_zip).unwrap();
let mut archive = ZipArchive::new(file).unwrap();

let registries = archive
.by_name("generated/reports/registries.json")
.unwrap();

generator_build::GeneratorConfig {
input: out_dir.join("generated"),
registries,
output: None,
}
.build()
Expand Down

0 comments on commit f7cec4c

Please sign in to comment.