From 8c4ef3d42dffec11b2e119ea4b0e54ba6ac91e13 Mon Sep 17 00:00:00 2001 From: cophilot <philxsb@gmail.com> Date: Wed, 14 Feb 2024 08:32:15 +0100 Subject: [PATCH] v0.4.1 --- .phil-project | 2 +- CHANGELOG.md | 7 ++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 12 ++---- src/commands.rs | 12 +----- src/utils.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++---- 7 files changed, 107 insertions(+), 29 deletions(-) diff --git a/.phil-project b/.phil-project index 64ea341..7d669f8 100644 --- a/.phil-project +++ b/.phil-project @@ -1,4 +1,4 @@ logo:assets/logo.png logo_small:assets/logo.png description_translate:de -version:0.4.0 +version:0.4.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 742aafc..409cf51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ --- +## [v0.4.1](https://github.com/cophilot/templify/tree/0.4.1) (2024-2-14) + +- Refactoring +- `.source` attribute will be set in the `.templify` file when a template is loaded + +--- + ## [v0.4.0](https://github.com/cophilot/templify/tree/0.4.0) (2024-2-12) - `.templify` file is now optional diff --git a/Cargo.lock b/Cargo.lock index b2c99ba..50e33a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -845,7 +845,7 @@ dependencies = [ [[package]] name = "templify" -version = "0.4.0" +version = "0.4.1" dependencies = [ "chrono", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index dd82925..7ddd77c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "templify" -version = "0.4.0" +version = "0.4.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 252ef82..d174122 100644 --- a/README.md +++ b/README.md @@ -213,14 +213,10 @@ tpy load https://github.com/cophilot/templify-vault/tree/main/React-ts ## [Release Notes](https://github.com/cophilot/templify/blob/master/CHANGELOG.md) -### [v0.4.0](https://github.com/cophilot/templify/tree/0.4.0) - -- `.templify` file is now optional -- Added `-blank` flag for the `init` command -- Added placeholder `$$year$$` -- Added placeholder `$$month$$` -- Added placeholder `$$day$$` -- Added placeholder `$$git-name$$` +### [v0.4.1](https://github.com/cophilot/templify/tree/0.4.1) + +- Refactoring +- `.source` attribute will be set in the `.templify` file when a template is loaded --- diff --git a/src/commands.rs b/src/commands.rs index 27d5524..215ccdf 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -46,20 +46,13 @@ pub fn load(command: &Command) -> Status { let url = command.get_argument("url").value.clone(); if !url.starts_with("https://github.com") { - println!("Could not load template: {}", url); - println!("Only github templates are supported at the moment."); return Status::error(format!( "Invalid url: {}\nOnly github templates are supported at the moment.", url )); } println!("Loading template from {}...", url); - utils::load_remote_template_dir( - ".templates", - url.as_str(), - command.get_bool_flag("force"), - true, - ); + utils::load_remote_template_repo(".templates", url.as_str(), command.get_bool_flag("force")); return Status::ok(); } @@ -228,11 +221,10 @@ pub fn init(command: &Command) -> Status { // check if there is an internet connection if utils::check_internet_connection() && !command.get_bool_flag("offline") { println!("Loading example template from templify-vault..."); - utils::load_remote_template_dir( + utils::load_remote_template_repo( ".templates", "https://github.com/cophilot/templify-vault/tree/main/Example", true, - true, ); } println!("templify initialized successfully."); diff --git a/src/utils.rs b/src/utils.rs index d1be59c..1bb3c81 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -49,13 +49,36 @@ pub fn parse_templify_file(file_path: &str) -> std::collections::HashMap<String, return map; } -pub fn load_remote_template_dir(path: &str, url: &str, force: bool, first: bool) { - if !first && !force { - if Path::new(path).exists() { - println!("Directory {} already exists...", path); - return; +pub fn load_remote_template_repo(path: &str, url: &str, force: bool) { + let response = reqwest::blocking::get(url).unwrap(); + let response: serde_json::Value = response.json().unwrap(); + let items = response["payload"]["tree"]["items"].as_array().unwrap(); + + for item in items { + if item["contentType"] == "directory" { + load_remote_template( + format!("{}/{}", path, item["name"]) + .replace("\"", "") + .as_str(), + format!("{}/{}", url, item["name"]) + .replace("\"", "") + .as_str(), + force, + ); } + } +} +fn load_remote_template(path: &str, url: &str, force: bool) { + if !force && Path::new(path).exists() { + println!( + "Template {} already exists...", + path.replace(".templates/", "") + ); + return; + } + + if !Path::new(path).exists() { std::fs::create_dir(path).unwrap(); } @@ -73,12 +96,72 @@ pub fn load_remote_template_dir(path: &str, url: &str, force: bool, first: bool) .replace("\"", "") .as_str(), force, - false, ); continue; } - if first { + load_remote_template_file( + format!("{}/{}", path, item["name"]) + .replace("\"", "") + .as_str(), + format!("{}/{}", url, item["name"]) + .replace("\"", "") + .as_str(), + force, + ); + } + + let temp_file = format!("{}/.templify", path); + + if !Path::new(temp_file.as_str()).exists() { + // create .templify file + std::fs::File::create(temp_file).unwrap(); + } + + // write to .templify file + let mut file = std::fs::OpenOptions::new() + .write(true) + .append(true) + .open(format!("{}/.templify", path).as_str()) + .unwrap(); + + // check if url already exists in .templify file + let file_content = std::fs::read_to_string(format!("{}/.templify", path).as_str()); + if file_content.is_err() { + return; + } + let file_content = file_content.unwrap(); + if !file_content.contains(".source") { + file.write_all(format!("\n\n.source:{}", url).as_bytes()) + .unwrap(); + } + + println!("Loaded template: {}", path.replace(".templates/", "")); +} + +fn load_remote_template_dir(path: &str, url: &str, force: bool) { + if !force && Path::new(path).exists() { + println!("Directory {} already exists...", path); + return; + } + + std::fs::create_dir(path).unwrap(); + + let response = reqwest::blocking::get(url).unwrap(); + let response: serde_json::Value = response.json().unwrap(); + let items = response["payload"]["tree"]["items"].as_array().unwrap(); + + for item in items { + if item["contentType"] == "directory" { + load_remote_template_dir( + format!("{}/{}", path, item["name"]) + .replace("\"", "") + .as_str(), + format!("{}/{}", url, item["name"]) + .replace("\"", "") + .as_str(), + force, + ); continue; } @@ -94,7 +177,7 @@ pub fn load_remote_template_dir(path: &str, url: &str, force: bool, first: bool) } } -pub fn load_remote_template_file(path: &str, url: &str, force: bool) { +fn load_remote_template_file(path: &str, url: &str, force: bool) { if Path::new(path).exists() && !force { println!("File {} already exists.", path); return;