Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench: initial tool #773

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move asc install to sqf library
BrettMayson committed Sep 25, 2024
commit a92e3cec5e2e113e9209089afaaa3a00ad6d3cad
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
@@ -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"
4 changes: 2 additions & 2 deletions bench/src/sqf/mod.rs
Original file line number Diff line number Diff line change
@@ -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> {
@@ -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);
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},
@@ -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"
@@ -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(())
}
4 changes: 3 additions & 1 deletion libs/sqf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]

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
@@ -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")]
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")]