Skip to content

Commit

Permalink
v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Feb 17, 2024
1 parent 8c4ef3d commit f6c58a4
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 124 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.1
version:0.5.0
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

---

## [v0.5.0](https://github.com/cophilot/templify/tree/0.5.0) (2024-2-17)

- Refactoring
- Added `reload` command
- Added `-name` flag for the `list` command
- Added `-path` flag for the `list` command
- Added `-template` flag for the `load` command
- Support for `.tpykeep` file to prevent a directory from being deleted

---

## [v0.4.1](https://github.com/cophilot/templify/tree/0.4.1) (2024-2-14)

- Refactoring
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.1"
version = "0.5.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,14 @@ 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.1](https://github.com/cophilot/templify/tree/0.4.1)
### [v0.5.0](https://github.com/cophilot/templify/tree/0.5.0)

- Refactoring
- `.source` attribute will be set in the `.templify` file when a template is loaded
- Added `reload` command
- Added `-name` flag for the `list` command
- Added `-path` flag for the `list` command
- Added `-template` flag for the `load` command
- Support for `.tpykeep` file to prevent a directory from being deleted

---

Expand Down
41 changes: 39 additions & 2 deletions src/command_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,30 @@ pub fn get_all_commands() -> Vec<Command> {

// *** list ***

let list_com = Command::new(
let mut list_com = Command::new(
vec!["list".to_string(), "ls".to_string()],
commands::list,
"List all available templates in the current project.".to_string(),
);

list_com.add_flag(Flag::new_bool_flag(
vec!["name".to_string(), "n".to_string()],
"Show only the names of the templates.".to_string(),
));

list_com.add_flag(Flag::new_bool_flag(
vec!["path".to_string(), "p".to_string()],
"Show the path of the templates.".to_string(),
));

commands.push(list_com);

// *** load ***

let mut load_com = Command::new(
vec!["load".to_string(), "l".to_string()],
commands::load,
"Load templates from a github repository.".to_string(),
"Load templates from a remote repository.".to_string(),
);

load_com.add_argument(Argument::new(
Expand All @@ -125,8 +135,35 @@ pub fn get_all_commands() -> Vec<Command> {
"Force the load, even if the folder already exists.".to_string(),
));

load_com.add_flag(Flag::new_bool_flag(
vec!["template".to_string(), "t".to_string()],
"Load only one template.".to_string(),
));

commands.push(load_com);

// *** reload ***

let mut reload_com = Command::new(
vec!["reload".to_string(), "rl".to_string()],
commands::reload,
"Reload templates from a github repository.".to_string(),
);

reload_com.add_argument(Argument::new(
"template-name".to_string(),
0,
false,
"The name of the template to reload (reload all if not provided).".to_string(),
));

reload_com.add_flag(Flag::new_bool_flag(
vec!["strict".to_string()],
"If enabled the template name must match exactly.".to_string(),
));

commands.push(reload_com);

// *** generate ***

let mut generate_com = Command::new(
Expand Down
176 changes: 122 additions & 54 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
types::{Command, Status},
types::{self, Command, Status},
utils, version_control,
};
use chrono::{self, Datelike};
use std::fs::read_dir;

pub fn list(_command: &Command) -> Status {
pub fn list(command: &Command) -> Status {
let st = utils::check_if_templify_initialized();
if !st.is_ok {
return st;
Expand All @@ -14,20 +14,26 @@ pub fn list(_command: &Command) -> Status {
// get all folders in .templates
let paths = read_dir(".templates").unwrap();

let print_path = command.get_bool_flag("path");
let only_name = command.get_bool_flag("name");

println!("Available templates:");
for path in paths {
let path = path.unwrap().path();
if path.is_dir() {
let template_name = path.file_name().unwrap().to_str().unwrap();
let description =
utils::parse_templify_file(&format!(".templates/{}/.templify", template_name))
["description"]
.clone();
if description.is_empty() {
println!(" {}", template_name);
} else {
println!(" {} - {}", template_name, description);

let meta = types::TemplateMeta::parse(template_name.to_string());

let mut print_string = template_name.to_string();

if !meta.get_description().is_empty() && !only_name {
print_string = format!("{} - {}", print_string, meta.get_description());
}
if print_path {
print_string = format!("{} [{}]", print_string, meta.get_path());
}
println!(" {}", print_string);
}
}
return Status::ok();
Expand All @@ -47,70 +53,132 @@ pub fn load(command: &Command) -> Status {
let url = command.get_argument("url").value.clone();
if !url.starts_with("https://github.com") {
return Status::error(format!(
"Invalid url: {}\nOnly github templates are supported at the moment.",
"Invalid url: {}\nOnly templates from GitHub are supported at the moment.",
url
));
}
println!("Loading template from {}...", url);
utils::load_remote_template_repo(".templates", url.as_str(), command.get_bool_flag("force"));

let load_template = command.get_bool_flag("template");
if load_template {
println!("Loading template from {}...", url);
let name = url.split("/").last().unwrap();
let st = utils::load_remote_template(
format!(".templates/{}", name).as_str(),
url.as_str(),
command.get_bool_flag("force"),
);
if !st.is_ok {
return st;
}
} else {
println!("Loading template collection from {}...", url);
let st = utils::load_remote_template_collection(
".templates",
url.as_str(),
command.get_bool_flag("force"),
);
if !st.is_ok {
return st;
}
}
return Status::ok();
}

pub fn generate(command: &Command) -> Status {
pub fn reload(command: &Command) -> Status {
if !utils::check_internet_connection() {
println!("You need a internet connection for this command!");
return Status::error("You need a internet connection for this command!".to_string());
}

let st = utils::check_if_templify_initialized();
if !st.is_ok {
return st;
}

let strict = command.get_bool_flag("strict");
let dry_run = command.get_bool_flag("dry-run");

let mut template_name = command.get_argument("template-name").value.clone();
let parsed_template_name = template_name.clone().to_lowercase().to_string();
let template_name_raw = template_name.clone().to_string();
let mut name = command.get_argument("template-name").value.clone();
if name != "" {
let st = utils::parse_template_name(&mut name, strict);
if !st.is_ok {
return st;
}
let meta = types::TemplateMeta::parse(name.clone().to_string());
if meta.get_source().is_empty() {
return Status::error(format!("Template {} has no source.", name));
}
println!(
"Reloading template {} from {}...",
meta.get_template_name(),
meta.get_source()
);
let st = utils::load_remote_template(
format!(".templates/{}", name).as_str(),
meta.get_source().as_str(),
true,
);
if !st.is_ok {
return st;
}
println!("Template {} reloaded successfully.", name);
return Status::ok();
}

let given_name = command.get_argument("new-name").value.clone();
let paths: std::fs::ReadDir = read_dir(".templates").unwrap();

let paths = std::fs::read_dir(".templates").unwrap();
let mut found = false;
for path in paths {
let path = path.unwrap().path();

let path_name = path
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string()
.clone();

let parsed_path_name = path_name.clone().to_lowercase().to_string();

if path.is_dir() && parsed_path_name.starts_with(parsed_template_name.as_str()) && !strict {
if found {
return Status::error(format!(
"Template {} is not unique. Please use a more specific name.",
template_name_raw
));
}
template_name = path_name.clone();
found = true;
} else if path.is_dir() && path_name == template_name && strict {
template_name = path_name.clone();
found = true;
break;
if !path.is_dir() {
continue;
}
let template_name = path.file_name().unwrap().to_str().unwrap();
let meta = types::TemplateMeta::parse(template_name.to_string());
if meta.get_source().is_empty() {
continue;
}
println!(
"Reloading template {} from {}...",
meta.get_template_name(),
meta.get_source()
);
let st = utils::load_remote_template(
format!(".templates/{}", template_name).as_str(),
meta.get_source().as_str(),
true,
);
if !st.is_ok {
println!("Error: Template {} could not be reloaded!", template_name);
println!("");
continue;
}
println!("Template {} reloaded successfully.", template_name);
println!("");
}

if !found {
return Status::error(format!("Template {} not found.", template_name));
return Status::ok();
}

pub fn generate(command: &Command) -> Status {
let st = utils::check_if_templify_initialized();
if !st.is_ok {
return st;
}

println!("Generating new files from template {}...", template_name);
let strict = command.get_bool_flag("strict");
let dry_run = command.get_bool_flag("dry-run");

let mut template_name = command.get_argument("template-name").value.clone();
let given_name = command.get_argument("new-name").value.clone();

let st = utils::parse_template_name(&mut template_name, strict);
if !st.is_ok {
return st;
}

let mut new_path =
utils::parse_templify_file(&format!(".templates/{}/.templify", template_name))["path"]
.clone();
println!(
"Generating new files from template {}...",
template_name.clone()
);
let meta = types::TemplateMeta::parse(template_name.clone().to_string());
let mut new_path = meta.get_path();

new_path = new_path.replace("$$name$$", given_name.as_str());
new_path = new_path.replace("$$year$$", chrono::Local::now().year().to_string().as_str());
Expand Down Expand Up @@ -221,7 +289,7 @@ 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_repo(
utils::load_remote_template_collection(
".templates",
"https://github.com/cophilot/templify-vault/tree/main/Example",
true,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ fn main() {

unsafe { env::BASE_COMMAND_NAME = args[0].clone() };

utils::handle_dev_mode(&args);

if args.len() < 2 {
println!("Welcome to templify!");
println!("");
Expand Down
Loading

0 comments on commit f6c58a4

Please sign in to comment.