From 9a35cef77ab80ad322af936bdfb9a8a8e4011fec Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Tue, 12 Apr 2016 16:04:24 -0400 Subject: [PATCH] Mason is now able to compile the resources in a single threaded run. --- resources/resources.msnr | 11 ++- src/processor.rs | 169 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 176 insertions(+), 4 deletions(-) diff --git a/resources/resources.msnr b/resources/resources.msnr index 68571ab..82d295c 100644 --- a/resources/resources.msnr +++ b/resources/resources.msnr @@ -1,4 +1,11 @@ name=test src=resources.msnr -dst=resources.msnr -compiler=copy +dst=files/resources.msnr +compiler=echo +args=-i -o -kkfjfllsaf + +name=test2 +src=resources.msnr +dst=files/resources.msnr +compiler=echo +args=-i -o -kkfjfllsaf diff --git a/src/processor.rs b/src/processor.rs index 8678751..9685af7 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -1,7 +1,8 @@ use std::env; -use std::fs::File; +use std::fs::{DirBuilder, File}; use std::io::{BufRead, BufReader}; use std::path::{Path, PathBuf}; +use std::process::Command; @@ -14,7 +15,7 @@ const DEFAULT_MANIFEST_FILENAME: &'static str = "resources.msnr"; /// Defines a resource to be compiled. -struct Section +pub struct Section { /// A unique name used to reference the /// resource defined in this Section. @@ -304,5 +305,169 @@ impl Processor /// Compile all the resources in the Section list. fn compile_resources(&self) { + let mut dst_path: &Path; + let mut dst_dir: PathBuf; + + for section in self.sections.iter() + { + // Get a copy of the output directory + // that can be manipulated. + dst_dir = self.output_dir.clone(); + + dst_path = Path::new(§ion.dst_path); + match dst_path.parent() + { + Some(dir) => + { + dst_dir.push(dir); + } + + None => + { + } + } + + if dst_dir.as_os_str() != "" + { + // Create any directory needed for the output + // of the compilation of the resource in this Section. + match DirBuilder::new().recursive(true).create(dst_dir.as_path()) + { + Ok(_) => + { + debug!("Creating directories for the path: {:?}", + dst_dir); + } + + Err(error) => + { + error!("{}", error); + } + } + } + + self.compile_section(section); + } + } + + /// Run a Section's compiler for its resource. + fn compile_section(&self, section: &Section) + { + let exit_code: i32; + let args: Vec; + let out: String; + let err: String; + let mut compiler: Command; + let mut arg_compiler: &mut Command; + + // Build the arguments to pass to teh compiler. + args = self.build_args(section); + + println!("Running compiler {} for resource {} with args '{:?}'.", + section.compiler, section.name, args); + + // Create a new command to call the compiler to compile this + // Section's resource. + compiler = Command::new(section.compiler.clone()); + arg_compiler = compiler.args(&args); + + // Execute the compiler command. + match arg_compiler.output() + { + Ok(output) => + { + if output.status.success() == true + { + // Log that the compiler succeeded. + info!("{} compiled by {} successfully.", + section.name, section.compiler); + } + else + { + // Get the exit code of the command. + match output.status.code() + { + Some(code) => + { + exit_code = code; + } + + None => + { + // Mark that the compiler exited successfully. + exit_code = 0; + } + } + + // Get the stdout and stderr strings. + match String::from_utf8(output.stdout) + { + Ok(command_stdout) => + { + out = command_stdout; + } + + Err(error) => + { + error!("{}", error); + } + } + + match String::from_utf8(output.stderr) + { + Ok(command_stderr) => + { + err = command_stderr; + } + + Err(error) => + { + error!("{}", error); + } + } + + // Write a log message that there was an err compiling this + // Section's resource. + warn!("{} exited with code {} while compiling {}.\n{}\n{}", + section.compiler, exit_code, section.name, out, err); + } + } + + Err(error) => + { + error!("{}", error); + } + } + } + + /// Create an array of string arguments to pass + /// to a compiler based on the given Section. + fn build_args(&self, section: &Section) -> Vec + { + let mut arg: String; + let mut args: Vec; + + // Create a new array of determined arguments. + args = Vec::new(); + + // First add the input argument. This is a mason compiler must have. + arg = String::new(); + arg.push_str("-i"); + arg.push_str(" "); + arg.push_str(section.src_path.as_str()); + args.push(arg); + + // Next add the output argument. This is a mason compiler must have. + arg = String::new(); + arg.push_str("-o"); + arg.push_str(" "); + arg.push_str(section.dst_path.as_str()); + args.push(arg); + + // Parse all the arguments to pass to the compiler. + arg = section.arguments.clone(); + args.push(arg); + + args } }