Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions examples/custom-includes/download
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :command.root_command
root_command() {
# :src/root_command.sh
# Call our custom library function
echo "Before custom code"
my_extra_function
echo "After custom code"
}

# :command.version_command
version_command() {
echo "$version"
}

# :command.usage
download_usage() {
echo -e "download - Sample minimal application with custom strings"
echo
echo -e "Usage:"
echo -e " download [SOURCE] [options]"
echo


if [[ -n $long_usage ]]; then
echo -e "Options:"
# :command.usage_fixed_flags
echo " --help, -h"
echo -e " Show this help"
echo
echo " --version"
echo -e " Show version number"
echo

# :command.usage_args
echo -e "Arguments:"

# :argument.usage
echo " SOURCE"
echo " URL to download from"
echo



fi
}


# :command.inspect_args
inspect_args() {
echo args:
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
}

# :command.user_lib
# :src/lib/my_extra_function.sh
my_extra_function() {
echo "---"
echo "This is a place to write custom code that is needed by more than"
echo "one part of the application."
echo "Under most circumstances, you will not need it, but it is provided"
echo "for extra flexibility."
echo "---"
}


# :command.command_functions

# :command.parse_args
parse_args() {
# :command.fixed_flag_filter
case "$1" in
--version | -v )
version_command
exit 1
;;

--help | -h )
long_usage=yes
download_usage
exit 1
;;

esac
# :command.command_filter
action=root
# :command.required_args_filter
# :command.required_flags_filter
# :command.parse_args_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in

-* )
echo -e "invalid option: $key"
exit 1
;;

* )
# :command.parse_args_case
if [[ ! ${args[source]} ]]; then
args[source]=$1
shift
else
echo -e "invalid argument: $key"
exit 1
fi
;;

esac
done
}


# :command.initialize
initialize() {
version="0.1.0"
long_usage=''
set -e
}

# :command.run
run() {
declare -A args
parse_args "$@"

if [[ ${args[--version]} ]]; then
version_command
elif [[ ${args[--help]} ]]; then
long_usage=yes
download_usage
elif [[ $action == "root" ]]; then
root_command
fi
}

initialize
run "$@"
7 changes: 7 additions & 0 deletions examples/custom-includes/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: download
help: Sample minimal application with custom strings
version: 0.1.0

args:
- name: source
help: URL to download from
8 changes: 8 additions & 0 deletions examples/custom-includes/src/lib/my_extra_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
my_extra_function() {
echo "---"
echo "This is a place to write custom code that is needed by more than"
echo "one part of the application."
echo "Under most circumstances, you will not need it, but it is provided"
echo "for extra flexibility."
echo "---"
}
4 changes: 4 additions & 0 deletions examples/custom-includes/src/root_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Call our custom library function
echo "Before custom code"
my_extra_function
echo "After custom code"
7 changes: 7 additions & 0 deletions examples/custom-includes/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -x

bashly generate

./download
1 change: 1 addition & 0 deletions examples/custom-strings/download
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ inspect_args() {
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
}


# :command.command_functions

# :command.parse_args
Expand Down
1 change: 1 addition & 0 deletions examples/minimal/download
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ inspect_args() {
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
}


# :command.command_functions

# :command.parse_args
Expand Down
1 change: 1 addition & 0 deletions examples/subcommands/cli
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ inspect_args() {
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
}


# :command.command_functions
# :command.function
cli_download_command() {
Expand Down
14 changes: 13 additions & 1 deletion lib/bashly/commands/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ class Add < Base
help "Add extra features and customization to your script"

usage "bashly add strings [--force]"
usage "bashly add lib [--force]"
usage "bashly add (-h|--help)"

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

command "strings", "Copy an additional configuration file to your project, allowing you to customize all the tips and error strings."
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."

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

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

def lib_command
safe_copy asset("templates/sample_lib_function.sh"), "#{Settings.source_dir}/lib/sample_lib_function.sh"
end

private

def safe_copy(source, target)
Expand All @@ -26,10 +32,16 @@ def safe_copy(source, target)
if File.exist? target and !args['--force']
say "skipped !txtgrn!#{target}!txtrst! (exists)"
else
FileUtils.cp source, target
deep_copy source, target
say "created !txtgrn!#{target}"
end
end

def deep_copy(source, target)
target_dir = File.dirname target
FileUtils.mkdir_p target_dir unless Dir.exist? target_dir
FileUtils.cp source, target
end
end
end
end
9 changes: 9 additions & 0 deletions lib/bashly/models/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ def usage_string
result.join " "
end

# Returns an array of files to include as is inside the script
# This is meant to provide the user with the ability to add custom
# functions
def user_lib
@user_lib ||= Dir["#{Settings.source_dir}/lib/**/*.sh"]
end

# Raise an exception if there are some serious issues with the command
# definition.
def verify
verify_commands if commands.any?
end
Expand Down
13 changes: 13 additions & 0 deletions lib/bashly/templates/sample_lib_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Add any function here that is needed in more than one parts of your
# application, or that you otherwise wish to extract from the main function
# scripts.
#
# Note that code here should be wrapped inside bash functions, and it is
# recommended to have a separate file for each function.
#
# Subdirectories will also be scanned for *.sh, so you have no reason not
# to organize your code nearly.
#
sample_lib_function() {
echo "it works"
}
1 change: 1 addition & 0 deletions lib/bashly/views/command/master_script.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<%= render :version_command %>
<%= render :usage %>
<%= render :inspect_args %>
<%= render :user_lib if user_lib.any? %>
<%= render :command_functions %>
<%= render :parse_args %>
<%= render :initialize %>
Expand Down
6 changes: 6 additions & 0 deletions lib/bashly/views/command/user_lib.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# :command.user_lib
<%- user_lib.each do |file| -%>
# <%= ":#{file}" %>
<%= File.read file %>

<%- end -%>
5 changes: 5 additions & 0 deletions spec/approvals/cli/add/help
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ Add extra features and customization to your script

Usage:
bashly add strings [--force]
bashly add lib [--force]
bashly add (-h|--help)

Commands:
strings
Copy an additional configuration file to your project, allowing you to
customize all the tips and error strings.

lib
Create the additional lib directory for additional user scripts. All *.sh
scripts in this folder will be included in the final bash script.

Options:
-f --force
Overwrite existing files
Expand Down
2 changes: 2 additions & 0 deletions spec/approvals/cli/add/lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
created spec/tmp/src/lib/sample_lib_function.sh

1 change: 1 addition & 0 deletions spec/approvals/cli/generate/usage
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Usage:
bashly add strings [--force]
bashly add lib [--force]
bashly add (-h|--help)
14 changes: 14 additions & 0 deletions spec/approvals/examples/custom-includes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
+ bashly generate
creating user files in src
skipped src/root_command.sh (exists)
created ./download
run ./download --help to test your bash script
+ ./download
Before custom code
---
This is a place to write custom code that is needed by more than
one part of the application.
Under most circumstances, you will not need it, but it is provided
for extra flexibility.
---
After custom code
13 changes: 13 additions & 0 deletions spec/bashly/commands/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,17 @@
end
end

context "with lib command" do
let(:lib_file) { "#{source_dir}/lib/sample_lib_function.sh" }

before do
reset_tmp_dir create_src: true
end

it "copies a sample function to the user space under lib directory" do
expect { subject.run %w[add lib] }.to output_fixture('cli/add/lib')
expect(File).to exist(lib_file)
end
end

end