finish most work
This commit is contained in:
88
src/app.rs
88
src/app.rs
@@ -1,8 +1,11 @@
|
|||||||
use std::{
|
use std::{
|
||||||
env::set_current_dir,
|
env::set_current_dir,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
process::Command,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use glob::glob;
|
||||||
|
|
||||||
const MAIN_C: &str = include_str!("templates/main.c");
|
const MAIN_C: &str = include_str!("templates/main.c");
|
||||||
|
|
||||||
#[derive(clap::Parser)]
|
#[derive(clap::Parser)]
|
||||||
@@ -24,6 +27,8 @@ enum Subcommand {
|
|||||||
Run {
|
Run {
|
||||||
/// The build mode to use
|
/// The build mode to use
|
||||||
mode: Option<String>,
|
mode: Option<String>,
|
||||||
|
/// Arguments to pass to the project binary
|
||||||
|
args: Option<Vec<String>>,
|
||||||
},
|
},
|
||||||
/// Build the local project
|
/// Build the local project
|
||||||
Build {
|
Build {
|
||||||
@@ -49,8 +54,30 @@ impl App {
|
|||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Subcommand::Run { mode } => todo!(),
|
Subcommand::Run { mode, args } => {
|
||||||
Subcommand::Build { mode } => todo!(),
|
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()
|
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() {
|
let conf = match get_config() {
|
||||||
Some(conf) => conf,
|
Some(conf) => conf,
|
||||||
None => {
|
None => {
|
||||||
@@ -108,5 +135,58 @@ fn build(mode: Option<String>) -> std::io::Result<()> {
|
|||||||
"build layout not found",
|
"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(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
if let Some(mode) = mode {
|
||||||
self.build.iter().find(|bc| bc.name == mode)
|
self.build.iter().find(|bc| bc.name == *mode)
|
||||||
} else {
|
} else {
|
||||||
self.build.iter().find(|bc| bc.name == self.default_build)
|
self.build.iter().find(|bc| bc.name == self.default_build)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user