diff --git a/README.md b/README.md index dab5794d..844bebc3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@
- + Bashly - Bash CLI Framework and Generator ================================================== diff --git a/bashly-logo.svg b/bashly-logo.svg deleted file mode 100644 index c9620981..00000000 --- a/bashly-logo.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/examples/colors/colorly b/examples/colors/colorly index cfe2dcb7..934fcb57 100644 --- a/examples/colors/colorly +++ b/examples/colors/colorly @@ -149,6 +149,14 @@ initialize() { version="0.1.0" long_usage='' set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. } # :command.run diff --git a/examples/colors/src/initialize.sh b/examples/colors/src/initialize.sh new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/examples/colors/src/initialize.sh @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/examples/config-ini/configly b/examples/config-ini/configly index d2462c81..4f3125ed 100644 --- a/examples/config-ini/configly +++ b/examples/config-ini/configly @@ -299,18 +299,21 @@ parse_args() { ;; set | s ) + action=set shift set_parse_args "$@" shift $# ;; get | g ) + action=get shift get_parse_args "$@" shift $# ;; list | l ) + action=list shift list_parse_args "$@" shift $# @@ -502,6 +505,14 @@ initialize() { version="0.1.0" long_usage='' set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. } # :command.run diff --git a/examples/config-ini/src/initialize.sh b/examples/config-ini/src/initialize.sh new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/examples/config-ini/src/initialize.sh @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/examples/custom-includes/download b/examples/custom-includes/download index 6deda3fc..935a2a03 100644 --- a/examples/custom-includes/download +++ b/examples/custom-includes/download @@ -120,6 +120,14 @@ initialize() { version="0.1.0" long_usage='' set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. } # :command.run diff --git a/examples/custom-includes/src/initialize.sh b/examples/custom-includes/src/initialize.sh new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/examples/custom-includes/src/initialize.sh @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/examples/custom-strings/download b/examples/custom-strings/download index 0d42dd7e..9ffa4a31 100644 --- a/examples/custom-strings/download +++ b/examples/custom-strings/download @@ -139,6 +139,14 @@ initialize() { version="0.1.0" long_usage='' set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. } # :command.run diff --git a/examples/custom-strings/src/initialize.sh b/examples/custom-strings/src/initialize.sh new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/examples/custom-strings/src/initialize.sh @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/examples/custom-strings/test.sh b/examples/custom-strings/test.sh index 111ac5c7..f6958596 100644 --- a/examples/custom-strings/test.sh +++ b/examples/custom-strings/test.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash -set -x - rm -f ./src/*.sh +set -x + bashly generate ./download diff --git a/examples/minimal/download b/examples/minimal/download index b566a397..5a2d6cf2 100644 --- a/examples/minimal/download +++ b/examples/minimal/download @@ -137,6 +137,14 @@ initialize() { version="0.1.0" long_usage='' set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. } # :command.run diff --git a/examples/minimal/src/initialize.sh b/examples/minimal/src/initialize.sh new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/examples/minimal/src/initialize.sh @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/examples/minimal/test.sh b/examples/minimal/test.sh index f6197016..799ae295 100644 --- a/examples/minimal/test.sh +++ b/examples/minimal/test.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash -set -x - rm -f ./src/*.sh +set -x + bashly generate ./download diff --git a/examples/subcommands/cli b/examples/subcommands/cli index dbee4684..1bc7851e 100644 --- a/examples/subcommands/cli +++ b/examples/subcommands/cli @@ -186,12 +186,14 @@ parse_args() { ;; download | d ) + action=download shift download_parse_args "$@" shift $# ;; upload | u ) + action=upload shift upload_parse_args "$@" shift $# @@ -371,6 +373,14 @@ initialize() { version="0.1.0" long_usage='' set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. } # :command.run diff --git a/examples/subcommands/src/initialize.sh b/examples/subcommands/src/initialize.sh new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/examples/subcommands/src/initialize.sh @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/examples/subcommands/test.sh b/examples/subcommands/test.sh index dfa6b2bd..3e8cae3e 100644 --- a/examples/subcommands/test.sh +++ b/examples/subcommands/test.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash -set -x - rm -f ./src/*.sh +set -x + bashly generate ./cli diff --git a/lib/bashly/commands/generate.rb b/lib/bashly/commands/generate.rb index 705f9085..27f6eacf 100644 --- a/lib/bashly/commands/generate.rb +++ b/lib/bashly/commands/generate.rb @@ -22,10 +22,20 @@ def run def create_user_files say "creating user files in !txtgrn!#{Settings.source_dir}" + create_file "#{Settings.source_dir}/initialize.sh", command.render(:default_initialize_script) + if command.commands.empty? - create_file "#{Settings.source_dir}/root_command.sh", command.render(:default_root_script) + create_root_command_file + else + create_all_command_files end - + end + + def create_root_command_file + create_file "#{Settings.source_dir}/root_command.sh", command.render(:default_root_script) + end + + def create_all_command_files command.commands.each do |subcommand| file = "#{Settings.source_dir}/#{subcommand.full_name.to_underscore}_command.sh" content = subcommand.render :default_script diff --git a/lib/bashly/models/command.rb b/lib/bashly/models/command.rb index 875c1b3c..f69de0fc 100644 --- a/lib/bashly/models/command.rb +++ b/lib/bashly/models/command.rb @@ -50,13 +50,16 @@ def full_name # Reads a file from the userspace (Settings.source_dir) and returns # its contents. # If the file is not found, returns a string with a hint. - def load_user_file(file) + def load_user_file(file, placeholder: true) path = "#{Settings.source_dir}/#{file}" + default_content = placeholder ? "# error: cannot load file" : '' + content = if File.exist? path File.read path - else - "# error: cannot load file" + else + default_content end + "# :#{path}\n#{content}" end diff --git a/lib/bashly/version.rb b/lib/bashly/version.rb index 99945489..d3054fb4 100644 --- a/lib/bashly/version.rb +++ b/lib/bashly/version.rb @@ -1,3 +1,3 @@ module Bashly - VERSION = "0.2.0" + VERSION = "0.2.1" end \ No newline at end of file diff --git a/lib/bashly/views/command/command_filter.erb b/lib/bashly/views/command/command_filter.erb index 46e78272..4b222710 100644 --- a/lib/bashly/views/command/command_filter.erb +++ b/lib/bashly/views/command/command_filter.erb @@ -8,6 +8,7 @@ case $action in <%- commands.each do |command| -%> <%= command.aliases.join " | " %> ) + action=<%= command.name %> shift <%= command.name %>_parse_args "$@" shift $# diff --git a/lib/bashly/views/command/default_initialize_script.erb b/lib/bashly/views/command/default_initialize_script.erb new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/lib/bashly/views/command/default_initialize_script.erb @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/lib/bashly/views/command/initialize.erb b/lib/bashly/views/command/initialize.erb index 520c0224..35f65866 100644 --- a/lib/bashly/views/command/initialize.erb +++ b/lib/bashly/views/command/initialize.erb @@ -3,4 +3,6 @@ initialize() { version="<%= version %>" long_usage='' set -e + +<%= load_user_file("initialize.sh", placeholder: false).indent 2 %> } diff --git a/logo.svg b/logo.svg new file mode 100644 index 00000000..8b759796 --- /dev/null +++ b/logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/spec/approvals/cli/generate/minimal b/spec/approvals/cli/generate/minimal index e2ebcecc..778c8497 100644 --- a/spec/approvals/cli/generate/minimal +++ b/spec/approvals/cli/generate/minimal @@ -1,4 +1,5 @@ creating user files in spec/tmp/src +created spec/tmp/src/initialize.sh created spec/tmp/src/root_command.sh created spec/tmp/download run spec/tmp/download --help to test your bash script diff --git a/spec/approvals/cli/generate/no-args b/spec/approvals/cli/generate/no-args index b536cf0b..09b40731 100644 --- a/spec/approvals/cli/generate/no-args +++ b/spec/approvals/cli/generate/no-args @@ -1,4 +1,5 @@ creating user files in spec/tmp/src +created spec/tmp/src/initialize.sh created spec/tmp/src/cli_download_command.sh created spec/tmp/src/cli_upload_command.sh created spec/tmp/cli diff --git a/spec/approvals/cli/generate/no-args-skip b/spec/approvals/cli/generate/no-args-skip index d5d400c2..baee5bde 100644 --- a/spec/approvals/cli/generate/no-args-skip +++ b/spec/approvals/cli/generate/no-args-skip @@ -1,5 +1,6 @@ creating user files in spec/tmp/src -skipped spec/tmp/src/cli_download_command.sh (exists) +skipped spec/tmp/src/initialize.sh (exists) +skipped spec/tmp/src/cli_download_command.sh (exists) skipped spec/tmp/src/cli_upload_command.sh (exists) created spec/tmp/cli run spec/tmp/cli --help to test your bash script diff --git a/spec/approvals/examples/colors b/spec/approvals/examples/colors index 98e8fd52..72474b0e 100644 --- a/spec/approvals/examples/colors +++ b/spec/approvals/examples/colors @@ -1,5 +1,6 @@ + bashly generate creating user files in src +created src/initialize.sh skipped src/root_command.sh (exists) created ./colorly run ./colorly --help to test your bash script diff --git a/spec/approvals/examples/config-ini b/spec/approvals/examples/config-ini index aaebb181..b83f960f 100644 --- a/spec/approvals/examples/config-ini +++ b/spec/approvals/examples/config-ini @@ -1,5 +1,6 @@ + bashly generate creating user files in src +created src/initialize.sh skipped src/configly_set_command.sh (exists) skipped src/configly_get_command.sh (exists) skipped src/configly_list_command.sh (exists) diff --git a/spec/approvals/examples/custom-includes b/spec/approvals/examples/custom-includes index e624779f..34ae28e4 100644 --- a/spec/approvals/examples/custom-includes +++ b/spec/approvals/examples/custom-includes @@ -1,5 +1,6 @@ + bashly generate creating user files in src +created src/initialize.sh skipped src/root_command.sh (exists) created ./download run ./download --help to test your bash script diff --git a/spec/approvals/examples/custom-strings b/spec/approvals/examples/custom-strings index 4bd97e42..798379db 100644 --- a/spec/approvals/examples/custom-strings +++ b/spec/approvals/examples/custom-strings @@ -1,6 +1,6 @@ -+ rm -f ./src/root_command.sh + bashly generate creating user files in src +created src/initialize.sh created src/root_command.sh created ./download run ./download --help to test your bash script diff --git a/spec/approvals/examples/minimal b/spec/approvals/examples/minimal index 2a57a19c..4f2416ac 100644 --- a/spec/approvals/examples/minimal +++ b/spec/approvals/examples/minimal @@ -1,6 +1,6 @@ -+ rm -f ./src/root_command.sh + bashly generate creating user files in src +created src/initialize.sh created src/root_command.sh created ./download run ./download --help to test your bash script diff --git a/spec/approvals/examples/short-flag b/spec/approvals/examples/short-flag new file mode 100644 index 00000000..5d135697 --- /dev/null +++ b/spec/approvals/examples/short-flag @@ -0,0 +1,15 @@ ++ bashly generate +creating user files in src +created src/initialize.sh +created src/rush_config_command.sh +created src/rush_get_command.sh +created ./rush +run ./rush --help to test your bash script ++ ./rush c +# this file is located in 'src/rush_config_command.sh' +# code for 'rush config' goes here +# you can edit it freely and regenerate (it will not be overwritten) +args: ++ ./rush g +missing required argument: REPO +usage: rush get REPO [options] diff --git a/spec/approvals/examples/subcommands b/spec/approvals/examples/subcommands index f6b4c215..72212462 100644 --- a/spec/approvals/examples/subcommands +++ b/spec/approvals/examples/subcommands @@ -1,6 +1,6 @@ -+ rm -f ./src/cli_download_command.sh ./src/cli_upload_command.sh + bashly generate creating user files in src +created src/initialize.sh created src/cli_download_command.sh created src/cli_upload_command.sh created ./cli diff --git a/spec/bashly/integration/script_spec.rb b/spec/bashly/integration/examples_spec.rb similarity index 67% rename from spec/bashly/integration/script_spec.rb rename to spec/bashly/integration/examples_spec.rb index 45ccbde3..486fdcfe 100644 --- a/spec/bashly/integration/script_spec.rb +++ b/spec/bashly/integration/examples_spec.rb @@ -6,11 +6,19 @@ # folder describe 'generated bash scripts' do + # Test public examples from the examples folder... examples = Dir["examples/*"].select { |f| File.directory? f } + # ...as well as internal examples, not suitable for public view + fixtures = Dir["spec/fixtures/workspaces/*"].select { |f| File.directory? f } + + test_cases = fixtures + examples + leeway = ENV['CI'] ? 40 : 0 - examples.each do |example| + test_cases.each do |example| + approval_name = example.gsub "spec/fixtures/workspaces", "examples" + describe example do it "works" do output = "not executed" @@ -22,7 +30,7 @@ # This was observed in at least these two cases: # - The "+ ..." shell messages driven by `set -x` have no space # - The order of our `inspect_args` sometimes differs - expect(output).to match_fixture(example).diff(leeway) + expect(output).to match_fixture(approval_name).diff(leeway) end end end diff --git a/spec/fixtures/workspaces/short-flag/.gitignore b/spec/fixtures/workspaces/short-flag/.gitignore new file mode 100644 index 00000000..8314185d --- /dev/null +++ b/spec/fixtures/workspaces/short-flag/.gitignore @@ -0,0 +1,2 @@ +rush +src/*.sh \ No newline at end of file diff --git a/spec/fixtures/workspaces/short-flag/README.md b/spec/fixtures/workspaces/short-flag/README.md new file mode 100644 index 00000000..39cc7303 --- /dev/null +++ b/spec/fixtures/workspaces/short-flag/README.md @@ -0,0 +1,2 @@ +This fixture tests that short flags work properly +Reference issue: https://github.com/DannyBen/bashly/issues/16 \ No newline at end of file diff --git a/spec/fixtures/workspaces/short-flag/src/bashly.yml b/spec/fixtures/workspaces/short-flag/src/bashly.yml new file mode 100644 index 00000000..97705f0c --- /dev/null +++ b/spec/fixtures/workspaces/short-flag/src/bashly.yml @@ -0,0 +1,17 @@ +name: rush +help: Personal package manager +version: 0.1.0 + +commands: +- name: config + short: c + help: Show the configuration file + +- name: get + short: g + help: Install a package + + args: + - name: repo + required: true + help: Repository name diff --git a/spec/fixtures/workspaces/short-flag/test.sh b/spec/fixtures/workspaces/short-flag/test.sh new file mode 100644 index 00000000..09de7c7e --- /dev/null +++ b/spec/fixtures/workspaces/short-flag/test.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +# This fixture tests that short flags work properly +# Reference issue: https://github.com/DannyBen/bashly/issues/16 + +rm -f ./src/*.sh +rm -f ./rush + +set -x + +bashly generate + +./rush c +./rush g