From e49934c99d510c1b4294d89549cbb2a284ebb7bf Mon Sep 17 00:00:00 2001 From: James Patrick Date: Sat, 27 Apr 2019 18:09:36 -0400 Subject: [PATCH] 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. --- lib/helper/mk_link | 14 ++++++++++++++ lib/helper/report | 23 +++++++++++++++++++++++ lib/helper/rm_link | 20 ++++++++++++++++++++ lib/shared.mk | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100755 lib/helper/mk_link create mode 100755 lib/helper/report create mode 100755 lib/helper/rm_link create mode 100644 lib/shared.mk diff --git a/lib/helper/mk_link b/lib/helper/mk_link new file mode 100755 index 0000000..50a3197 --- /dev/null +++ b/lib/helper/mk_link @@ -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`" diff --git a/lib/helper/report b/lib/helper/report new file mode 100755 index 0000000..75794b8 --- /dev/null +++ b/lib/helper/report @@ -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 diff --git a/lib/helper/rm_link b/lib/helper/rm_link new file mode 100755 index 0000000..fa951d4 --- /dev/null +++ b/lib/helper/rm_link @@ -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 diff --git a/lib/shared.mk b/lib/shared.mk new file mode 100644 index 0000000..ebde197 --- /dev/null +++ b/lib/shared.mk @@ -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