bashly 0.6.8 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/lib/bashly/cli.rb +1 -0
  4. data/lib/bashly/commands/add.rb +35 -82
  5. data/lib/bashly/commands/generate.rb +49 -8
  6. data/lib/bashly/commands/init.rb +1 -1
  7. data/lib/bashly/commands/preview.rb +2 -2
  8. data/lib/bashly/commands/validate.rb +19 -0
  9. data/lib/bashly/concerns/asset_helper.rb +4 -0
  10. data/lib/bashly/concerns/command_scopes.rb +68 -0
  11. data/lib/bashly/concerns/completions.rb +5 -1
  12. data/lib/bashly/config_validator.rb +135 -0
  13. data/lib/bashly/extensions/file.rb +13 -0
  14. data/lib/bashly/extensions/string.rb +5 -1
  15. data/lib/bashly/libraries/base.rb +19 -0
  16. data/lib/bashly/libraries/completions.rb +14 -0
  17. data/lib/bashly/libraries/completions_function.rb +38 -0
  18. data/lib/bashly/libraries/completions_script.rb +29 -0
  19. data/lib/bashly/libraries/completions_yaml.rb +27 -0
  20. data/lib/bashly/libraries.yml +39 -0
  21. data/lib/bashly/library.rb +63 -0
  22. data/lib/bashly/refinements/compose_refinements.rb +45 -0
  23. data/lib/bashly/{models → script}/argument.rb +1 -1
  24. data/lib/bashly/{models → script}/base.rb +4 -2
  25. data/lib/bashly/script/catch_all.rb +49 -0
  26. data/lib/bashly/{models → script}/command.rb +9 -111
  27. data/lib/bashly/{models → script}/environment_variable.rb +1 -1
  28. data/lib/bashly/{models → script}/flag.rb +1 -1
  29. data/lib/bashly/{models/script.rb → script/wrapper.rb} +2 -2
  30. data/lib/bashly/templates/lib/colors.sh +41 -31
  31. data/lib/bashly/templates/lib/config.sh +34 -35
  32. data/lib/bashly/templates/lib/sample_function.sh +10 -10
  33. data/lib/bashly/templates/lib/validations/validate_dir_exists.sh +4 -0
  34. data/lib/bashly/templates/lib/validations/validate_file_exists.sh +4 -0
  35. data/lib/bashly/templates/lib/validations/validate_integer.sh +4 -0
  36. data/lib/bashly/templates/lib/validations/validate_not_empty.sh +4 -0
  37. data/lib/bashly/templates/lib/yaml.sh +12 -15
  38. data/lib/bashly/templates/strings.yml +1 -0
  39. data/lib/bashly/version.rb +1 -1
  40. data/lib/bashly/views/argument/validations.erb +8 -0
  41. data/lib/bashly/views/command/catch_all_filter.erb +2 -2
  42. data/lib/bashly/views/command/command_filter.erb +1 -1
  43. data/lib/bashly/views/command/default_assignments.erb +2 -2
  44. data/lib/bashly/views/command/default_initialize_script.erb +6 -6
  45. data/lib/bashly/views/command/environment_variables_filter.erb +1 -1
  46. data/lib/bashly/views/command/fixed_flags_filter.erb +1 -1
  47. data/lib/bashly/views/command/initialize.erb +1 -1
  48. data/lib/bashly/views/command/parse_requirements.erb +1 -1
  49. data/lib/bashly/views/command/parse_requirements_case.erb +4 -3
  50. data/lib/bashly/views/command/parse_requirements_while.erb +2 -2
  51. data/lib/bashly/views/command/required_args_filter.erb +1 -5
  52. data/lib/bashly/views/command/required_flags_filter.erb +1 -4
  53. data/lib/bashly/views/command/run.erb +4 -4
  54. data/lib/bashly/views/command/usage.erb +1 -1
  55. data/lib/bashly/views/command/usage_args.erb +3 -3
  56. data/lib/bashly/views/command/usage_commands.erb +1 -1
  57. data/lib/bashly/views/flag/case.erb +2 -1
  58. data/lib/bashly/views/flag/validations.erb +8 -0
  59. data/lib/bashly/views/{script → wrapper}/bash3_bouncer.erb +0 -0
  60. data/lib/bashly/views/{script → wrapper}/header.erb +0 -0
  61. data/lib/bashly/views/{script → wrapper}/wrapper.erb +0 -0
  62. data/lib/bashly.rb +3 -1
  63. metadata +33 -14
