-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a3b64f1
Showing
11 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../libexec/basher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
eval "echo \$$1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -)" | ||
} |