Skip to content

Commit e436f15

Browse files
authored
Merge pull request #221 from DannyBen/refactor
Add deprecation warning for `command.short`
2 parents 8a301d3 + 80e342e commit e436f15

25 files changed

+264
-48
lines changed

lib/bashly/commands/base.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,28 @@ module Commands
66
class Base < MisterBin::Command
77
include AssetHelper
88

9+
def config
10+
@config ||= Config.new "#{Settings.source_dir}/bashly.yml"
11+
end
12+
13+
def config_validator
14+
@config_validator ||= ConfigValidator.new config
15+
end
16+
917
def validate_config
10-
config = Config.new "#{Settings.source_dir}/bashly.yml"
11-
validator = ConfigValidator.new config
12-
validator.validate
18+
config_validator.validate
19+
end
20+
21+
def with_valid_config
22+
validate_config
23+
yield
24+
show_deprecations
25+
end
26+
27+
def show_deprecations
28+
return if config_validator.deprecations.empty? or ENV['BASHLY_HIDE_DEPRECATIONS']
29+
messages = "\n" + config_validator.deprecations.map(&:message).join("\n\n") + "\n\n"
30+
say! messages
1331
end
1432
end
1533
end

lib/bashly/commands/generate.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class Generate < Base
2929
example "bashly generate --wrap my_function"
3030

3131
def run
32-
validate_config
33-
Settings.env = args['--env'] if args['--env']
34-
quiet_say "creating !txtgrn!production!txtrst! version" if Settings.production?
35-
create_user_files
36-
upgrade_libs if args['--upgrade']
37-
create_master_script
38-
quiet_say "run !txtpur!#{master_script_path} --help!txtrst! to test your bash script"
32+
with_valid_config do
33+
Settings.env = args['--env'] if args['--env']
34+
quiet_say "creating !txtgrn!production!txtrst! version" if Settings.production?
35+
generate_all_files
36+
quiet_say "run !txtpur!#{master_script_path} --help!txtrst! to test your bash script"
37+
end
3938
end
4039

4140
private
@@ -44,6 +43,12 @@ def quiet_say(message)
4443
say message unless args['--quiet']
4544
end
4645

46+
def generate_all_files
47+
create_user_files
48+
upgrade_libs if args['--upgrade']
49+
create_master_script
50+
end
51+
4752
def upgrade_libs
4853
generated_files.each do |file|
4954
content = File.read file
@@ -128,10 +133,6 @@ def master_script_path
128133
"#{Settings.target_dir}/#{command.name}"
129134
end
130135

131-
def config
132-
@config ||= Config.new "#{Settings.source_dir}/bashly.yml"
133-
end
134-
135136
def command
136137
@command ||= Script::Command.new config
137138
end

lib/bashly/commands/preview.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class Preview < Base
99
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
1010

1111
def run
12-
config = Config.new "#{Settings.source_dir}/bashly.yml"
13-
command = Script::Command.new(config)
14-
script = Script::Wrapper.new command
15-
puts script.code
12+
with_valid_config do
13+
command = Script::Command.new config
14+
script = Script::Wrapper.new command
15+
puts script.code
16+
end
1617
end
1718
end
1819
end

lib/bashly/commands/validate.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ class Validate < Base
1010

1111
def run
1212
validate_config
13-
say "!txtgrn!OK"
13+
show_deprecations
14+
deprecations = config_validator.deprecations
15+
if deprecations.empty?
16+
say "!txtgrn!OK"
17+
else
18+
say "!txtred!WARNING!txtrst! Found #{deprecations.count} deprecations"
19+
end
1420
end
1521
end
1622
end

lib/bashly/config_validator.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def validate
1010
assert_command "root", data
1111
end
1212

13+
def deprecations
14+
@deprecations ||= []
15+
end
16+
1317
private
1418

1519
def assert(valid, message)
@@ -20,6 +24,10 @@ def refute(invalid, message)
2024
assert !invalid, message
2125
end
2226

27+
def deprecate(key, **options)
28+
deprecations.push Deprecation.new(key, **options)
29+
end
30+
2331
def assert_string(key, value)
2432
assert value.is_a?(String), "#{key} must be a string"
2533
end
@@ -183,6 +191,11 @@ def assert_command(key, value)
183191
refute value['version'], "#{key}.version makes no sense"
184192
refute value['extensible'], "#{key}.extensible makes no sense"
185193
end
194+
195+
# DEPRECATION 0.8.0
196+
if value['short']
197+
deprecate "#{key}.short", replacement: "alias", reference: "https://github.com/DannyBen/bashly/pull/220"
198+
end
186199
end
187200
end
188201
end

lib/bashly/deprecation.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Bashly
2+
class Deprecation
3+
attr_reader :old, :replacement, :reference
4+
5+
def initialize(old, replacement: nil, reference: nil)
6+
@old, @replacement, @reference = old, replacement, reference
7+
end
8+
9+
def message
10+
result = ["Deprecation Warning:", "!txtred!#{old}!txtrst! is deprecated"]
11+
result.push "use !txtgrn!#{replacement}!txtrst! instead" if replacement
12+
result.push "see !undblu!#{reference}!txtrst!" if reference
13+
14+
result.map { |line| "!txtred!▐!txtrst! #{line}"}.join("\n")
15+
end
16+
17+
def to_h
18+
{
19+
old: old,
20+
replacement: replacement,
21+
reference: reference
22+
}
23+
end
24+
end
25+
end

lib/bashly/script/command.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def option_keys
1212
extensible filename filters flags
1313
footer group help name
1414
private version
15+
short
1516
]
17+
# DEPRECATION 0.8.0
1618
end
1719
end
1820

@@ -32,6 +34,8 @@ def aliases
3234

3335
# Returns an array of alternative aliases if any
3436
def alt
37+
# DEPRECATION 0.8.0
38+
options['alias'] ||= options['short']
3539
return [] unless options["alias"]
3640
options['alias'].is_a?(String) ? [options['alias']] : options['alias']
3741
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
▐ Deprecation Warning:
3+
▐ root.commands[0].short is deprecated
4+
▐ use alias instead
5+
▐ see https://github.com/DannyBen/bashly/pull/220
6+
7+
▐ Deprecation Warning:
8+
▐ root.commands[1].short is deprecated
9+
▐ use alias instead
10+
▐ see https://github.com/DannyBen/bashly/pull/220
11+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WARNING Found 2 deprecations

spec/approvals/deprecation/message

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
!txtred!▐!txtrst! Deprecation Warning:
2+
!txtred!▐!txtrst! !txtred!old!txtrst! is deprecated
3+
!txtred!▐!txtrst! use !txtgrn!new!txtrst! instead
4+
!txtred!▐!txtrst! see !undblu!https://somewhere!txtrst!

0 commit comments

Comments
 (0)