finish most work
This commit is contained in:
88
src/app.rs
88
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<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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user