2024-09-13 16:41:31 -05:00
|
|
|
#![allow(unused)]
|
2024-05-31 10:07:22 -05:00
|
|
|
use std::{
|
2024-05-31 13:31:06 -05:00
|
|
|
fmt::format,
|
2024-05-31 10:07:22 -05:00
|
|
|
fs::{read_to_string, File},
|
|
|
|
io::{BufWriter, Write},
|
|
|
|
process::exit,
|
|
|
|
};
|
|
|
|
|
2024-07-07 08:35:07 -05:00
|
|
|
use {error_stack::Report, hblang::Options};
|
2024-05-31 10:07:22 -05:00
|
|
|
|
|
|
|
use crate::Error;
|
2024-05-31 09:11:45 -05:00
|
|
|
pub struct Package {
|
2024-05-31 10:07:22 -05:00
|
|
|
name: String,
|
|
|
|
binaries: Vec<String>,
|
|
|
|
build_cmd: String,
|
2024-05-31 09:11:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Package {
|
|
|
|
pub fn load_from_file(path: String) -> Self {
|
|
|
|
let contents = match std::fs::read_to_string(path.clone()) {
|
|
|
|
// If successful return the files text as `contents`.
|
|
|
|
// `c` is a local variable.
|
|
|
|
Ok(c) => c,
|
|
|
|
// Handle the `error` case.
|
|
|
|
Err(_) => {
|
|
|
|
// Write `msg` to `stderr`.
|
|
|
|
eprintln!("Could not read file `{}`", path);
|
|
|
|
// Exit the program with exit code `1`.
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
let mut data: Value = toml::from_str(&contents).unwrap();
|
2024-05-31 10:07:22 -05:00
|
|
|
let mut name = data
|
2024-05-31 09:11:45 -05:00
|
|
|
.get("package")
|
|
|
|
.unwrap()
|
|
|
|
.get("name")
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
2024-05-31 10:07:22 -05:00
|
|
|
name.pop();
|
|
|
|
name.remove(0);
|
|
|
|
|
|
|
|
let dependants = data.get("dependants").unwrap();
|
|
|
|
let bin_table = dependants.get("binaries").unwrap().as_table().unwrap();
|
|
|
|
let mut binaries = vec![];
|
2024-05-31 09:11:45 -05:00
|
|
|
|
2024-05-31 10:07:22 -05:00
|
|
|
for (count, (name, table)) in bin_table.into_iter().enumerate() {
|
|
|
|
// if count != 0 {
|
|
|
|
binaries.push(name.clone());
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
let build_table = data.get("build").unwrap();
|
|
|
|
|
|
|
|
let mut build_cmd: String = build_table.get("command").unwrap().as_str().unwrap().into();
|
|
|
|
build_cmd.remove(0);
|
|
|
|
// build_cmd.pop();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
name,
|
|
|
|
binaries,
|
|
|
|
build_cmd,
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 08:04:10 -06:00
|
|
|
pub fn build(&self, out: &mut Vec<u8>) -> std::io::Result<()> {
|
2024-05-31 10:07:22 -05:00
|
|
|
if self.binaries.contains(&"hblang".to_string()) {
|
2024-07-06 09:24:23 -05:00
|
|
|
let file = self.build_cmd.split_ascii_whitespace().last().unwrap();
|
|
|
|
|
|
|
|
let path = format!("sysdata/programs/{}/{}", self.name, file);
|
|
|
|
// compile here
|
2024-05-31 10:07:22 -05:00
|
|
|
|
2024-10-25 10:37:38 -05:00
|
|
|
hblang::run_compiler(
|
2024-07-19 07:31:01 -05:00
|
|
|
&path,
|
|
|
|
Options {
|
|
|
|
fmt: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2024-11-08 08:04:10 -06:00
|
|
|
out,
|
2024-10-25 10:37:38 -05:00
|
|
|
)?;
|
|
|
|
|
2024-11-08 08:04:10 -06:00
|
|
|
match std::fs::create_dir("target/programs") {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => (),
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
}
|
|
|
|
|
2024-10-25 10:37:38 -05:00
|
|
|
hblang::run_compiler(
|
|
|
|
&path,
|
|
|
|
Options {
|
|
|
|
..Default::default()
|
|
|
|
},
|
2024-11-08 08:04:10 -06:00
|
|
|
out,
|
2024-10-25 10:37:38 -05:00
|
|
|
)?;
|
2024-11-08 08:04:10 -06:00
|
|
|
std::fs::write(format!("target/programs/{}.hbf", self.name), &out)?;
|
|
|
|
out.clear();
|
2024-07-19 05:47:59 -05:00
|
|
|
|
2024-10-25 10:37:38 -05:00
|
|
|
hblang::run_compiler(
|
2024-09-04 12:14:30 -05:00
|
|
|
&path,
|
|
|
|
Options {
|
|
|
|
dump_asm: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2024-11-08 08:04:10 -06:00
|
|
|
out,
|
2024-10-25 10:37:38 -05:00
|
|
|
)?;
|
2024-11-08 08:04:10 -06:00
|
|
|
std::fs::write(format!("target/programs/{}.hba", self.name), &out)?;
|
|
|
|
out.clear();
|
2024-05-31 10:07:22 -05:00
|
|
|
}
|
2024-10-25 10:37:38 -05:00
|
|
|
Ok(())
|
2024-05-31 09:11:45 -05:00
|
|
|
}
|
|
|
|
}
|