Skip to content

Commit c3edf8c

Browse files
committed
- Update dependencies option to support 'any' (e.g. wget or curl)
1 parent b5c3ec6 commit c3edf8c

File tree

7 files changed

+79
-12
lines changed

7 files changed

+79
-12
lines changed

lib/bashly/config_validator.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,30 @@ def assert_dependencies(key, value)
5050
when Array
5151
assert_array key, value, of: :string
5252
when Hash
53-
assert_hash key, value, of: :string
53+
assert_dependencies_hash key, value
5454
else
5555
assert [Array, Hash].include?(value.class),
5656
"#{key} must be an array or a hash"
5757
end
5858
end
5959

60+
def assert_dependencies_hash(key, value)
61+
value.each do |k, v|
62+
assert_dependency "#{key}.#{k}", v
63+
end
64+
end
65+
66+
def assert_dependency(key, value)
67+
assert [String, Hash].include?(value.class),
68+
"#{key} must be a string or a hash"
69+
70+
return if value.is_a? String
71+
72+
assert_hash key, value, keys: Script::Dependency.option_keys
73+
assert_string_or_array "#{key}.command", value['command']
74+
assert_optional_string "#{key}.help", value['help']
75+
end
76+
6077
def assert_extensible(key, value)
6178
return unless value
6279

lib/bashly/script/command.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def caption_string
5353
help.empty? ? full_name : "#{full_name} - #{summary}"
5454
end
5555

56+
# Returns an object representing the catch_all configuration
5657
def catch_all
5758
@catch_all ||= CatchAll.from_config options['catch_all']
5859
end
@@ -133,7 +134,16 @@ def default_flags
133134
flags.select(&:default)
134135
end
135136

136-
# Returns an array of EnvironmentVariables
137+
# Returns an array of Dependency objects
138+
def dependencies
139+
return [] unless options['dependencies']
140+
141+
@dependencies ||= options['dependencies'].map do |key, value|
142+
Dependency.from_config key, value
143+
end
144+
end
145+
146+
# Returns an array of EnvironmentVariable objects
137147
def environment_variables
138148
return [] unless options['environment_variables']
139149

lib/bashly/script/dependency.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Bashly
2+
module Script
3+
class Dependency
4+
attr_reader :label, :commands, :help
5+
6+
class << self
7+
def option_keys
8+
@option_keys ||= %i[command help]
9+
end
10+
11+
def from_config(key, value)
12+
options = case value
13+
when nil
14+
{ label: key, commands: key }
15+
when String
16+
{ label: key, commands: key, help: value }
17+
when Hash
18+
{ label: key, commands: value['command'], help: value['help'] }
19+
else
20+
{}
21+
end
22+
23+
new(**options)
24+
end
25+
end
26+
27+
def initialize(label: nil, commands: nil, help: nil)
28+
@label = label
29+
@commands = commands.is_a?(String) ? [commands] : commands
30+
@help = help&.empty? ? nil : help
31+
end
32+
33+
def name
34+
@name ||= if commands.size == 1
35+
commands.first
36+
else
37+
"#{label} (#{commands.join '/'})"
38+
end
39+
end
40+
end
41+
end
42+
end

lib/bashly/views/command/dependencies_filter.gtx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
if dependencies
22
= view_marker
33

4-
dependencies.each do |dependency, message|
5-
> if ! command -v {{ dependency }} >/dev/null 2>&1; then
6-
> printf "{{ strings[:missing_dependency] % { dependency: dependency } }}\n" >&2
7-
if message and !message.empty?
8-
> printf "%s\n" "{{ message }}" >&2
4+
dependencies.each do |dependency|
5+
> if ! command -v {{ dependency.commands.join(' ') }} >/dev/null 2>&1; then
6+
> printf "{{ strings[:missing_dependency] % { dependency: dependency.name } }}\n" >&2
7+
if dependency.help
8+
> printf "%s\n" "{{ dependency.help }}" >&2
99
end
1010
> exit 1
1111
> fi
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#<Bashly::ConfigurationError: root.dependencies.docker must be a string>
1+
#<Bashly::ConfigurationError: root.dependencies.docker must be a string or a hash>

spec/bashly/settings_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
end
5858

5959
describe 'strict_string' do
60-
original_value = Settings.strict
60+
original_value = described_class.strict
6161

6262
after { subject.strict = original_value }
6363

support/runfile/debug.runfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
action do
2-
uri = "git:https://github.com/DannyBen/bashly.git//spec/fixtures/libraries@master"
3-
4-
matches = uri.match(%r{git:(?<url>.*\.git)//?(?<path>[^@]+)?@?(?<ref>.*)})
2+
53

64
p matches[:url]
75
p matches[:path]

0 commit comments

Comments
 (0)