finish most work

This commit is contained in:
2026-03-22 18:04:40 -05:00
parent b9b35f3f0f
commit 40685b7c4d
2 changed files with 86 additions and 6 deletions

View File

@@ -1,8 +1,11 @@
use std::{
env::set_current_dir,
path::{Path, PathBuf},
process::Command,
};
use glob::glob;
const MAIN_C: &str = include_str!("templates/main.c");
#[derive(clap::Parser)]
@@ -24,6 +27,8 @@ enum Subcommand {
Run {
/// The build mode to use
mode: Option<String>,
/// Arguments to pass to the project binary
args: Option<Vec<String>>,
},
/// Build the local project
Build {
@@ -49,8 +54,30 @@ impl App {
std::process::exit(1);
}
},
Subcommand::Run { mode } => todo!(),
Subcommand::Build { mode } => todo!(),
Subcommand::Run { mode, args } => {
match build(&mode) {
Ok(_) => {
println!("Successfully built project.");
}
Err(e) => {
eprintln!("Error building project: {e}");
std::process::exit(1);
}
};
if let Err(e) = run(&mode, args) {
eprintln!("Error running project: {e}");
std::process::exit(1);
}
}
Subcommand::Build { mode } => match build(&mode) {
Ok(_) => {
println!("Successfully built project.");
}
Err(e) => {
eprintln!("Error building project: {e}");
std::process::exit(1);
}
},
}
}
}
@@ -94,7 +121,7 @@ fn get_config() -> Option<crate::config::Config> {
toml::from_str(&raw).ok()
}
fn build(mode: Option<String>) -> std::io::Result<()> {
fn build(mode: &Option<String>) -> std::io::Result<()> {
let conf = match get_config() {
Some(conf) => conf,
None => {
@@ -108,5 +135,58 @@ fn build(mode: Option<String>) -> std::io::Result<()> {
"build layout not found",
))?;
todo!()
std::fs::create_dir_all(format!("target/{}", build_config.name))?;
let mut command = Command::new("gcc");
for arg in &build_config.args {
command.arg(arg);
}
for entry in glob("src/*.c")
.map_err(|e| std::io::Error::new(std::io::ErrorKind::NotFound, format!("{e}").as_str()))?
{
if let Ok(path) = entry {
command.arg(path.to_string_lossy().to_string());
}
}
command
.arg("-o")
.arg(format!("target/{}/{}", build_config.name, conf.name));
let mut child = command.spawn()?;
child.wait()?;
Ok(())
}
fn run(mode: &Option<String>, args: Option<Vec<String>>) -> std::io::Result<()> {
let conf = match get_config() {
Some(conf) => conf,
None => {
eprintln!("Error opening config file. No Pallet.toml in current directory.");
std::process::exit(1);
}
};
let build_config = conf.get_or_default(mode).ok_or(std::io::Error::new(
std::io::ErrorKind::NotFound,
"build layout not found",
))?;
let mut command = Command::new(format!("target/{}/{}", build_config.name, conf.name));
if let Some(args) = args {
for arg in args {
command.arg(arg);
}
}
let mut child = command.spawn()?;
child.wait()?;
Ok(())
}

View File

@@ -17,9 +17,9 @@ impl Config {
}
}
pub fn get_or_default(&self, mode: Option<String>) -> Option<&BuildConf> {
pub fn get_or_default(&self, mode: &Option<String>) -> Option<&BuildConf> {
if let Some(mode) = mode {
self.build.iter().find(|bc| bc.name == mode)
self.build.iter().find(|bc| bc.name == *mode)
} else {
self.build.iter().find(|bc| bc.name == self.default_build)
}