Skip to content

Commit 38bfb80

Browse files
committed
Bunch of new functionality
1 parent 2558d7b commit 38bfb80

File tree

11 files changed

+664
-15
lines changed

11 files changed

+664
-15
lines changed

brew.bash

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
import brew_portable
4+
import log
5+
import exec
6+
7+
brew::is_installed() {
8+
local pkg=$1
9+
if brew::brew list "$1" &>/dev/null;then
10+
return 0
11+
fi
12+
return 1
13+
}
14+
15+
brew::install() {
16+
local pkg=$1
17+
log::info "Installing $pkg..."
18+
exec::exec_preview brew::brew install "$pkg"
19+
}
20+
21+
brew::upgrade() {
22+
local pkg=$1
23+
log::info "Upgrading $pkg..."
24+
exec::exec_preview brew::brew upgrade "$pkg"
25+
}
26+
27+
brew::upgrade_all() {
28+
log::info "Upgrading packages..."
29+
exec::exec_preview brew::brew upgrade
30+
}
31+
32+
# $1 pkg name
33+
# $2 (optional) binary name for quick instalation check
34+
brew::require() {
35+
local pkg=$1
36+
local binary=${2:-$1}
37+
brew::_assert_env
38+
if [ -f "$BASHLIB_BREW_DIR/bin/$binary" ];then
39+
return
40+
fi
41+
if ! brew::is_installed "$pkg";then
42+
brew::install "$pkg"
43+
fi
44+
}

brew_portable.bash

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env bash
2+
3+
# No imports to be able to use as portable install
4+
# This is only patial of brew module that doesn't contain any depenndencies
5+
# and is able to work with bash3
6+
# This is used to bootstrap brew envirionment and restart application under bash4
7+
8+
brew::_install() {
9+
local dir=$1
10+
mkdir -p "$dir"
11+
echo "Initializing private homebrew instance" 1>&2
12+
curl -sL https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C "$dir"
13+
}
14+
15+
brew::_setup() {
16+
local dir=$1
17+
local mode=$2
18+
if ! [[ "$OSTYPE" == "darwin"* ]]; then
19+
echo "brew can only we used on MacOs" 1>&2
20+
exit 1
21+
fi
22+
if [ "$BASHLIB_BREW_DIR" != "$dir" ] || [ "$BASHLIB_BREW_MODE" != "$mode" ];then
23+
brew::exit
24+
fi
25+
if ! [ -f "$dir/bin/brew" ];then
26+
brew::_install "$dir"
27+
fi
28+
export BASHLIB_BREW_DIR="$dir"
29+
export BASHLIB_BREW_MODE="$mode"
30+
if [ "$mode" = "exclusive" ];then
31+
export PATH="$dir/bin:$dir/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
32+
else
33+
export PATH="$dir/bin:$dir/sbin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
34+
fi
35+
}
36+
37+
brew::exit() {
38+
if [ -z "$BASHLIB_BREW_DIR" ];then
39+
return
40+
fi
41+
42+
local path_components
43+
local path_component
44+
mapfile -d':' -t path_components <<< "$PATH"
45+
local new_path=()
46+
for path_component in "${path_components[@]}";do
47+
if ! [[ path_component == ${BASHLIB_BREW_DIR}* ]];then
48+
new_path+=( "$path_component" )
49+
fi
50+
done
51+
52+
PATH="$(brew::_join ":" "${new_path[@]}")"
53+
unset BASHLIB_BREW_DIR
54+
unset BASHLIB_BREW_MODE
55+
}
56+
57+
brew::_join() {
58+
local IFS="$1"
59+
shift
60+
echo "$*"
61+
}
62+
63+
brew::_assert_env() {
64+
if [ -z "$BASHLIB_BREW_DIR" ];then
65+
echo "You need to run this comand in brew envirionment" 1>&2
66+
exit 1
67+
fi
68+
}
69+
70+
brew::brew() {
71+
brew::_assert_env
72+
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" brew "$@"
73+
}
74+
75+
brew::_quote () {
76+
for a in "${@//\"/\\\"}";do echo -n "\"$a\" ";done;
77+
}
78+
79+
brew::enter() {
80+
local dir=$1
81+
local mode=$2
82+
brew::_setup "$dir" "$mode"
83+
}
84+
85+
brew::exec_in() {
86+
local dir=$1
87+
shift
88+
brew::_setup "$dir"
89+
exec bash -c "$(brew::_quote "$@")"
90+
}

cli.bash

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import log
44

55
declare -Ag cli__options=()
66
declare -Ag cli__descriptions=()
7+
cli__extra_info=""
8+
cli__app_description=""
79
cli__program_name="$0"
810
cli__order=()
911

1012
cli::reset() {
1113
cli__order=()
14+
cli__extra_info=""
15+
cli__app_description=""
1216
declare -Ag cli__options=()
1317
declare -Ag cli__descriptions=()
1418
}
@@ -19,6 +23,20 @@ cli::set_program_name() {
1923
cli__program_name="${program_name}"
2024
}
2125

26+
# Additional info that will be displayed under option listing
27+
cli::set_extra_info() {
28+
local info=$1
29+
30+
cli__extra_info="$info"
31+
}
32+
33+
# Description visible under first line with arguments
34+
cli::set_app_description() {
35+
local desc=$1
36+
37+
cli__app_description="$desc"
38+
}
39+
2240
cli::_option_encode() {
2341
local option="$1"
2442

@@ -65,7 +83,15 @@ cli::print_help() {
6583
usage+="\n "
6684
fi
6785
done
68-
echo -e "${usage}\n\nOptions:\n${description}"
86+
local app_description_formatted
87+
if [ -n "$cli__app_description" ];then
88+
app_description_formatted="\n\n ${cli__app_description//\\n/\\n }"
89+
fi
90+
local extra_info_formatted
91+
if [ -n "$cli__extra_info" ];then
92+
extra_info_formatted="\nAdditional informations:\n\n ${cli__extra_info//\\n/\\n }"
93+
fi
94+
echo -e "${usage}${app_description_formatted}\n\nOptions:\n${description}${extra_info_formatted}"
6995
}
7096

7197
cli::handle() {

develop.bash

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
import exec
4+
import git
5+
6+
develop::lint() {
7+
local dir=$1
8+
shift
9+
local files_raw
10+
local files
11+
if ! develop::is_in_dev_mode "$dir";then
12+
# we are not in git or git ins't dirty, then we run in production mode
13+
return 0
14+
fi
15+
exec::is_cmd_available shellcheck || return 0
16+
log::info "Running in development mode, linting files first"
17+
files_raw=$(find -L "$dir" -type f -name '*.bash' -not -path "$dir/.*")
18+
log::info "Linting: ${files_raw}"
19+
mapfile -t files <<< "$files_raw"
20+
exec::exec_preview develop::_shellcheck "$dir" "$@" "${files[@]}" || log::fatal "There are issues in source code, aborting"
21+
log::success "Lint succeed, continuing"
22+
}
23+
24+
develop::is_in_dev_mode() {
25+
local dir=$1
26+
if ! git::is_git_repo "$dir" || ! git::is_dirty "$dir";then
27+
# we are not in git or git ins't dirty, then we run in production mode
28+
return 0
29+
fi
30+
return 1
31+
}
32+
33+
develop::_shellcheck() {
34+
local dir=$1
35+
shift
36+
echo "DEV MODE: Linting..."
37+
(
38+
cd "$dir" || return 1
39+
shellcheck -Calways -S style "$@"
40+
)
41+
}

0 commit comments

Comments
 (0)