@@ -0,0 +1,38 @@
1
+ module Bashly
2
+ module Libraries
3
+ class CompletionsFunction < Completions
4
+ def files
5
+ [
6
+ {
7
+ path: "#{Settings.source_dir}/lib/#{function_name}.sh",
8
+ content: completions_function_code(function_name)
9
+ }
10
+ ]
11
+ end
12
+
13
+ def post_install_message
14
+ <<~EOF
15
+ In order to enable completions in your script, create a command or a flag (for example: !txtgrn!#{command.name} completions!txtrst! or !txtgrn!#{command.name} --completions!txtrst!) that calls the !txtgrn!#{function_name}!txtrst! function.
16
+
17
+ Your users can then run something like this to enable completions:
18
+
19
+ !txtpur!$ eval \"$(#{command.name} completions)\"
20
+ EOF
21
+ end
22
+
23
+ private
24
+
25
+ def function_name
26
+ @function_name ||= args[0] || 'send_completions'
27
+ end
28
+
29
+ def completions_function_code(function_name)
30
+ [
31
+ "## [@bashly-upgrade completions #{function_name}]",
32
+ command.completion_function(function_name)
33
+ ].join "\n"
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ module Bashly
2
+ module Libraries
3
+ class CompletionsScript < Completions
4
+ def files
5
+ [
6
+ {
7
+ path: target_path,
8
+ content: command.completion_script
9
+ }
10
+ ]
11
+ end
12
+
13
+ def post_install_message
14
+ <<~EOF
15
+ In order to enable completions, run:
16
+
17
+ !txtpur!$ source #{target_path}
18
+ EOF
19
+ end
20
+
21
+ private
22
+
23
+ def target_path
24
+ @target_path ||= args[0] || "#{Settings.target_dir}/completions.bash"
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ module Bashly
2
+ module Libraries
3
+ class CompletionsYAML < Completions
4
+ def files
5
+ [
6
+ {
7
+ path: target_path,
8
+ content: command.completion_data.to_yaml
9
+ }
10
+ ]
11
+ end
12
+
13
+ def post_install_message
14
+ <<~EOF
15
+ This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem.
16
+ EOF
17
+ end
18
+
19
+ private
20
+
21
+ def target_path
22
+ @target_path ||= args[0] || "#{Settings.target_dir}/completions.yml"
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ colors:
2
+ files:
3
+ - source: "templates/lib/colors.sh"
4
+ target: "%{user_source_dir}/lib/colors.sh"
5
+
6
+ config:
7
+ files:
8
+ - source: "templates/lib/config.sh"
9
+ target: "%{user_source_dir}/lib/config.sh"
10
+
11
+ yaml:
12
+ files:
13
+ - source: "templates/lib/yaml.sh"
14
+ target: "%{user_source_dir}/lib/yaml.sh"
15
+
16
+ lib:
17
+ files:
18
+ - source: "templates/lib/sample_function.sh"
19
+ target: "%{user_source_dir}/lib/sample_function.sh"
20
+
21
+ strings:
22
+ files:
23
+ - source: "templates/strings.yml"
24
+ target: "%{user_source_dir}/bashly-strings.yml"
25
+
26
+ validations:
27
+ files:
28
+ - source: "templates/lib/validations/validate_dir_exists.sh"
29
+ target: "%{user_source_dir}/lib/validations/validate_dir_exists.sh"
30
+ - source: "templates/lib/validations/validate_file_exists.sh"
31
+ target: "%{user_source_dir}/lib/validations/validate_file_exists.sh"
32
+ - source: "templates/lib/validations/validate_integer.sh"
33
+ target: "%{user_source_dir}/lib/validations/validate_integer.sh"
34
+ - source: "templates/lib/validations/validate_not_empty.sh"
35
+ target: "%{user_source_dir}/lib/validations/validate_not_empty.sh"
36
+
37
+ completions: :CompletionsFunction
38
+ completions_script: :CompletionsScript
39
+ completions_yaml: :CompletionsYAML
@@ -0,0 +1,63 @@
1
+ module Bashly
2
+ class Library
3
+ class << self
4
+ def exist?(name)
5
+ config.has_key? name.to_s
6
+ end
7
+
8
+ def config
9
+ @config ||= YAML.load_file(config_path)
10
+ end
11
+
12
+ def config_path
13
+ @config_path ||= File.expand_path 'libraries.yml', __dir__
14
+ end
15
+ end
16
+
17
+ include AssetHelper
18
+ attr_reader :name, :args
19
+
20
+ def initialize(name, *args)
21
+ @name, @args = name.to_s, args
22
+ end
23
+
24
+ def files
25
+ if custom_handler
26
+ custom_handler.files
27
+
28
+ else
29
+ config['files'].map do |file|
30
+ { path: file['target'] % target_file_args,
31
+ content: asset_content(file['source']) }
32
+ end
33
+ end
34
+ end
35
+
36
+ def post_install_message
37
+ if custom_handler
38
+ custom_handler.post_install_message
39
+ else
40
+ config['post_install_message']
41
+ end
42
+ end
43
+
44
+ def find_file(path)
45
+ files.select { |f| f[:path] == path }.first
46
+ end
47
+
48
+ private
49
+
50
+ def custom_handler
51
+ return nil unless config.is_a? Symbol
52
+ @custom_handler ||= Bashly::Libraries.const_get(config).new(*args)
53
+ end
54
+
55
+ def config
56
+ @config ||= self.class.config[name]
57
+ end
58
+
59
+ def target_file_args
60
+ { user_source_dir: Settings.source_dir }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ module ComposeRefinements
4
+ refine Hash do
5
+ def compose(keyword = 'import')
6
+ result = {}
7
+ each do |k, v|
8
+ if k.to_s == keyword
9
+ sub = safe_load_yaml(v).compose keyword
10
+ if sub.is_a? Array
11
+ result = sub
12
+ else
13
+ result.merge! sub
14
+ end
15
+ elsif v.respond_to? :compose
16
+ result[k] = v.compose keyword
17
+ else
18
+ result[k] = v
19
+ end
20
+ end
21
+ result
22
+ end
23
+
24
+ def safe_load_yaml(path)
25
+ loaded = YAML.load_file path
26
+ return loaded if loaded.is_a? Array or loaded.is_a? Hash
27
+ raise Bashly::ConfigurationError, "Cannot find a valid YAML in !txtgrn!#{path}"
28
+
29
+ rescue Errno::ENOENT
30
+ raise Bashly::ConfigurationError, "Cannot find import file !txtgrn!#{path}"
31
+ end
32
+ end
33
+
34
+ refine Array do
35
+ def compose(keyword = 'import')
36
+ map do |x|
37
+ if x.respond_to? :compose
38
+ x.compose keyword
39
+ else
40
+ x
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Argument < Base
4
4
  def usage_string
5
5
  required ? name.upcase : "[#{name.upcase}]"
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Base
4
4
  include Renderable
5
5
 
@@ -23,15 +23,17 @@ module Bashly
23
23
  long
24
24
  name
25
25
  parent_name
26
+ private
26
27
  required
27
28
  short
29
+ validate
28
30
  version
29
31
  ]
