Add new binary build

This commit is contained in:
2013-10-03 23:38:47 +01:00
parent 7ca1911636
commit e682ade7a8

View File

@@ -1,5 +1,6 @@
#! /usr/bin/env bash #! /usr/bin/env bash
set -e set -e
shopt -s extglob
[ -n "$DOTIFY_DEBUG" ] && set -x [ -n "$DOTIFY_DEBUG" ] && set -x
# dotify 0.0.1 # dotify 0.0.1
@@ -116,43 +117,65 @@ trim() {
# Internal functions # Internal functions
# #
parse-dotfile-options() { compile-dotfile() {
OPT_ROOT_LINK="$(parse-dotfile-root_link-option)" local dotfile="$1"
OPT_DEFAULT_ACTION="$(parse-dotfile-default_action-option)" if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi
}
parse-dotfile-root_link-option() { local output=""
local dotfile="$DOTFILE" local line=""
if [ -n "$1" ]; then dotfile="$1"; fi while IFS= read line; do
# Ignore comments and blank lines.
if [[ "$line" =~ ^(\ *\#.*|\ *)$ ]]; then
continue
# Set default. # Parse "<action>: <source> -> <target>" lines.
local root_link=".dotfiles" 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"
while read line; do # Parse "<source> -> <target>" lines.
if [[ "$line" == "root_link "* ]]; then elif [[ "$line" =~ ^(\ +)?(.+)\ -[\>]\ (.+)$ ]]; then
root_link="$(trim "${line/#root_link /}")" output="${output}${BASH_REMATCH[1]}dotify-action default "
break output="${output}$(trim "${BASH_REMATCH[2]}") "
output="${output}$(trim "${BASH_REMATCH[3]}")\n"
# Append line without modifications.
else
output="${output}${line}\n"
fi fi
done < "$dotfile" done < "$dotfile"
echo "$root_link" echo -e "$output"
} }
parse-dotfile-default_action-option() { create-symlink() {
local dotfile="$DOTFILE" local source="$1"
if [ -n "$1" ]; then dotfile="$1"; fi local target="$2"
# Set default value. if [ ! -e "$target" ] && [ ! -h "$target" ]; then
default_action="link" ln -s "$source" "$target"
return 0
while read line; do elif [ -h "$target" ]; then
if [[ "$line" == "default_action "* ]]; then if [ "$(readlink "$target")" != "$source" ]; then
default_action="$(trim "${line/#default_action /}")" echo "ERROR: \"$target\" exists, is a symlink to:" \
break "$(readlink "$target")" >&2
return 1
fi fi
done < "$dotfile" else
echo "ERROR: \"$target\" exists" >&2
return 1
fi
}
echo "$default_action" execute-dotfile() {
local dotfile_source="$(dotify-compile)"
locate-target
if [ "$?" != "0" ]; then return 1; fi
echo "$dotfile_source"
return $?
} }
locate-dotfile() { locate-dotfile() {
@@ -185,68 +208,44 @@ locate-target() {
fi fi
} }
create-symlink() { # Parse Dotfile options and set relevant global variables.
local source="$1" parse-dotfile-options() {
local target="$2" DOTIFY_OPT_ROOT_LINK="$(parse-dotfile-root_link-option)"
DOTIFY_OPT_DEFAULT_ACTION="$(parse-dotfile-default_action-option)"
} }
execute-dotfile() { # Parse root_link option.
parse-dotfile-options parse-dotfile-root_link-option() {
local root_dir="$(dirname "$DOTFILE")" echo "$(parse-dotfile-option "root_link" ".dotfiles" "$1")"
}
local cwd="$(pwd)" # Parse default_action option.
cd "$root_dir" parse-dotfile-default_action-option() {
echo "$(parse-dotfile-option "default_action" "link" "$1")"
}
create-symlink "$root_dir" "$TARGET/$OPT_ROOT_LINK" # Extract a specific option from Dotfile.
if [ -n "$?" ]; then return 1; fi #
# Arguments:
# - $1: Name of option to extract.
# - $2: (optional) Default value of option if not present in Dotfile.
# - $3: (optional) Specific Dotfile to read. Uses $DOTFILE if empty.
#
parse-dotfile-option() {
local name="$1"
local value="$2"
local dotfile="$3"
if [ -z "$dotfile" ]; then dotfile="$DOTFILE"; fi
local line=""
while read line; do while read line; do
parse-dotfile-line "$line" if [[ "$line" == "$name "* ]]; then
if [ -n "$?" ]; then return 1; fi value="$(trim "${line/#$name }")"
done < "$DOTFILE" break
fi
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" done < "$dotfile"
cd "$cwd" echo "$value"
} }
@@ -268,47 +267,73 @@ dotify-help() {
} }
dotify-info() { dotify-info() {
local dotfile="$(locate-dotfile "$@")" locate-dotfile
if [ -z "$dotfile" ]; then return 1; fi if [ "$?" != "0" ]; then return 1; fi
local target="$(locate-target "$@")" locate-target
if [ -z "$target" ]; then return 1; fi if [ "$?" != "0" ]; then return 1; fi
echo "dotify $(dotify-version)" echo "$(dotify-print-version)"
echo " Dotfile: $dotfile" echo " Dotfile: $DOTFILE"
echo " Root: $(dirname "$dotfile")" echo " Root: $(dirname "$DOTFILE")"
echo " Target: $target" echo " Target: $TARGET"
}
dotify-compile() {
locate-dotfile
if [ "$?" != "0" ]; then return 1; fi
compile-dotfile
return $?
} }
dotify-install() { dotify-install() {
locate-dotfile DOTIFY_RUN_MODE="install"
if [ -n "$?" ]; then return 1; fi
locate-target
if [ -n "$?" ]; then return 1; fi
execute-dotfile execute-dotfile
return $? return $?
} }
#
# Built-in Plugins
#
dotify-action-link() {
echo "link: $@"
}
dotify-action-git() {
echo "git: $@"
}
# #
# Argument Parsing # Argument Parsing
# #
DOTFILE="" # --dotfile / -f DOTFILE="" # --dotfile / -F
TARGET="" # --target / -t TARGET="" # --target / -t
FORCE="" # --force / -f
DRY_RUN="" # --dry-run / -d
HELP="" # --help / -h HELP="" # --help / -h
VERSION="" # --version / -v VERSION="" # --version / -v
if has-argument dotfile f "$@"; then if has-argument dotfile F "$@"; then
DOTFILE="$(parse-argument dotfile f "$@")" DOTFILE="$(parse-argument dotfile F "$@")"
fi fi
if has-argument target t "$@"; then if has-argument target t "$@"; then
TARGET="$(parse-argument target t "$@")" TARGET="$(parse-argument target t "$@")"
fi fi
if has-argument dry-run d "$@"; then
DRY_RUN="1"
fi
if has-argument force f "$@"; then
FORCE="1"
fi
if has-argument help h "$@"; then if has-argument help h "$@"; then
HELP="1" HELP="1"
fi fi
@@ -324,7 +349,11 @@ fi
# Command is first argument that does not start with a dash or plus. # Command is first argument that does not start with a dash or plus.
for arg in "$@"; do for arg in "$@"; do
if [[ "$arg" != "-"* ]] && [[ "$arg" != "+"* ]]; then if [ -n "$skip_next" ]; then
skip_next=
elif [[ "$arg" =~ ^(--dotfile|-f|--taraget|-t)$ ]]; then
skip_next=1
elif [[ "$arg" != "-"* ]] && [[ "$arg" != "+"* ]]; then
command="$arg" command="$arg"
break break
fi fi
@@ -347,10 +376,15 @@ case "$command" in
"info" ) "info" )
dotify-info dotify-info
;; ;;
"compile" )
dotify-compile
;;
"" | "install" ) "" | "install" )
dotify-install dotify-install
;; ;;
"uninstall" | "remove" )
dotify-uninstall
;;
esac esac
exit $? exit $?