diff --git a/.gitignore b/.gitignore index 5d22b73..f72e813 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .vscode/* ideas.md .templify -.templates \ No newline at end of file +.templates +templify-example \ No newline at end of file diff --git a/.phil-project b/.phil-project index 20ce27b..139703c 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.0.2 +version:0.1.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 65a6923..7e970a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,11 @@ --- -## [v0.0.2](https://github.com/cophilot/templify/tree/0.0.2) (2024-1-29) +## [v0.1.0](https://github.com/cophilot/templify/tree/0.1.0) (2024-1-29) -- test +- Added self updating feature +- Minor design changes +- Bug fixes --- diff --git a/Cargo.lock b/Cargo.lock index 6ee4e12..b88641b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,7 @@ dependencies = [ [[package]] name = "templify" -version = "0.0.1" +version = "0.1.0" dependencies = [ "reqwest", "self-replace", diff --git a/Cargo.toml b/Cargo.toml index c348a31..711e0c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "templify" -version = "0.0.1" +version = "0.1.0" 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 44c853f..dd847e2 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ ## Concept -Working on a project often requires the creation of files with a similar structure. For example, a React component often consists of a `.tsx` file, a `.scss` file and a `.test.tsx` file. Templify allows you to create templates for such files and generate them from the command line. +Working on a project often requires the creation of files with a similar structure. For example, a React component often consists of a `.tsx` file, a `.scss` file and a `.test.tsx` file. templify allows you to create templates for such files and generate them from the command line. It also allows you to specify the location of the generated files to keep your project structure clean. You can see a real world example [here](https://github.com/cophilot/templify-docs/tree/main/.templates). @@ -157,9 +157,11 @@ Placeholders are used to replace parts of the template with the given values. Th ## [Release Notes](https://github.com/cophilot/templify/blob/master/CHANGELOG.md) -### [v0.0.1](https://github.com/cophilot/templify/tree/0.0.1) +### [v0.1.0](https://github.com/cophilot/templify/tree/0.1.0) -- _Initial release_ +- Added self updating feature +- Minor design changes +- Bug fixes --- diff --git a/src/commands.rs b/src/commands.rs index 7dacfa2..38c1fd7 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,6 +1,6 @@ use std::fs::read_dir; -use crate::{env, utils, version_control}; +use crate::{utils, version_control}; pub fn list() { if !utils::check_if_templify_initialized() { @@ -29,7 +29,7 @@ pub fn list() { } pub fn generate(args: Vec) { - if args.len() < 3 { + if args.len() < 4 { println!("Missing argument!"); println!("Usage: tpy generate "); return; @@ -39,17 +39,17 @@ pub fn generate(args: Vec) { return; } - let template_name = &args[2]; + let template_name = &args[2].to_string(); let given_name = &args[3]; - println!("Generating new file from template {}...", template_name); - let paths = std::fs::read_dir(".templates").unwrap(); let mut found = false; for path in paths { let path = path.unwrap().path(); - if path.is_dir() && path.file_name().unwrap().to_str().unwrap() == template_name { + if path.is_dir() && path.file_name().unwrap().to_str().unwrap() == template_name.to_string() + { found = true; + break; } } @@ -58,6 +58,8 @@ pub fn generate(args: Vec) { return; } + println!("Generating new files from template {}...", template_name); + let new_path = utils::parse_templify_file(&format!(".templates/{}/.templify", template_name)) ["path"] .clone() @@ -65,14 +67,16 @@ pub fn generate(args: Vec) { // create dir and all subdirs if they don't exist std::fs::create_dir_all(&new_path).unwrap(); - //let files = std::fs::read_dir(&format!(".templates/{}/.templify", template_name)).unwrap(); - utils::generate_template_dir( + + if utils::generate_template_dir( &format!(".templates/{}", template_name), &new_path, given_name, - ); - - println!("Files generated successfully."); + ) { + println!("Files generated successfully."); + } else { + println!("Files could not be generated."); + } } pub fn new(args: Vec) { @@ -116,7 +120,7 @@ pub fn update() { } */ if !version_control::is_newer_version_available() { - println!("Templify is already up to date."); + println!("templify is already up to date."); return; } @@ -136,7 +140,7 @@ pub fn init() { println!("Initializing templify..."); // check if .templates folder exists if std::path::Path::new(".templates").exists() { - println!("Templify is already initialized in this project."); + println!("templify is already initialized in this project."); return; } std::fs::create_dir(".templates").unwrap(); @@ -146,6 +150,7 @@ pub fn init() { ) .unwrap(); std::fs::create_dir(".templates/Example").unwrap(); + std::fs::create_dir(".templates/Example/styles").unwrap(); std::fs::write( ".templates/Example/.templify", crate::data::get_init_example_templify_content(), @@ -156,6 +161,16 @@ pub fn init() { crate::data::get_init_example_index_content(), ) .unwrap(); + std::fs::write( + ".templates/Example/NOTE", + "This is only an example template. Feel free to delete the whole Example folder.", + ) + .unwrap(); + std::fs::write( + ".templates/Example/styles/$$name$$Style.css", + crate::data::get_init_example_style_content(), + ) + .unwrap(); } pub fn help() { diff --git a/src/data.rs b/src/data.rs index f48546b..cf91270 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,8 +1,8 @@ pub fn templify_file_blank() -> String { let content = "# 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 +# 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) description: path:. "; @@ -11,20 +11,59 @@ path:. } pub fn get_init_readme_content() -> String { - let content = "# Welcome to templify! + let content = format!("\"\" + +# Welcome to templify (v{}) This is a README file generated by templify. +--- + ## What is templify? templify is a command line tool that helps you manage your project templates. +--- + ## How to use templify? -Run `templify init` to initialize Templify in your project. +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. + +### Create a new template + +To create a new template, you can use the following command: + +```bash +tpy new +``` + +This will create a new folder in the `.templates` folder with the given name. You can now add files to this folder that will be used to generate new files from this template. +Inside the folder you will also find a `.templify` file. This file is used by templify to generate new files from this template. Please open and read the file to learn more about how to use it. + +### List all templates + +To list all templates, you can use the following command: + +```bash +tpy list +``` + +### Generate a new file from a template + +To generate a new file from a template, you can use the following command: + +```bash +tpy generate +``` + +This will create a new file from the given template in the current project folder with the given name. + +--- + +To learn more about templify, please visit the [GitHub repository](https://github.com/cophilot/templify). by Philipp B. -"; +", env!("CARGO_PKG_VERSION")); return content.to_string(); } @@ -36,7 +75,55 @@ pub fn get_init_example_templify_content() -> String { # description: The description of the template # path: The path of the template description:This is an example template. -path:templify-example/$$name$$Example +path:templify-example/web/$$name$$Example +"; + + return content.to_string(); +} + +pub fn get_init_example_style_content() -> String { + let content = " +body { + font-family: sans-serif; + background-color: #1d1d1d; + color: #fff; +} +.centerBox { + width: 50%; + margin: 0 auto; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + text-align: center; +} +a { + color: #fff; +} +.mainText { + font-size: 1.5rem; +} +.logo { + animation: wiggle 30s linear infinite; +} + +@keyframes wiggle { + 0% { + transform: rotate(0deg); + } + 25% { + transform: rotate(3deg); + } + 50% { + transform: rotate(0deg); + } + 75% { + transform: rotate(-3deg); + } + 100% { + transform: rotate(0deg); + } +} "; return content.to_string(); @@ -49,6 +136,7 @@ pub fn get_init_example_index_content() -> String { + Welcome to templify