Skip to content

Commit

Permalink
refactor(#39): update meatbox scripts to be fully posix compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
meatwallace committed May 19, 2019
1 parent fd3e17a commit 46d0652
Show file tree
Hide file tree
Showing 33 changed files with 250 additions and 146 deletions.
8 changes: 4 additions & 4 deletions .bash_profile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# if [[ $- == *i* ]]; then
# shellcheck source=.bashrc
. "$HOME/.bashrc"
# fi
if [[ $- == *i* ]]; then
# shellcheck source=.bashrc
. "$HOME/.bashrc"
fi
2 changes: 1 addition & 1 deletion .bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ asdf_dir="$MEATBOX_LIBS_DIR/asdf"

# load `asdf`, our version manager
if [ -d "$asdf_dir" ]; then
# shellcheck source=.meatbox/libs/asdf/asdf.sh
# shellcheck source=libs/asdf/asdf.sh
. "$asdf_dir/asdf.sh"
fi

Expand Down
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
name: "Build Dockerfile"
command: |
apk add --no-cache git >/dev/null 2>&1
cd scripts/linux/alpine
cd ./scripts/alpine
GIT_COMMIT_SHA1=$CIRCLE_SHA1 ./build.sh
no_output_timeout: 30m

Expand All @@ -46,7 +46,7 @@ jobs:
name: "Build Dockerfile"
command: |
apk add --no-cache git >/dev/null 2>&1
cd scripts/linux/arch
cd ./scripts/arch
GIT_COMMIT_SHA1=$CIRCLE_SHA1 ./build.sh
no_output_timeout: 30m

Expand Down
4 changes: 0 additions & 4 deletions .zprofile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ if (( $#commands[(i)lesspipe(|.sh)] )); then
export LESSOPEN="| /usr/bin/env $commands[(i)lesspipe(|.sh)] %s 2>&-"
fi

# }}}

#: tmp directory {{{

# if we don't have a TMPDIR variable set or it's empty, export it
if [ -z "${TMPDIR}" ]; then
export TMPDIR="/tmp/zsh-${UID}"
Expand Down
14 changes: 8 additions & 6 deletions bin/git-clone-or-update
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

git_clone_or_update() {
local repo_url="$1"
local repo_dir="$2"
repo_url="$1"
repo_dir="$2"

# use the repository's name as the directory if we didn't explicitly pass one
if is empty "$repo_dir"; then
if [ -z "$repo_dir" ]; then
repo_dir="${repo_dir%.git}"
fi

if is not a directory "$repo_dir"; then
if [ ! -d "$repo_dir" ]; then
mkdir -p "$repo_dir"

git clone "$repo_url" "$repo_dir" >/dev/null
else
(
Expand Down
70 changes: 30 additions & 40 deletions bin/meatbox
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

set -e
set -ex

usage() {
cat << HEREDOC
Expand All @@ -25,63 +25,53 @@ log_message() {
}

meatbox_run_script() {
directory="$1"
script="$2"
ctx="$1"
cmd="$2"

(cd "$MEATBOX_SCRIPTS_DIR/$directory" && "./$script")
}

meatbox_run_universal_script() {
script="$1"

meatbox_run_script "universal" "$script"
}

meatbox_run_os_script() {
script="$1"
os="$(uname -s | lowercase)"

if [ -f "$MEATBOX_SCRIPTS_DIR/$os/$script" ]; then
meatbox_run_script "$os" "$script"
if [ -f "$MEATBOX_SCRIPTS_DIR/$ctx/$cmd.sh" ]; then
(cd "$MEATBOX_SCRIPTS_DIR/$ctx" && "./$cmd.sh")
fi
}

meatbox_help() {
usage
}
meatbox_run() {
kernel="$(uname -s | lowercase)"

meatbox_bootstrap() {
start-sudo-loop "$MEATBOX_PASSWORD"
meatbox_run_script "$kernel" "$cmd"

if [ "$kernel" = "linux" ]; then
distro=""

meatbox_run_os_script "bootstrap.sh"
meatbox_run_universal_script "bootstrap.sh"
}
# cat /etc/*release

if [ "$1" = "bootstrap" ]; then
meatbox_bootstrap
# ID=alpine
# NAME="Alpine Linux"

exit 0
fi
# ID=antergos
# ID_LIKE=archlinux
# NAME="Antergos Linux"

meatbox_setup() {
start-sudo-loop "$MEATBOX_PASSWORD"
# DISTRIB_ID=Arch
# NAME="ArchBang Linux"
# ID=arch

meatbox_run_os_script "setup.sh"
meatbox_run_universal_script "setup.sh"
}
meatbox_run_script "$distro" "$cmd"
fi

meatbox_update() {
start-sudo-loop "$MEATBOX_PASSWORD"
meatbox_run_script "agnostic" "$cmd"
}

meatbox_run_os_script "update.sh"
meatbox_run_universal_script "update.sh"
meatbox_help() {
usage
}

meatbox() {
cmd="$1"

case "$cmd" in
bootstrap|setup|update) "meatbox_$cmd" ;;
bootstrap|setup|update)
start-sudo-loop "$MEATBOX_PASSWORD"
meatbox_run "$cmd"
;;
*) meatbox_help ;;
esac
}
Expand Down
27 changes: 27 additions & 0 deletions bin/meatman
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

set -eu

# TODO: requires much smarter implementation w/ handling of various managers,
# operations, and flags
meatman() {
cmd="$1"
packages="$2"

if [ ! "$cmd" = "add" ]; then
echo "command unsupported"
exit 1
fi

# TODO: handle flags
if [ -x "$(command -v pacman)" ]; then
sudo pacman -Sy --config "$HOME/.config/pacman/pacman.conf" --noconfirm --needed $packages
elif [ -x "$(command -v apk)" ]; then
sudo apk add --no-cache $packages
else
echo "unsupported distro"
exit 1
fi
}

meatman "$@"
2 changes: 1 addition & 1 deletion bin/pacfast
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ download_packages() {
--dir="$cache_dir"

rm "$dl_list_file"
}
}

# if we have unfinished jobs from a previous run, complete them
if [ -f "$dl_list_file" ]; then
Expand Down
6 changes: 3 additions & 3 deletions bin/start-sudo-loop
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
#!/bin/sh

set -eo pipefail
set -e

start_sudo_loop() {
local password="$1"
password="$1"

if [ -n "$password" ]; then
echo "$password" | sudo -Sv
Expand Down
15 changes: 9 additions & 6 deletions index.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash

set -e

Expand All @@ -12,23 +12,26 @@ export MEATBOX_SCRIPTS_DIR="$MEATBOX_DIR/scripts"
cd "$HOME"

# ensure the user has a /bin directory, and add it to the path
mkdir "$MEATBOX_BIN_DIR"
if [ ! -d "$MEATBOX_BIN_DIR" ]; then
mkdir "$MEATBOX_BIN_DIR"
fi

export PATH="$MEATBOX_BIN_DIR:$PATH"

# pull down `yadm` and symlink it into our user's /bin
YADM_DIR="$MEATBOX_LIBS_DIR/yadm"
yadm_dir="$MEATBOX_LIBS_DIR/yadm"

if [ ! -d "$YADM_DIR" ]; then
git clone https://github.com/TheLocehiliosan/yadm.git "$YADM_DIR" >/dev/null
if [ ! -d "$yadm_dir" ]; then
git clone https://github.com/thelocehiliosan/yadm.git "$yadm_dir" >/dev/null

ln -fs "$YADM_DIR/yadm" "$MEATBOX_BIN_DIR/yadm"
ln -fs "$yadm_dir/yadm" "$MEATBOX_BIN_DIR/yadm"
fi

# clone our system config and update origin to use SSL in the future
yadm clone -f https://github.com/meatwallace/dotfiles >/dev/null
yadm remote set-url origin "git@github.com:meatwallace/dotfiles.git"

# check out a specific commit if it's been specified e.g. CI pull request
if [ -n "$MEATBOX_CHECKOUT_SHA1" ]; then
yadm checkout "$MEATBOX_CHECKOUT_SHA1"
fi
3 changes: 2 additions & 1 deletion scripts/agnostic/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
set -eu

scripts="
install_or_update_asdf.sh
install_or_update_asdf.sh
install_yarn.sh
"

bootstrap() {
Expand Down
31 changes: 17 additions & 14 deletions scripts/agnostic/install_or_update_asdf.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#!/bin/sh
#!/usr/bin/env bash

set -eu
set -eux

asdf_dir="$MEATBOX_LIBS_DIR/asdf"

if [ ! -x "$(command -v asdf)" ]; then
# TODO(#27): create an `unzip` install script as `lua` requires it
sudo pacman -Sy --noconfirm unzip >/dev/null

# clone in our version manager
git clone https://github.com/asdf-vm/asdf.git "$asdf_dir" >/dev/null

Expand All @@ -24,30 +21,36 @@ else
asdf update >/dev/null 2>&1
fi

if [ ! -x "$(command -v unzip)" ]; then
meatman add unzip
fi

# asdf returns an error code when no plugins are installed and we try to list
# them, cheers.
set +e
installed_plugins="$(asdf plugin-list)"
set -e
installed_plugin_packages="$(asdf current)"

desired_plugins="lua nodejs python ruby"

# add our version manager plugins
for plugin in $desired_plugins; do
if ! echo "$installed_plugins" | grep -q "$plugin"; then
asdf plugin-add "$plugin" >/dev/null 2>&1
if ! echo "$installed_plugin_packages" | grep -q "$plugin"; then
asdf plugin-add "$plugin"
#>/dev/null 2>&1
else
asdf plugin-update "$plugin" >/dev/null 2>&1
asdf plugin-update "$plugin"
#>/dev/null 2>&1
fi
done

# import the nodejs GPG keys to verify installation
bash "$HOME/.asdf/plugins/nodejs/bin/import-release-team-keyring" >/dev/null 2>&1
bash "$HOME/.asdf/plugins/nodejs/bin/import-release-team-keyring" #>/dev/null 2>&1

# upgrade and use the latest versions of our packages
for plugin in $desired_plugins; do
latest_version="$(asdf list-all "$plugin" | grep -E '^[0-9.]+$' | tail -n 1)"

asdf install "$plugin" "$latest_version" >/dev/null 2>&1
asdf global "$plugin" "$latest_version" >/dev/null
asdf install "$plugin" "$latest_version"
#>/dev/null 2>&1
asdf global "$plugin" "$latest_version"
#>/dev/null
done
4 changes: 2 additions & 2 deletions scripts/agnostic/install_or_update_python_packages.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

pip install --user --upgrade \
pip \
Expand Down
4 changes: 2 additions & 2 deletions scripts/agnostic/install_or_update_zplugin.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

sh -c "$(curl -fsSL https://raw.githubusercontent.com/meatwallace/zplugin/master/doc/install.sh)"
15 changes: 15 additions & 0 deletions scripts/agnostic/install_yarn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -eu

install_yarn() {
if [ ! -x "$(command -v yarn)" ]; then
# source our bashrc to make sure `asdf` is available
# shellcheck source=../../.bashrc
. "$HOME/.bashrc"

meatman add yarn
fi
}

install_yarn "$@"
4 changes: 2 additions & 2 deletions scripts/agnostic/load_zsh.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

# fire up zsh to trigger zplugin initialization
zsh
2 changes: 1 addition & 1 deletion scripts/agnostic/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -eu

declare scripts="
scripts="
install_or_update_asdf.sh
install_or_update_node_modules.sh
install_or_update_python_packages.sh
Expand Down
Loading

0 comments on commit 46d0652

Please sign in to comment.