From a92e3cec5e2e113e9209089afaaa3a00ad6d3cad Mon Sep 17 00:00:00 2001 From: Brett Mayson Date: Wed, 25 Sep 2024 23:59:22 +0000 Subject: [PATCH] move asc install to sqf library --- Cargo.lock | 1 + Cargo.toml | 1 + bench/src/sqf/mod.rs | 4 +- bin/src/modules/asc.rs | 49 +------------- libs/sqf/Cargo.toml | 4 +- {bin/dist/asc => libs/sqf/dist}/linux/asc | Bin .../asc => libs/sqf/dist}/windows/asc.exe | Bin libs/sqf/src/asc.rs | 63 ++++++++++++++++++ libs/sqf/src/error.rs | 3 + libs/sqf/src/lib.rs | 2 + 10 files changed, 77 insertions(+), 50 deletions(-) rename {bin/dist/asc => libs/sqf/dist}/linux/asc (100%) rename {bin/dist/asc => libs/sqf/dist}/windows/asc.exe (100%) create mode 100644 libs/sqf/src/asc.rs diff --git a/Cargo.lock b/Cargo.lock index 1ce611ad..8c67d79b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1621,6 +1621,7 @@ dependencies = [ "hemtt-workspace", "linkme", "paste", + "rust-embed", "toml 0.8.19", "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 6543e2dd..2133fb95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ peekmore = "1.3.0" pest = "2.7.11" pest_derive = "2.7.11" regex = "1.10.5" +rust-embed = "8.5.0" serde = { version = "1.0.210", features = ["derive"] } serde_json = "1.0.122" sha-1 = "0.10.1" diff --git a/bench/src/sqf/mod.rs b/bench/src/sqf/mod.rs index 95af3d52..44c671a0 100644 --- a/bench/src/sqf/mod.rs +++ b/bench/src/sqf/mod.rs @@ -3,7 +3,7 @@ use std::{fs::File, io::Write, process::Command}; use arma_bench::{Client, CompareRequest, CompareResult}; use hemtt::modules::asc::ASCConfig; use hemtt_preprocessor::Processor; -use hemtt_sqf::parser::database::Database; +use hemtt_sqf::{asc::install, parser::database::Database}; use hemtt_workspace::reporting::Processed; pub fn compare(client: &Client, content: &str) -> Result, String> { @@ -79,7 +79,7 @@ fn hemtt(processed: &Processed) -> (Vec, Vec) { fn asc(processed: &Processed) -> Result, String> { let asc_dir = std::env::temp_dir().join("hemtt_bench_asc"); if !asc_dir.exists() { - hemtt::modules::asc::install(&asc_dir).expect("Failed to install ASC"); + install(&asc_dir).expect("Failed to install ASC"); } let source = asc_dir.join("source"); let _ = std::fs::create_dir_all(&source); diff --git a/bin/src/modules/asc.rs b/bin/src/modules/asc.rs index 1c6873ea..dbb26d9f 100644 --- a/bin/src/modules/asc.rs +++ b/bin/src/modules/asc.rs @@ -1,7 +1,6 @@ use std::{ fs::{create_dir_all, File}, io::{Read, Write}, - path::Path, process::Command, sync::{ atomic::{AtomicU16, Ordering}, @@ -11,7 +10,6 @@ use std::{ use hemtt_preprocessor::Processor; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; -use rust_embed::RustEmbed; use serde::Serialize; use std::time::Instant; @@ -19,25 +17,9 @@ use crate::{context::Context, error::Error, report::Report}; use super::Module; -#[cfg(windows)] -#[derive(RustEmbed)] -#[folder = "dist/asc/windows"] -struct Distributables; - -#[cfg(not(windows))] -#[derive(RustEmbed)] -#[folder = "dist/asc/linux"] -struct Distributables; - #[derive(Default)] pub struct ArmaScriptCompiler; -#[cfg(windows)] -const SOURCE: [&str; 1] = ["asc.exe"]; - -#[cfg(not(windows))] -const SOURCE: [&str; 1] = ["asc"]; - impl Module for ArmaScriptCompiler { fn name(&self) -> &'static str { "ArmaScriptCompiler" @@ -57,7 +39,7 @@ impl Module for ArmaScriptCompiler { File::create(".hemttout/asc.log").expect("Unable to create `.hemttout/asc.log`"); let mut config = ASCConfig::new(); let tmp = ctx.tmp().join("asc"); - install(&tmp)?; + hemtt_sqf::asc::install(&tmp)?; let sqf_ext = Some(String::from("sqf")); let files = Arc::new(RwLock::new(Vec::new())); let start = Instant::now(); @@ -137,7 +119,7 @@ impl Module for ArmaScriptCompiler { f.write_all(serde_json::to_string_pretty(&config)?.as_bytes())?; std::env::set_current_dir(&tmp)?; let start = Instant::now(); - let command = Command::new(tmp.join(SOURCE[0])).output()?; + let command = Command::new(tmp.join(hemtt_sqf::asc::command())).output()?; out_file.write_all(&command.stdout)?; out_file.write_all(&command.stderr)?; if String::from_utf8(command.stdout.clone()) @@ -224,30 +206,3 @@ impl ASCConfig { self.worker_threads = threads; } } - -/// Install Arma Script Compiler -/// -/// # Errors -/// [`Error::Io`] if the file couldn't be created or written to -/// -/// # Panics -/// If an expected file didn't get packed into the binary -pub fn install(path: &Path) -> Result<(), Error> { - let _ = std::fs::create_dir_all(path); - for file in SOURCE { - let out = path.join(file); - trace!("unpacking {:?} to {:?}", file, out.display()); - let mut f = File::create(&out)?; - f.write_all( - &Distributables::get(file) - .expect("dist files should exist") - .data, - )?; - #[cfg(target_os = "linux")] - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(out, PermissionsExt::from_mode(0o744))?; - } - } - Ok(()) -} diff --git a/libs/sqf/Cargo.toml b/libs/sqf/Cargo.toml index ae9872b7..693b0964 100644 --- a/libs/sqf/Cargo.toml +++ b/libs/sqf/Cargo.toml @@ -22,11 +22,13 @@ byteorder = { workspace = true, optional = true } chumsky = { workspace = true, optional = true} float-ord = "0.3.2" linkme = { workspace = true } +rust-embed = { workspace = true, optional = true } toml = { workspace = true } tracing = { workspace = true } [features] -default = ["compiler", "parser"] +default = ["asc", "compiler", "parser"] +asc = ["rust-embed"] compiler = ["byteorder", "hemtt-lzo"] parser = ["chumsky"] diff --git a/bin/dist/asc/linux/asc b/libs/sqf/dist/linux/asc similarity index 100% rename from bin/dist/asc/linux/asc rename to libs/sqf/dist/linux/asc diff --git a/bin/dist/asc/windows/asc.exe b/libs/sqf/dist/windows/asc.exe similarity index 100% rename from bin/dist/asc/windows/asc.exe rename to libs/sqf/dist/windows/asc.exe diff --git a/libs/sqf/src/asc.rs b/libs/sqf/src/asc.rs new file mode 100644 index 00000000..9ca3d0ef --- /dev/null +++ b/libs/sqf/src/asc.rs @@ -0,0 +1,63 @@ +use std::{fs::File, io::Write, path::Path}; + +use hemtt_common::error::thiserror; +use rust_embed::RustEmbed; +use tracing::trace; + +#[cfg(windows)] +#[derive(RustEmbed)] +#[folder = "dist/windows"] +struct Distributables; + +#[cfg(not(windows))] +#[derive(RustEmbed)] +#[folder = "dist/linux"] +struct Distributables; + +#[cfg(windows)] +const SOURCE: [&str; 1] = ["asc.exe"]; + +#[cfg(not(windows))] +const SOURCE: [&str; 1] = ["asc"]; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error(transparent)] + Io(#[from] std::io::Error), +} + +#[must_use] +pub const fn command() -> &'static str { + SOURCE[0] +} + +/// Install Arma Script Compiler +/// +/// # Errors +/// [`Error::Io`] if the file couldn't be created or written to +/// +/// # Panics +/// If an expected file didn't get packed into the binary +pub fn install(path: &Path) -> Result<(), super::Error> { + _install(path).map_err(super::Error::AscError) +} + +fn _install(path: &Path) -> Result<(), Error> { + let _ = std::fs::create_dir_all(path); + for file in SOURCE { + let out = path.join(file); + trace!("unpacking {:?} to {:?}", file, out.display()); + let mut f = File::create(&out)?; + f.write_all( + &Distributables::get(file) + .expect("dist files should exist") + .data, + )?; + #[cfg(target_os = "linux")] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(out, PermissionsExt::from_mode(0o744))?; + } + } + Ok(()) +} diff --git a/libs/sqf/src/error.rs b/libs/sqf/src/error.rs index ce0170ad..985c7b68 100644 --- a/libs/sqf/src/error.rs +++ b/libs/sqf/src/error.rs @@ -2,6 +2,9 @@ use hemtt_common::error::thiserror; #[derive(Debug, thiserror::Error)] pub enum Error { + #[cfg(feature = "asc")] + #[error(transparent)] + AscError(#[from] crate::asc::Error), #[error(transparent)] ParserError(#[from] crate::parser::ParserError), #[cfg(feature = "compiler")] diff --git a/libs/sqf/src/lib.rs b/libs/sqf/src/lib.rs index fb240601..f4dd6f4e 100644 --- a/libs/sqf/src/lib.rs +++ b/libs/sqf/src/lib.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "asc")] +pub mod asc; #[cfg(feature = "compiler")] pub mod compiler; #[cfg(feature = "parser")]