Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tweaking production/development output #577

Merged
merged 9 commits into from
Dec 19, 2024
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
2 changes: 1 addition & 1 deletion examples/render-mandoc/docs/download.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Automatically generated by Pandoc 3.2
.\"
.TH "download" "1" "November 2024" "Version 0.1.0" "Sample application"
.TH "download" "1" "December 2024" "Version 0.1.0" "Sample application"
.SH NAME
\f[B]download\f[R] \- Sample application
.SH SYNOPSIS
Expand Down
2 changes: 1 addition & 1 deletion examples/render-mandoc/docs/download.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% download(1) Version 0.1.0 | Sample application
% Lana Lang
% November 2024
% December 2024

NAME
==================================================
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/concerns/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def strings
# Outputs a comment that describes the view unless in production mode
def view_marker(id = nil)
id ||= ":#{caller_locations(1..1).first.path}"
"# #{id}" unless Settings.production?
"# #{id}" if Settings.enabled? :view_markers
end

# Reads a file from the userspace (Settings.source_dir) and returns
Expand Down
51 changes: 43 additions & 8 deletions lib/bashly/libraries/settings/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
# If you wish to change the path to this file, set the environment variable
# BASHLY_SETTINGS_PATH.


#-------------------------------------------------------------------------------
# PATH OPTIONS
#-------------------------------------------------------------------------------

# The path containing the bashly source files
source_dir: src

Expand All @@ -27,6 +32,14 @@ lib_dir: lib
# directory, and each command will get its own subdirectory
commands_dir: ~

# The extension to use when reading/writing partial script snippets
partials_extension: sh


#-------------------------------------------------------------------------------
# FORMAT OPTIONS
#-------------------------------------------------------------------------------

# Configure the bash options that will be added to the initialize function:
# strict: true Bash strict mode (set -euo pipefail)
# strict: false Only exit on errors (set -e)
Expand All @@ -38,6 +51,11 @@ strict: false
# (every 2 leading spaces will be converted to a tab character)
tab_indent: false


#-------------------------------------------------------------------------------
# INTERFACE OPTIONS
#-------------------------------------------------------------------------------

# When true, the generated script will consider any argument in the form of
# `-abc` as if it is `-a -b -c`.
compact_short_flags: true
Expand All @@ -47,14 +65,6 @@ compact_short_flags: true
# respectively.
conjoined_flag_args: true

# Set to 'production' or 'development':
# env: production Generate a smaller script, without file markers
# env: development Generate with file markers
env: development

# The extension to use when reading/writing partial script snippets
partials_extension: sh

# Show command examples (if any) whenever the user does not provide the
# required arguments
show_examples_on_error: false
Expand All @@ -75,3 +85,28 @@ usage_colors:
arg: ~
flag: ~
environment_variable: ~


#-------------------------------------------------------------------------------
# FEATURE TOGGLES
#-------------------------------------------------------------------------------

# Set to 'production' or 'development'.
# Determines which features are enabled in the rendered script.
# Use the `enable_*` options below to adjust settings for each environment.
# It is recommended to leave this set to 'development' and run
# `bashly generate --production` when the slimmer production script is needed.
env: development

# Tweak the script output by enabling or disabling some script output.
# These options accept one of the following strings:
# - production render this feature only when env == production
# - development render this feature only when env == development
# - always render this feature in any environment
# - never do not render this feature
enable_header_comment: always
enable_bash3_bouncer: always
enable_view_markers: development
enable_inspect_args: development
enable_deps_array: always
enable_env_var_names_array: always
2 changes: 1 addition & 1 deletion lib/bashly/script/dependency.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Bashly
module Script
class Dependency
class Dependency < Base
attr_reader :label, :commands, :help

class << self
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/script/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def header!

def default_header
result = render 'header'
result += render('bash3_bouncer') unless function_name
result += render('bash3_bouncer') unless function_name || !Settings.enabled?(:bash3_bouncer)
result
end

Expand Down
36 changes: 36 additions & 0 deletions lib/bashly/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class << self
:compact_short_flags,
:conjoined_flag_args,
:config_path,
:enable_bash3_bouncer,
:enable_deps_array,
:enable_env_var_names_array,
:enable_header_comment,
:enable_inspect_args,
:enable_view_markers,
:lib_dir,
:partials_extension,
:private_reveal_key,
Expand Down Expand Up @@ -35,6 +41,36 @@ def config_path
@config_path ||= get(:config_path) % { source_dir: source_dir }
end

def enabled?(feature)
send(:"enable_#{feature}") == 'always' ||
(send(:"enable_#{feature}") == 'production' && production?) ||
(send(:"enable_#{feature}") == 'development' && !production?)
end

def enable_header_comment
@enable_header_comment ||= get :enable_header_comment
end

def enable_bash3_bouncer
@enable_bash3_bouncer ||= get :enable_bash3_bouncer
end

def enable_view_markers
@enable_view_markers ||= get :enable_view_markers
end

def enable_inspect_args
@enable_inspect_args ||= get :enable_inspect_args
end

