Skip to content

Commit 0b8e7f2

Browse files
authored
Merge pull request #14 from DannyBen/user-lib
Add support for extra custom scripts
2 parents 1003a85 + b667d78 commit 0b8e7f2

File tree

18 files changed

+247
-1
lines changed

18 files changed

+247
-1
lines changed

examples/custom-includes/download

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :command.root_command
6+
root_command() {
7+
# :src/root_command.sh
8+
# Call our custom library function
9+
echo "Before custom code"
10+
my_extra_function
11+
echo "After custom code"
12+
}
13+
14+
# :command.version_command
15+
version_command() {
16+
echo "$version"
17+
}
18+
19+
# :command.usage
20+
download_usage() {
21+
echo -e "download - Sample minimal application with custom strings"
22+
echo
23+
echo -e "Usage:"
24+
echo -e " download [SOURCE] [options]"
25+
echo
26+
27+
28+
if [[ -n $long_usage ]]; then
29+
echo -e "Options:"
30+
# :command.usage_fixed_flags
31+
echo " --help, -h"
32+
echo -e " Show this help"
33+
echo
34+
echo " --version"
35+
echo -e " Show version number"
36+
echo
37+
38+
# :command.usage_args
39+
echo -e "Arguments:"
40+
41+
# :argument.usage
42+
echo " SOURCE"
43+
echo " URL to download from"
44+
echo
45+
46+
47+
48+
fi
49+
}
50+
51+
52+
# :command.inspect_args
53+
inspect_args() {
54+
echo args:
55+
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
56+
}
57+
58+
# :command.user_lib
59+
# :src/lib/my_extra_function.sh
60+
my_extra_function() {
61+
echo "---"
62+
echo "This is a place to write custom code that is needed by more than"
63+
echo "one part of the application."
64+
echo "Under most circumstances, you will not need it, but it is provided"
65+
echo "for extra flexibility."
66+
echo "---"
67+
}
68+
69+
70+
# :command.command_functions
71+
72+
# :command.parse_args
73+
parse_args() {
74+
# :command.fixed_flag_filter
75+
case "$1" in
76+
--version | -v )
77+
version_command
78+
exit 1
79+
;;
80+
81+
--help | -h )
82+
long_usage=yes
83+
download_usage
84+
exit 1
85+
;;
86+
87+
esac
88+
# :command.command_filter
89+
action=root
90+
# :command.required_args_filter
91+
# :command.required_flags_filter
92+
# :command.parse_args_while
93+
while [[ $# -gt 0 ]]; do
94+
key="$1"
95+
case "$key" in
96+
97+
-* )
98+
echo -e "invalid option: $key"
99+
exit 1
100+
;;
101+
102+
* )
103+
# :command.parse_args_case
104+
if [[ ! ${args[source]} ]]; then
105+
args[source]=$1
106+
shift
107+
else
108+
echo -e "invalid argument: $key"
109+
exit 1
110+
fi
111+
;;
112+
113+
esac
114+
done
115+
}
116+
117+
118+
# :command.initialize
119+
initialize() {
120+
version="0.1.0"
121+
long_usage=''
122+
set -e
123+
}
124+
125+
# :command.run
126+
run() {
127+
declare -A args
128+
parse_args "$@"
129+
130+
if [[ ${args[--version]} ]]; then
131+
version_command
132+
elif [[ ${args[--help]} ]]; then
133+
long_usage=yes
134+
download_usage
135+
elif [[ $action == "root" ]]; then
136+
root_command
137+
fi
138+
}
139+
140+
initialize
141+
run "$@"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: download
2+
help: Sample minimal application with custom strings
3+
version: 0.1.0
4+
5+
args:
6+
- name: source
7+
help: URL to download from
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
my_extra_function() {
2+
echo "---"
3+
echo "This is a place to write custom code that is needed by more than"
4+
echo "one part of the application."
5+
echo "Under most circumstances, you will not need it, but it is provided"
6+
echo "for extra flexibility."
7+
echo "---"
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Call our custom library function
2+
echo "Before custom code"
3+
my_extra_function
4+
echo "After custom code"

examples/custom-includes/test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bashly generate
6+
7+
./download

examples/custom-strings/download

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ inspect_args() {
6161
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
6262
}
6363

64+
6465
# :command.command_functions
6566

6667
# :command.parse_args

examples/minimal/download

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ inspect_args() {
6767
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
6868
}
6969

70+
7071
# :command.command_functions
7172

7273
# :command.parse_args

examples/subcommands/cli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ inspect_args() {
141141
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
142142
}
143143

144+
144145
# :command.command_functions
145146
# :command.function
146147
cli_download_command() {

lib/bashly/commands/add.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ class Add < Base
44
help "Add extra features and customization to your script"
55

66
usage "bashly add strings [--force]"
7+
usage "bashly add lib [--force]"
78
usage "bashly add (-h|--help)"
89

910
option "-f --force", "Overwrite existing files"
1011

1112
command "strings", "Copy an additional configuration file to your project, allowing you to customize all the tips and error strings."
13+
command "lib", "Create the additional lib directory for additional user scripts. All *.sh scripts in this folder will be included in the final bash script."
1214

1315
environment "BASHLY_SOURCE_DIR", "The path to use for creating the configuration file [default: src]"
1416

1517
def strings_command
1618
safe_copy asset("templates/strings.yml"), "#{Settings.source_dir}/bashly-strings.yml"
1719
end
1820

21+
def lib_command
22+
safe_copy asset("templates/sample_lib_function.sh"), "#{Settings.source_dir}/lib/sample_lib_function.sh"
23+
end
24+
1925
private
2026

2127
def safe_copy(source, target)
@@ -26,10 +32,16 @@ def safe_copy(source, target)
2632
if File.exist? target and !args['--force']
2733
say "skipped !txtgrn!#{target}!txtrst! (exists)"
2834
else
29-
FileUtils.cp source, target
35+
deep_copy source, target
3036
say "created !txtgrn!#{target}"
3137
end
3238
end
39+
40+
def deep_copy(source, target)
41+
target_dir = File.dirname target
42+
FileUtils.mkdir_p target_dir unless Dir.exist? target_dir
43+
FileUtils.cp source, target
44+
end
3345
end
3446
end
3547
end

lib/bashly/models/command.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ def usage_string
8787
result.join " "
8888
end
8989

90+
# Returns an array of files to include as is inside the script
91+
# This is meant to provide the user with the ability to add custom
92+
# functions
93+
def user_lib
94+
@user_lib ||= Dir["#{Settings.source_dir}/lib/**/*.sh"]
95+
end
96+
97+
# Raise an exception if there are some serious issues with the command
98+
# definition.
9099
def verify
91100
verify_commands if commands.any?
92101
end

0 commit comments

Comments
 (0)