Skip to content

Commit

Permalink
v0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Feb 14, 2024
1 parent e8a329b commit 8c4ef3d
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .phil-project
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
logo:assets/logo.png
logo_small:assets/logo.png
description_translate:de
version:0.4.0
version:0.4.1
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

Expand Down
12 changes: 2 additions & 10 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.");
Expand Down
99 changes: 91 additions & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand All @@ -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;
Expand Down

0 comments on commit 8c4ef3d

Please sign in to comment.