Skip to content

Commit bcd35dc

Browse files
committed
- Add support for git-sourced libraries
1 parent 0058d77 commit bcd35dc

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

lib/bashly/commands/add.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def run
2525
else
2626
add_lib args['LIBRARY']
2727
end
28+
29+
lib_source.cleanup if lib_source.git?
2830
end
2931

3032
private
@@ -41,8 +43,7 @@ def show_list
4143
lib_source.config.each do |key, config|
4244
usage = key
4345
usage += " #{config['usage']}" if config['usage']
44-
usage = "--source #{source} #{usage}" if source
45-
say "g`bashly add #{usage}`"
46+
say "g`#{usage}`"
4647
say word_wrap(" #{config['help']}")
4748
say ''
4849
end

lib/bashly/library_source.rb

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,81 @@
11
module Bashly
22
class LibrarySource
3-
attr_reader :path
3+
attr_reader :uri
44

5-
def initialize(path = nil)
6-
@path = path || File.expand_path('libraries', __dir__)
7-
raise "Cannot find #{config_path}" unless File.exist? config_path
5+
def initialize(uri = nil)
6+
@uri = uri || File.expand_path('libraries', __dir__)
7+
transform_github_uri if /^github(:|-)/.match? @uri
88
end
99

10-
def config
11-
@config ||= YAML.properly_load_file config_path
10+
def git?
11+
/^(git|github|github-ssh):/.match? uri
1212
end
1313

14-
def config_path
15-
@config_path ||= "#{path}/libraries.yml"
14+
def config
15+
@config ||= YAML.properly_load_file config_path
1616
end
1717

1818
def libraries
1919
config.to_h do |name, spec|
2020
[name.to_sym, Library.new(path, spec)]
2121
end
2222
end
23+
24+
def config_path
25+
@config_path ||= if File.exist?("#{path}/libraries.yml")
26+
"#{path}/libraries.yml"
27+
else
28+
raise "Cannot find #{path}/libraries.yml"
29+
end
30+
end
31+
32+
def cleanup
33+
FileUtils.rm_rf(File.join(Dir.tmpdir, 'bashly-libs-*'))
34+
end
35+
36+
private
37+
38+
def path
39+
@path ||= if uri.start_with? 'git:'
40+
git_clone
41+
else
42+
uri
43+
end
44+
end
45+
46+
def git_clone
47+
dir = Dir.mktmpdir 'bashly-libs-'
48+
safe_run "git clone --depth 1 #{git_specs[:url]} #{dir}"
49+
safe_run "git checkout #{git_specs[:ref]}" if git_specs[:ref]
50+
51+
"#{dir}#{git_specs[:path]}"
52+
end
53+
54+
def git_specs
55+
@git_specs ||= begin
56+
parts = uri.match(%r{git:(?<url>.*\.git)(?:/)?(?<path>/[^@]+)?@?(?<ref>.*)})
57+
raise 'Invalid source' unless parts
58+
59+
url = parts[:url]
60+
raise 'Invalid git URL' unless url
61+
62+
path = parts[:path]
63+
ref = parts[:ref].empty? ? nil : parts[:ref]
64+
65+
{ url: url, path: path, ref: ref }
66+
end
67+
end
68+
69+
def safe_run(cmd)
70+
raise "Failed running command:\nm`#{cmd}`" unless system cmd
71+
end
72+
73+
def transform_github_uri
74+
if (matches = uri.match(%r{github-ssh:(?<user>[^/]+)/(?<repo>[^/]+)(?<rest>.*)}))
75+
@uri = "git:git@github.com:#{matches[:user]}/#{matches[:repo]}.git#{matches[:rest]}"
76+
elsif (matches = uri.match(%r{github:(?<user>[^/]+)/(?<repo>[^/]+)(?<rest>.*)}))
77+
@uri = "git:https://github.com/#{matches[:user]}/#{matches[:repo]}.git#{matches[:rest]}"
78+
end
79+
end
2380
end
2481
end

spec/approvals/cli/add/list

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
bashly add colors
1+
colors
22
Add standard functions for printing colorful and formatted text to the lib
33
directory.
44

5-
bashly add completions [PATH]
5+
completions [PATH]
66
Generate a bash completions function.
77

8-
bashly add completions_script [PATH]
8+
completions_script [PATH]
99
Generate a standalone bash completions script.
1010

11-
bashly add completions_yaml [PATH]
11+
completions_yaml [PATH]
1212
Generate a completions YAML configuration for Completely.
1313

14-
bashly add config
14+
config
1515
Add standard functions for handling INI files to the lib directory.
1616

17-
bashly add help
17+
help
1818
Add a help command, in addition to the standard --help flag.
1919

20-
bashly add lib
20+
lib
2121
Create the lib directory for any additional user scripts.
2222
All *.sh scripts in this directory will be included in the final bash script.
2323
Note that if you configured a different partials_extension, then the
2424
extensions of the files in this directory need to match.
2525

26-
bashly add settings
26+
settings
2727
Copy a sample settings.yml file to your project, allowing you to customize
2828
some bashly options.
2929

30-
bashly add strings
30+
strings
3131
Copy an additional configuration file to your project, allowing you to
3232
customize all the tips and error strings.
3333

34-
bashly add test
34+
test
3535
Add approval testing.
3636

37-
bashly add validations
37+
validations
3838
Add argument validation functions to the lib directory.
3939

40-
bashly add yaml
40+
yaml
4141
Add standard functions for reading YAML files.
4242

spec/approvals/cli/add/list-path

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bashly add --source spec/fixtures/libraries database
1+
database
22
Add database utilities
33

0 commit comments

Comments
 (0)