Skip to content

Commit

Permalink
working templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Mar 9, 2024
1 parent 476498c commit 5d5dd5b
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 41 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
64 changes: 43 additions & 21 deletions src/conf.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -17,45 +25,59 @@ 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);
}
};

data
}

pub fn read_file(path: &str) -> std::io::Result<String> {
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<String, String> {
let value = get_value(path);

let keys_table: &toml::map::Map<String, toml::Value> = 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
}
8 changes: 7 additions & 1 deletion src/runner/build.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -42,6 +42,12 @@ pub fn build(target: &str) -> Result<bool, std::io::Error> {
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?;
Expand Down
42 changes: 33 additions & 9 deletions src/runner/new.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -59,30 +59,54 @@ 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));
return Ok(());
},
};

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" } );

Expand Down
Binary file modified templates/std.zip
Binary file not shown.
5 changes: 0 additions & 5 deletions templates/std/cpack.toml

This file was deleted.

1 change: 1 addition & 0 deletions templates/std/template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[dependencies]
Binary file modified templates/std_lib.zip
Binary file not shown.
5 changes: 0 additions & 5 deletions templates/std_lib/cpack.toml

This file was deleted.

1 change: 1 addition & 0 deletions templates/std_lib/template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[dependencies]

0 comments on commit 5d5dd5b

Please sign in to comment.