rlbuild/src/environ/old.rs
2025-03-19 17:29:08 -05:00

325 lines
11 KiB
Rust

use std::{
fs::{self, File},
io::Write,
};
use rlisp_library::{
Environ,
Expr::{self, Bool},
RispError, default_env, parse_eval,
};
pub
fn extend_environ<'a>(mut env: Environ<'a>) -> Environ<'a> {
env.data.insert(
"quit".to_string(),
Expr::Func(|_args: &[Expr]| -> Result<Expr, RispError> {
// TODO: let this function take in arguments
println!("Exiting.");
std::process::exit(0);
#[allow(unreachable_code)]
Err(RispError::Reason("Cannot exit process".to_string()))
}),
);
env.data.insert(
"print".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let mut stri = String::new();
for arg in args {
let fmted = match arg {
Expr::Str(string) => {
let string = string.clone();
// string.pop();
// string.remove(0);
format!("{}", string)
}
_ => {
format!("{}", arg)
}
};
stri.push_str(&fmted);
if args.len() > 1 {
stri.push_str(" ")
}
}
print!("{}", stri);
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"println".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let mut stri = String::new();
for arg in args {
let fmted = match arg {
Expr::Str(string) => {
let string = string.clone();
// string.pop();
// string.remove(0);
format!("{}", string)
}
_ => {
format!("{}", arg)
}
};
stri.push_str(&fmted);
if args.len() > 1 {
stri.push_str(" ")
}
}
println!("{}", stri);
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"use-repo".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let repo_name = &args[0].clone();
let mut repo_name_str = repo_name.to_string();
repo_name_str.remove(0);
let repo_url = &args[1].clone();
let use_sources = &args[2].clone();
let msg = match use_sources {
Bool(b) => match b {
true => "build_from_src = true",
false => "build_from_src = false",
},
_ => {
panic!("AHHH");
}
};
let path = format!("out/system/repos");
fs::create_dir_all(path).unwrap();
let path = format!("out/system/repos/{}.repo", repo_name_str);
println!("repo name {} repo url {} {}", repo_name, repo_url, msg);
let mut file = File::create(path).unwrap();
let msg = format!("url = {}\n{}", repo_url, msg);
let _ = file.write(msg.as_bytes());
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"bootloader-install".to_string(),
Expr::Func(|_args: &[Expr]| -> Result<Expr, RispError> {
// let loader_name = &args[1].clone();
// let mut loader_name_str = loader_name.to_string();
// loader_name_str.remove(0);
let path = format!("out/boot/limine");
fs::create_dir_all(path).unwrap();
// let path = format!("out/boot/limine/config.rl");
// let mut file = File::create(path).unwrap();
// let _ = file.write_all(b"()");
let path = format!("out/boot/limine/limine.conf");
let mut file = File::create(path).unwrap();
let _ = file.write_all(b"");
let path = format!("out/boot/limine/BOOTX64.EFI");
let mut x64_file = File::create(&path).unwrap();
std::fs::copy("limine/BOOTX64.EFI", &path).unwrap(); // Copy foo.txt to bar.txt
let path = format!("out/boot/limine/BOOTAA64.EFI");
let mut x64_file = File::create(&path).unwrap();
std::fs::copy("limine/BOOTAA64.EFI", &path).unwrap(); // Copy foo.txt to bar.txt
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"pkg-install".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let repo_name = &args[0].clone();
let pkg_name = &args[1].clone();
let mut pkg_name_str = pkg_name.to_string();
pkg_name_str.remove(0);
println!(
"installing package {} from repo {}",
pkg_name_str, repo_name
);
let path = format!("out/programs/{}", pkg_name_str);
fs::create_dir_all(path).unwrap();
let path = format!("out/programs/{}/src", pkg_name_str);
fs::create_dir_all(path).unwrap();
// If package is from :src repo, copy source files
if repo_name.to_string() == ":src" {
// Try original name first
let src_path = format!("sysdata/programs/{}", pkg_name_str);
println!("Checking source path: {}", src_path);
// If not found, try with dashes converted to underscores
let underscored_name = pkg_name_str.replace('-', "_");
let src_path_alt = format!("sysdata/programs/{}", underscored_name);
let (found_path, found_name) = if fs::metadata(&src_path).is_ok() {
(src_path, pkg_name_str.clone())
} else if fs::metadata(&src_path_alt).is_ok() {
println!("Found source directory with underscored name at {}", src_path_alt);
(src_path_alt, underscored_name)
} else {
return Err(RispError::Reason(format!(
"Source directory not found for package {} at {} or {}",
pkg_name_str, src_path, src_path_alt
)));
};
println!("Found source directory at {}", found_path);
// Copy all files from src directory
for entry in fs::read_dir(&found_path).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let file_name = path.file_name().unwrap().to_str().unwrap();
let dest_path = format!("out/programs/{}/{}", pkg_name_str, file_name);
println!("Copying {} to {}", path.display(), dest_path);
fs::copy(&path, &dest_path).unwrap();
}
}
// Copy contents of src directory if it exists
let src_src_path = format!("sysdata/programs/{}/src", found_name);
println!("Checking src directory at {}", src_src_path);
if fs::metadata(&src_src_path).is_ok() {
println!("Found src directory at {}", src_src_path);
for entry in fs::read_dir(&src_src_path).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let file_name = path.file_name().unwrap().to_str().unwrap();
let dest_path = format!("out/programs/{}/src/{}", pkg_name_str, file_name);
println!("Copying {} to {}", path.display(), dest_path);
fs::copy(&path, &dest_path).unwrap();
}
}
}
// Check if meta.rl exists in source, if not generate one
let meta_src_path = format!("sysdata/programs/{}/meta.rl", found_name);
let meta_dest_path = format!("out/programs/{}/meta.rl", pkg_name_str);
if !fs::metadata(&meta_src_path).is_ok() {
println!("Generating meta.rl for {}", pkg_name_str);
let mut file = File::create(&meta_dest_path).unwrap();
let meta_content = format!(
"(package \"{}\"\n (version 1 0 0)\n :authors (\"\")\n :tags ()\n (deps))\n",
pkg_name_str
);
file.write_all(meta_content.as_bytes()).unwrap();
}
}
let path = format!("out/programs/{}/app.axe", pkg_name_str);
let mut file = File::create(path).unwrap();
let _ = file.write_all(b"");
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"pkg-configure".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let pkg_name = &args[0].clone();
let mut pkg_name_str = pkg_name.to_string();
pkg_name_str.remove(0);
println!("installing package {}.", pkg_name_str);
let path = format!("out/programs/{}/config.rl", pkg_name_str);
println!("configuring package {}", pkg_name_str);
// TODO: build the code with the hblang compiler.
// TODO: use the meta.rli to map dependencies.
let mut file = File::create(path).unwrap();
let _ = file.write_all(b"()");
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"drivers".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let mut stri = String::new();
for arg in args {
let fmted = match arg {
Expr::Str(string) => {
let string = string.clone();
format!("{}", string)
}
_ => {
format!("{}", arg)
}
};
stri.push_str(&fmted);
if args.len() > 1 {
stri.push_str(" ")
}
}
println!("Drivers {}", stri);
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"services".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let mut stri = String::new();
for arg in args {
let fmted = match arg {
Expr::Str(string) => {
let string = string.clone();
format!("{}", string)
}
_ => {
format!("{}", arg)
}
};
stri.push_str(&fmted);
if args.len() > 1 {
stri.push_str(" ")
}
}
println!("Services {}", stri);
Ok(Expr::Bool(true))
}),
);
env.data.insert(
"reincarnation-server".to_string(),
Expr::Func(|args: &[Expr]| -> Result<Expr, RispError> {
let reinc = &args[0].clone();
println!("Reincarnation Server {}", reinc);
Ok(Expr::Bool(true))
}),
);
env
}