From 55e15cde31e8539be7d0c6795e4bc0571b2477b0 Mon Sep 17 00:00:00 2001 From: suyashmahar Date: Tue, 9 Jan 2018 17:35:16 +0530 Subject: [PATCH 1/6] Add .gitignore --- .gitignore | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..abf2cae --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +# Gitignore configurations for emacs +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el \ No newline at end of file From 759c3062471fc3109110998624568dea1fcfb952 Mon Sep 17 00:00:00 2001 From: suyashmahar Date: Tue, 9 Jan 2018 17:37:36 +0530 Subject: [PATCH 2/6] Refactor code --- custpathhandler.sh | 31 +++++++++++++++++++++ paramparser.sh | 46 +++++++++++++++++++++++++++++++ pine | 69 ++++++++-------------------------------------- 3 files changed, 88 insertions(+), 58 deletions(-) create mode 100644 custpathhandler.sh create mode 100644 paramparser.sh diff --git a/custpathhandler.sh b/custpathhandler.sh new file mode 100644 index 0000000..744efbb --- /dev/null +++ b/custpathhandler.sh @@ -0,0 +1,31 @@ +#! /usr/bin/env bash + +# Contains functions for handling all custom .gitignore +# directory related features + +function load_dirs() { + if [ -f "${SCRIPTPATH}/dirs.list" ]; then + dirNames=`cat ${SCRIPTPATH}/dirs.list` + else + echo "WARNING: Directory list not compiled" + echo "INFO: Adding default default gitignore directories." + + # Add 2 default locations present in github's repository + echo ":${SCRIPTPATH}/gitignore" >> "${SCRIPTPATH}/dirs.list" + if [ -d "${SCRIPTPATH}/global" ]; then + echo "global:${SCRIPTPATH}/global" >> "${SCRIPTPATH}/dirs.list" + fi + + load_dirs + return + fi + + # Read each line from dirs.list and add an entry to map. + # Lines in dirs.list have pattern: + # alias:path + while read -r line; do + lineSplit=("$line") + dirNameMap["${lineSplit[0]}"]="${lineSplit[1]}" + done < "${SCRIPTPATH}/dirs.list" + +} diff --git a/paramparser.sh b/paramparser.sh new file mode 100644 index 0000000..7fb4e0d --- /dev/null +++ b/paramparser.sh @@ -0,0 +1,46 @@ +#! /usr/bin/env bash + +# From: https://stackoverflow.com/a/38153758/6556360 +# parse the arguments. +parse_params () +{ + local existing_named + local ARGV=() + echo "local ARGV=(); " + while [[ "$1" != "" ]]; do + # If equals delimited named parameter + if [[ "$1" =~ ^..*=..* ]]; then + # key is part before first = + local _key=$(echo "$1" | cut -d = -f 1) + # val is everything after key and = (protect from param==value error) + local _val="${1/$_key=}" + # remove dashes from key name + _key=${_key//\-} + # search for existing parameter name + if (echo "$existing_named" | grep "\b$_key\b" >/dev/null); then + # if name already exists then it's a multi-value named parameter + # re-declare it as an array if needed + if ! (declare -p _key 2> /dev/null | grep -q 'declare \-a'); then + echo "$_key=(\"\$$_key\");" + fi + # append new value + echo "$_key+=('$_val');" + else + # single-value named parameter + echo "local $_key=\"$_val\";" + existing_named=" $_key" + fi + # If standalone named parameter + elif [[ "$1" =~ ^\-. ]]; then + # remove dashes + local _key=${1//\-} + echo "local $_key=\"$_key\";" + # non-named parameter + else + # escape asterisk to prevent bash asterisk expansion + _escaped=${1/\*/\'\"*\"\'} + echo "ARGV+=('$_escaped');" + fi + shift + done +} diff --git a/pine b/pine index 218e2b6..3c2e277 100755 --- a/pine +++ b/pine @@ -4,59 +4,18 @@ PINE_VERSION=1.0.0 FILE_NOT_FOUND_EX=2 LOG_FILE=/tmp/pine_logs/log.txt -SRC_PATH="" -SCRIPT_PATH="" +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +SRC_PATH="$PWD" GITIGNORE_REPO="https://github.com/github/gitignore.git" -# Global variables -declare -A map # map for easy searching of names +# Import functions +source "${SCRIPTPATH}/paramparser.sh" # For parameter parsing +source "${SCRIPTPATH}/inc/error.sh" # For error handling +source "${SCRIPTPATH}/custpathhandler.sh" # For handling custom dir -# -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ -# From: https://stackoverflow.com/a/38153758/6556360 -# parse the arguments. -parse_params () -{ - local existing_named - local ARGV=() - echo "local ARGV=(); " - while [[ "$1" != "" ]]; do - # If equals delimited named parameter - if [[ "$1" =~ ^..*=..* ]]; then - # key is part before first = - local _key=$(echo "$1" | cut -d = -f 1) - # val is everything after key and = (protect from param==value error) - local _val="${1/$_key=}" - # remove dashes from key name - _key=${_key//\-} - # search for existing parameter name - if (echo "$existing_named" | grep "\b$_key\b" >/dev/null); then - # if name already exists then it's a multi-value named parameter - # re-declare it as an array if needed - if ! (declare -p _key 2> /dev/null | grep -q 'declare \-a'); then - echo "$_key=(\"\$$_key\");" - fi - # append new value - echo "$_key+=('$_val');" - else - # single-value named parameter - echo "local $_key=\"$_val\";" - existing_named=" $_key" - fi - # If standalone named parameter - elif [[ "$1" =~ ^\-. ]]; then - # remove dashes - local _key=${1//\-} - echo "local $_key=\"$_key\";" - # non-named parameter - else - # escape asterisk to prevent bash asterisk expansion - _escaped=${1/\*/\'\"*\"\'} - echo "ARGV+=('$_escaped');" - fi - shift - done -} -# -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ +# Global variables +declare -A map # map for easy searching of names +declare -A dirNameMap # map for easy searching of names # Updates all gitignore files in ${SCRIPTPATH}/gitignore function update_config_files() { @@ -195,11 +154,10 @@ function remove_config_from_file() { echo "$currentcontent" | awk -v a="$1" -f ${SCRIPTPATH}/utilities/remove.awk > ${SRC_PATH}/.gitignore } + function init() { set -e - SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" - SRC_PATH="$PWD" - + # Checks the presence of config files check_and_update_config_files @@ -214,9 +172,6 @@ function init() { mkdir -p /tmp/pine_logs fi - # Load error.sh or display error and exit - source "${SCRIPTPATH}/inc/error.sh" - load_names eval $(parse_params "$@") # Parse parameters @@ -246,8 +201,6 @@ function init() { list_all_config exit fi - - # Search each parameter for arg in "${ARGV[@]}"; do From 5a9eaba28f5273fc72f88a4b33e549b66e5374c4 Mon Sep 17 00:00:00 2001 From: suyashmahar Date: Tue, 9 Jan 2018 18:50:27 +0530 Subject: [PATCH 3/6] Added additional blank line to content added by pine to avoid situations where pine would add content starting from the end of the last line --- pine | 1 + 1 file changed, 1 insertion(+) diff --git a/pine b/pine index 3c2e277..c832313 100755 --- a/pine +++ b/pine @@ -126,6 +126,7 @@ function add_config_to_file() { echo "ERROR: Configuration exists, skipping" } || { echo "Adding configuration for $arg" + echo "" >> ${SRC_PATH}/.gitignore echo -e "#### ${1} ##########################" >> ${SRC_PATH}/.gitignore echo -e "#### DO NOT DELETE PRECEDING LINE" >> ${SRC_PATH}/.gitignore echo -e "#### PINE" >> ${SRC_PATH}/.gitignore From 8900613a93714f53f98cb8818dc9efbda1d447c0 Mon Sep 17 00:00:00 2001 From: suyashmahar Date: Tue, 9 Jan 2018 18:55:57 +0530 Subject: [PATCH 4/6] Update help.txt to add content for --list switch --- HELP.txt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/HELP.txt b/HELP.txt index 26ac70b..5fc235e 100644 --- a/HELP.txt +++ b/HELP.txt @@ -8,7 +8,10 @@ Options: -r --remove Remove configuration for all config files specified from .gitignore file in current directory. - -u --update Update local configuration files and exit. + -u --update Update local configuration files and exit. + + --list List all configuration files present in current directory's + .gitignore file -v --version Print current version number and exit. @@ -18,11 +21,14 @@ Examples ======== To add configuration for java, c: - pine c java + pine c java + +To remove configuration for java: + pine -r java -To remove configuration for java - pine -r java +To list all configurations present in current directory's .gitignore file: + pine --list -To update - pine --update +To update: + pine --update From dc194a9f098ecd148288d6cc314479909627009b Mon Sep 17 00:00:00 2001 From: Suyash Mahar Date: Wed, 20 Jun 2018 16:15:07 -0400 Subject: [PATCH 5/6] Increment version number to 1.1 --- pine | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pine b/pine index c832313..4b92f71 100755 --- a/pine +++ b/pine @@ -1,7 +1,7 @@ #! /usr/bin/env bash # Global constants -PINE_VERSION=1.0.0 +PINE_VERSION=1.1.0 FILE_NOT_FOUND_EX=2 LOG_FILE=/tmp/pine_logs/log.txt SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" From 8a2f7872e8120e8d241bd451ead6f08dee297583 Mon Sep 17 00:00:00 2001 From: Suyash Mahar Date: Wed, 20 Jun 2018 16:23:01 -0400 Subject: [PATCH 6/6] Add quotes to installation commands in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 881f5e1..7c90cc6 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,12 @@ Run the following command in your terminal to download and install pine. If you are using bash: ```shell -git clone https://github.com/suyashmahar/pine.git $HOME/pine && echo 'export PATH="$HOME/pine:$PATH"' >> $HOME/.bashrc && source $HOME/.bashrc +git clone https://github.com/suyashmahar/pine.git "$HOME/pine" && echo 'export PATH="$HOME/pine:$PATH"' >> "$HOME/.bashrc" && source "$HOME/.bashrc" ``` If you are using zsh: ```shell -git clone https://github.com/suyashmahar/pine.git $HOME/pine && echo 'export PATH="$HOME/pine:$PATH"' >> $HOME/.zshrc && source $HOME/.zshrc +git clone https://github.com/suyashmahar/pine.git "$HOME/pine" && echo 'export PATH="$HOME/pine:$PATH"' >> "$HOME/.zshrc" && source "$HOME/.zshrc" ``` Usage