Skip to content

Commit

Permalink
Initial working prototype.
Browse files Browse the repository at this point in the history
  • Loading branch information
juanibiapina committed Jun 24, 2013
0 parents commit a3b64f1
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
modules/
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
1 change: 1 addition & 0 deletions bin/basher
6 changes: 6 additions & 0 deletions lib/basher.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

require() {
local module_name="$1"
source "$BASHER_ROOT/modules/$module_name/lib/$module_name.bash"
}
41 changes: 41 additions & 0 deletions libexec/basher
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions libexec/basher-init
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

echo "export BASHER_ROOT=$BASHER_ROOT"
echo "source $BASHER_ROOT/lib/basher.bash"
74 changes: 74 additions & 0 deletions tests/assertions.bash
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions tests/basher.bats
Original file line number Diff line number Diff line change
@@ -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"
}
13 changes: 13 additions & 0 deletions tests/init.bats
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 3 additions & 0 deletions tests/libexec/basher-echo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

eval "echo \$$1"
9 changes: 9 additions & 0 deletions tests/test_helper.bash
Original file line number Diff line number Diff line change
@@ -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 -)"
}

0 comments on commit a3b64f1

Please sign in to comment.