def enable_deps_array
@enable_deps_array ||= get :enable_deps_array
end

def enable_env_var_names_array
@enable_env_var_names_array ||= get :enable_env_var_names_array
end

def env
@env ||= get(:env)&.to_sym
end
Expand Down
4 changes: 3 additions & 1 deletion lib/bashly/views/command/default_root_script.gtx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
> echo "# this file is located in '{{ "#{Settings.source_dir}/#{filename}" }}'"
> echo "# you can edit it freely and regenerate (it will not be overwritten)"
> inspect_args
if Settings.enabled? :inspect_args
> inspect_args
end
>
4 changes: 3 additions & 1 deletion lib/bashly/views/command/default_script.gtx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
> echo "# this file is located in '{{ "#{Settings.source_dir}/#{filename}" }}'"
> echo "# code for '{{ full_name }}' goes here"
> echo "# you can edit it freely and regenerate (it will not be overwritten)"
> inspect_args
if Settings.enabled? :inspect_args
> inspect_args
end
>
11 changes: 1 addition & 10 deletions lib/bashly/views/command/dependencies_filter.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ if dependencies.any?
= view_marker

dependencies.each do |dependency|
> if command -v {{ dependency.commands.join(' ') }} >/dev/null 2>&1; then
> deps['{{ dependency.label }}']="$(command -v {{ dependency.commands.join(' ') }} | head -n1)"
> else
> printf "{{ strings[:missing_dependency] % { dependency: dependency.name } }}\n" >&2
if dependency.help
> printf "%s\n" "{{ dependency.help.sanitize_for_print }}" >&2
end
> exit 1
> fi
>
= dependency.render :filter
end
end
6 changes: 4 additions & 2 deletions lib/bashly/views/command/environment_variables_filter.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ if environment_variables.any?
= view_marker
= render(:environment_variables_default)

environment_variables.each do |env_var|
> env_var_names+=("{{ env_var.name.upcase }}")
if Settings.enabled? :env_var_names_array
environment_variables.each do |env_var|
> env_var_names+=("{{ env_var.name.upcase }}")
end
end
end

Expand Down
38 changes: 21 additions & 17 deletions lib/bashly/views/command/inspect_args.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@
> done
> fi
>
> if ((${#deps[@]})); then
> readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
> echo
> echo deps:
> for k in "${sorted_keys[@]}"; do
> echo "- \${deps[$k]} = ${deps[$k]}"
> done
> fi
>
> if ((${#env_var_names[@]})); then
> readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort)
> echo
> echo "environment variables:"
> for k in "${sorted_names[@]}"; do
> echo "- \$$k = ${!k:-}"
> done
> fi
if Settings.enabled? :deps_array
> if ((${#deps[@]})); then
> readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
> echo
> echo deps:
> for k in "${sorted_keys[@]}"; do
> echo "- \${deps[$k]} = ${deps[$k]}"
> done
> fi
>
end
if Settings.enabled? :env_var_names_array
> if ((${#env_var_names[@]})); then
> readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort)
> echo
> echo "environment variables:"
> for k in "${sorted_names[@]}"; do
> echo "- \$$k = ${!k:-}"
> done
> fi
end
> }
>
2 changes: 1 addition & 1 deletion lib/bashly/views/command/master_script.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
= render :version_command
= render :usage
= render :normalize_input
= render :inspect_args unless Settings.production?
= render :inspect_args if Settings.enabled? :inspect_args
= render :user_lib if user_lib.any?
= render :command_functions
= render :parse_requirements
Expand Down
8 changes: 6 additions & 2 deletions lib/bashly/views/command/run.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

> run() {
> declare -g -A args=()
> declare -g -A deps=()
if Settings.enabled? :deps_array
> declare -g -A deps=()
end
> declare -g -a other_args=()
> declare -g -a env_var_names=()
if Settings.enabled? :env_var_names_array
> declare -g -a env_var_names=()
end
> declare -g -a input=()
if has_unique_args_or_flags?
> declare -g -A unique_lookup=()
Expand Down
14 changes: 14 additions & 0 deletions lib/bashly/views/dependency/filter.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
= view_marker

> if ! command -v {{ commands.join(' ') }} >/dev/null 2>&1; then
> printf "{{ strings[:missing_dependency] % { dependency: name } }}\n" >&2
if help
> printf "%s\n" "{{ help.sanitize_for_print }}" >&2
end
> exit 1
if Settings.enabled? :deps_array
> else
> deps['{{ label }}']="$(command -v {{ commands.join(' ') }} | head -n1)"
end
> fi
>
6 changes: 4 additions & 2 deletions lib/bashly/views/wrapper/header.gtx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
> #!/usr/bin/env bash
> # This script was generated by bashly {{ Bashly::VERSION }} (https://bashly.dannyb.co)
> # Modifying it manually is not recommended
if Settings.enabled? :header_comment
> # This script was generated by bashly {{ Bashly::VERSION }} (https://bashly.dannyb.co)
> # Modifying it manually is not recommended
end
>
>
Loading
Loading