Skip to content

Commit cbba58c

Browse files
authored
Merge pull request #147 from DannyBen/refactor/libraries
Refactor library handling (internal)
2 parents 4359510 + 5885694 commit cbba58c

38 files changed

+518
-374
lines changed

lib/bashly.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
requires 'bashly/exceptions'
1212
requires 'bashly/script/base'
1313
requires 'bashly/commands/base'
14-
requires 'bashly/library/base'
14+
requires 'bashly/libraries/base'
1515
requires 'bashly'

lib/bashly/commands/add.rb

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,63 +32,52 @@ class Add < Base
3232
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
3333

3434
def strings_command
35-
add_lib Library::Strings.new
35+
add_lib 'strings'
3636
end
3737

3838
def lib_command
39-
add_lib Library::Sample.new
39+
add_lib 'lib'
4040
end
4141

4242
def config_command
43-
add_lib Library::Config.new
43+
add_lib 'config'
4444
end
4545

4646
def colors_command
47-
add_lib Library::Colors.new
47+
add_lib 'colors'
4848
end
4949

5050
def yaml_command
51-
add_lib Library::YAML.new
51+
add_lib 'yaml'
5252
end
5353

5454
def validations_command
55-
add_lib Library::Validations.new
55+
add_lib 'validations'
5656
end
5757

5858
def comp_command
5959
format = args['FORMAT']
6060
output = args['OUTPUT']
6161

6262
case format
63-
when "script"
64-
path = output || "#{Settings.target_dir}/completions.bash"
65-
add_lib Library::CompletionsScript.new(path)
66-
67-
when "function"
68-
function = output || "send_completions"
69-
path = "#{Settings.source_dir}/lib/#{function}.sh"
70-
add_lib Library::CompletionsFunction.new(path, function: function)
71-
72-
when "yaml"
73-
path = output || "#{Settings.target_dir}/completions.yml"
74-
add_lib Library::CompletionsYAML.new(path)
75-
76-
else
77-
raise Error, "Unrecognized format: #{format}"
78-
63+
when "script" then add_lib 'completions_script', output
64+
when "function" then add_lib 'completions', output
65+
when "yaml" then add_lib 'completions_yaml', output
66+
else raise Error, "Unrecognized format: #{format}"
7967
end
8068

8169
end
8270

8371
private
8472

85-
def add_lib(handler)
73+
def add_lib(name, *args)
74+
library = Bashly::Library.new name, *args
8675
files_created = 0
87-
handler.files.each do |file|
76+
library.files.each do |file|
8877
created = safe_write file[:path], file[:content]
8978
files_created += 1 if created
9079
end
91-
message = handler.post_install_message
80+
message = library.post_install_message
9281
say "\n#{message}" if message and files_created > 0
9382
end
9483

lib/bashly/commands/generate.rb

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,9 @@ def upgrade_libs
3535
content = File.read file
3636

3737
if content =~ /\[@bashly-upgrade (.+)\]/
38-
lib = $1
39-
40-
case lib
41-
when "colors"
42-
upgrade file, Library::Colors.new
43-
when "config"
44-
upgrade file, Library::Config.new
45-
when "yaml"
46-
upgrade file, Library::YAML.new
47-
when "validations"
48-
upgrade file, Library::Validations.new
49-
when /completions (.+)/
50-
upgrade file, Library::CompletionsFunction.new(file, function: $1)
51-
end
38+
args = $1.split ' '
39+
library_name = args.shift
40+
upgrade file, library_name, *args
5241
end
5342
end
5443
end
@@ -57,8 +46,17 @@ def generated_files
5746
Dir["#{Settings.source_dir}/**/*.*"].sort
5847
end
5948

60-
def upgrade(existing_file, handler)
61-
file = handler.files.select { |f| f[:path] == existing_file }.first
49+
def upgrade(existing_file, library_name, *args)
50+
if Library.exist? library_name
51+
upgrade! existing_file, library_name, *args
52+
else
53+
quiet_say "!txtred!warning!txtrst! not upgrading !txtcyn!#{existing_file}!txtrst!, unknown library '#{library_name}'"
54+
end
55+
end
56+
57+
def upgrade!(existing_file, library_name, *args)
58+
library = Bashly::Library.new library_name, *args
59+
file = library.find_file existing_file
6260

6361
if file
6462
File.deep_write file[:path], file[:content]

