mirror of
https://github.com/jimeh/dotify.git
synced 2026-02-19 10:06:39 +00:00
610 lines
13 KiB
Bash
Executable File
610 lines
13 KiB
Bash
Executable File
#! /usr/bin/env bash
|
|
set -e
|
|
shopt -s extglob
|
|
[ -n "$DOTIFY_DEBUG" ] && set -x
|
|
|
|
# dotify 0.0.1
|
|
# https://github.com/jimeh/dotify
|
|
#
|
|
# Copyright (c) 2014 Jim Myhrberg.
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to
|
|
# deal in the Software without restriction, including without limitation the
|
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
# sell copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
# IN THE SOFTWARE.
|
|
|
|
|
|
#
|
|
# Helper functions
|
|
#
|
|
|
|
# Checks for specified argument.
|
|
#
|
|
# Example:
|
|
#
|
|
# $ has-argument help h "-t none"
|
|
# > returns 1
|
|
# $ has-argument help h "-t none --help"
|
|
# > returns 0
|
|
# $ has-argument help h "-t none -h"
|
|
# > returns 0
|
|
#
|
|
# Returns 0 if argument was found, returns 1 otherwise.
|
|
has-argument() {
|
|
local long short
|
|
|
|
long="--$1"
|
|
short="-$2"
|
|
shift 2
|
|
|
|
if [[ " $@ " == *" $long "* ]] || [[ " $@ " == *" $long="* ]]; then
|
|
return 0
|
|
elif [[ " $@ " == *" $short "* ]] || [[ " $@ " == *" $short="* ]]; then
|
|
return 0
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
# Parses and echos value of specified argument.
|
|
#
|
|
# Example:
|
|
#
|
|
# $ parse-argument file f -t none --file /tmp/foobar.txt
|
|
# /tmp/foobar.txt
|
|
# $ parse-argument file f -t none --file="/tmp/foo bar.txt"
|
|
# /tmp/foo bar.txt
|
|
# $ parse-argument file f -t none -f /tmp/foobar.txt
|
|
# /tmp/foobar.txt
|
|
# $ parse-argument file f -t none -f=/tmp/foo\ bar.txt
|
|
# /tmp/foo bar.txt
|
|
#
|
|
# Returns 0 and echos value if argument was found, returns 1 otherwise.
|
|
parse-argument() {
|
|
local long short arg next_arg
|
|
|
|
long="--$1"
|
|
short="-$2"
|
|
shift 2
|
|
|
|
for arg in "$@"; do
|
|
if [ -n "$next_arg" ]; then
|
|
echo "$arg"
|
|
return 0
|
|
elif [[ " $arg " == *" $long "* ]] || [[ " $arg " == *" $short "* ]]; then
|
|
next_arg="yes"
|
|
elif [[ " $arg " == *" $long="* ]]; then
|
|
arg="${arg/#$long=/}"
|
|
echo "$arg"
|
|
return 0
|
|
elif [[ " $arg " == *" $short="* ]]; then
|
|
arg="${arg/#$short=/}"
|
|
echo "$arg"
|
|
return 0
|
|
fi
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# Trim leading and trailing whitespace.
|
|
#
|
|
# Example:
|
|
#
|
|
# $ trim " foo bar "
|
|
# foo bar
|
|
#
|
|
trim() {
|
|
local string="$@"
|
|
string="${string#"${string%%[![:space:]]*}"}"
|
|
string="${string%"${string##*[![:space:]]}"}"
|
|
echo -n "$string"
|
|
}
|
|
|
|
|
|
#
|
|
# Internal functions
|
|
#
|
|
|
|
dotify-action() {
|
|
local action="$1"
|
|
|
|
if [ $# -lt 3 ]; then
|
|
local target="$2"
|
|
else
|
|
local source="$2"
|
|
local target="$3"
|
|
fi
|
|
|
|
dotify-setup-root-link
|
|
|
|
if [ "$action" == "default" ]; then
|
|
action="$(dotify-get-default-action)"
|
|
fi
|
|
|
|
! local valid_action="$(command -v "dotify-action-${action}")"
|
|
if [ -z "$valid_action" ]; then
|
|
echo "ERROR: \"$action\" is not a valid action." >&2
|
|
return 1
|
|
fi
|
|
|
|
if [ -n "$source" ]; then
|
|
dotify-action-${action} "$(dotify-get-run-mode)" "$target" "$source"
|
|
else
|
|
dotify-action-${action} "$(dotify-get-run-mode)" "$target"
|
|
fi
|
|
}
|
|
|
|
dotify-compile-dotfile() {
|
|
local dotfile="$1"
|
|
if [ -z "$dotfile" ]; then dotfile="$(dotify-get-dotfile-path)"; fi
|
|
|
|
if [ ! -f "$dotfile" ]; then
|
|
echo "ERROR: \"$dotfile\" does not exist." >&2
|
|
return 1
|
|
fi
|
|
|
|
local output=""
|
|
local line=""
|
|
while IFS= read line; do
|
|
# Ignore comments and blank lines.
|
|
if [[ "$line" =~ ^(\ *\#.*|\ *)$ ]]; then
|
|
continue
|
|
|
|
# Parse "<action>: <source> -> <target>" lines.
|
|
elif [[ "$line" =~ ^(\ +)?([a-zA-Z0-9_-]+):\ (.+)\ +-[\>]\ +(.+)$ ]]; then
|
|
output="${output}${BASH_REMATCH[1]}dotify-action ${BASH_REMATCH[2]} "
|
|
output="${output}$(trim "${BASH_REMATCH[3]}") "
|
|
output="${output}$(trim "${BASH_REMATCH[4]}")\n"
|
|
|
|
# Parse "<action>: -> <target>" lines.
|
|
elif [[ "$line" =~ ^(\ +)?([a-zA-Z0-9_-]+):\ *-[\>]\ +(.+)$ ]]; then
|
|
output="${output}${BASH_REMATCH[1]}dotify-action ${BASH_REMATCH[2]} "
|
|
output="${output}$(trim "${BASH_REMATCH[3]}")\n"
|
|
|
|
# Parse "<source> -> <target>" lines.
|
|
elif [[ "$line" =~ ^(\ +)?(.+)\ -[\>]\ (.+)$ ]]; then
|
|
output="${output}${BASH_REMATCH[1]}dotify-action default "
|
|
output="${output}$(trim "${BASH_REMATCH[2]}") "
|
|
output="${output}$(trim "${BASH_REMATCH[3]}")\n"
|
|
|
|
# Parse "-> <target>" lines.
|
|
elif [[ "$line" =~ ^(\ +)?-[\>]\ (.+)$ ]]; then
|
|
output="${output}${BASH_REMATCH[1]}dotify-action default "
|
|
output="${output}$(trim "${BASH_REMATCH[2]}")\n"
|
|
|
|
# Append line without modifications.
|
|
else
|
|
output="${output}${line}\n"
|
|
fi
|
|
done < "$dotfile"
|
|
|
|
echo -e "$output"
|
|
}
|
|
|
|
dotify-create-symlink() {
|
|
local source="$1"
|
|
local target="$2"
|
|
|
|
if [ ! -e "$target" ] && [ ! -h "$target" ]; then
|
|
ln -s "$source" "$target"
|
|
echo "created"
|
|
return 0
|
|
elif [ -h "$target" ]; then
|
|
if [ "$(readlink "$target")" == "$source" ]; then
|
|
echo "exists"
|
|
return 0
|
|
else
|
|
echo "ERROR: \"$target\" exists, is a symlink to:" \
|
|
"$(readlink "$target")" >&2
|
|
return 1
|
|
fi
|
|
else
|
|
echo "ERROR: \"$target\" exists" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
dotify-execute-dotfile() {
|
|
local dotfile_source="$(dotify-command-compile)"
|
|
|
|
dotify-valid-target-path
|
|
if [ "$?" != "0" ]; then return 1; fi
|
|
|
|
eval "$dotfile_source"
|
|
return $?
|
|
}
|
|
|
|
dotify-has-action() {
|
|
if [[ " ${DOTIFY_ACTIONS[@]} " != *" $1 "* ]]; then
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
dotify-register-action() {
|
|
if [ -z "$DOTIFY_ACTIONS" ]; then
|
|
DOTIFY_ACTIONS=()
|
|
fi
|
|
|
|
if [[ " ${DOTIFY_ACTIONS[@]} " == *" $1 "* ]]; then
|
|
return 1
|
|
fi
|
|
|
|
DOTIFY_ACTIONS+=("$1")
|
|
}
|
|
|
|
dotify-setup-root-link() {
|
|
return 0
|
|
}
|
|
|
|
|
|
#
|
|
# Dotify attributes
|
|
#
|
|
|
|
dotify-set-default-action() {
|
|
DOTIFY_ATTR_DEFAULT_ACTION="$@"
|
|
}
|
|
|
|
dotify-get-default-action() {
|
|
if [ -z "$DOTIFY_ATTR_DEFAULT_ACTION" ]; then
|
|
DOTIFY_ATTR_DEFAULT_ACTION="link" # Default value.
|
|
fi
|
|
|
|
echo "$DOTIFY_ATTR_DEFAULT_ACTION"
|
|
}
|
|
|
|
dotify-get-dotfile-path() {
|
|
if [ -n "$DOTIFY_ATTR_DOTFILE_PATH" ]; then
|
|
echo "$DOTIFY_ATTR_DOTFILE_PATH"
|
|
return 0
|
|
fi
|
|
|
|
if [ -n "$DOTIFY_ARG_DOTFILE" ]; then
|
|
if [ -f "$DOTIFY_ARG_DOTFILE" ]; then
|
|
DOTIFY_ATTR_DOTFILE_PATH="$DOTIFY_ARG_DOTFILE"
|
|
else
|
|
echo "ERROR: \"$DOTIFY_ARG_DOTFILE\" does not exist." >&2
|
|
return 1
|
|
fi
|
|
elif [ -f "$(pwd)/Dotfile" ]; then
|
|
DOTIFY_ATTR_DOTFILE_PATH="$(pwd)/Dotfile"
|
|
else
|
|
echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2
|
|
return 1
|
|
fi
|
|
|
|
echo "$DOTIFY_ATTR_DOTFILE_PATH"
|
|
}
|
|
|
|
dotify-valid-dotfile-path() {
|
|
dotify-get-dotfile-path >/dev/null 2>&1
|
|
return "$?"
|
|
}
|
|
|
|
dotify-get-dry-run() {
|
|
if [ -n "$DOTIFY_ATTR_DRY_RUN" ]; then
|
|
DOTIFY_ATTR_DRY_RUN="$DOTIFY_ARG_DRY_RUN"
|
|
fi
|
|
|
|
echo "$DOTIFY_ATTR_DRY_RUN"
|
|
}
|
|
|
|
dotify-set-root-link() {
|
|
DOTIFY_ATTR_ROOT_LINK="$@"
|
|
}
|
|
|
|
dotify-get-root-link() {
|
|
if [ -z "$DOTIFY_ATTR_ROOT_LINK" ]; then
|
|
DOTIFY_ATTR_ROOT_LINK=".dotfiles" # Default value.
|
|
fi
|
|
|
|
echo "$DOTIFY_ATTR_ROOT_LINK"
|
|
}
|
|
|
|
dotify-set-run-mode() {
|
|
DOTIFY_ATTR_RUN_MODE="$1"
|
|
}
|
|
|
|
dotify-get-run-mode() {
|
|
echo "$DOTIFY_ATTR_RUN_MODE"
|
|
}
|
|
|
|
dotify-get-source-path() {
|
|
local dotfile="$(dotify-get-dotfile-path)"
|
|
if [ "$?" != "0" ]; then return 1; fi
|
|
|
|
echo "$(dirname "$dotfile")"
|
|
}
|
|
|
|
dotify-valid-source-path() {
|
|
dotify-get-source-path >/dev/null 2>&1
|
|
return "$?"
|
|
}
|
|
|
|
dotify-get-target-path() {
|
|
if [ -n "$DOTIFY_ATTR_TARGET" ]; then
|
|
echo "$DOTIFY_ATTR_TARGET"
|
|
return 0
|
|
fi
|
|
|
|
if [ -n "$DOTIFY_ARG_TARGET" ]; then
|
|
if [ -d "$DOTIFY_ARG_TARGET" ]; then
|
|
DOTIFY_ATTR_TARGET="$DOTIFY_ARG_TARGET"
|
|
else
|
|
echo "ERROR: Target \"$DOTIFY_ARG_TARGET\" is not a directory." >&2
|
|
return 1
|
|
fi
|
|
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
|
|
DOTIFY_ATTR_TARGET="$HOME"
|
|
elif [ -d ~ ]; then
|
|
DOTIFY_ATTR_TARGET=~
|
|
else
|
|
echo "ERROR: Your \$HOME folder could not be found." >&2
|
|
return 1
|
|
fi
|
|
|
|
echo "$DOTIFY_ATTR_TARGET"
|
|
}
|
|
|
|
dotify-valid-target-path() {
|
|
dotify-get-target-path >/dev/null 2>&1
|
|
return "$?"
|
|
}
|
|
|
|
|
|
#
|
|
# Dotify commands
|
|
#
|
|
|
|
dotify-command-clean() {
|
|
dotify-set-run-mode "clean"
|
|
dotify-execute-dotfile
|
|
return $?
|
|
}
|
|
|
|
dotify-command-compile() {
|
|
dotify-valid-dotfile-path
|
|
if [ "$?" != "0" ]; then return 1; fi
|
|
|
|
dotify-compile-dotfile "$DOTFILE"
|
|
return $?
|
|
}
|
|
|
|
dotify-command-help() {
|
|
echo "$(dotify-command-print-version)"
|
|
echo "usage: dotify <command> [<args>]"
|
|
}
|
|
|
|
dotify-command-info() {
|
|
dotify-valid-dotfile-path
|
|
if [ "$?" != "0" ]; then return 1; fi
|
|
|
|
dotify-valid-source-path
|
|
if [ "$?" != "0" ]; then return 1; fi
|
|
|
|
dotify-valid-target-path
|
|
if [ "$?" != "0" ]; then return 1; fi
|
|
|
|
echo "$(dotify-print-version)"
|
|
echo " Dotfile: $(dotify-get-dotfile-path)"
|
|
echo " Source: $(dotify-get-source-path)"
|
|
echo " Target: $(dotify-get-target-path)"
|
|
}
|
|
|
|
dotify-command-install() {
|
|
dotify-set-run-mode "install"
|
|
dotify-execute-dotfile
|
|
return $?
|
|
}
|
|
|
|
dotify-command-print-version() {
|
|
echo "dotify $(dotify-command-version)"
|
|
}
|
|
|
|
dotify-command-uninstall() {
|
|
dotify-set-run-mode "uninstall"
|
|
dotify-execute-dotfile
|
|
return $?
|
|
}
|
|
|
|
dotify-command-version() {
|
|
echo "0.0.1"
|
|
}
|
|
|
|
|
|
#
|
|
# Built-in action plugins
|
|
#
|
|
|
|
# Register link action.
|
|
dotify-register-action "link"
|
|
|
|
# Link action.
|
|
dotify-action-link() {
|
|
local mode="$1"
|
|
! local valid_mode="$(command -v "dotify-action-link-${mode}")"
|
|
|
|
if [ -n "$valid_mode" ]; then
|
|
shift 1
|
|
dotify-action-link-${mode} $@
|
|
fi
|
|
}
|
|
|
|
dotify-action-link-install() {
|
|
echo "link install: $@"
|
|
}
|
|
|
|
dotify-action-link-uninstall() {
|
|
echo "link uninstall: $@"
|
|
}
|
|
|
|
dotify-action-link-cleanup() {
|
|
echo "link cleanup: $@"
|
|
}
|
|
|
|
dotify-action-link-post-run() {
|
|
if [ "$1" == "cleanup" ]; then
|
|
shift 1
|
|
dotify-action-link-cleanup $@
|
|
fi
|
|
}
|
|
|
|
# Register git action
|
|
dotify-register-action "git"
|
|
|
|
# Git action.
|
|
dotify-action-git() {
|
|
local mode="$1"
|
|
! local valid_mode="$(command -v "dotify-action-git-${mode}")"
|
|
|
|
if [ -n "$valid_mode" ]; then
|
|
shift 1
|
|
dotify-action-git-${mode} $@
|
|
fi
|
|
}
|
|
|
|
dotify-action-git-install() {
|
|
echo "git install: $@"
|
|
}
|
|
|
|
dotify-action-git-uninstall() {
|
|
echo "git uninstall: $@"
|
|
}
|
|
|
|
dotify-action-git-cleanup() {
|
|
echo "git cleanup: $@"
|
|
}
|
|
|
|
dotify-action-git-post-run() {
|
|
if [ "$1" == "cleanup" ]; then
|
|
shift 1
|
|
dotify-action-git-cleanup $@
|
|
fi
|
|
}
|
|
|
|
|
|
#
|
|
# Dotfile commands
|
|
#
|
|
|
|
root_link () {
|
|
dotify-set-root-link $@
|
|
}
|
|
|
|
default_action() {
|
|
dotify-set-default-action $@
|
|
}
|
|
|
|
include() {
|
|
echo "include: $@"
|
|
}
|
|
|
|
|
|
#
|
|
# Main
|
|
#
|
|
|
|
dotify-main-parse-arguments() {
|
|
DOTIFY_ARG_DOTFILE="" # --dotfile / -f
|
|
DOTIFY_ARG_TARGET="" # --target / -t
|
|
DOTIFY_ARG_DRY_RUN="" # --dry-run / -d
|
|
DOTIFY_ARG_HELP="" # --help / -h
|
|
DOTIFY_ARG_VERSION="" # --version / -v
|
|
|
|
if has-argument dotfile f "$@"; then
|
|
DOTIFY_ARG_DOTFILE="$(parse-argument dotfile f "$@")"
|
|
fi
|
|
|
|
if has-argument target t "$@"; then
|
|
DOTIFY_ARG_TARGET="$(parse-argument target t "$@")"
|
|
fi
|
|
|
|
if has-argument dry-run d "$@"; then
|
|
DOTIFY_ARG_DRY_RUN="1"
|
|
fi
|
|
|
|
if has-argument help h "$@"; then
|
|
DOTIFY_ARG_HELP="1"
|
|
fi
|
|
|
|
if has-argument version v "$@"; then
|
|
DOTIFY_ARG_VERSION="1"
|
|
fi
|
|
}
|
|
|
|
dotify-main-parse-command() {
|
|
local skip_next
|
|
|
|
# Command is first argument that does not start with a dash or plus.
|
|
for arg in "$@"; do
|
|
if [ -n "$skip_next" ]; then
|
|
skip_next=
|
|
elif [[ "$arg" =~ ^(--dotfile|-f|--taraget|-t)$ ]]; then
|
|
skip_next=1
|
|
elif [[ "$arg" != "-"* ]] && [[ "$arg" != "+"* ]]; then
|
|
DOTIFY_COMMAND="$arg"
|
|
break
|
|
fi
|
|
done
|
|
}
|
|
|
|
dotify-main-dispatcher() {
|
|
# Show help and exit if help arguments or command are given.
|
|
if [ -n "$DOTIFY_ARG_HELP" ] || [ "$DOTIFY_COMMAND" == "help" ]; then
|
|
dotify-command-help
|
|
exit
|
|
fi
|
|
|
|
# Show version info and exit if version arguments or command are given.
|
|
if [ -n "$DOTIFY_ARG_VERSION" ] || [ "$DOTIFY_COMMAND" == "version" ]; then
|
|
dotify-command-help | head -1
|
|
exit
|
|
fi
|
|
|
|
# Deal with the commands.
|
|
case "$DOTIFY_COMMAND" in
|
|
"info" )
|
|
dotify-command-info
|
|
;;
|
|
"compile" )
|
|
dotify-command-compile
|
|
;;
|
|
"" | "install" )
|
|
dotify-command-install
|
|
;;
|
|
"uninstall" )
|
|
dotify-command-uninstall
|
|
;;
|
|
"clean" )
|
|
dotify-command-clean
|
|
;;
|
|
esac
|
|
|
|
return $?
|
|
}
|
|
|
|
dotify-main() {
|
|
dotify-main-parse-arguments $@
|
|
dotify-main-parse-command $@
|
|
dotify-main-dispatcher $@
|
|
|
|
return $?
|
|
}
|
|
|
|
|
|
dotify-main $@
|
|
|
|
exit $?
|