Skip to content

Commit

Permalink
move asc install to sqf library
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson committed Sep 25, 2024
1 parent 62933ca commit a92e3ce
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 50 deletions.
1 change: 1 addition & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions bench/src/sqf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<CompareResult>, String> {
Expand Down Expand Up @@ -79,7 +79,7 @@ fn hemtt(processed: &Processed) -> (Vec<u8>, Vec<u8>) {
fn asc(processed: &Processed) -> Result<Vec<u8>, 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);
Expand Down
49 changes: 2 additions & 47 deletions bin/src/modules/asc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
fs::{create_dir_all, File},
io::{Read, Write},
path::Path,
process::Command,
sync::{
atomic::{AtomicU16, Ordering},
Expand All @@ -11,33 +10,16 @@ use std::{

use hemtt_preprocessor::Processor;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use rust_embed::RustEmbed;
use serde::Serialize;
use std::time::Instant;

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"
Expand All @@ -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();
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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(())
}
4 changes: 3 additions & 1 deletion libs/sqf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions libs/sqf/src/asc.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
3 changes: 3 additions & 0 deletions libs/sqf/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 2 additions & 0 deletions libs/sqf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "asc")]
pub mod asc;
#[cfg(feature = "compiler")]
pub mod compiler;
#[cfg(feature = "parser")]
Expand Down

0 comments on commit a92e3ce

Please sign in to comment.