Compare commits

..

No commits in common. "0fbf3f2522b14570a36164173ac2d63f30ee16ff" and "9b6c3c9422cebff1efc78f8b36052d32145742d5" have entirely different histories.

3 changed files with 12 additions and 46 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "oink" name = "oink"
version = "0.2.3" version = "0.2.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -3,15 +3,13 @@ use std::{
env::var, env::var,
fs::{self, read_to_string, File }, fs::{self, read_to_string, File },
io::Write, io::Write,
path::{ Path, PathBuf }, path::{ Path, PathBuf }
time::SystemTime
}; };
use termion::{ use termion::{
color::{ self, Fg }, color::{ self, Fg },
style::{ style::{
Bold as BOLD, Bold as BOLD,
Faint as FAINT,
Italic as ITALIC, Italic as ITALIC,
Reset as RESET Reset as RESET
} }
@ -21,8 +19,7 @@ use upon::Engine;
use crate::{ use crate::{
config::Config, config::Config,
filter, filter
util::time
}; };
static SUCCESS: Fg<color::Green> = Fg(color::Green); static SUCCESS: Fg<color::Green> = Fg(color::Green);
@ -30,12 +27,9 @@ static WARNING: Fg<color::Yellow> = Fg(color::Yellow);
static FAILURE: Fg<color::Red> = Fg(color::Red); static FAILURE: Fg<color::Red> = Fg(color::Red);
pub fn apply(targets: &Vec<Map<String, Value>>) { pub fn apply(targets: &Vec<Map<String, Value>>) {
let start = SystemTime::now();
let home = var("HOME").unwrap(); let home = var("HOME").unwrap();
println!("running apply:"); println!("running apply:");
for target in targets { for target in targets {
let start = SystemTime::now();
// get path and name // get path and name
let path = target.get("path"); let path = target.get("path");
let i_name = target.get("name"); let i_name = target.get("name");
@ -64,22 +58,16 @@ pub fn apply(targets: &Vec<Map<String, Value>>) {
// copy and print // copy and print
let result = fs::copy(source, destination); let result = fs::copy(source, destination);
let time = time(start);
if result.is_err() { if result.is_err() {
print!(" {BOLD}{FAILURE}failed to copy{RESET}"); println!(" {BOLD}{FAILURE}failed to copy{RESET}");
} }
else { else {
print!(" {BOLD}{SUCCESS}completed{RESET}"); println!(" {BOLD}{SUCCESS}completed{RESET}");
} }
println!(" {FAINT}({time}){RESET}");
} }
let time = time(start);
println!("{FAINT}(apply: {time}){RESET}");
} }
pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &Config) { pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &Config) {
let start = SystemTime::now();
let home = var("HOME").unwrap(); let home = var("HOME").unwrap();
println!("running build:"); println!("running build:");
@ -87,7 +75,6 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
engine.add_filter("has", filter::has); engine.add_filter("has", filter::has);
for target in targets { for target in targets {
let start = SystemTime::now();
let context = config.context(target); let context = config.context(target);
// get name property // get name property
let i_name = target.get("name"); let i_name = target.get("name");
@ -102,31 +89,25 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
println!(" building \"{name}\":"); println!(" building \"{name}\":");
// compile // compile
let compile_start = SystemTime::now(); println!(" {ITALIC}compiling{RESET}");
print!(" {ITALIC}compiling{RESET}");
let mut path = PathBuf::from(&template_dir); let mut path = PathBuf::from(&template_dir);
if let Some(Value::String(base)) = target.get("base") { path.push(base); } if let Some(Value::String(base)) = target.get("base") { path.push(base); }
else { path.push(name); } else { path.push(name); }
let content = read_to_string(path).unwrap(); let content = read_to_string(path).unwrap();
let template = engine.compile(&content); let template = engine.compile(&content);
let compile_time = time(compile_start);
print!(" {FAINT}({compile_time}){RESET}");
if let Err(error) = template { if let Err(error) = template {
println!("\n {BOLD}{FAILURE}failed to compile template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}"); println!(" {BOLD}{FAILURE}failed to compile template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
continue; continue;
} else { println!(); } }
// render // render
let render_start = SystemTime::now(); println!(" {ITALIC}rendering{RESET}");
print!(" {ITALIC}rendering{RESET}");
let render = template.unwrap().render(&engine, &context).to_string(); let render = template.unwrap().render(&engine, &context).to_string();
let render_time = time(render_start);
print!(" {FAINT}({render_time}){RESET}");
if let Err(error) = render { if let Err(error) = render {
println!("\n {BOLD}{FAILURE}failed to render template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}"); println!(" {BOLD}{FAILURE}failed to render template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
continue; continue;
} else { println!(); } }
// get rendered text and open destination file // get rendered text and open destination file
let output = render.unwrap(); let output = render.unwrap();
@ -141,12 +122,7 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
// write to destination file // write to destination file
let written = write!(&mut file, "{output}"); let written = write!(&mut file, "{output}");
if written.is_err() { println!(" {FAILURE}failed to write to destination file at {path:?}{RESET}"); } if written.is_err() { println!(" {FAILURE}failed to write to destination file at {path:?}{RESET}"); }
else { else { println!(" {BOLD}{SUCCESS}completed{RESET}"); }
let time = time(start);
println!(" {BOLD}{SUCCESS}completed{RESET} {FAINT}({time}){RESET}");
} }
} }
let time = time(start);
println!("{FAINT}(build: {time}){RESET}");
}

View file

@ -1,4 +1,3 @@
use std::time::SystemTime;
use upon::Value as ContextValue; use upon::Value as ContextValue;
use toml::{ value::Array, Value }; use toml::{ value::Array, Value };
@ -24,12 +23,3 @@ pub fn convert(value: &Value) -> Option<ContextValue> {
} }
} }
pub fn time(start: SystemTime) -> String {
let now = SystemTime::now();
if let Ok(duration) = now.duration_since(start) {
let ms = duration.as_millis();
if ms > 0 { format!("{ms} ms") }
else { "< 1 ms".to_owned() }
} else { String::new() }
}