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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/bashly/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ def header!
if File.exist? custom_header_path
File.read custom_header_path
else
render('header')
default_header
end
end

def default_header
result = render('header')
result += render('bash3_bouncer') unless function_name
result
end

def body
@body ||= command.render('master_script')
end
Expand Down
5 changes: 0 additions & 5 deletions lib/bashly/views/command/initialize.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@ initialize() {
long_usage=''
set -e

if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "<%= strings[:unsupported_bash_version] -%>\n"
exit 1
fi

<%= load_user_file("initialize.sh", placeholder: false).indent 2 %>
}
5 changes: 5 additions & 0 deletions lib/bashly/views/script/bash3_bouncer.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# :script.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "<%= strings[:unsupported_bash_version] -%>\n"
exit 1
fi
1 change: 1 addition & 0 deletions lib/bashly/views/script/header.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

11 changes: 11 additions & 0 deletions spec/approvals/cli/generate/wrap-script
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :script.wrapper
function() {

# :command.version_command
version_command() {
echo "$version"
}
15 changes: 15 additions & 0 deletions spec/approvals/examples/bash-3-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
creating user files in src
skipped src/initialize.sh (exists)
skipped src/test_command.sh (exists)
created ./cli
run ./cli --help to test your bash script
cli - Sample application

Usage:
cli [command]
cli [command] --help | -h
cli --version | -v

Commands:
test Run test

13 changes: 13 additions & 0 deletions spec/approvals/models/script/code
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :script.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n"
exit 1
fi

# :command.root_command
root_command() {
# :spec/tmp/src/root_command.sh
13 changes: 13 additions & 0 deletions spec/approvals/models/script/code-wrapped
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :script.wrapper
my_super_function() {
# :command.root_command
root_command() {
# :spec/tmp/src/root_command.sh
echo "error: cannot load file"
}

# :command.version_command
4 changes: 2 additions & 2 deletions spec/bashly/commands/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@
expect(success).to be true
end

it "generates the cli script wrapped in a function" do
it "generates the cli script wrapped in a function without bash3 bouncer" do
expect { subject.run %w[generate -w function] }.to output_approval('cli/generate/wrap-function')
expect(File).to exist(cli_script)
lines = File.readlines cli_script
expect(lines[5]).to eq "function() {\n"
expect(lines[0..10].join).to match_approval('cli/generate/wrap-script')
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/bashly/integration/bash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
before { system "docker pull bash:3 >/dev/null" }

it "errors gracefully" do
command = "docker run --rm -v $PWD:/app bash:3 bash /app/download"
command = "docker run --rm -v $PWD:/app bash:3 bash /app/cli"

Dir.chdir "examples/minimal" do
Dir.chdir "spec/fixtures/workspaces/bash-3-syntax" do
system "bashly generate 2>&1 >/dev/null"
expect(`#{command} 2>&1`).to match_approval('bash/error')
end
Expand Down
11 changes: 3 additions & 8 deletions spec/bashly/models/script_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@
context "without function name" do
it "returns the complete script" do
lines = subject.code.split "\n"
expect(lines[0]).to eq '#!/usr/bin/env bash'
expect(lines[1]).to start_with '# This script was generated by'
expect(lines[5]).to eq 'root_command() {'
expect(lines[0..12].join("\n")).to match_approval('models/script/code')
expect(lines[-1]).to eq 'run "$@"'
end
end

context "with function name" do
subject { described_class.new command, 'my_super_function' }

it "returns the complete script wrapped in a function" do
it "returns the complete script wrapped in a function without a bash3 bouncer" do
lines = subject.code.split "\n"
expect(lines[0]).to eq '#!/usr/bin/env bash'
expect(lines[1]).to start_with '# This script was generated by'
expect(lines[5]).to eq 'my_super_function() {'
expect(lines[6]).to eq ' # :command.root_command'
expect(lines[0..12].join("\n")).to match_approval('models/script/code-wrapped')
expect(lines[-1]).to eq '(return 0 2>/dev/null) || my_super_function "$@"'
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/workspaces/bash-3-syntax/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli
1 change: 1 addition & 0 deletions spec/fixtures/workspaces/bash-3-syntax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This fixture tests that bash 3 exits with a helpful error even when faced with syntax it doesn't understand
7 changes: 7 additions & 0 deletions spec/fixtures/workspaces/bash-3-syntax/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: cli
help: Sample application
version: 0.1.0

commands:
- name: test
help: Run test
6 changes: 6 additions & 0 deletions spec/fixtures/workspaces/bash-3-syntax/src/initialize.sh
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions spec/fixtures/workspaces/bash-3-syntax/src/test_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this syntax (`[[ -v`) cannot be parsed by bash 3
if [[ -v missingno ]]; then
echo the variable was not defined
fi
8 changes: 8 additions & 0 deletions spec/fixtures/workspaces/bash-3-syntax/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

# This fixture tests Bash 4+-only syntax
# It is executed as part of the Runfile examples test

bundle exec bashly generate

./cli
11 changes: 6 additions & 5 deletions spec/fixtures/workspaces/flag-args-with-dash/argflag
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :script.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n"
exit 1
fi

# :command.root_command
root_command() {
# :src/root_command.sh
Expand Down Expand Up @@ -170,11 +176,6 @@ initialize() {
long_usage=''
set -e

if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n"
exit 1
fi

# :src/initialize.sh
# Code here runs inside the initialize() function
# Use it for anything that you need to run before any other function, like
Expand Down