Skip to content

Commit

Permalink
Improve dist command
Browse files Browse the repository at this point in the history
Now packages more than just the info.json.
  • Loading branch information
Sharparam committed Aug 26, 2023
1 parent 16363cf commit 5079d4a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 14 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ facti-api = { version = "0.2.0", path = "../api", default-features = false, feat
facti-lib = { version = "0.2.1", path = "../lib" }
git2 = { version = "0.17.2", default-features = false }
human-panic = "1.1.5"
ignore = "0.4.20"
indoc = "2.0.3"
ron = { version = "0.8.1", optional = true }
rpassword = "7.2.0"
Expand Down
65 changes: 51 additions & 14 deletions crates/cli/src/cli/dist.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{
env,
fs::{self, File},
io::{BufWriter, Write},
io::{BufWriter, Read, Write},
path::PathBuf,
};

use anyhow::{Context, Result};
use clap::{Args, ValueHint};
use tracing::{debug, info};
use tracing::{debug, error, info};
use zip::ZipWriter;

use crate::project::Project;
Expand All @@ -27,7 +27,12 @@ impl DistArgs {
.path
.to_owned()
.unwrap_or(env::current_dir().context("Failed to get current directory")?);
let project = Project::load(&path)?;
let project = Project::load(&path).with_context(|| {
format!(
"Failed to load Factorio mod project from {}",
path.display()
)
})?;

info!(
"Loaded project ({} v{} by {}) at {}",
Expand All @@ -51,24 +56,56 @@ impl DistArgs {
let dist_name = format!("{}_{}", project.mod_info.name, project.mod_info.version);
let zip_name = format!("{}.zip", dist_name);
let zip_path = dist_path.join(zip_name);
let zip_inner_prefix = PathBuf::from(&dist_name);

info!("Target dist zip: {}", zip_path.display());

let zip_file = File::create(&zip_path).context("Failed to create ZIP file for writing")?;
let writer = BufWriter::new(zip_file);
let mut zip = ZipWriter::new(writer);
let options = zip::write::FileOptions::default();
zip.add_directory(&dist_name, options)
.context("Failed to add dist_name inner dir to ZIP")?;
zip.start_file(format!("{}/info.json", dist_name), options)
.context("Failed to start adding info.json to ZIP")?;

let infojson_str = serde_json::to_string_pretty(&project.mod_info)
.context("Failed to serialize mod info")?;
let infojson_bytes = infojson_str.as_bytes();
info!("Writing info.json to ZIP package");
zip.write_all(infojson_bytes)
.context("Failed to write mod info to ZIP")?;

let mut overrides = ignore::overrides::OverrideBuilder::new(&project.mod_path);
overrides
.add("!/dist/")
.context("Failed to add dist dir to ignore overrides")?;
let overrides = overrides
.build()
.context("Failed to build ignore overrides")?;
let mut builder = ignore::WalkBuilder::new(&project.mod_path);
builder.overrides(overrides);
let walker = builder.build();
for entry in walker {
match entry {
Ok(path) if path.path().is_file() => {
let rel_path = path
.path()
.strip_prefix(&project.mod_path)
.context("Failed to strip mod path prefix")?;
let zip_path = zip_inner_prefix.join(rel_path);
let zip_path_str = zip_path
.to_str()
.context("Failed to convert zip path to str")?;
info!(
"Adding path {} to ZIP as {}",
path.path().display(),
zip_path.display()
);
zip.start_file(zip_path_str, options).with_context(|| {
format!("Failed to start adding {} to ZIP", rel_path.display())
})?;
let mut file =
File::open(path.path()).context("Failed to open file for reading")?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.context("Failed to read file contents")?;
zip.write(&buffer)
.context("Failed to write file contents to ZIP")?;
}
Ok(path) => debug!("Ignoring non-file: {}", path.path().display()),
Err(e) => error!("Glob error: {:?}", e),
}
}

debug!("Finishing ZIP file");
zip.finish().context("Failed to finish ZIP file")?;
Expand Down

0 comments on commit 5079d4a

Please sign in to comment.