30
32
 
31
33
  def initialize(options)
32
34
  raise Error, "Invalid options provided" unless options.respond_to? :keys
33
35
  @options = options
34
- verify if respond_to? :verify
36
+ validate_options if respond_to? :validate_options
35
37
  end
36
38
 
37
39
  def optional
@@ -0,0 +1,49 @@
1
+ module Bashly
2
+ module Script
3
+ class CatchAll
4
+ class << self
5
+ def from_config(config)
6
+ options = case config
7
+ when nil
8
+ { enabled: false }
9
+ when String
10
+ { label: config }
11
+ when Hash
12
+ { label: config['label'], help: config['help'], required: config['required'] }
13
+ else
14
+ {}
15
+ end
16
+
17
+ new **options
18
+ end
19
+ end
20
+
21
+ def initialize(label: nil, help: nil, required: false, enabled: true)
22
+ @label, @help, @required, @enabled = label, help, required, enabled
23
+ end
24
+
25
+ def enabled?
26
+ @enabled
27
+ end
28
+
29
+ def label
30
+ enabled? ? "#{@label&.upcase}..." : nil
31
+ end
32
+
33
+ def help
34
+ enabled? ? @help : nil
35
+ end
36
+
37
+ def required?
38
+ @required
39
+ end
40
+
41
+ def usage_string
42
+ return nil unless enabled?
43
+ required? ? label : "[#{label}]"
44
+ end
45
+
46
+ end
47
+ end
48
+ end
49
+
@@ -1,7 +1,8 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Command < Base
4
4
  include Completions
