From c8fa72fca8b59b056d65a19f7583cba53c049c7f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 12 Jul 2020 17:02:36 +0800 Subject: [PATCH] feat(commit-msg): if crs script file not exists use binaray file --- scripts/commit-msg.crs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/scripts/commit-msg.crs b/scripts/commit-msg.crs index 787da3c..0b52dad 100755 --- a/scripts/commit-msg.crs +++ b/scripts/commit-msg.crs @@ -47,11 +47,12 @@ fn main() { } } -const RED: &str = "\x1B[91m"; -const GREEN: &str = "\x1B[92m"; -const BOLD: &str = "\x1B[1m"; -const UNDER: &str = "\x1B[4m"; -const END: &str = "\x1B[0m"; +const RED: &str = "\x1B[91m"; +const GREEN: &str = "\x1B[92m"; +const YELLOW: &str = "\x1B[93m"; +const BOLD: &str = "\x1B[1m"; +const UNDER: &str = "\x1B[4m"; +const END: &str = "\x1B[0m"; fn print_usage() { print_info(&format!(r#"Please follow the commit spec: @@ -83,9 +84,13 @@ fn install_commit_msg(force: bool) { exit_with_error_message(&format!("Get env HOME failed: {}!", e)); })); let commit_msg_crs = home_path.join("bin").join("commit-msg.crs"); - if !commit_msg_crs.exists() { - exit_with_error_message(&format!("File {:?} NOT exists!", commit_msg_crs)); - } + let commig_msg_exec_file = if !commit_msg_crs.exists() { + //exit_with_error_message(&format!("File {:?} NOT exists!", commit_msg_crs)); + print_warn(&format!("File {:?} NOT exists!", commit_msg_crs)); + PathBuf::from(env::args().next().unwrap()) + } else { + commit_msg_crs + }; let git_hooks = PathBuf::from(".git").join("hooks"); if !git_hooks.exists() { @@ -96,10 +101,11 @@ fn install_commit_msg(force: bool) { exit_with_error_message(&format!("File {:?} exists! or try forceinstall.", git_hooks_commit_msg)); } - let commit_msg_crs_content = fs::read_to_string(&commit_msg_crs).unwrap_or_else(|e| { - exit_with_error_message(&format!("Read file: {:?} failed: {}.", commit_msg_crs, e)); + print_info(&format!("Copy file: {:?} to : {:?}", commig_msg_exec_file, git_hooks_commit_msg)); + let commig_msg_exec_file_content = fs::read(&commig_msg_exec_file).unwrap_or_else(|e| { + exit_with_error_message(&format!("Read file: {:?} failed: {}.", commig_msg_exec_file, e)); }); - fs::write(&git_hooks_commit_msg, commit_msg_crs_content).unwrap_or_else(|e| { + fs::write(&git_hooks_commit_msg, commig_msg_exec_file_content).unwrap_or_else(|e| { exit_with_error_message(&format!("Write file: {:?} failed: {}.", git_hooks_commit_msg, e)); }); Command::new("chmod").args(&["+x", git_hooks_commit_msg.to_str().unwrap()]).output().unwrap_or_else(|e| { @@ -114,4 +120,5 @@ fn exit_with_error_message(msg: &str) -> ! { print_error(msg); exit_with_error() fn print_info(msg: &str) { println!("{b}[INFO ]{e} {m}", b = BOLD, e = END, m = msg); } fn print_ok(msg: &str) { println!("{g}{b}[OK ]{e} {g}{m}{e}", g = GREEN, b = BOLD, e = END, m = msg); } +fn print_warn(msg: &str) { println!("{y}{b}[WARN ]{e} {y}{m}{e}", y = YELLOW, b = BOLD, e = END, m = msg); } fn print_error(msg: &str) { println!("{r}{b}[ERROR]{e} {r}{m}{e}", r = RED, b = BOLD, e = END, m = msg); }