Massive restructuring code-base

This commit is contained in:
2014-03-19 21:03:31 +00:00
parent 4974654e75
commit 5567a98bb3
47 changed files with 610 additions and 395 deletions

View File

@@ -117,9 +117,38 @@ trim() {
# Internal functions
#
compile-dotfile() {
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="$DOTFILE"; fi
if [ -z "$dotfile" ]; then dotfile="$(dotify-get-dotfile-path)"; fi
if [ ! -f "$dotfile" ]; then
echo "ERROR: \"$dotfile\" does not exist." >&2
@@ -164,15 +193,19 @@ compile-dotfile() {
echo -e "$output"
}
create-symlink() {
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
if [ "$(readlink "$target")" == "$source" ]; then
echo "exists"
return 0
else
echo "ERROR: \"$target\" exists, is a symlink to:" \
"$(readlink "$target")" >&2
return 1
@@ -183,137 +216,213 @@ create-symlink() {
fi
}
execute-dotfile() {
local dotfile_source="$(dotify-compile)"
dotify-execute-dotfile() {
local dotfile_source="$(dotify-command-compile)"
locate-target
dotify-valid-target-path
if [ "$?" != "0" ]; then return 1; fi
ROOT_DIR="$(dirname "$DOTFILE")"
eval "$dotfile_source"
return $?
}
locate-dotfile() {
if [ -n "$DOTFILE" ]; then
if [ ! -f "$DOTFILE" ]; then
echo "ERROR: \"$DOTFILE\" does not exist." >&2
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="$1"
}
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 "$ARG_DOTFILE" ]; then
if [ -f "$ARG_DOTFILE" ]; then
DOTIFY_ATTR_DOTFILE_PATH="$ARG_DOTFILE"
else
echo "ERROR: \"$ARG_DOTFILE\" does not exist." >&2
return 1
fi
elif [ -f "$(pwd)/Dotfile" ]; then
DOTFILE="$(pwd)/Dotfile"
DOTIFY_ATTR_DOTFILE_PATH="$(pwd)/Dotfile"
else
echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2
return 1
fi
echo "$DOTIFY_ATTR_DOTFILE_PATH"
}
locate-target() {
if [ -n "$TARGET" ]; then
if [ ! -d "$TARGET" ]; then
echo "ERROR: Target \"$TARGET\" is not a directory." >&2
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="$ARG_DRY_RUN"
fi
echo "$DOTIFY_ATTR_DRY_RUN"
}
dotify-set-root-link() {
DOTIFY_ATTR_ROOT_LINK="$1"
}
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 "$ARG_TARGET" ]; then
if [ -d "$ARG_TARGET" ]; then
DOTIFY_ATTR_TARGET="$ARG_TARGET"
else
echo "ERROR: Target \"$ARG_TARGET\" is not a directory." >&2
return 1
fi
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
TARGET="$HOME"
DOTIFY_ATTR_TARGET="$HOME"
elif [ -d ~ ]; then
TARGET=~
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 "$?"
}
#
# Main functions
# Dotify commands
#
dotify-action() {
local action="$1"
if [ $# -lt 3 ]; then
local target="$2"
else
local source="$2"
local target="$3"
fi
if [ "$action" == "default" ]; then
action="$DOTIFY_OPT_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_RUN_MODE" "$target" "$source"
else
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target"
fi
}
dotify-clean() {
DOTIFY_RUN_MODE="clean"
execute-dotfile
dotify-command-clean() {
dotify-set-run-mode "clean"
dotify-execute-dotfile
return $?
}
dotify-compile() {
locate-dotfile
dotify-command-compile() {
dotify-valid-dotfile-path
if [ "$?" != "0" ]; then return 1; fi
compile-dotfile "$DOTFILE"
dotify-compile-dotfile "$DOTFILE"
return $?
}
dotify-help() {
echo "$(dotify-print-version)"
dotify-command-help() {
echo "$(dotify-command-print-version)"
echo "usage: dotify <command> [<args>]"
}
dotify-info() {
locate-dotfile
dotify-command-info() {
dotify-valid-dotfile-path
if [ "$?" != "0" ]; then return 1; fi
locate-target
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: $DOTFILE"
echo " Root: $(dirname "$DOTFILE")"
echo " Target: $TARGET"
echo " Dotfile: $(dotify-get-dotfile-path)"
echo " Source: $(dotify-get-source-path)"
echo " Target: $(dotify-get-target-path)"
}
dotify-install() {
DOTIFY_RUN_MODE="install"
execute-dotfile
dotify-command-install() {
dotify-set-run-mode "install"
dotify-execute-dotfile
return $?
}
dotify-register-action() {
if [ -z "$DOTIFY_ACTIONS" ]; then DOTIFY_ACTIONS=(); fi
DOTIFY_ACTIONS+=("$1")
dotify-command-print-version() {
echo "dotify $(dotify-command-version)"
}
dotify-uninstall() {
DOTIFY_RUN_MODE="uninstall"
execute-dotfile
dotify-command-uninstall() {
dotify-set-run-mode "uninstall"
dotify-execute-dotfile
return $?
}
dotify-version() {
dotify-command-version() {
echo "0.0.1"
}
dotify-print-version() {
echo "dotify $(dotify-version)"
}
#
# Built-in action plugins
@@ -325,30 +434,30 @@ dotify-register-action "link"
# Link action.
dotify-action-link() {
local mode="$1"
! local valid_mode="$(command -v "dotify-action-link-do-${mode}")"
! local valid_mode="$(command -v "dotify-action-link-${mode}")"
if [ -n "$valid_mode" ]; then
shift 1
dotify-action-link-do-${mode} $@
dotify-action-link-${mode} $@
fi
}
dotify-action-link-do-install() {
dotify-action-link-install() {
echo "link install: $@"
}
dotify-action-link-do-uninstall() {
dotify-action-link-uninstall() {
echo "link uninstall: $@"
}
dotify-action-link-do-cleanup() {
dotify-action-link-cleanup() {
echo "link cleanup: $@"
}
dotify-action-link-post-run() {
if [ "$1" == "cleanup" ]; then
shift 1
dotify-action-link-do-cleanup $@
dotify-action-link-cleanup $@
fi
}
@@ -358,30 +467,30 @@ dotify-register-action "git"
# Git action.
dotify-action-git() {
local mode="$1"
! local valid_mode="$(command -v "dotify-action-git-do-${mode}")"
! local valid_mode="$(command -v "dotify-action-git-${mode}")"
if [ -n "$valid_mode" ]; then
shift 1
dotify-action-git-do-${mode} $@
dotify-action-git-${mode} $@
fi
}
dotify-action-git-do-install() {
dotify-action-git-install() {
echo "git install: $@"
}
dotify-action-git-do-uninstall() {
dotify-action-git-uninstall() {
echo "git uninstall: $@"
}
dotify-action-git-do-cleanup() {
dotify-action-git-cleanup() {
echo "git cleanup: $@"
}
dotify-action-git-post-run() {
if [ "$1" == "cleanup" ]; then
shift 1
dotify-action-link-do-cleanup $@
dotify-action-git-cleanup $@
fi
}
@@ -391,11 +500,11 @@ dotify-action-git-post-run() {
#
root_link () {
DOTIFY_OPT_ROOT_LINK="$@"
dotify-set-root-link "$@"
}
default_action() {
DOTIFY_OPT_DEFAULT_ACTION="$@"
dotify-set-default-action "$@"
}
include() {
@@ -403,41 +512,34 @@ include() {
}
#
# Default Options
#
DOTIFY_OPT_ROOT_LINK=".dotfiles"
DOTIFY_OPT_DEFAULT_ACTION="link"
#
# Argument Parsing
#
DOTFILE="" # --dotfile / -f
TARGET="" # --target / -t
DRY_RUN="" # --dry-run / -d
HELP="" # --help / -h
VERSION="" # --version / -v
ARG_DOTFILE="" # --dotfile / -f
ARG_TARGET="" # --target / -t
ARG_DRY_RUN="" # --dry-run / -d
ARG_HELP="" # --help / -h
ARG_VERSION="" # --version / -v
if has-argument dotfile f "$@"; then
DOTFILE="$(parse-argument dotfile f "$@")"
ARG_DOTFILE="$(parse-argument dotfile f "$@")"
fi
if has-argument target t "$@"; then
TARGET="$(parse-argument target t "$@")"
ARG_TARGET="$(parse-argument target t "$@")"
fi
if has-argument dry-run d "$@"; then
DRY_RUN="1"
ARG_DRY_RUN="1"
fi
if has-argument help h "$@"; then
HELP="1"
ARG_HELP="1"
fi
if has-argument version v "$@"; then
VERSION="1"
ARG_VERSION="1"
fi
@@ -458,33 +560,33 @@ for arg in "$@"; do
done
# Show help and exit if help arguments or command are given.
if [ -n "$HELP" ] || [ "$command" == "help" ]; then
dotify-help
if [ -n "$ARG_HELP" ] || [ "$command" == "help" ]; then
dotify-command-help
exit
fi
# Show version info and exit if version arguments or command are given.
if [ -n "$VERSION" ] || [ "$command" == "version" ]; then
dotify-help | head -1
if [ -n "$ARG_VERSION" ] || [ "$command" == "version" ]; then
dotify-command-help | head -1
exit
fi
# Deal with the commands.
case "$command" in
"info" )
dotify-info
dotify-command-info
;;
"compile" )
dotify-compile
dotify-command-compile
;;
"" | "install" )
dotify-install
dotify-command-install
;;
"uninstall" | "remove" )
dotify-uninstall
"uninstall" )
dotify-command-uninstall
;;
"clean" )
dotify-clean
dotify-command-clean
;;
esac

View File

@@ -1,5 +1,5 @@
#! /usr/bin/env bash
source "src/lib/dotify-version.sh"
source "src/lib/commands/version.sh"
source "src/lib/helpers/trim.sh"
resolve_link() {
@@ -46,7 +46,7 @@ output=""
while IFS= read line; do
if [[ "$line" == "#"* ]]; then
# Replace {{DOTIFY_VERSION}} placeholder in comments.
line="${line/\{\{DOTIFY_VERSION\}\}/$(dotify-version)}"
line="${line/\{\{DOTIFY_VERSION\}\}/$(dotify-command-version)}"
line="${line/\{\{COPYRIGHT_YEAR\}\}/$(date +"%Y")}"
fi

View File

@@ -4,29 +4,29 @@ dotify-register-action "git"
# Git action.
dotify-action-git() {
local mode="$1"
! local valid_mode="$(command -v "dotify-action-git-do-${mode}")"
! local valid_mode="$(command -v "dotify-action-git-${mode}")"
if [ -n "$valid_mode" ]; then
shift 1
dotify-action-git-do-${mode} $@
dotify-action-git-${mode} $@
fi
}
dotify-action-git-do-install() {
dotify-action-git-install() {
echo "git install: $@"
}
dotify-action-git-do-uninstall() {
dotify-action-git-uninstall() {
echo "git uninstall: $@"
}
dotify-action-git-do-cleanup() {
dotify-action-git-cleanup() {
echo "git cleanup: $@"
}
dotify-action-git-post-run() {
if [ "$1" == "cleanup" ]; then
shift 1
dotify-action-link-do-cleanup $@
dotify-action-git-cleanup $@
fi
}

View File

@@ -4,29 +4,29 @@ dotify-register-action "link"
# Link action.
dotify-action-link() {
local mode="$1"
! local valid_mode="$(command -v "dotify-action-link-do-${mode}")"
! local valid_mode="$(command -v "dotify-action-link-${mode}")"
if [ -n "$valid_mode" ]; then
shift 1
dotify-action-link-do-${mode} $@
dotify-action-link-${mode} $@
fi
}
dotify-action-link-do-install() {
dotify-action-link-install() {
echo "link install: $@"
}
dotify-action-link-do-uninstall() {
dotify-action-link-uninstall() {
echo "link uninstall: $@"
}
dotify-action-link-do-cleanup() {
dotify-action-link-cleanup() {
echo "link cleanup: $@"
}
dotify-action-link-post-run() {
if [ "$1" == "cleanup" ]; then
shift 1
dotify-action-link-do-cleanup $@
dotify-action-link-cleanup $@
fi
}

View File

@@ -38,25 +38,38 @@ source "../lib/helpers/trim.sh"
# Internal functions
#
source "../lib/internals/action.sh"
source "../lib/internals/compile-dotfile.sh"
source "../lib/internals/create-symlink.sh"
source "../lib/internals/execute-dotfile.sh"
source "../lib/internals/locate-dotfile.sh"
source "../lib/internals/locate-target.sh"
source "../lib/internals/has-action.sh"
source "../lib/internals/register-action.sh"
source "../lib/internals/setup-root-link.sh"
#
# Main functions
# Dotify attributes
#
source "../lib/dotify-action.sh"
source "../lib/dotify-clean.sh"
source "../lib/dotify-compile.sh"
source "../lib/dotify-help.sh"
source "../lib/dotify-info.sh"
source "../lib/dotify-install.sh"
source "../lib/dotify-register-action.sh"
source "../lib/dotify-uninstall.sh"
source "../lib/dotify-version.sh"
source "../lib/attributes/default-action.sh"
source "../lib/attributes/dotfile-path.sh"
source "../lib/attributes/dry-run.sh"
source "../lib/attributes/root-link.sh"
source "../lib/attributes/run-mode.sh"
source "../lib/attributes/source-path.sh"
source "../lib/attributes/target-path.sh"
#
# Dotify commands
#
source "../lib/commands/clean.sh"
source "../lib/commands/compile.sh"
source "../lib/commands/help.sh"
source "../lib/commands/info.sh"
source "../lib/commands/install.sh"
source "../lib/commands/print-version.sh"
source "../lib/commands/uninstall.sh"
source "../lib/commands/version.sh"
#
# Built-in action plugins
@@ -73,41 +86,34 @@ source "../lib/dotfile-commands/root_link.sh"
source "../lib/dotfile-commands/default_action.sh"
source "../lib/dotfile-commands/include.sh"
#
# Default Options
#
DOTIFY_OPT_ROOT_LINK=".dotfiles"
DOTIFY_OPT_DEFAULT_ACTION="link"
#
# Argument Parsing
#
DOTFILE="" # --dotfile / -f
TARGET="" # --target / -t
DRY_RUN="" # --dry-run / -d
HELP="" # --help / -h
VERSION="" # --version / -v
ARG_DOTFILE="" # --dotfile / -f
ARG_TARGET="" # --target / -t
ARG_DRY_RUN="" # --dry-run / -d
ARG_HELP="" # --help / -h
ARG_VERSION="" # --version / -v
if has-argument dotfile f "$@"; then
DOTFILE="$(parse-argument dotfile f "$@")"
ARG_DOTFILE="$(parse-argument dotfile f "$@")"
fi
if has-argument target t "$@"; then
TARGET="$(parse-argument target t "$@")"
ARG_TARGET="$(parse-argument target t "$@")"
fi
if has-argument dry-run d "$@"; then
DRY_RUN="1"
ARG_DRY_RUN="1"
fi
if has-argument help h "$@"; then
HELP="1"
ARG_HELP="1"
fi
if has-argument version v "$@"; then
VERSION="1"
ARG_VERSION="1"
fi
@@ -128,33 +134,33 @@ for arg in "$@"; do
done
# Show help and exit if help arguments or command are given.
if [ -n "$HELP" ] || [ "$command" == "help" ]; then
dotify-help
if [ -n "$ARG_HELP" ] || [ "$command" == "help" ]; then
dotify-command-help
exit
fi
# Show version info and exit if version arguments or command are given.
if [ -n "$VERSION" ] || [ "$command" == "version" ]; then
dotify-help | head -1
if [ -n "$ARG_VERSION" ] || [ "$command" == "version" ]; then
dotify-command-help | head -1
exit
fi
# Deal with the commands.
case "$command" in
"info" )
dotify-info
dotify-command-info
;;
"compile" )
dotify-compile
dotify-command-compile
;;
"" | "install" )
dotify-install
dotify-command-install
;;
"uninstall" | "remove" )
dotify-uninstall
"uninstall" )
dotify-command-uninstall
;;
"clean" )
dotify-clean
dotify-command-clean
;;
esac

View File

@@ -0,0 +1,11 @@
dotify-set-default-action() {
DOTIFY_ATTR_DEFAULT_ACTION="$1"
}
dotify-get-default-action() {
if [ -z "$DOTIFY_ATTR_DEFAULT_ACTION" ]; then
DOTIFY_ATTR_DEFAULT_ACTION="link" # Default value.
fi
echo "$DOTIFY_ATTR_DEFAULT_ACTION"
}

View File

@@ -0,0 +1,27 @@
dotify-get-dotfile-path() {
if [ -n "$DOTIFY_ATTR_DOTFILE_PATH" ]; then
echo "$DOTIFY_ATTR_DOTFILE_PATH"
return 0
fi
if [ -n "$ARG_DOTFILE" ]; then
if [ -f "$ARG_DOTFILE" ]; then
DOTIFY_ATTR_DOTFILE_PATH="$ARG_DOTFILE"
else
echo "ERROR: \"$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 "$?"
}

View File

@@ -0,0 +1,7 @@
dotify-get-dry-run() {
if [ -n "$DOTIFY_ATTR_DRY_RUN" ]; then
DOTIFY_ATTR_DRY_RUN="$ARG_DRY_RUN"
fi
echo "$DOTIFY_ATTR_DRY_RUN"
}

View File

@@ -0,0 +1,11 @@
dotify-set-root-link() {
DOTIFY_ATTR_ROOT_LINK="$1"
}
dotify-get-root-link() {
if [ -z "$DOTIFY_ATTR_ROOT_LINK" ]; then
DOTIFY_ATTR_ROOT_LINK=".dotfiles" # Default value.
fi
echo "$DOTIFY_ATTR_ROOT_LINK"
}

View File

@@ -0,0 +1,7 @@
dotify-set-run-mode() {
DOTIFY_ATTR_RUN_MODE="$1"
}
dotify-get-run-mode() {
echo "$DOTIFY_ATTR_RUN_MODE"
}

View File

@@ -0,0 +1,11 @@
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 "$?"
}

View File

@@ -0,0 +1,29 @@
dotify-get-target-path() {
if [ -n "$DOTIFY_ATTR_TARGET" ]; then
echo "$DOTIFY_ATTR_TARGET"
return 0
fi
if [ -n "$ARG_TARGET" ]; then
if [ -d "$ARG_TARGET" ]; then
DOTIFY_ATTR_TARGET="$ARG_TARGET"
else
echo "ERROR: Target \"$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 "$?"
}

View File

@@ -0,0 +1,5 @@
dotify-command-clean() {
dotify-set-run-mode "clean"
dotify-execute-dotfile
return $?
}

View File

@@ -0,0 +1,7 @@
dotify-command-compile() {
dotify-valid-dotfile-path
if [ "$?" != "0" ]; then return 1; fi
dotify-compile-dotfile "$DOTFILE"
return $?
}

4
src/lib/commands/help.sh Normal file
View File

@@ -0,0 +1,4 @@
dotify-command-help() {
echo "$(dotify-command-print-version)"
echo "usage: dotify <command> [<args>]"
}

15
src/lib/commands/info.sh Normal file
View File

@@ -0,0 +1,15 @@
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)"
}

View File

@@ -0,0 +1,5 @@
dotify-command-install() {
dotify-set-run-mode "install"
dotify-execute-dotfile
return $?
}

View File

@@ -0,0 +1,3 @@
dotify-command-print-version() {
echo "dotify $(dotify-command-version)"
}

View File

@@ -0,0 +1,5 @@
dotify-command-uninstall() {
dotify-set-run-mode "uninstall"
dotify-execute-dotfile
return $?
}

View File

@@ -0,0 +1,3 @@
dotify-command-version() {
echo "0.0.1"
}

View File

@@ -1,3 +1,3 @@
default_action() {
DOTIFY_OPT_DEFAULT_ACTION="$@"
dotify-set-default-action "$@"
}

View File

@@ -1,3 +1,3 @@
root_link () {
DOTIFY_OPT_ROOT_LINK="$@"
dotify-set-root-link "$@"
}

View File

@@ -1,5 +0,0 @@
dotify-clean() {
DOTIFY_RUN_MODE="clean"
execute-dotfile
return $?
}

View File

@@ -1,7 +0,0 @@
dotify-compile() {
locate-dotfile
if [ "$?" != "0" ]; then return 1; fi
compile-dotfile "$DOTFILE"
return $?
}

View File

@@ -1,4 +0,0 @@
dotify-help() {
echo "$(dotify-print-version)"
echo "usage: dotify <command> [<args>]"
}

View File

@@ -1,12 +0,0 @@
dotify-info() {
locate-dotfile
if [ "$?" != "0" ]; then return 1; fi
locate-target
if [ "$?" != "0" ]; then return 1; fi
echo "$(dotify-print-version)"
echo " Dotfile: $DOTFILE"
echo " Root: $(dirname "$DOTFILE")"
echo " Target: $TARGET"
}

View File

@@ -1,5 +0,0 @@
dotify-install() {
DOTIFY_RUN_MODE="install"
execute-dotfile
return $?
}

View File

@@ -1,4 +0,0 @@
dotify-register-action() {
if [ -z "$DOTIFY_ACTIONS" ]; then DOTIFY_ACTIONS=(); fi
DOTIFY_ACTIONS+=("$1")
}

View File

@@ -1,5 +0,0 @@
dotify-uninstall() {
DOTIFY_RUN_MODE="uninstall"
execute-dotfile
return $?
}

View File

@@ -1,7 +0,0 @@
dotify-version() {
echo "0.0.1"
}
dotify-print-version() {
echo "dotify $(dotify-version)"
}

View File

@@ -8,8 +8,10 @@ dotify-action() {
local target="$3"
fi
dotify-setup-root-link
if [ "$action" == "default" ]; then
action="$DOTIFY_OPT_DEFAULT_ACTION"
action="$(dotify-get-default-action)"
fi
! local valid_action="$(command -v "dotify-action-${action}")"
@@ -19,8 +21,8 @@ dotify-action() {
fi
if [ -n "$source" ]; then
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" "$source"
dotify-action-${action} "$(dotify-get-run-mode)" "$target" "$source"
else
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target"
dotify-action-${action} "$(dotify-get-run-mode)" "$target"
fi
}

View File

@@ -1,6 +1,6 @@
compile-dotfile() {
dotify-compile-dotfile() {
local dotfile="$1"
if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi
if [ -z "$dotfile" ]; then dotfile="$(dotify-get-dotfile-path)"; fi
if [ ! -f "$dotfile" ]; then
echo "ERROR: \"$dotfile\" does not exist." >&2

View File

@@ -1,12 +1,16 @@
create-symlink() {
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
if [ "$(readlink "$target")" == "$source" ]; then
echo "exists"
return 0
else
echo "ERROR: \"$target\" exists, is a symlink to:" \
"$(readlink "$target")" >&2
return 1

View File

@@ -1,11 +1,9 @@
execute-dotfile() {
local dotfile_source="$(dotify-compile)"
dotify-execute-dotfile() {
local dotfile_source="$(dotify-command-compile)"
locate-target
dotify-valid-target-path
if [ "$?" != "0" ]; then return 1; fi
ROOT_DIR="$(dirname "$DOTFILE")"
eval "$dotfile_source"
return $?
}

View File

@@ -0,0 +1,5 @@
dotify-has-action() {
if [[ " ${DOTIFY_ACTIONS[@]} " != *" $1 "* ]]; then
return 1
fi
}

View File

@@ -1,13 +0,0 @@
locate-dotfile() {
if [ -n "$DOTFILE" ]; then
if [ ! -f "$DOTFILE" ]; then
echo "ERROR: \"$DOTFILE\" does not exist." >&2
return 1
fi
elif [ -f "$(pwd)/Dotfile" ]; then
DOTFILE="$(pwd)/Dotfile"
else
echo "ERROR: \"$(pwd)\" does not have a Dotfile." >&2
return 1
fi
}

View File

@@ -1,15 +0,0 @@
locate-target() {
if [ -n "$TARGET" ]; then
if [ ! -d "$TARGET" ]; then
echo "ERROR: Target \"$TARGET\" is not a directory." >&2
return 1
fi
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
TARGET="$HOME"
elif [ -d ~ ]; then
TARGET=~
else
echo "ERROR: Your \$HOME folder could not be found." >&2
return 1
fi
}

View File

@@ -0,0 +1,11 @@
dotify-register-action() {
if [ -z "$DOTIFY_ACTIONS" ]; then
DOTIFY_ACTIONS=()
fi
if [[ " ${DOTIFY_ACTIONS[@]} " == *" $1 "* ]]; then
return 1
fi
DOTIFY_ACTIONS+=("$1")
}

View File

@@ -0,0 +1,3 @@
dotify-setup-root-link() {
return 0
}

View File

@@ -0,0 +1,33 @@
#! /usr/bin/env bash
source "../test-helper.sh"
source "../../src/lib/dotify-action.sh"
#
# Integration test: link action
#
# Create temp files/folders used for tests.
TEST_SOURCE="tmp/source"
TEST_TARGET="tmp/target"
MY_DOTFILE="$TEST_SOURCE/Dotfile"
mkdir -p "$TEST_SOURCE" "$TEST_TARGET"
PROFILE_TXT="# I am a .profile file"
echo "$PROFILE_TXT" > "tmp/source/profile"
# Basic of basics.
echo -e "profile -> .profile" > "$MY_DOTFILE"
assert_raises "../../bin/dotify -f '$MY_DOTFILE' -t '$TEST_TARGET'" 0
assert "../../bin/dotify -f '$MY_DOTFILE' -t '$TEST_TARGET'" \
" Create symlink: $TEST_TARGET/.profile -> .dotfiles/profile"
assert "readlink '$TEST_TARGET/.profile'" ".dotfiles/profile"
assert "cat '$TEST_TARGET/.profile'" "$PROFILE_TXT"
rm "TEST_TARGET/.profile"
rm "$MY_DOTFILE"
# Remove temp files/folders used for locate-dotfile() tests.
rm "tmp/source/profile"
rmdir "tmp/source" "tmp/target" "tmp"
assert_end 'Integration: link action'

View File

@@ -1,20 +1,15 @@
#! /usr/bin/env bash
source "../test-helper.sh"
source "../../src/lib/dotify-action.sh"
source "../../test-helper.sh"
source "../../../src/lib/internals/action.sh"
#
# dotify-action() tests
#
# Set required environment variables.
DOTIFY_RUN_MODE="install"
DOTIFY_OPT_DEFAULT_ACTION="link"
# Simple mock for link action.
dotify-action-link() {
echo "link stub: $@"
}
stub "dotify-setup-root-link"
stub_and_echo "dotify-get-run-mode" "install"
stub_and_echo "dotify-get-default-action" "link"
stub_and_echo "dotify-action-link" "link stub: \$@"
# Given a specific action.
assert "dotify-action link ackrc .ackrc" "link stub: install .ackrc ackrc"
@@ -34,4 +29,8 @@ assert_raises "dotify-action foo ackrc .ackrc" 1
assert "dotify-action foo ackrc .ackrc" ""
assert "dotify-action foo ackrc .ackrc 2>&1" "ERROR: \"foo\" is not a valid action."
restore "dotify-setup-root-link"
restore "dotify-get-run-mode"
restore "dotify-action-link"
assert_end "dotify-action()"

View File

@@ -4,7 +4,7 @@ source "../../../src/lib/helpers/trim.sh"
source "../../../src/lib/internals/compile-dotfile.sh"
#
# compile-dotfile() tests
# dotify-compile-dotfile() tests
#
# Create temp files/folders used for locate-dotfile() tests.
@@ -14,27 +14,28 @@ echo "hostname='air'" > $dotfile
# Compiles file given as first argument.
assert "compile-dotfile $dotfile" "hostname='air'"
assert_raises "compile-dotfile tmp/fake" 1
assert "compile-dotfile tmp/fake" ""
assert "compile-dotfile tmp/fake 2>&1" "ERROR: \"tmp/fake\" does not exist."
assert "dotify-compile-dotfile $dotfile" "hostname='air'"
assert_raises "dotify-compile-dotfile tmp/fake" 1
assert "dotify-compile-dotfile tmp/fake" ""
assert "dotify-compile-dotfile tmp/fake 2>&1" \
"ERROR: \"tmp/fake\" does not exist."
# Compiles file from global $DOTFILE variable.
DOTFILE=$dotfile
assert "compile-dotfile" "hostname='air'"
DOTFILE="tmp/fake"
assert_raises "compile-dotfile" 1
assert "compile-dotfile" ""
assert "compile-dotfile 2>&1" "ERROR: \"tmp/fake\" does not exist."
unset DOTFILE
# Compiles file from dotify-get-dotfile-path attribute helper.
stub_and_echo "dotify-get-dotfile-path" "$dotfile"
assert "dotify-compile-dotfile" "hostname='air'"
stub_and_echo "dotify-get-dotfile-path" "tmp/fake"
assert_raises "dotify-compile-dotfile" 1
assert "dotify-compile-dotfile" ""
assert "dotify-compile-dotfile 2>&1" "ERROR: \"tmp/fake\" does not exist."
restore "dotify-get-dotfile-path"
# Compiles standard actions
echo -e "root_link .dotfiles
link: ackrc -> .ackrc
link: gitconfig -> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles
assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action link ackrc .ackrc
dotify-action link gitconfig \".gitconfig\""
@@ -43,7 +44,7 @@ dotify-action link gitconfig \".gitconfig\""
echo -e "root_link .dotfiles
link: -> .ackrc
link:-> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles
assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action link .ackrc
dotify-action link \".gitconfig\""
@@ -52,7 +53,7 @@ dotify-action link \".gitconfig\""
echo -e "root_link .dotfiles
link: ackrc -> .ackrc
gitconfig -> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles
assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action link ackrc .ackrc
dotify-action default gitconfig \".gitconfig\""
@@ -61,7 +62,7 @@ dotify-action default gitconfig \".gitconfig\""
echo -e "root_link .dotfiles
-> .ackrc
-> \".gitconfig\"" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles
assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action default .ackrc
dotify-action default \".gitconfig\""
@@ -71,7 +72,7 @@ echo -e "root_link .dotfiles
if [ true ]; then
ackrc -> .ackrc
fi" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles
assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
if [ true ]; then
dotify-action default ackrc .ackrc
fi"
@@ -87,7 +88,7 @@ profile -> .profile
# Apps
ackrc -> .ackrc
gitconfig -> .gitconfig" > $dotfile
assert "compile-dotfile $dotfile" "root_link .dotfiles
assert "dotify-compile-dotfile $dotfile" "root_link .dotfiles
dotify-action default profile .profile
dotify-action default ackrc .ackrc
dotify-action default gitconfig .gitconfig"
@@ -98,4 +99,4 @@ rm "tmp/Dotfile"
rmdir "tmp"
assert_raises "test -d tmp" 1
assert_end 'compile-dotfile()'
assert_end 'dotify-compile-dotfile()'

View File

@@ -3,48 +3,53 @@ source "../../test-helper.sh"
source "../../../src/lib/internals/create-symlink.sh"
#
# create-symlink() tests
# dotify-create-symlink() tests
#
# Create temp files/folders used for create-symlink() tests.
# Create temp files/folders used for dotify-create-symlink() tests.
mkdir -p "tmp/source" "tmp/target"
touch "tmp/source/profile"
# Creates a normal symlink.
assert_raises 'create-symlink ../source tmp/target/.dotfiles' 0
assert_raises 'dotify-create-symlink ../source tmp/target/.dotfiles' 0
assert 'readlink tmp/target/.dotfiles' "../source"
rm "tmp/target/.dotfiles"
assert 'dotify-create-symlink ../source tmp/target/.dotfiles' "created"
rm "tmp/target/.dotfiles"
assert 'dotify-create-symlink ../source tmp/target/.dotfiles 2>&1' "created"
rm "tmp/target/.dotfiles"
# When target exists an is a symlink to the same source.
# When target exists and is a symlink to the same source.
ln -s "../source" "tmp/target/.dotfiles"
assert_raises 'create-symlink ../source tmp/target/.dotfiles' 0
assert 'create-symlink ../source tmp/target/.dotfiles 2>&1' ""
assert_raises 'dotify-create-symlink ../source tmp/target/.dotfiles' 0
assert 'dotify-create-symlink ../source tmp/target/.dotfiles' "exists"
assert 'dotify-create-symlink ../source tmp/target/.dotfiles 2>&1' "exists"
rm "tmp/target/.dotfiles"
# When target exists and is a symlink to a different source.
ln -s "../other" "tmp/target/.dotfiles"
assert_raises 'create-symlink ../source tmp/target/.dotfiles' 1
assert 'create-symlink ../source tmp/target/.dotfiles' ""
assert 'create-symlink ../source tmp/target/.dotfiles 2>&1' \
assert_raises 'dotify-create-symlink ../source tmp/target/.dotfiles' 1
assert 'dotify-create-symlink ../source tmp/target/.dotfiles' ""
assert 'dotify-create-symlink ../source tmp/target/.dotfiles 2>&1' \
"ERROR: \"tmp/target/.dotfiles\" exists, is a symlink to: ../other"
rm "tmp/target/.dotfiles"
# When target exists and is a file.
touch "tmp/target/.profile"
assert_raises 'create-symlink ../source/profile tmp/target/.profile' 1
assert 'create-symlink ../source/profile tmp/target/.profile' ""
assert 'create-symlink ../source/profile tmp/target/.profile 2>&1' \
assert_raises 'dotify-create-symlink ../source/profile tmp/target/.profile' 1
assert 'dotify-create-symlink ../source/profile tmp/target/.profile' ""
assert 'dotify-create-symlink ../source/profile tmp/target/.profile 2>&1' \
"ERROR: \"tmp/target/.profile\" exists"
rm "tmp/target/.profile"
# When target exists and is a directory.
assert_raises 'create-symlink ../source tmp/target' 1
assert 'create-symlink ../source tmp/target' ""
assert 'create-symlink ../source tmp/target 2>&1' \
assert_raises 'dotify-create-symlink ../source tmp/target' 1
assert 'dotify-create-symlink ../source tmp/target' ""
assert 'dotify-create-symlink ../source tmp/target 2>&1' \
"ERROR: \"tmp/target\" exists"
# Remove temp files/folders used for locate-dotfile() tests.
rm "tmp/source/profile"
rmdir "tmp/source" "tmp/target" "tmp"
assert_end 'create-symlink()'
assert_end 'dotify-create-symlink()'

View File

@@ -0,0 +1,32 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/register-action.sh"
source "../../../src/lib/internals/has-action.sh"
#
# dotify-register-action() tests
#
# When no actions are registered.
assert_raises 'dotify-has-action "link"' 1
# When checking an action that is registered.
dotify-register-action "link"
assert_raises 'dotify-has-action "link"' 0
unset DOTIFY_ACTIONS
# When checking an action that is not registered.
dotify-register-action "link"
assert_raises 'dotify-has-action "git"' 1
unset DOTIFY_ACTIONS
# When registering and checking multiple actions
dotify-register-action "link"
dotify-register-action "git"
assert_raises 'dotify-has-action "link"' 0
assert_raises 'dotify-has-action "git"' 0
assert_raises 'dotify-has-action "copy"' 1
unset DOTIFY_ACTIONS
assert_end "dotify-register-action() tests"

View File

@@ -1,51 +0,0 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/locate-dotfile.sh"
#
# locate-dotfile() tests
#
# Create temp files/folders used for locate-dotfile() tests.
mkdir -p "test-tmp/with" "test-tmp/without"
touch "test-tmp/with/Dotfile"
# When $DOTFILE is empty and current path has a Dotfile.
cd "test-tmp/with"
DOTFILE=""
assert_raises 'locate-dotfile' 0
assert 'locate-dotfile; echo "$DOTFILE"' "$(pwd)/Dotfile"
unset DOTFILE
cd-back
# When $DOTFILE is empty and current path does not have a Dotfile.
cd "test-tmp/without"
DOTFILE=""
assert_raises 'locate-dotfile' 1
assert 'locate-dotfile 2>&1' "ERROR: \"$(pwd)\" does not have a Dotfile."
assert 'locate-dotfile; echo "$DOTFILE"' ""
unset DOTFILE
cd-back
# When $DOTFILE is not empty and points at a existing Dotfile.
DOTFILE="test-tmp/with/Dotfile"
assert_raises 'locate-dotfile' 0
assert 'locate-dotfile; echo "$DOTFILE"' "$DOTFILE"
unset DOTFILE
# When $DOTFILE is not empty and points at a non-existing Dotfile.
DOTFILE="test-tmp/without/Dotfile"
assert_raises 'locate-dotfile' 1
assert 'locate-dotfile 2>&1' "ERROR: \"$DOTFILE\" does not exist."
assert 'locate-dotfile; echo "$DOTFILE"' "$DOTFILE"
unset DOTFILE
# Remove temp files/folders used for locate-dotfile() tests.
rm "test-tmp/with/Dotfile"
rmdir "test-tmp/with" "test-tmp/without" "test-tmp"
# Ensure temp files/folder were cleaned up.
assert_raises "test -d test-tmp" 1
# End of locate-dotfile() tests.
assert_end 'locate-dotfile()'

View File

@@ -1,36 +0,0 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/locate-target.sh"
#
# locate-target() tests
#
# When $TARGET is empty.
TARGET=""
assert_raises 'locate-target' 0
assert 'locate-target; echo "$TARGET"' "$HOME"
unset TARGET
# When $TARGET is not empty and is a directory.
TARGET="$(pwd)"
assert_raises 'locate-target' 0
assert 'locate-target; echo "$TARGET"' "$(pwd)"
unset TARGET
# When $TARGET is not empty and is not a directory.
TARGET="/tmp/this/does/not/exist"
assert_raises 'locate-target' 1
assert 'locate-target 2>&1' "ERROR: Target \"$TARGET\" is not a directory."
assert 'locate-target; echo "$TARGET"' "$TARGET"
unset TARGET
# If neither $TARGET or $HOME is set, ~ is expanded to find home folder
original_home="$HOME"
unset HOME
assert_raises 'locate-target' 0
assert 'locate-target; echo "$TARGET"' "$original_home"
HOME="$original_home"
# End of locate-target() tests.
assert_end 'locate-target()'

View File

@@ -0,0 +1,28 @@
#! /usr/bin/env bash
source "../../test-helper.sh"
source "../../../src/lib/internals/register-action.sh"
#
# dotify-register-action() tests
#
# Registering an action.
dotify-register-action "link"
assert 'echo ${DOTIFY_ACTIONS[@]}' "link"
unset DOTIFY_ACTIONS
# Registering multiple actions.
dotify-register-action "link"
dotify-register-action "git"
assert 'echo ${DOTIFY_ACTIONS[@]}' "link git"
unset DOTIFY_ACTIONS
# Registering the same action multiple times.
dotify-register-action "link"
dotify-register-action "link"
assert 'echo ${DOTIFY_ACTIONS[@]}' "link"
assert_raises 'dotify-register-action "link"' 1
unset DOTIFY_ACTIONS
assert_end "dotify-register-action() tests"