Skip to content

Commit

Permalink
Merge pull request #4 from mbland/walkers
Browse files Browse the repository at this point in the history
Rename lib/process-files to lib/walk; add apps-home
  • Loading branch information
mbland authored Aug 24, 2017
2 parents 4e52b76 + 91e015e commit ca89c1c
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 89 deletions.
4 changes: 3 additions & 1 deletion dotfiles/.bash_dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ umask 022
APPS_HOME="/usr/local/mbland"

for script in "$APPS_HOME"/etc/profile.d/*; do
. "$script"
if [[ -f "$script" ]]; then
. "$script"
fi
done

PS1="\[\033[01;32m\]\u@\h \[\033[00m\]\[\033[01;34m\]\w\$\[\033[00m\] "
Expand Down
6 changes: 3 additions & 3 deletions scripts/diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

export DIFF_EDITOR="${DIFF_EDITOR:-vimdiff}"

. "$_GO_USE_MODULES" 'log' 'process-files'
. "$_GO_USE_MODULES" 'log' 'walk'

_diff_edit_if_different() {
if [[ ! -f "$1" ]]; then
Expand All @@ -26,8 +26,8 @@ _diff_user_bin_script() {
}

_diff() {
process_dotfiles _diff_dotfile
process_user_bin_scripts _diff_user_bin_script
walk_dotfiles _diff_dotfile
walk_user_bin_scripts _diff_user_bin_script
}

_diff "$@"
5 changes: 2 additions & 3 deletions scripts/install.d/dotfiles
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Installs dotfiles in the user's `HOME` directory

. "$_GO_USE_MODULES" 'copy'
. "$_GO_USE_MODULES" 'copy' 'walk'

_dotfiles_add_source_bash_dev() {
local candidates=(
Expand Down Expand Up @@ -35,8 +35,7 @@ _dotfiles() {
local dotfile

@go.log INFO Copying dotfiles into "$HOME"
. "$_GO_USE_MODULES" 'process-files'
process_dotfiles _dotfiles_copy_to_user_home_dir
walk_dotfiles _dotfiles_copy_to_user_home_dir
_dotfiles_add_source_bash_dev
}

Expand Down
19 changes: 3 additions & 16 deletions scripts/install.d/languages
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,10 @@
# RUBY_VERSION: Version of Ruby to install
# RBENV_VERSION: Tag or commit hash of rbenv version to install

_languages() {
local app_sys_root="$1"
local language

if [ ! -d $app_sys_root ]; then
mkdir -p $app_sys_root
fi
. "$_GO_USE_MODULES" 'apps-home'

if [ ! -d /etc/profile.d ]; then
mkdir /etc/profile.d
cat >>/etc/profile <<'END_PROFILE'
for script in /etc/profile.d/*.sh
do
. $script
done
END_PROFILE
fi
_languages() {
create_apps_home

@go install languages "go" "$GO_VERSION" "$GVM_VERSION"
@go install languages "node" "$NODEJS_VERSION" "$NVM_VERSION"
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.d/languages.d/go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# Will install gvm in `<app-sys-root>/gvm` and add `/etc/profile.d/gvm.sh`.

declare -r _GVM_PROFILE='/etc/profile.d/gvm.sh'
declare -r _GVM_PROFILE="$APPS_HOME/etc/profile.d/gvm.sh"
declare -r _GVM_INSTALLER_URL='https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer'

_install_gvm() {
Expand Down
5 changes: 2 additions & 3 deletions scripts/install.d/user-bin
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# These may be common or platform-specific scripts.

. "$_GO_USE_MODULES" 'platform' 'copy'
. "$_GO_USE_MODULES" 'platform' 'copy' 'walk'

install_user_bin_scripts_do_copy() {
copy_file_safely "$1" "${1%/*}/" "$HOME/bin" '700' '700'
Expand All @@ -14,8 +14,7 @@ install_user_bin_scripts() {
local script

@go.log INFO Copying user-bin scripts into "$HOME/bin"
. "$_GO_USE_MODULES" 'process-files'
process_user_bin_scripts install_user_bin_scripts_do_copy
walk_user_bin_scripts install_user_bin_scripts_do_copy
}

install_user_bin_scripts "$@"
28 changes: 28 additions & 0 deletions scripts/lib/apps-home
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env bash
#
# Utilities to manage the APPS_HOME directory structure
#
# Exports:
# create_apps_home
# Creates the APPS_HOME directory structure

. "$_GO_USE_MODULES" 'log' 'walk'

export APPS_HOME_DIRS=(
"$APPS_HOME/etc/profile.d"
)
export APPS_HOME_DIR_PERMISSIONS="${APPS_HOME_DIR_PERMISSIONS:-755}"
export APPS_HOME_FILE_PERMISSIONS="${APPS_HOME_FILE_PERMISSIONS:-644}"

if [[ -z "$APPS_HOME" ]]; then
@go.log FATAL "APPS_HOME not defined; set it in settings.bash"
fi

# Creates the APPS_HOME directory structure
create_apps_home() {
local dir

for dir in "${APPS_HOME_DIRS[@]}"; do
create_dirs_with_permissions "$dir" "$APPS_HOME_DIR_PERMISSIONS"
done
}
16 changes: 3 additions & 13 deletions scripts/lib/copy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# copy_file_safely
# Copy a file, creating directories if need be, only if it doesn't exist

. "$_GO_USE_MODULES" 'walk'

# Copy a file, creating directories if need be, only if it doesn't exist
#
# Arguments:
Expand All @@ -23,20 +25,8 @@ copy_file_safely() {
local dest="$dest_dir/${src#$src_prefix}"
local dest_parent="${dest_dir%/*}"

dest_dir="${dest%/*}"
printf '%s => %s\n' "$src" "$dest"

if [[ ! -d "$dest_dir" ]]; then
if ! mkdir -p "$dest_dir"; then
@go.log FATAL Failed to create "$dest_dir"
fi
while [[ "$dest_dir" != "$dest_parent" ]]; do
if ! chmod "$dir_perms" "$dest_dir"; then
@go.log FATAL Failed to set permissions on "$dest_dir"
fi
dest_dir="${dest_dir%/*}"
done
fi
create_dirs_with_permissions "$dest_dir" "$dir_perms"

if [[ ! -f "$dest" ]]; then
if ! cp "$src" "$dest"; then
Expand Down
48 changes: 0 additions & 48 deletions scripts/lib/process-files

This file was deleted.

129 changes: 129 additions & 0 deletions scripts/lib/walk
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#! /usr/bin/env bash
#
# Functions for processing lists of dev setup files
#
# Exports:
# walk_files
# Perform an operation on each path that is a regular file
#
# walk_dotfiles
# Perform an operation on each dotfile path
#
# walk_user_bin_scripts
# Perform an operation on all user-bin script paths
#
# walk_path_forward
# Processes a file path from the first component to the last
#
# create_dir_with_permissions
# Creates a directory with the specified permissions
#
# create_dirs_with_permissions
# Creates a directory and its parents with the specified permissions

# Perform an operation on each path that is a regular file
#
# The recursion is terminated when `operation` returns a nonzero value.
#
# Arguments:
# operation: Name of the function taking a file path as an argument
# ${@:1}: List of paths to begin examining
walk_files() {
local f
for f in "${@:1}"; do
if [[ -f "$f" ]] && ! "$1" "$f"; then
return
elif [[ -d "$f" ]]; then
walk_files "$1" "$f"/*
fi
done
}

# Perform an operation on each dotfile path
#
# Arguments:
# operation: Function taking a dotfile path as an argument
walk_dotfiles() {
walk_files "$1" dotfiles/.[A-Za-z0-9_-]*
}

# Perform an operation on all user-bin script paths
#
# Arguments:
# operation: Function taking a user-bin script path as an argument
walk_user_bin_scripts() {
if [[ -z "$PLATFORM_ID" ]]; then
. "$_GO_USE_MODULES" 'platform'
fi
walk_files "$1" user-bin/{common,$PLATFORM_ID}/*
}

# Processes a file path from the first component to the last
#
# The first call to `operation` receives the first component of the path as its
# path argument. Each successive call to `operation` receives the previous path
# plus its child component. The processing is terminated when `operation`
# returns a nonzero value.
#
# Arguments:
# operation: Name of the function taking a file path as an argument
# ${@:1}: List of paths to begin examining
walk_path_forward() {
local operation="$1"
local oldIFS="$IFS"
local IFS='/'
local components=($2)
local component
local current_path

IFS="$oldIFS"

for component in "${components[@]}"; do
current_path+="$component/"
if ! "$operation" "$current_path"; then
break
fi
done
}

# Creates a directory with the specified permissions
#
# If a directory already exists, this function does not update its permissions.
#
# Globals:
# permissions: May be defined as an alternative to passing as a parameter
#
# Arguments:
# dir: The path of the directory to create
# permissions: The permissions to set on the directory, if created
create_dir_with_permissions() {
local dir="$1"
local permissions="${2:-$permissions}"

if [[ -z "$dir" ]]; then
@go.log FATAL "Directory argument not specified"
elif [[ -z "$permissions" ]]; then
@go.log FATAL "Permissions not specified"
fi

if [[ ! -d "$dir" ]]; then
@go.log INFO "Creating $dir"

if ! mkdir "$dir"; then
@go.log FATAL "Could not create $dir"
elif ! chmod "$permissions" "$dir"; then
@go.log FATAL "Could not set permissions for $dir"
fi
fi
}

# Creates a directory and its parents with the specified permissions
#
# If a directory already exists, this function does not update its permissions.
#
# Arguments:
# dir: The path of the directory to create
# permissions: The permissions to set on any created directory
create_dirs_with_permissions() {
permissions="$2" walk_path_forward create_dir_with_permissions "$1"
}
2 changes: 1 addition & 1 deletion settings.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Root directory for language manager installs.
declare -r APPS_ROOT='/usr/local/mbland'
declare -r APPS_HOME='/usr/local/mbland'

# List of language for which to install language managers.
declare -r INSTALL_LANGUAGES=('go')
Expand Down

0 comments on commit ca89c1c

Please sign in to comment.