Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Feb 12, 2024
1 parent e2cecb5 commit e8a329b
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ ideas.md
.templify
.templates
templify-example
install2
install2
dev/
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.3.1
version:0.4.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.4.0](https://github.com/cophilot/templify/tree/0.4.0) (2024-2-12)

- `.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.3.1](https://github.com/cophilot/templify/tree/0.3.1) (2024-2-9)

- Added `-dry-run` flag for the `generate` command
Expand Down
73 changes: 72 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "templify"
version = "0.3.1"
version = "0.4.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -9,3 +9,4 @@ edition = "2021"
reqwest = { version = "0.11", features = ["blocking", "json"] }
self-replace = "1.3.6"
serde_json = "1.0.1"
chrono = "0.4.19"
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +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.3.1](https://github.com/cophilot/templify/tree/0.3.1)

- Added `-dry-run` flag for the `generate` command
### [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$$`

---

Expand Down
4 changes: 4 additions & 0 deletions src/command_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub fn get_all_commands() -> Vec<Command> {
vec!["offline".to_string(), "o".to_string()],
"Do not fetch the example template from the internet.".to_string(),
));
init_com.add_flag(Flag::new_bool_flag(
vec!["blank".to_string(), "b".to_string()],
"Initialize only a blank .templates folder.".to_string(),
));

commands.push(init_com);

Expand Down
25 changes: 17 additions & 8 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::fs::read_dir;

use crate::{
types::{Command, Status},
utils, version_control,
};
use chrono::{self, Datelike};
use std::fs::read_dir;

pub fn list(_command: &Command) -> Status {
let st = utils::check_if_templify_initialized();
Expand Down Expand Up @@ -115,10 +115,15 @@ pub fn generate(command: &Command) -> Status {

println!("Generating new files from template {}...", template_name);

let new_path = utils::parse_templify_file(&format!(".templates/{}/.templify", template_name))
["path"]
.clone()
.replace("$$name$$", given_name.as_str());
let mut new_path =
utils::parse_templify_file(&format!(".templates/{}/.templify", template_name))["path"]
.clone();

new_path = new_path.replace("$$name$$", given_name.as_str());
new_path = new_path.replace("$$year$$", chrono::Local::now().year().to_string().as_str());
new_path = new_path.replace("$$month$$", &chrono::Local::now().month().to_string());
new_path = new_path.replace("$$day$$", &chrono::Local::now().day().to_string());
new_path = new_path.replace("$$git-name$$", &utils::get_git_name());

// create dir and all subdirs if they don't exist
if !dry_run {
Expand Down Expand Up @@ -205,11 +210,15 @@ pub fn init(command: &Command) -> Status {

// check if .templates folder exists
if std::path::Path::new(".templates").exists() {
println!("templify is already initialized in this project.");
return Status::ok();
return Status::error("templify is already initialized in this project.".to_string());
}

std::fs::create_dir(".templates").unwrap();

if command.get_bool_flag("blank") {
println!("templify initialized successfully.");
return Status::ok();
}
std::fs::write(
".templates/README.md",
crate::data::get_init_readme_content(),
Expand Down
7 changes: 4 additions & 3 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ pub fn templify_file_blank(description: String, path: String) -> String {
let content = format!("# This file is used by templify to generate new files from this template.
# You can use the following variables in this file:
# description:The description of the template
# path:The path of the template based on the project root (You can also use the placeholder $$name$$ to use the name of the template for the path)
# path: The path where the file should be generated based on the project root (you can also use placeholders like $$name$$)
description:{}
path:{}
", description, path);
Expand All @@ -25,13 +26,13 @@ templify is a command line tool that helps you manage your project templates.
---
To learn more about templify, please visit the [GitHub repository](https://github.com/cophilot/templify).
To learn more about templify, please visit [templify.philipp-bonin.com](https://templify.philipp-bonin.com/).
---
## How to use templify?
When you can see this file, you have already initialized templify in this folder. You can now create your own templates and generate new files from them.
When you can see this file, you have already initialized templify in this project. You can now create your own templates and generate new files from them.
### Create a new template
Expand Down
55 changes: 51 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{self, Datelike};
use std::{io::Write, path::Path};

use crate::types::Status;
Expand All @@ -8,15 +9,33 @@ pub fn parse_templify_file(file_path: &str) -> std::collections::HashMap<String,
map.insert("description".to_string(), "".to_string());
map.insert("path".to_string(), ".".to_string());

let file_content = std::fs::read_to_string(file_path).unwrap();
let file_content = std::fs::read_to_string(file_path);
if file_content.is_err() {
return map;
}
let file_content = file_content.unwrap();

let mut divider = ":".to_string();

let first_line = file_content.lines().next();
if first_line.is_none() {
return map;
}

let first_line = first_line.unwrap().replace(" ", "");
if first_line.starts_with("#!") {
let new_divider = first_line.clone().replace("#!", "");

divider = new_divider.to_string();
}

for line in file_content.lines() {
let line = line.trim();
if line.starts_with("#") || line.is_empty() {
continue;
}

let parts: Vec<&str> = line.split(":").collect();
let parts: Vec<&str> = line.split(divider.as_str()).collect();
if parts.len() < 2 {
continue;
}
Expand Down Expand Up @@ -114,7 +133,13 @@ pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str, dry_r
continue;
}

let new_file_name = file_name.replace("$$name$$", given_name);
let mut new_file_name = file_name.replace("$$name$$", given_name);
new_file_name =
new_file_name.replace("$$year$$", chrono::Local::now().year().to_string().as_str());
new_file_name =
new_file_name.replace("$$month$$", &chrono::Local::now().month().to_string());
new_file_name = new_file_name.replace("$$day$$", &chrono::Local::now().day().to_string());
new_file_name = new_file_name.replace("$$git-name$$", &crate::utils::get_git_name());
let new_path = format!("{}/{}", new_path, new_file_name);

// check if new_path already exists
Expand All @@ -139,7 +164,12 @@ pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str, dry_r

pub fn generate_template_file(path: &str, new_path: &str, given_name: &str, dry_run: bool) -> bool {
let file_content = std::fs::read_to_string(path).unwrap();
let file_content = file_content.replace("$$name$$", given_name);
let mut file_content = file_content.replace("$$name$$", given_name);
file_content =
file_content.replace("$$year$$", chrono::Local::now().year().to_string().as_str());
file_content = file_content.replace("$$month$$", &chrono::Local::now().month().to_string());
file_content = file_content.replace("$$day$$", &chrono::Local::now().day().to_string());
file_content = file_content.replace("$$git-name$$", &crate::utils::get_git_name());

if Path::new(new_path).exists() {
println!("File {} already exists.", new_path);
Expand Down Expand Up @@ -177,3 +207,20 @@ pub fn check_internet_connection() -> bool {
}
return false;
}

pub fn get_git_name() -> String {
let output = std::process::Command::new("git")
.arg("config")
.arg("user.name")
.output();
if output.is_err() {
return "unknown".to_string();
}
let output = output.unwrap();
let output = String::from_utf8_lossy(&output.stdout);
let mut name = output.trim().to_string();
if name.is_empty() {
name = "unknown".to_string();
}
return name;
}

0 comments on commit e8a329b

Please sign in to comment.