5
+ include CommandScopes
5
6
 
6
7
  # Returns the name to be used as an action.
7
8
  # - If it is the root command, the action is "root"
@@ -30,44 +31,8 @@ module Bashly
30
31
  help ? "#{full_name} - #{summary}" : full_name
31
32
  end
32
33
 
33
- # Returns a label for the catch_all directive
34
- def catch_all_label
35
- return nil unless catch_all
36
-
37
- if catch_all.is_a? String
38
- "#{catch_all.upcase}..."
39
- elsif catch_all.is_a?(Hash) and catch_all['label'].is_a?(String)
40
- "#{catch_all['label'].upcase}..."
41
- else
42
- "..."
43
- end
44
- end
45
-
46
- # Returns a used defined help string for the catch_all directive
47
- def catch_all_help
48
- return nil unless catch_all
49
-
50
- if catch_all.is_a?(Hash) and catch_all['help'].is_a?(String)
51
- catch_all['help']
52
- else
53
- nil
54
- end
55
- end
56
-
57
- # Returns true if catch_all is required
58
- def catch_all_required?
59
- catch_all.is_a?(Hash) and catch_all['required']
60
- end
61
-
62
- # Returns a string suitable for catch_all Usage pattern
63
- def catch_all_usage
64
- return nil unless catch_all
65
- catch_all_required? ? catch_all_label : "[#{catch_all_label}]"
66
- end
67
-
68
- # Returns only the names of the Commands
69
- def command_names
70
- commands.map &:name
34
+ def catch_all
35
+ @catch_all ||= CatchAll.from_config options['catch_all']
71
36
  end
72
37
 
73
38
  # Returns an array of the Commands
@@ -79,40 +44,6 @@ module Bashly
79
44
  end
80
45
  end
81
46
 
82
- # Returns a flat array containing all the commands in this tree.
83
- # This includes self + children + grandchildres + ...
84
- def deep_commands
85
- result = []
86
- commands.each do |command|
87
- result << command
88
- if command.commands.any?
89
- result += command.deep_commands
90
- end
91
- end
92
- result
93
- end
94
-
95
- # Returns an array of all the default Args
96
- def default_args
97
- args.select &:default
98
- end
99
-
100
- # If any of this command's subcommands has the default option set to
101
- # true, this default command will be returned, nil otherwise.
102
- def default_command
103
- commands.find { |c| c.default }
104
- end
105
-
106
- # Returns an array of all the default Environment Variables
107
- def default_environment_variables
108
- environment_variables.select &:default
109
- end
110
-
111
- # Returns an array of all the default Flags
112
- def default_flags
113
- flags.select &:default
114
- end
115
-
116
47
  # Returns an array of EnvironmentVariables
117
48
  def environment_variables
118
49
  return [] unless options["environment_variables"]
@@ -154,7 +85,7 @@ module Bashly
154
85
  default_content = placeholder ? "echo \"error: cannot load file\"" : ''
155
86
 
156
87
  content = if File.exist? path
157
- File.read path
88
+ File.read(path).remove_front_matter
158
89
  else
159
90
  default_content
160
91
  end
@@ -168,21 +99,6 @@ module Bashly
168
99
  options['parents'] || []
169
100
  end
170
101
 
171
- # Returns an array of all the required Arguments
172
- def required_args
173
- args.select &:required
174
- end
175
-
176
- # Returns an array of all the required EnvironmentVariables
177
- def required_environment_variables
178
- environment_variables.select &:required
179
- end
180
-
181
- # Returns an array of all the required Flags
182
- def required_flags
183
- flags.select &:required
184
- end
185
-
186
102
  # Returns trus if this is the root command (no parents)
187
103
  def root_command?
188
104
  parents.empty?
@@ -201,7 +117,7 @@ module Bashly
201
117
  result << arg.usage_string
202
118
  end
203
119
  result << "[options]" unless flags.empty?
204
- result << catch_all_usage if catch_all
120
+ result << catch_all.usage_string if catch_all.enabled?
205
121
  result.join " "
206
122
  end
207
123
 
@@ -213,27 +129,9 @@ module Bashly
213
129
  end
214
130
 
215
131
  # Raise an exception if there are some serious issues with the command
