From a3b64f13ac419f249da0f9bd3531733e99f2509b Mon Sep 17 00:00:00 2001 From: Juan Ibiapina Date: Mon, 24 Jun 2013 09:29:33 -0300 Subject: [PATCH] Initial working prototype. --- .gitignore | 1 + README.md | 37 ++++++++++++++++++++ bin/basher | 1 + lib/basher.bash | 6 ++++ libexec/basher | 41 ++++++++++++++++++++++ libexec/basher-init | 4 +++ tests/assertions.bash | 74 +++++++++++++++++++++++++++++++++++++++ tests/basher.bats | 13 +++++++ tests/init.bats | 13 +++++++ tests/libexec/basher-echo | 3 ++ tests/test_helper.bash | 9 +++++ 11 files changed, 202 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 120000 bin/basher create mode 100644 lib/basher.bash create mode 100755 libexec/basher create mode 100755 libexec/basher-init create mode 100644 tests/assertions.bash create mode 100644 tests/basher.bats create mode 100644 tests/init.bats create mode 100755 tests/libexec/basher-echo create mode 100644 tests/test_helper.bash diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..52146d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e2b1021 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# What is it? + +A tool to make it easier to manage bash modules. + +# Installation + +1. Checkout basher on `~/.basher` + + ~~~ sh + $ git clone git://github.com/juanibiapina/basher.git ~/.basher + ~~~ + +2. Add `~/.basher/bin` to `$PATH` for easy access to the basher command-line utility. + + ~~~ sh + $ echo 'export PATH="$HOME/.basher/bin:$PATH"' >> ~/.bash_profile + ~~~ + + **Ubuntu note**: Modify your `~/.profile` instead of `~/.bash_profile`. + + **Zsh note**: Modify your `~/.zshrc` file instead of `~/.bash_profile`. + +3. Add `basher init` to your shell to enable basher runtime functions + + ~~~ sh + $ echo 'eval "$(basher init -)"' >> ~/.bash_profile + ~~~ + + _Same as in previous step, use `~/.profile` on Ubuntu, `~/.zshrc` for Zsh._ + +# Usage + +## Runtime functions + +### require + +`require module_name` diff --git a/bin/basher b/bin/basher new file mode 120000 index 0000000..35af24c --- /dev/null +++ b/bin/basher @@ -0,0 +1 @@ +../libexec/basher \ No newline at end of file diff --git a/lib/basher.bash b/lib/basher.bash new file mode 100644 index 0000000..0412d0a --- /dev/null +++ b/lib/basher.bash @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +require() { + local module_name="$1" + source "$BASHER_ROOT/modules/$module_name/lib/$module_name.bash" +} diff --git a/libexec/basher b/libexec/basher new file mode 100755 index 0000000..9bdf968 --- /dev/null +++ b/libexec/basher @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +resolve_link() { + $(type -p greadlink readlink | head -1) "$1" +} + +abs_dirname() { + local cwd="$(pwd)" + local path="$1" + + while [ -n "$path" ]; do + cd "${path%/*}" + local name="${path##*/}" + path="$(resolve_link "$name" || true)" + done + + pwd + cd "$cwd" +} + +if [ -z "$BASHER_ROOT" ]; then + BASHER_ROOT="$HOME/.basher" +fi +export BASHER_ROOT + +bin_path="$(abs_dirname "$0")" +export PATH="${bin_path}:${PATH}" + +command="$1" +case "$command" in +* ) + command_path="$(command -v "basher-$command" || true)" + if [ -z "$command_path" ]; then + echo "basher: no such command '$command'" >&2 + exit 1 + fi + + shift 1 + exec "$command_path" "$@" + ;; +esac diff --git a/libexec/basher-init b/libexec/basher-init new file mode 100755 index 0000000..b41a4fb --- /dev/null +++ b/libexec/basher-init @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +echo "export BASHER_ROOT=$BASHER_ROOT" +echo "source $BASHER_ROOT/lib/basher.bash" diff --git a/tests/assertions.bash b/tests/assertions.bash new file mode 100644 index 0000000..c3a4994 --- /dev/null +++ b/tests/assertions.bash @@ -0,0 +1,74 @@ +flunk() { + if [ "$#" -eq 0 ]; then + cat - + else + echo "$@" + fi + return 1 +} + +assert_success() { + if [ "$status" -ne 0 ]; then + flunk "command failed with exit status $status" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_failure() { + if [ "$status" -eq 0 ]; then + flunk "expected failed exit status" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_equal() { + if [ "$1" != "$2" ]; then + { echo "expected: $1" + echo "actual: $2" + } | flunk + fi +} + +assert_output() { + local expected + if [ $# -eq 0 ]; then expected="$(cat -)" + else expected="$1" + fi + assert_equal "$expected" "$output" +} + +assert_line() { + if [ "$1" -ge 0 ] 2>/dev/null; then + assert_equal "$2" "${lines[$1]}" + else + local line + for line in "${lines[@]}"; do + if [ "$line" = "$1" ]; then return 0; fi + done + flunk "expected line \`$1'" + fi +} + +refute_line() { + if [ "$1" -ge 0 ] 2>/dev/null; then + local num_lines="${#lines[@]}" + if [ "$1" -lt "$num_lines" ]; then + flunk "output has $num_lines lines" + fi + else + local line + for line in "${lines[@]}"; do + if [ "$line" = "$1" ]; then + flunk "expected to not find line \`$line'" + fi + done + fi +} + +assert() { + if ! "$@"; then + flunk "failed: $@" + fi +} diff --git a/tests/basher.bats b/tests/basher.bats new file mode 100644 index 0000000..7b4c4e8 --- /dev/null +++ b/tests/basher.bats @@ -0,0 +1,13 @@ +#!/usr/bin/env bats + +load test_helper + +@test "default BASHER_ROOT" { + BASHER_ROOT= run basher echo BASHER_ROOT + assert_output "$HOME/.basher" +} + +@test "inherited BASHER_ROOT" { + BASHER_ROOT=/tmp/basher run basher echo BASHER_ROOT + assert_output "/tmp/basher" +} diff --git a/tests/init.bats b/tests/init.bats new file mode 100644 index 0000000..6662331 --- /dev/null +++ b/tests/init.bats @@ -0,0 +1,13 @@ +#!/usr/bin/env bats + +load test_helper + +@test "sources main basher lib" { + run basher-init - + assert_line "source $BASHER_ROOT/lib/basher.bash" +} + +@test "exports BASHER_ROOT" { + BASHER_ROOT=/lol run basher-init - + assert_line 0 "export BASHER_ROOT=/lol" +} diff --git a/tests/libexec/basher-echo b/tests/libexec/basher-echo new file mode 100755 index 0000000..6ad3190 --- /dev/null +++ b/tests/libexec/basher-echo @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +eval "echo \$$1" diff --git a/tests/test_helper.bash b/tests/test_helper.bash new file mode 100644 index 0000000..0b16ef9 --- /dev/null +++ b/tests/test_helper.bash @@ -0,0 +1,9 @@ +load assertions + +export PATH="${BATS_TEST_DIRNAME}/libexec:$PATH" +export PATH="${BATS_TEST_DIRNAME}/../libexec:$PATH" + +setup() { + export BASHER_ROOT="$BATS_TEST_DIRNAME/.." + eval "$(basher init -)" +}