Skip to content

Commit

Permalink
v0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Feb 5, 2024
1 parent 2950a95 commit 6836741
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 26 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.2.1
version:0.2.2
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.2.2](https://github.com/cophilot/templify/tree/0.2.2) (2024-2-5)

- Added `-force` flag for `load` command
- Added `-version` flag for `update` command

---

## [v0.2.1](https://github.com/cophilot/templify/tree/0.2.1) (2024-2-4)

- 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.2.1"
version = "0.2.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,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.2.1](https://github.com/cophilot/templify/tree/0.2.1)
### [v0.2.2](https://github.com/cophilot/templify/tree/0.2.2)

- Refactoring
- Added `-offline` flag for `init` command
- Added `-description` flag for `new` command
- Added `-path` flag for `new` command
- Added `-force` flag for `load` command
- Added `-version` flag for `update` command

---

Expand Down
17 changes: 14 additions & 3 deletions src/command_storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands;
use crate::commands::{self};
use crate::types::{Argument, Command, Flag, Status};

pub fn get_all_commands() -> Vec<Command> {
Expand Down Expand Up @@ -26,12 +26,18 @@ pub fn get_all_commands() -> Vec<Command> {

// *** update ***

let update_com = Command::new(
let mut update_com = Command::new(
vec!["update".to_string()],
commands::update,
"Update templify to the latest version.".to_string(),
);

update_com.add_flag(Flag::new_value_flag(
vec!["version".to_string(), "v".to_string()],
"".to_string(),
"Update to a specific version.".to_string(),
));

commands.push(update_com);

// *** init ***
Expand Down Expand Up @@ -93,7 +99,7 @@ pub fn get_all_commands() -> Vec<Command> {
let mut load_com = Command::new(
vec!["load".to_string(), "l".to_string()],
commands::load,
"Load templates from a github repository (provide a url that points to an folder in a github repository).".to_string(),
"Load templates from a github repository.".to_string(),
);

load_com.add_argument(Argument::new(
Expand All @@ -103,6 +109,11 @@ pub fn get_all_commands() -> Vec<Command> {
"The url of the github repository.".to_string(),
));

load_com.add_flag(Flag::new_bool_flag(
vec!["force".to_string(), "f".to_string()],
"Force the load, even if the folder already exists.".to_string(),
));

commands.push(load_com);

// *** generate ***
Expand Down
27 changes: 22 additions & 5 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ pub fn load(command: &Command) -> Status {
));
}
println!("Loading template from {}...", url);
utils::load_remote_template_dir(".templates", url.as_str(), true);
utils::load_remote_template_dir(
".templates",
url.as_str(),
command.get_bool_flag("force"),
true,
);
return Status::ok();
}

Expand Down Expand Up @@ -135,19 +140,28 @@ pub fn new(command: &Command) -> Status {
return Status::ok();
}

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

if !version_control::is_newer_version_available() {
let version = command.get_value_flag("version").clone();

if !version_control::is_newer_version_available() && version == "" {
println!("templify is already up to date.");
return Status::ok();
}

println!("Updating templify...");
if version != "" {
println!("Updating templify to version {}...", version);
} else {
println!("Updating templify...");
}

version_control::update().unwrap();
let st = version_control::update(version);
if st.is_err() {
return Status::error(format!("{}", st.err().unwrap()));
}

println!("templify updated successfully.");
std::process::exit(0);
Expand Down Expand Up @@ -181,6 +195,7 @@ pub fn init(command: &Command) -> Status {
".templates",
"https://github.com/cophilot/templify-vault/tree/main/Example",
true,
true,
);
}
println!("templify initialized successfully.");
Expand All @@ -204,5 +219,7 @@ pub fn help(_command: &Command) -> Status {
println!("{}", command.to_help_string());
}

println!("To get more information please visit: https://templify.philipp-bonin.com");

return Status::ok();
}
10 changes: 6 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub fn parse_templify_file(file_path: &str) -> std::collections::HashMap<String,
return map;
}

pub fn load_remote_template_dir(path: &str, url: &str, first: bool) {
if !first {
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;
Expand All @@ -53,6 +53,7 @@ pub fn load_remote_template_dir(path: &str, url: &str, first: bool) {
format!("{}/{}", url, item["name"])
.replace("\"", "")
.as_str(),
force,
false,
);
continue;
Expand All @@ -69,12 +70,13 @@ pub fn load_remote_template_dir(path: &str, url: &str, first: bool) {
format!("{}/{}", url, item["name"])
.replace("\"", "")
.as_str(),
force,
);
}
}

pub fn load_remote_template_file(path: &str, url: &str) {
if Path::new(path).exists() {
pub 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
44 changes: 38 additions & 6 deletions src/version_control.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@
use crate::{env, utils};

pub fn update() -> Result<(), Box<dyn std::error::Error>> {
pub fn update(v: String) -> Result<(), Box<dyn std::error::Error>> {
let mut binary_ending = "";
if env::is_windows() {
binary_ending = ".exe";
}

let mut version = v;

if version == "" {
version = get_latest_version();
}

if version.starts_with("v") {
version = version[1..].to_string();
}

let url = format!(
"
https://github.com/cophilot/templify/releases/download/{}/tpy{}",
get_latest_version(),
binary_ending
"https://github.com/cophilot/templify/releases/download/{}/tpy{}",
version, binary_ending
);
// download the new binary and save it somewhere temporarily
let mut response = reqwest::blocking::get(url)?;
let response = reqwest::blocking::get(url).unwrap();

// check if the download was successful
if response.status() != 200 {
if response.status() == 404 {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Could not download the new binary. The version {} does not exist.",
version
),
)));
} else {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Could not download the new binary. Status code: {}",
response.status()
),
)));
}
}

let mut response = response;
let mut dest = {
let mut d = std::env::temp_dir();
d.push("tpy");
std::fs::File::create(d)?
};
std::io::copy(&mut response, &mut dest)?;

// replace the current binary with the new one
let new_binary = format!("{}/tpy", std::env::temp_dir().to_str().unwrap());

Expand Down

0 comments on commit 6836741

Please sign in to comment.