Adding shared makefile logic and helper scripts

Shared logic can be imported via `include ../lib/shared.mk`
Helper functions include 3 current opperations

1. mk_link:: This will create a symbolic link if the file doesn't already exist.
2. rm_link:: Will remove the link if it is a symlink.
3. report :: This color codes the status of a message and adds a leading symbol.

report also can be called with `LAST_RETURN=$? report "message"`
and will expand this to be either `report info "message"` Or `report error
"message"` based on the last return value.
Current status are info, warn, and error.
This commit is contained in:
James Patrick 2019-04-27 18:09:36 -04:00
parent 649b158166
commit e49934c99d
4 changed files with 89 additions and 0 deletions

14
lib/helper/mk_link Executable file
View File

@ -0,0 +1,14 @@
#! env zsh
REPORT=`dirname $0`/report
if [ -z "$1" ] || [ -z "$2" ] ; then
$REPORT error "ERROR: Both to and from string must be defined.\n source: $1 \n symlnk: $2"
exit
fi
if [[ -e $2 ]] ; then
$REPORT warn "$2 already exist. Skipping."
exit
fi
$REPORT info "Symlinked `ln -svf $1 $2`"

23
lib/helper/report Executable file
View File

@ -0,0 +1,23 @@
#! env zsh
R='\033[0;31m'
Y='\033[0;33m'
G='\033[0;32m'
NC='\033[0m' # No Color
format(){
case $1 in
"error") echo -e "$R x $2$NC";;
"warn") echo -e "$Y ⚠ $2$NC";;
"info") echo -e "$G ✔ $2$NC";;
*) echo -e "$1: $2";;
esac
}
if [[ -z $2 ]] ; then
case "$LAST_RETURN" in
0) format info $1;;
*) format error "FAILURE DURING: \"$1\"";;
esac
else
format $1 $2
fi

20
lib/helper/rm_link Executable file
View File

@ -0,0 +1,20 @@
#! env zsh
REPORT=`dirname $0`/report
if [[ -z $1 ]] ; then
$REPORT error "ERROR: Symlink to delete does not exist.\n symlink: $1"
exit
fi
if ! ( [[ -e $1 ]] || [[ -L $1 ]] ) ; then
$REPORT warn "$1 does not exist."
exit
fi
if [[ ! -L $1 ]] ; then
$REPORT warn "$1 is not a symbolic link. Skipping."
exit
fi
$REPORT info "Deleting $1"
rm $1

32
lib/shared.mk Normal file
View File

@ -0,0 +1,32 @@
# This will make heavy use of the "$+commands[cmd]" function in bash. Make sure its installed.
_SHELL := $(shell which zsh )
ifndef _SHELL
$(error ZSH is not installed on this machine. This makefile requires ZSH features)
endif
SHELL := $(_SHELL)
# Helper scripts for setting up and taking down links.
LIB_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
mk_link := $(LIB_DIR)/helper/mk_link
rm_link := $(LIB_DIR)/helper/rm_link
report := LAST_RETURN=$$? $(LIB_DIR)/helper/report
cmd_exist = $(shell (($$+commands[$1])) && echo true || echo false )
# Shortcut for the XDG dir if it exist. default to ~/.confg
XDG_DIR := $${XDG_CONFIG_DIR:-~/.config}
# Some things will OS specific.
# With this you can use `$(IS_MAC) && echo "I'm a Mac" || echo "I'm not a Mac"`
ifeq ($(shell uname), Darwin)
IS_MAC=true
else
IS_LINUX=true
endif
IS_MAC?=false
IS_LINUX?=false
# TURN THIS OFF FOR DEBUGGING
MAKEFLAGS += --silent
.PHONY: install update init remove