Skip to content

Commit bb1c285

Browse files
authored
Merge pull request #206 from DannyBen/add/markerless-generation
Allow generating script without file marker comments
2 parents 563a958 + 93c9446 commit bb1c285

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+147
-55
lines changed

lib/bashly.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@
1515
requires 'bashly/commands/base'
1616
requires 'bashly/libraries/base'
1717
requires 'bashly'
18+
19+
module Bashly
20+
class << self
21+
def env
22+
ENV['BASHLY_ENV']&.to_sym
23+
end
24+
25+
def production?
26+
env == :production
27+
end
28+
end
29+
end

lib/bashly/commands/generate.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@ module Commands
33
class Generate < Base
44
help "Generate the bash script and required files"
55

6-
usage "bashly generate [--force --quiet --upgrade --wrap FUNCTION]"
6+
usage "bashly generate [options]"
77
usage "bashly generate (-h|--help)"
88

99
option "-f --force", "Overwrite existing files"
1010
option "-q --quiet", "Disable on-screen progress report"
1111
option "-u --upgrade", "Upgrade all added library functions"
1212
option "-w --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
13+
option "-e --env ENV", "Force the generation environment (see BASHLY_ENV)"
1314

1415
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
1516
environment "BASHLY_TARGET_DIR", "The path to use for creating the bash script [default: .]"
1617
environment "BASHLY_LIB_DIR", "The path to use for upgrading library files, relative to the source dir [default: lib]"
1718
environment "BASHLY_STRICT", "When not empty, enable bash strict mode (set -euo pipefail)"
19+
environment "BASHLY_ENV", <<~EOF
20+
Set to 'production' or 'development':
21+
- production generate a smaller script, without file markers
22+
- development generate with file markers
23+
24+
Can be overridden with --env [default: development]
25+
EOF
1826

1927
example "bashly generate --force"
2028
example "bashly generate --wrap my_function"
2129

2230
def run
2331
validate_config
32+
ENV['BASHLY_ENV'] = args['--env'] if args['--env']
33+
quiet_say "creating !txtgrn!production!txtrst! version" if Bashly.production?
2434
create_user_files
2535
upgrade_libs if args['--upgrade']
2636
create_master_script

lib/bashly/concerns/renderable.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ module Bashly
44
module Renderable
55
def render(view)
66
template = File.read view_path(view)
7-
ERB.new(template, trim_mode: '%-').result(binding)
7+
erb = ERB.new(template, trim_mode: '%-')
8+
erb.filename = "#{views_subfolder}.#{view}"
9+
erb.result binding
810
end
911

1012
def strings
1113
@strings ||= MessageStrings.new
1214
end
1315

16+
def view_marker(id = nil)
17+
id ||= ":#{caller_locations.first.path}"
18+
"# #{id}" unless Bashly.production?
19+
end
20+
1421
private
1522

1623
def view_path(view)

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def load_user_file(file, placeholder: true)
102102
default_content
103103
end
104104

105-
"# :#{path}\n#{content}"
105+
Bashly.production? ? content : "#{view_marker path}\n#{content}"
106106
end
107107

108108
# Returns an array of all parents. For example, the command

lib/bashly/views/argument/usage.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# :argument.usage
1+
<%= view_marker %>
22
echo " <%= label %>"
33
printf "<%= help.wrap(76).indent(4).sanitize_for_print %>\n"
44
% if allowed

lib/bashly/views/argument/validations.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# :argument.validations
1+
<%= view_marker %>
22
% if validate
33
if [[ -n $(validate_<%= validate %> "$1") ]]; then
44
printf "<%= strings[:validation_error] %>\n" "<%= name.upcase %>" "$(validate_<%= validate %> "$1")"

lib/bashly/views/command/catch_all_filter.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# :command.catch_all_filter
1+
<%= view_marker %>
22
% if catch_all.required?
33
if [[ ${#other_args[@]} -eq 0 ]]; then
44
printf "<%= strings[:missing_required_argument] % { arg: catch_all.label, usage: usage_string } %>\n"

lib/bashly/views/command/command_fallback.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# :command.command_fallback
1+
<%= view_marker %>
22
% if default_command
33
"" )
44
<%= function_name %>_usage

lib/bashly/views/command/command_filter.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# :command.command_filter
1+
<%= view_marker %>
22
% if commands.any?
33
action=${1:-}
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# :command.command_functions
1+
<%= view_marker %>
22
% deep_commands.each do |command|
33
<%= command.render :function unless command.commands.any? %>
44
% end

0 commit comments

Comments
 (0)