lib/bashly/libraries.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
colors:
2+
files:
3+
- source: "templates/lib/colors.sh"
4+
target: "%{user_source_dir}/lib/colors.sh"
5+
6+
config:
7+
files:
8+
- source: "templates/lib/config.sh"
9+
target: "%{user_source_dir}/lib/config.sh"
10+
11+
yaml:
12+
files:
13+
- source: "templates/lib/yaml.sh"
14+
target: "%{user_source_dir}/lib/yaml.sh"
15+
16+
lib:
17+
files:
18+
- source: "templates/lib/sample_function.sh"
19+
target: "%{user_source_dir}/lib/sample_function.sh"
20+
21+
strings:
22+
files:
23+
- source: "templates/strings.yml"
24+
target: "%{user_source_dir}/bashly-strings.yml"
25+
26+
validations:
27+
files:
28+
- source: "templates/lib/validations/validate_dir_exists.sh"
29+
target: "%{user_source_dir}/lib/validations/validate_dir_exists.sh"
30+
- source: "templates/lib/validations/validate_file_exists.sh"
31+
target: "%{user_source_dir}/lib/validations/validate_file_exists.sh"
32+
- source: "templates/lib/validations/validate_integer.sh"
33+
target: "%{user_source_dir}/lib/validations/validate_integer.sh"
34+
- source: "templates/lib/validations/validate_not_empty.sh"
35+
target: "%{user_source_dir}/lib/validations/validate_not_empty.sh"
36+
37+
completions: :CompletionsFunction
38+
completions_script: :CompletionsScript
39+
completions_yaml: :CompletionsYAML

lib/bashly/libraries/base.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Bashly
2+
module Libraries
3+
class Base
4+
attr_reader :args
5+
6+
def initialize(*args)
7+
@args = args
8+
end
9+
10+
def files
11+
raise NotImplementedError, "Please implement #files"
12+
end
13+
14+
def post_install_message
15+
nil
16+
end
17+
end
18+
end
19+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Bashly
2+
module Libraries
3+
class Completions < Base
4+
protected
5+
def command
6+
@command ||= Script::Command.new config
7+
end
8+
9+
def config
10+
@config ||= Bashly::Config.new "#{Settings.source_dir}/bashly.yml"
11+
end
12+
end
13+
end
14+
end
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Bashly
2-
module Library
2+
module Libraries
33
class CompletionsFunction < Completions
4-
def file_content
4+
def files
55
[
6-
"# [@bashly-upgrade completions #{function_name}]",
7-
command.completion_function(function_name)
8-
].join "\n"
6+
{
7+
path: "#{Settings.source_dir}/lib/#{function_name}.sh",
8+
content: completions_function_code(function_name)
9+
}
10+
]
911
end
1012

1113
def post_install_message
@@ -18,9 +20,19 @@ def post_install_message
1820
EOF
1921
end
2022

23+
private
24+
2125
def function_name
22-
options[:function]
26+
@function_name ||= args[0] || 'send_completions'
2327
end
28+
29+
def completions_function_code(function_name)
30+
[
31+
"# [@bashly-upgrade completions #{function_name}]",
32+
command.completion_function(function_name)
33+
].join "\n"
34+
end
35+
2436
end
2537
end
2638
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Bashly
2+
module Libraries
3+
class CompletionsScript < Completions
4+
def files
5+
[
6+
{
7+
path: target_path,
8+
content: command.completion_script
9+
}
10+
]
11+
end
12+
13+
def post_install_message
14+
<<~EOF
15+
In order to enable completions, run:
16+
17+
!txtpur!$ source #{target_path}
18+
EOF
19+
end
20+
21+
private
22+
23+
def target_path
24+
@target_path ||= args[0] || "#{Settings.target_dir}/completions.bash"
25+
end
26+
27+
end
28+
end
29+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Bashly
2+
module Libraries
3+
class CompletionsYAML < Completions
4+
def files
5+
[
6+
{
7+
path: target_path,
8+
content: command.completion_data.to_yaml
9+
}
10+
]
11+
end
12+
13+
def post_install_message
14+
<<~EOF
15+
This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem.
16+
EOF
17+
end
18+
19+
private
20+
21+
def target_path
22+
@target_path ||= args[0] || "#{Settings.target_dir}/completions.yml"
23+
end
24+
25+
end
26+
end
27+
end

lib/bashly/library.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module Bashly
2+
class Library
3+
class << self
4+
def exist?(name)
5+
config.has_key? name.to_s
6+
end
7+
8+
def config
9+
@config ||= YAML.load_file(config_path)
10+
end
11+
12+
def config_path
13+
@config_path ||= File.expand_path 'libraries.yml', __dir__
14+
end
15+
end
16+
17+
include AssetHelper
18+
attr_reader :name, :args
19+
20+
def initialize(name, *args)
21+
@name, @args = name.to_s, args
22+
end
23+
24+
def files
25+
if custom_handler
26+
custom_handler.files
27+
28+
else
29+
config['files'].map do |file|
30+
{ path: file['target'] % target_file_args,
31+
content: asset_content(file['source']) }
32+
end
33+
end
34+
end
35+
36+
def post_install_message
37+
if custom_handler
38+
custom_handler.post_install_message
39+
else
40+
config['post_install_message']
41+
end
42+
end
43+
44+
def find_file(path)
45+
files.select { |f| f[:path] == path }.first
46+
end
47+
48+
private
49+
50+
def custom_handler
51+
return nil unless config.is_a? Symbol
52+
@custom_handler ||= Bashly::Libraries.const_get(config).new(*args)
53+
end
54+
55+
def config
56+
@config ||= self.class.config[name]
57+
end
58+
59+
def target_file_args
60+
{ user_source_dir: Settings.source_dir }
61+
end
62+
end
63+
end

0 commit comments

Comments
 (0)