Skip to content

Commit

Permalink
[feat] use configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
John authored and John committed Feb 14, 2021
1 parent ae9f663 commit b51d50e
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions asimov
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,60 @@ set -Eeu -o pipefail
# @license MIT

readonly ASIMOV_ROOT=~
readonly ASIMOV_CONFIG_DIR=~/.asimov
readonly ASIMOV_VENDOR_DIR_SENTINELS_FILE=${ASIMOV_CONFIG_DIR}/sentinels
readonly ASIMOV_SKIP_PATHS_FILE=${ASIMOV_CONFIG_DIR}/skip

# create config folder
if [ ! -d "${ASIMOV_CONFIG_DIR}" ]
then
echo "create config folder ${ASIMOV_CONFIG_DIR}"
mkdir ${ASIMOV_CONFIG_DIR}
fi

# Paths to unconditionally skip over. This prevents Asimov from modifying the
# Time Machine exclusions for these paths (and decendents). It has an important
# side-effect of speeding up the search.
readonly ASIMOV_SKIP_PATHS=(
~/.Trash
~/Library
)
if [ ! -f "$ASIMOV_SKIP_PATHS_FILE" ]; then
echo "init config file ${ASIMOV_SKIP_PATHS_FILE}"
echo "~/.Trash" >> ${ASIMOV_SKIP_PATHS_FILE}
echo "~/Library" >> ${ASIMOV_SKIP_PATHS_FILE}
fi

ASIMOV_SKIP_PATHS=()
while IFS= read -r line || [[ "$line" ]]; do
ASIMOV_SKIP_PATHS+=("$(eval echo "$line")")
done < ${ASIMOV_SKIP_PATHS_FILE}


# A list of "directory"/"sentinel" pairs.
#
# Directories will only be excluded if the dependency ("sentinel") file exists.
#
# For example, 'node_modules package.json' means "exclude node_modules/ from the
# Time Machine backups if there is a package.json file next to it."
readonly ASIMOV_VENDOR_DIR_SENTINELS=(
'.build Package.swift' # Swift
'.packages pubspec.yaml' # Pub (Dart)
'.stack-work stack.yaml' # Stack (Haskell)
'.vagrant Vagrantfile' # Vagrant
'Carthage Cartfile' # Carthage
'Pods Podfile' # CocoaPods
'bower_components bower.json' # Bower (JavaScript)
'node_modules package.json' # npm, Yarn (NodeJS)
'target Cargo.toml' # Cargo (Rust)
'target pom.xml' # Maven
'build build.gradle' # Gradle
'vendor composer.json' # Composer (PHP)
'vendor Gemfile' # Bundler (Ruby)
)
if [ ! -f "$ASIMOV_VENDOR_DIR_SENTINELS_FILE" ]
then
echo "init config file ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}"
echo ".build Package.swift # Swift" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo ".packages pubspec.yaml # Pub (Dart)" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo ".stack-work stack.yaml # Stack (Haskell)" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo ".vagrant Vagrantfile # Vagrant" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "Carthage Cartfile # Carthage" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "Pods Podfile # CocoaPods" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "bower_components bower.json # Bower (JavaScript)" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "node_modules package.json # npm, Yarn (NodeJS)" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "target Cargo.toml # Cargo (Rust)" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "target pom.xml # Maven" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "build build.gradle # Gradle" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "vendor composer.json # Composer (PHP)" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
echo "vendor Gemfile # Bundler (Ruby)" >> ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}
fi

ASIMOV_VENDOR_DIR_SENTINELS=()
while IFS= read -r line || [[ "$line" ]]; do
ASIMOV_VENDOR_DIR_SENTINELS+=("$line")
done < ${ASIMOV_VENDOR_DIR_SENTINELS_FILE}

# Exclude the given paths from Time Machine backups.
# Reads the newline-separated list of paths from stdin.
Expand All @@ -69,12 +93,23 @@ exclude_file() {
# Iterate over the skip directories to construct the `find` expression.
declare -a find_parameters_skip=()
for d in "${ASIMOV_SKIP_PATHS[@]}"; do
# safety against empty lines in the config file
if [ -z "${d}" ]
then
continue
fi
find_parameters_skip+=( -not \( -path "${d}" -prune \) )
done

# Iterate over the directory/sentinel pairs to construct the `find` expression.
declare -a find_parameters_vendor=()
for i in "${ASIMOV_VENDOR_DIR_SENTINELS[@]}"; do
# safety against empty lines in the config file
if [ -z "${i}" ]
then
continue
fi

read -ra parts <<< "${i}"

# Add this folder to the `find` list, allowing a single `find` command to find all
Expand Down

0 comments on commit b51d50e

Please sign in to comment.