216
- # definition.
217
- def verify
218
- verify_commands if commands.any?
219
- end
220
-
221
- # Returns an array of all the args with a whitelist
222
- def whitelisted_args
223
- args.select &:allowed
224
- end
225
-
226
- # Returns an array of all the flags with a whitelist arg
227
- def whitelisted_flags
228
- flags.select &:allowed
229
- end
230
-
231
- private
232
-
233
- def verify_commands
234
- if args.any? or flags.any?
235
- raise ConfigurationError, "Error in the !txtgrn!#{full_name}!txtrst! command.\nThe !txtgrn!commands!txtrst! key cannot be at the same level as the !txtgrn!args!txtrst! or !txtgrn!flags!txtrst! keys."
236
- end
132
+ # definition. This is called by Base#initialize.
133
+ def validate_options
134
+ Bashly::ConfigValidator.new(options).validate
237
135
  end
238
136
 
239
137
  end
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class EnvironmentVariable < Base
4
4
  def usage_string(extended: false)
5
5
  result = [name.upcase]
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Flag < Base
4
4
  def aliases
5
5
  if long and short
@@ -1,6 +1,6 @@
1
1
  module Bashly
2
- module Models
3
- class Script
2
+ module Script
3
+ class Wrapper
4
4
  include Renderable
5
5
 
6
6
  attr_reader :command, :function_name
@@ -1,32 +1,42 @@
1
- # ---
2
- # Color functions
3
- # This file is a part of Bashly standard library
4
- #
5
- # Usage:
6
- # Use any of the functions below to color or format a portion of a string.
7
- #
8
- # echo "before $(red this is red) after"
9
- # echo "before $(green_bold this is green_bold) after"
10
- #
11
- # ---
1
+ ## Color functions [@bashly-upgrade colors]
2
+ ## This file is a part of Bashly standard library
3
+ ##
4
+ ## Usage:
5
+ ## Use any of the functions below to color or format a portion of a string.
6
+ ##
7
+ ## echo "before $(red this is red) after"
8
+ ## echo "before $(green_bold this is green_bold) after"
9
+ ##
10
+ ## Color output will be disabled if `NO_COLOR` environment variable is set
11
+ ## in compliance with https://no-color.org/
12
+ ##
13
+ print_in_color() {
14
+ local color="$1"
15
+ shift
16
+ if [[ -z ${NO_COLOR+x} ]]; then
17
+ printf "$color%b\e[0m\n" "$*";
18
+ else
19
+ printf "%b\n" "$*";
20
+ fi
21
+ }
12
22
 
13
- red() { printf "\e[31m%b\e[0m\n" "$*"; }
14
- green() { printf "\e[32m%b\e[0m\n" "$*"; }
15
- yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
16
- blue() { printf "\e[34m%b\e[0m\n" "$*"; }
17
- magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
18
- cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
19
- bold() { printf "\e[1m%b\e[0m\n" "$*"; }
20
- underlined() { printf "\e[4m%b\e[0m\n" "$*"; }
21
- red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; }
22
- green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; }
23
- yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; }
24
- blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; }
25
- magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; }
26
- cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; }
27
- red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; }
28
- green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; }
29
- yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; }
30
- blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; }
31
- magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; }
32
- cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; }
23
+ red() { print_in_color "\e[31m" "$*"; }
24
+ green() { print_in_color "\e[32m" "$*"; }
25
+ yellow() { print_in_color "\e[33m" "$*"; }
26
+ blue() { print_in_color "\e[34m" "$*"; }
27
+ magenta() { print_in_color "\e[35m" "$*"; }
28
+ cyan() { print_in_color "\e[36m" "$*"; }
29
+ bold() { print_in_color "\e[1m" "$*"; }
30
+ underlined() { print_in_color "\e[4m" "$*"; }
31
+ red_bold() { print_in_color "\e[1;31m" "$*"; }
32
+ green_bold() { print_in_color "\e[1;32m" "$*"; }
33
+ yellow_bold() { print_in_color "\e[1;33m" "$*"; }
34
+ blue_bold() { print_in_color "\e[1;34m" "$*"; }
35
+ magenta_bold() { print_in_color "\e[1;35m" "$*"; }
36
+ cyan_bold() { print_in_color "\e[1;36m" "$*"; }
37
+ red_underlined() { print_in_color "\e[4;31m" "$*"; }
38
+ green_underlined() { print_in_color "\e[4;32m" "$*"; }
39
+ yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
40
+ blue_underlined() { print_in_color "\e[4;34m" "$*"; }
41
+ magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
42
+ cyan_underlined() { print_in_color "\e[4;36m" "$*"; }