Most of the refactor. Need to switch machines.
This commit is contained in:
80
crates/java/src/compiler.rs
Normal file
80
crates/java/src/compiler.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::JAVA_BIN_COMPILER;
|
||||
use crate::Result;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct CompilerBuilder {
|
||||
class_path: Option<PathBuf>,
|
||||
destination: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl CompilerBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn class_path<S: AsRef<Path>>(&mut self, class_path: S) -> &mut Self {
|
||||
self.class_path = Some(class_path.as_ref().to_path_buf());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn destination<S: AsRef<Path>>(&mut self, destination: S) -> &mut Self {
|
||||
self.destination = Some(destination.as_ref().to_path_buf());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(&self) -> Compiler {
|
||||
let mut flags = vec![];
|
||||
|
||||
if let Option::Some(path) = self.destination.to_owned() {
|
||||
flags.push(CompilerFlag::Destination { path });
|
||||
}
|
||||
|
||||
if let Option::Some(path) = self.class_path.to_owned() {
|
||||
flags.push(CompilerFlag::Classpath { path });
|
||||
}
|
||||
|
||||
Compiler { flags }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Compiler {
|
||||
flags: Vec<CompilerFlag>,
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
pub fn compile<P: AsRef<Path>>(self, path: P) -> Result<(Option<String>, Option<String>)> {
|
||||
let mut cmd: Vec<String> = vec![JAVA_BIN_COMPILER.to_string()];
|
||||
|
||||
cmd.extend(
|
||||
self.flags
|
||||
.clone()
|
||||
.into_iter()
|
||||
.flat_map(|f| Into::<Vec<String>>::into(f)),
|
||||
);
|
||||
cmd.extend(
|
||||
fs::expand_files(path)?
|
||||
.into_iter()
|
||||
.filter_map(|f| Some(f.to_str()?.to_string())),
|
||||
);
|
||||
|
||||
Ok(io::run_process(cmd.as_slice())?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum CompilerFlag {
|
||||
Classpath { path: PathBuf },
|
||||
Destination { path: PathBuf },
|
||||
}
|
||||
|
||||
impl Into<Vec<String>> for CompilerFlag {
|
||||
fn into(self) -> Vec<String> {
|
||||
match self {
|
||||
Self::Classpath { path } => vec!["-classpath".to_string(), path.display().to_string()],
|
||||
Self::Destination { path } => vec!["-d".to_string(), path.display().to_string()],
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user