Reorganize internal functions

This commit is contained in:
2013-10-03 22:59:38 +01:00
parent ffdfc5f889
commit 4efde780d4
11 changed files with 188 additions and 146 deletions

View File

@@ -37,8 +37,11 @@ source "../lib/trim.sh"
# Internal functions
#
source "../lib/parse-dotfile-options.sh"
source "../lib/internals.sh"
source "../lib/internals/compile-dotfile.sh"
source "../lib/internals/create-symlink.sh"
source "../lib/internals/locate-dotfile.sh"
source "../lib/internals/locate-target.sh"
source "../lib/internals/parse-dotfile-options.sh"
#
# Command functions

View File

@@ -1,93 +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
}
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
}
create-symlink() {
local source="$1"
local target="$2"
}
execute-dotfile() {
parse-dotfile-options
local root_dir="$(dirname "$DOTFILE")"
local cwd="$(pwd)"
cd "$root_dir"
create-symlink "$root_dir" "$TARGET/$OPT_ROOT_LINK"
if [ -n "$?" ]; then return 1; fi
while read line; do
parse-dotfile-line "$line"
if [ -n "$?" ]; then return 1; fi
done < "$DOTFILE"
cd "$cwd"
}
parse-dotfile-line() {
local line="$(trim "$1")"
local dotfile="$DOTFILE"
if [ -n "$2" ]; then dotfile="$2"; fi
# Ignore comment lines starting with "#".
if [[ "$line" == "#"* ]]; then return 0; fi
# Ignore Dotfile options.
if [[ "$line" == "root_link "* ]]; then return 0; fi
if [[ "$line" == "default_action "* ]]; then return 0; fi
# Handle include command.
if [[ "$line" == "include "* ]]; then
include-dotfile "$(trim "${line/#include /}")"
return "$?"
fi
echo "$line"
}
include-dotfile() {
local dotfile="$1"
local root_dir="$(dirname "$dotfile")"
local cwd="$(pwd)"
cd "$root_dir"
if [ ! -f "$dotfile" ]; then
echo "ERROR: Can not include \"$dotfile\", it does not exist." >&2
return 1
fi
while read line; do
parse-dotfile-line "$line" "$dotfile"
if [ -n "$?" ]; then return 1; fi
done < "$dotfile"
cd "$cwd"
}

View File

@@ -0,0 +1,31 @@
compile-dotfile() {
local dotfile="$1"
if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; 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 "<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"
# Append line without modifications.
else
output="${output}${line}\n"
fi
done < "$dotfile"
echo -e "$output"
}

View File

@@ -0,0 +1,18 @@
create-symlink() {
local source="$1"
local target="$2"
if [ ! -e "$target" ] && [ ! -h "$target" ]; then
ln -s "$source" "$target"
return 0
elif [ -h "$target" ]; then
if [ "$(readlink "$target")" != "$source" ]; then
echo "ERROR: \"$target\" exists, is a symlink to:" \
"$(readlink "$target")" >&2
return 1
fi
else
echo "ERROR: \"$target\" exists" >&2
return 1
fi
}

View File

@@ -0,0 +1,13 @@
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

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

@@ -1,7 +1,17 @@
# Parse Dotfile options and set relevant global variables.
parse-dotfile-options() {
OPT_ROOT_LINK="$(parse-dotfile-root_link-option)"
OPT_DEFAULT_ACTION="$(parse-dotfile-default_action-option)"
DOTIFY_OPT_ROOT_LINK="$(parse-dotfile-root_link-option)"
DOTIFY_OPT_DEFAULT_ACTION="$(parse-dotfile-default_action-option)"
}
# Parse root_link option.
parse-dotfile-root_link-option() {
echo "$(parse-dotfile-option "root_link" ".dotfiles" "$1")"
}
# Parse default_action option.
parse-dotfile-default_action-option() {
echo "$(parse-dotfile-option "default_action" "link" "$1")"
}
# Extract a specific option from Dotfile.
@@ -17,6 +27,7 @@ parse-dotfile-option() {
local dotfile="$3"
if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi
local line=""
while read line; do
if [[ "$line" == "$name "* ]]; then
value="$(trim "${line/#$name }")"
@@ -26,11 +37,3 @@ parse-dotfile-option() {
echo "$value"
}
parse-dotfile-root_link-option() {
echo "$(parse-dotfile-option "root_link" ".dotfiles" "$1")"
}
parse-dotfile-default_action-option() {
echo "$(parse-dotfile-option "default_action" "link" "$1")"
}