From 1ed413b3c52023e74a8573260b10736f5524f19b Mon Sep 17 00:00:00 2001 From: Michael Campagnaro Date: Wed, 3 Jul 2019 17:08:59 -0400 Subject: [PATCH] Relocate script helpers to this repo --- script_helpers/file_ops.sh | 146 +++++++++++++++++++++++++++++++++++++ script_helpers/printing.sh | 30 ++++++++ windows/symbolic-link.sh | 2 +- 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 script_helpers/file_ops.sh create mode 100644 script_helpers/printing.sh diff --git a/script_helpers/file_ops.sh b/script_helpers/file_ops.sh new file mode 100644 index 0000000..14bb6ef --- /dev/null +++ b/script_helpers/file_ops.sh @@ -0,0 +1,146 @@ +#!/bin/bash + +if which tput >/dev/null 2>&1; then + ncolors=$(tput colors) +fi +if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then + RED="$(tput setaf 1)" + GREEN="$(tput setaf 2)" + YELLOW="$(tput setaf 3)" + BLUE="$(tput setaf 4)" + BOLD="$(tput bold)" + NORMAL="$(tput sgr0)" +else + RED="" + GREEN="" + YELLOW="" + BLUE="" + BOLD="" + NORMAL="" +fi + +error() { + printf "${BOLD}${RED}$1${NORMAL}" +} + +log() { + msg="$1" + value="$2" + printf "@log ${BOLD}${YELLOW}$msg${GREEN}$value${NORMAL}\n" +} + +#--------------------------------------------------------------------------------------------------- +# API +#--------------------------------------------------------------------------------------------------- + +expand_path() { + # Expands a symlink path. If the root folder is the home directory symbol "~" then it'll be + # replaced by the full home path. + local ret="$1" + + IFS="/" read -ra parts <<< "$ret" + if [[ "${parts[0]}" == "~" ]]; then + ret="$HOME" + for ((i=1; i < ${#parts[@]}; i++)) + do + ret="$ret/${parts[$i]}" + done + fi + + ret=$(readlink -m "$ret") + echo $ret +} + +windows_path() { + ret=$1 + + if [[ $(is_absolute_unix_path $ret) -eq 1 ]]; then + ret="${ret/\//}" + # Fix the drive name, e.g. c\foo becomes c:\foo + ret=$(sed 's,\([a-zA-Z]*\),\1:,' <<< "$ret") + fi + + ret="${ret////\\}" # replace unix path with windows path + echo $ret +} + +windows_to_unix_path() { + ret=$1 + ret="/${ret/:/}" # Remove drive ':'. + ret="${ret//\\//}" # Replace Windows slashes. + ret="${ret// /\\ }" # Add a backslash before spaces. + echo "$ret" +} + +move_file() { + local src="$1" + local src_path=$(dirname "${src}") + local src_name=$(basename "${src}") + local dest=$2 + local src_type=$3 # e.g. "script", "dependency", etc + + if [[ $src_type != '' ]]; then + src_type="$src_type " + fi + + if [[ -e "$src" ]]; then + mkdir -p "$dest" + mv "$src" "$dest" + printf "${BOLD}${GREEN}Moved $src_type$src to $dest${NORMAL}\n" + else + error "Unable to find $src_type$src!\n" + fi +} + +copy_file() { + local src="$1" + local src_path=$(dirname "${src}") + local src_name=$(basename "${src}") + local dest=$2 + local src_type=$3 # e.g. "script", "dependency", etc + + if [[ $src_type != '' ]]; then + src_type="$src_type " + fi + + if [[ -e "$src" ]]; then + # @fixme If $dest is a file then strip the file name from the path and mkdir on that instead + echo "MAKE DIR $src $dest" + #mkdir -p "$dest" + cp "$src" "$dest" + printf "${BOLD}${GREEN}Copied $src_type$src to $dest${NORMAL}\n" + else + error "Unable to find $src_type$src!\n" + fi +} + +copy_dir_files() { + local src="$1" + local dest=$2 + + if [[ -d "$src" ]]; then + mkdir -p "$dest" + cp -r $src/* $dest + printf "${BOLD}${GREEN}Copied contents of $src into $dest${NORMAL}\n" + else + error "Unable to find $src!\n" + fi +} + +is_absolute_unix_path() { + if [[ $1 =~ ^/ ]]; then echo 1; else echo 0; fi +} + +is_sym_file() { + if [[ $1 =~ ^\.{1} ]]; then echo 1; else echo 0; fi +} + +# Will return false if the path has no slashes. +is_windows_path() { + if [[ ! $1 =~ \/+ ]]; then echo 1; else echo 0; fi +} + +# Will return false if the path has no slashes. +is_unix_path() { + echo $(! is_windows_path "$1") +} diff --git a/script_helpers/printing.sh b/script_helpers/printing.sh new file mode 100644 index 0000000..5afb88a --- /dev/null +++ b/script_helpers/printing.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +if which tput >/dev/null 2>&1; then + ncolors=$(tput colors) +fi +if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then + RED="$(tput setaf 1)" + GREEN="$(tput setaf 2)" + YELLOW="$(tput setaf 3)" + BLUE="$(tput setaf 4)" + BOLD="$(tput bold)" + NORMAL="$(tput sgr0)" +else + RED="" + GREEN="" + YELLOW="" + BLUE="" + BOLD="" + NORMAL="" +fi + +error() { + printf "${BOLD}${RED}$1${NORMAL}" +} + +log() { + msg="$1" + value="$2" + printf "@log ${BOLD}${YELLOW}$msg${GREEN}$value${NORMAL}\n" +} diff --git a/windows/symbolic-link.sh b/windows/symbolic-link.sh index da13028..90a3ee8 100644 --- a/windows/symbolic-link.sh +++ b/windows/symbolic-link.sh @@ -1,7 +1,7 @@ #!/bin/bash # @improve this path stuff -source_helpers="$HOME/.game-libs/project-templates/_helpers" +source_helpers="$HOME/.dotfiles/script_helpers" source "$source_helpers/printing.sh" source "$source_helpers/file_ops.sh"