From 40685b7c4de840e706cdfe2e26a167f5e81c9e79 Mon Sep 17 00:00:00 2001 From: Shea Frembling Date: Sun, 22 Mar 2026 18:04:40 -0500 Subject: [PATCH] finish most work --- src/app.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/config.rs | 4 +-- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/app.rs b/src/app.rs index 25e7227..73fed55 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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, + /// Arguments to pass to the project binary + args: Option>, }, /// 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 { toml::from_str(&raw).ok() } -fn build(mode: Option) -> std::io::Result<()> { +fn build(mode: &Option) -> std::io::Result<()> { let conf = match get_config() { Some(conf) => conf, None => { @@ -108,5 +135,58 @@ fn build(mode: Option) -> 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, args: Option>) -> 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(()) } diff --git a/src/config.rs b/src/config.rs index b0534fc..a865366 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,9 +17,9 @@ impl Config { } } - pub fn get_or_default(&self, mode: Option) -> Option<&BuildConf> { + pub fn get_or_default(&self, mode: &Option) -> 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) }