Skip to content

Commit 470c9c6

Browse files
committed
- Add auto upgrade support to custom library sources
1 parent da8c993 commit 470c9c6

File tree

18 files changed

+64
-6
lines changed

18 files changed

+64
-6
lines changed

lib/bashly/commands/generate.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def upgrade_libs
8282
next unless content =~ /\[@bashly-upgrade (.+)\]/
8383

8484
args = $1.split
85+
8586
library_name = args.shift
8687
upgrade file, library_name, *args
8788
end
@@ -92,7 +93,13 @@ def generated_files
9293
end
9394

9495
def upgrade(existing_file, library_name, *args)
95-
source = Bashly::LibrarySource.new
96+
if library_name.include? ';'
97+
source_name, library_name = library_name.split(';')
98+
source = Bashly::LibrarySource.new source_name
99+
else
100+
source = Bashly::LibrarySource.new
101+
end
102+
96103
library = source.libraries[library_name.to_sym]
97104

98105
if library

lib/bashly/library.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module Bashly
22
class Library
3-
attr_reader :path, :config
3+
attr_reader :path, :config, :upgrade_string
44
attr_accessor :args
55

6-
def initialize(path, config)
6+
def initialize(path, config, upgrade_string: nil)
77
@path = path.to_s
88
@config = config
9+
@upgrade_string = upgrade_string
910
end
1011

1112
def files
@@ -16,7 +17,7 @@ def files
1617
config['files'].map do |file|
1718
{
1819
path: file['target'] % target_file_args,
19-
content: File.read("#{path}/#{file['source']}"),
20+
content: file_contents("#{path}/#{file['source']}"),
2021
}
2122
end
2223
end
@@ -42,6 +43,10 @@ def custom_handler
4243
@custom_handler ||= Module.const_get(config['handler']).new(*args)
4344
end
4445

46+
def file_contents(path)
47+
File.read(path).sub('[@bashly-upgrade]', "[@bashly-upgrade #{upgrade_string}]")
48+
end
49+
4550
def target_file_args
4651
{
4752
user_source_dir: Settings.source_dir,

lib/bashly/library_source.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def config
1919

2020
def libraries
2121
config.to_h do |name, spec|
22-
[name.to_sym, Library.new(path, spec)]
22+
upgrade_string = "#{uri};#{name}"
23+
[name.to_sym, Library.new(path, spec, upgrade_string: upgrade_string)]
2324
end
2425
end
2526

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
creating user files in spec/tmp/src
2+
skipped spec/tmp/src/initialize.sh (exists)
3+
skipped spec/tmp/src/root_command.sh (exists)
4+
updated spec/tmp/src/lib/database.sh
5+
created spec/tmp/cli
6+
run spec/tmp/cli --help to test your bash script
File renamed without changes.

spec/bashly/commands/generate_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@
200200
expect { subject.execute %w[generate -u] }.to output_approval('cli/generate/upgrade-unknown-lib')
201201
end
202202
end
203+
204+
context 'when upgrading a library from an external source' do
205+
before do
206+
reset_tmp_dir
207+
cp 'spec/fixtures/workspaces/lib-custom-source/*'
208+
system "ln -fs #{Dir.pwd}/spec/fixtures/libraries /tmp/bashly-tmp-source"
209+
end
210+
211+
it 'upgrades the library' do
212+
expect { subject.execute %w[generate -u] }.to output_approval('cli/generate/upgrade-custom-source')
213+
expect(File.read 'spec/tmp/src/lib/database.sh').to include('dummy')
214+
end
215+
end
203216
end
204217

205218
context 'with --watch' do

spec/bashly/library_source_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@
110110
it 'returns Library objects as values' do
111111
expect(subject.libraries.values.map(&:class).uniq).to eq [Library]
112112
end
113+
114+
context 'with a custom source' do
115+
let(:uri) { 'spec/fixtures/libraries' }
116+
117+
it 'replaces [@bashly-upgrade] marker with a proper upgrade string' do
118+
expect(subject.libraries[:database].files.first[:content])
119+
.to start_with '## [@bashly-upgrade spec/fixtures/libraries;database]'
120+
end
121+
end
113122
end
114123

115124
describe '#cleanup' do
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# dummy
1+
## [@bashly-upgrade]
2+
# dummy

spec/fixtures/libraries/libraries

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../../../../../..//vagrant/gems/bashly/spec/fixtures/libraries
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cli
2+

0 commit comments

Comments
 (0)