diff --git a/Cargo.lock b/Cargo.lock index ab96143..8e2dc70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,6 +176,7 @@ dependencies = [ "arg", "serde", "serde_json", + "struct_iterable", "toml", "zip", ] @@ -240,6 +241,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "erased-serde" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c138974f9d5e7fe373eb04df7cae98833802ae4b11c24ac7039a21d5af4b26c" +dependencies = [ + "serde", +] + [[package]] name = "flate2" version = "1.0.28" @@ -507,6 +517,35 @@ dependencies = [ "digest", ] +[[package]] +name = "struct_iterable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "849a064c6470a650b72e41fa6c057879b68f804d113af92900f27574828e7712" +dependencies = [ + "struct_iterable_derive", + "struct_iterable_internal", +] + +[[package]] +name = "struct_iterable_derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bb939ce88a43ea4e9d012f2f6b4cc789deb2db9d47bad697952a85d6978662c" +dependencies = [ + "erased-serde", + "proc-macro2", + "quote", + "struct_iterable_internal", + "syn 2.0.52", +] + +[[package]] +name = "struct_iterable_internal" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9426b2a0c03e6cc2ea8dbc0168dbbf943f88755e409fb91bcb8f6a268305f4a" + [[package]] name = "subtle" version = "2.5.0" diff --git a/Cargo.toml b/Cargo.toml index 614c004..b4dd2e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ PrintLib = "1.5.1" arg = "0.4.1" serde = { version = "1.0.197", features=["derive"] } serde_json = "1.0.114" +struct_iterable = "0.1.1" toml = "0.8.10" zip = "0.6.6" diff --git a/src/conf.rs b/src/conf.rs index 7995fff..432286e 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,12 +1,20 @@ use serde::{Serialize, Deserialize}; -use std::{fs, fs::File, io::Read, path::Path}; +use std::collections::HashMap; +use std::fs; use std::process::exit; +use struct_iterable::Iterable; use toml; use crate::print; +#[derive(Serialize, Deserialize)] +pub struct TemplateData { + +} + #[derive(Serialize, Deserialize)] pub struct Data { pub package: Package, + pub dependencies: Dependencies, } #[derive(Serialize, Deserialize)] @@ -17,19 +25,23 @@ pub struct Package { pub description: String, } -pub fn load_tml_cfg(path: &str) -> Data { +#[derive(Iterable, Serialize, Deserialize)] +pub struct Dependencies { +} + +pub fn get_value(path: &str) -> toml::Value { let contents = match fs::read_to_string(path) { Ok(c) => {c} - Err(_) => { - print::error("E005", "couldn't read config file"); + Err(e) => { + print::error("E", &format!("couldn't read config file: {}", e.to_string())); exit(1); } }; - let data: Data = match toml::from_str(&contents) { + let data: toml::Value = match toml::from_str(&contents) { Ok(d) => d, Err(e) => { - print::error("E005", &format!("couldn't load toml file:\n{}", e)); + print::error("E", &format!("couldn't load toml file:\n{}", e)); exit(1); } }; @@ -37,25 +49,35 @@ pub fn load_tml_cfg(path: &str) -> Data { data } -pub fn read_file(path: &str) -> std::io::Result { - let path = Path::new(&path); - - let mut file = match File::open(path) { - Ok(f) => f, +pub fn load_tml_cfg(path: &str) -> Data { + let contents = match fs::read_to_string(path) { + Ok(c) => {c} Err(e) => { - print::error("E", &format!("error while opening conf file: {}", e)); - return Ok(String::new()); - }, + print::error("E", &format!("couldn't read config file: {}", e.to_string())); + exit(1); + } }; - let mut buf: String = String::new(); - match file.read_to_string(&mut buf) { - Ok(_) => {}, + let data: Data = match toml::from_str(&contents) { + Ok(d) => d, Err(e) => { - print::error("E", &format!("error while reading conf file: {}", e)); - return Ok(String::new()); - }, + print::error("E", &format!("couldn't load toml file:\n{}", e)); + exit(1); + } }; - Ok(buf) + data +} + +pub fn parse_dependencys(path: &str) -> HashMap { + let value = get_value(path); + + let keys_table: &toml::map::Map = value.get("dependencies").unwrap().as_table().unwrap(); + let mut keys = std::collections::HashMap::new(); + + for (key, value) in keys_table.iter() { + keys.insert(key.clone(), value.as_str().unwrap().to_string()); + } + + keys } \ No newline at end of file diff --git a/src/runner/build.rs b/src/runner/build.rs index a0bf5c0..b0f5b32 100644 --- a/src/runner/build.rs +++ b/src/runner/build.rs @@ -1,4 +1,4 @@ -use crate::{conf::{self}, print}; +use crate::{conf::{self, parse_dependencys}, print}; use std::{fs, process::Command}; use PrintLib::colorize::Colorize; @@ -42,6 +42,12 @@ pub fn build(target: &str) -> Result { return Ok(false); } + //print dependencies + let deps = parse_dependencys("cpack.toml"); + for (name, version) in deps { + println!(" - {}: {} {}", "Dependency".bold(), name, version); + } + // compile every file for file in src_dir { let file = file?; diff --git a/src/runner/new.rs b/src/runner/new.rs index 3a82829..65f7e36 100644 --- a/src/runner/new.rs +++ b/src/runner/new.rs @@ -1,4 +1,4 @@ -use crate::{print, conf::{self}}; +use crate::{conf::{self, Dependencies, Package}, print}; use std::{env, fs, io::{self, Write}, path::Path}; use PrintLib::colorize::Colorize; @@ -59,15 +59,31 @@ pub fn new(name: &str, libary: bool, template: &str) -> std::io::Result<()>{ fs::rename(template, name)?; // rewrite config - let path_str = format!("{}/cpack.toml", name); - let path = Path::new(&path_str); + let template_deps = conf::parse_dependencys(&format!("{}/template.toml", name)); + + let data = conf::Data { + package: Package { + name: name.into(), + version: "1.0.0".into(), + author: "your_name".into(), + description: format!("{}s epic description", name), + }, + dependencies: Dependencies { }, + }; - let mut buf = conf::read_file(&path_str)?; - buf = buf.replace("{name}", name); + let mut toml_string = match toml::to_string(&data) { + Ok(s) => s, + Err(e) => { + print::error("E", &format!("error while converting example conf to string: {}", e.to_string())); + String::new() + }, + }; - // rewriting file + for (name, version) in template_deps { + toml_string.push_str(&format!("{} = {}", name, version)); + } - let mut file = match fs::File::open(path) { + let mut file = match fs::File::create(Path::new(&format!("{}/cpack.toml", name))) { Ok(f) => f, Err(e) => { print::error("E", &format!("error while opening conf file: {}", e)); @@ -75,14 +91,22 @@ pub fn new(name: &str, libary: bool, template: &str) -> std::io::Result<()>{ }, }; - match file.write(buf.as_bytes()) { + match file.write(toml_string.as_bytes()) { Ok(_) => {}, Err(e) => { print::error("E", &format!("error while writing conf file: {}", e)); return Ok(()); }, }; - file.flush()?; + + // remove template.toml + match fs::remove_file(Path::new(&format!("{}/template.toml", name))) { + Ok(_) => {}, + Err(e) => { + print::error("E", &format!("error while removing template conf file: {}", e)); + return Ok(()); + }, + }; println!(" - {} {}: '{name}'", "Created".color(0, 42, 71).bold(), match libary { true => "libary", false => "package" } ); diff --git a/templates/std.zip b/templates/std.zip index 67f1313..c8ddf9b 100644 Binary files a/templates/std.zip and b/templates/std.zip differ diff --git a/templates/std/cpack.toml b/templates/std/cpack.toml deleted file mode 100644 index 308352b..0000000 --- a/templates/std/cpack.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -name = "{name}" -version = "1.0" -description = "{name}'s epic description" -author = "" \ No newline at end of file diff --git a/templates/std/template.toml b/templates/std/template.toml new file mode 100644 index 0000000..05ba453 --- /dev/null +++ b/templates/std/template.toml @@ -0,0 +1 @@ +[dependencies] \ No newline at end of file diff --git a/templates/std_lib.zip b/templates/std_lib.zip index a60020e..ffae972 100644 Binary files a/templates/std_lib.zip and b/templates/std_lib.zip differ diff --git a/templates/std_lib/cpack.toml b/templates/std_lib/cpack.toml deleted file mode 100644 index 308352b..0000000 --- a/templates/std_lib/cpack.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -name = "{name}" -version = "1.0" -description = "{name}'s epic description" -author = "" \ No newline at end of file diff --git a/templates/std_lib/template.toml b/templates/std_lib/template.toml new file mode 100644 index 0000000..05ba453 --- /dev/null +++ b/templates/std_lib/template.toml @@ -0,0 +1 @@ +[dependencies] \ No newline at end of file