Skip to content

Commit 0058d77

Browse files
authored
Merge pull request #340 from DannyBen/add/external-libraries
Add support for adding libraries from a custom directory
2 parents ab97e1a + e516477 commit 0058d77

File tree

10 files changed

+69
-8
lines changed

10 files changed

+69
-8
lines changed

lib/bashly/commands/add.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ module Commands
33
class Add < Base
44
help 'Add extra features and customization to your script'
55

6-
usage 'bashly add LIBRARY [ARGS...] [--force]'
7-
usage 'bashly add --list'
6+
usage 'bashly add [--source NAME] LIBRARY [ARGS...] [--force]'
7+
usage 'bashly add [--source NAME] --list'
88
usage 'bashly add (-h|--help)'
99

10+
option '-s --source NAME', <<~USAGE
11+
Specify a different libraries source. NAME can be:
12+
13+
* Path to a local libraries directory
14+
* Github repository, in the form of 'github:user/repo'
15+
* Remote git repository, in the form of 'git:clone_url.git'
16+
USAGE
1017
option '-f --force', 'Overwrite existing files'
1118
option '-l --list', 'Show available libraries'
1219

@@ -22,14 +29,19 @@ def run
2229

2330
private
2431

32+
def source
33+
args['--source']
34+
end
35+
2536
def lib_source
26-
@lib_source ||= Bashly::LibrarySource.new
37+
@lib_source ||= Bashly::LibrarySource.new source
2738
end
2839

2940
def show_list
3041
lib_source.config.each do |key, config|
3142
usage = key
3243
usage += " #{config['usage']}" if config['usage']
44+
usage = "--source #{source} #{usage}" if source
3345
say "g`bashly add #{usage}`"
3446
say word_wrap(" #{config['help']}")
3547
say ''

lib/bashly/library_source.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ class LibrarySource
44

55
def initialize(path = nil)
66
@path = path || File.expand_path('libraries', __dir__)
7+
raise "Cannot find #{config_path}" unless File.exist? config_path
78
end
89

910
def config
10-
@config ||= YAML.properly_load_file("#{path}/libraries.yml")
11+
@config ||= YAML.properly_load_file config_path
12+
end
13+
14+
def config_path
15+
@config_path ||= "#{path}/libraries.yml"
1116
end
1217

1318
def libraries
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
created spec/tmp/src/lib/database.sh

spec/approvals/cli/add/help

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
Add extra features and customization to your script
22

33
Usage:
4-
bashly add LIBRARY [ARGS...] [--force]
5-
bashly add --list
4+
bashly add [--source NAME] LIBRARY [ARGS...] [--force]
5+
bashly add [--source NAME] --list
66
bashly add (-h|--help)
77

88
Options:
9+
-s --source NAME
10+
Specify a different libraries source. NAME can be:
11+
12+
* Path to a local libraries directory
13+
* Github repository, in the form of 'github:user/repo'
14+
* Remote git repository, in the form of 'git:clone_url.git'
15+
916
-f --force
1017
Overwrite existing files
1118

spec/approvals/cli/add/list-path

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bashly add --source spec/fixtures/libraries database
2+
Add database utilities
3+

spec/approvals/cli/add/usage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Usage:
2-
bashly add LIBRARY [ARGS...] [--force]
3-
bashly add --list
2+
bashly add [--source NAME] LIBRARY [ARGS...] [--force]
3+
bashly add [--source NAME] --list
44
bashly add (-h|--help)

spec/bashly/commands/add_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
it 'shows list of available libraries' do
3030
expect { subject.execute %w[add --list] }.to output_approval('cli/add/list')
3131
end
32+
33+
context 'with --source path' do
34+
it 'shows the list from the specified path' do
35+
expect { subject.execute %w[add --source spec/fixtures/libraries --list] }
36+
.to output_approval('cli/add/list-path')
37+
end
38+
end
3239
end
3340

3441
context 'with colors command' do
@@ -184,4 +191,13 @@
184191
expect(File).to exist(lib_file)
185192
end
186193
end
194+
195+
context 'with a library from an external --source path' do
196+
before { reset_tmp_dir create_src: true }
197+
198+
it 'properly installs the library' do
199+
expect { subject.execute %w[add --source spec/fixtures/libraries database] }
200+
.to output_approval('cli/add/from-extenal-path')
201+
end
202+
end
187203
end

spec/bashly/library_source_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
expect(subject.config).to be_a Hash
77
expect(subject.config.keys.first).to eq 'colors'
88
end
9+
10+
context 'with an external path' do
11+
subject { described_class.new path }
12+
13+
let(:path) { 'spec/fixtures/libraries' }
14+
15+
it 'reads the config from the specified path' do
16+
expect(subject.config).to be_a Hash
17+
expect(subject.config.keys).to eq ['database']
18+
end
19+
end
920
end
1021

1122
describe '#libraries' do
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# dummy
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
database:
2+
help: Add database utilities
3+
files:
4+
- source: "database/database.sh"
5+
target: "%{user_lib_dir}/database.%{user_ext}"

0 commit comments

Comments
 (0)