Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Jan 29, 2024
1 parent 30de779 commit 4204080
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vscode/*
ideas.md
.templify
.templates
.templates
templify-example
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.0.2
version:0.1.0
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

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.0.1"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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

---

Expand Down
41 changes: 28 additions & 13 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -29,7 +29,7 @@ pub fn list() {
}

pub fn generate(args: Vec<String>) {
if args.len() < 3 {
if args.len() < 4 {
println!("Missing argument!");
println!("Usage: tpy generate <template-name> <given-name>");
return;
Expand All @@ -39,17 +39,17 @@ pub fn generate(args: Vec<String>) {
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;
}
}
Expand All @@ -58,21 +58,25 @@ pub fn generate(args: Vec<String>) {
return;
}

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);

// 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<String>) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
Expand All @@ -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(),
Expand All @@ -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() {
Expand Down
100 changes: 94 additions & 6 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -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:.
";
Expand All @@ -11,20 +11,59 @@ path:.
}

pub fn get_init_readme_content() -> String {
let content = "# Welcome to templify!
let content = format!("<img src=\"https://raw.githubusercontent.com/cophilot/templify/master/assets/logo.png\" alt=\"\" width=\"30%\"/>
# 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 <template-name>
```
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 <template-name> <given-name>
```
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();
}
Expand All @@ -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();
Expand All @@ -49,6 +136,7 @@ pub fn get_init_example_index_content() -> String {
<head>
<meta charset=\"UTF-8\" />
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
<style src=\"styles/$$name$$Style.css\"></style>
<title>Welcome to templify</title>
<style>
body {
Expand Down
4 changes: 2 additions & 2 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::env;

pub fn is_linux() -> bool {
/* pub fn is_linux() -> bool {
return env::consts::OS == "linux";
}
} */
pub fn is_windows() -> bool {
return env::consts::OS == "windows";
}
26 changes: 19 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn parse_templify_file(file_path: &str) -> std::collections::HashMap<String,
return map;
}

pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str) {
pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str) -> bool {
let paths = std::fs::read_dir(path).unwrap();
for path in paths {
let path = path.unwrap().path();
Expand All @@ -41,34 +41,46 @@ pub fn generate_template_dir(path: &str, new_path: &str, given_name: &str) {
let new_file_name = file_name.replace("$$name$$", given_name);
let new_path = format!("{}/{}", new_path, new_file_name);

// check if new_path already exists
if Path::new(&new_path).exists() {
println!("File {} already exists.", new_path);
return false;
}

if path.is_dir() {
std::fs::create_dir(&new_path).unwrap();
generate_template_dir(&path.to_str().unwrap(), &new_path, given_name);
if !generate_template_dir(&path.to_str().unwrap(), &new_path, given_name) {
return false;
}
} else {
generate_template_file(&path.to_str().unwrap(), &new_path, given_name);
if !generate_template_file(&path.to_str().unwrap(), &new_path, given_name) {
return false;
}
}
}
return true;
}

pub fn generate_template_file(path: &str, new_path: &str, given_name: &str) {
pub fn generate_template_file(path: &str, new_path: &str, given_name: &str) -> bool {
let file_content = std::fs::read_to_string(path).unwrap();
let file_content = file_content.replace("$$name$$", given_name);

if Path::new(new_path).exists() {
println!("File {} already exists.", new_path);
return;
return false;
}

let mut new_file = std::fs::File::create(new_path).unwrap();
new_file.write_all(file_content.as_bytes()).unwrap();

println!("Created file {}", new_path);
return true;
}

pub fn check_if_templify_initialized() -> bool {
if !Path::new(".templates").exists() {
println!("Templify is not initialized in this project.");
println!("Run `tpy init` to initialize Templify in your project.");
println!("templify is not initialized in this project.");
println!("Run `tpy init` to initialize templify in your project.");
return false;
}
return true;
Expand Down
Loading

0 comments on commit 4204080

Please sign in to comment.