Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bashly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
requires 'bashly/exceptions'
requires 'bashly/script/base'
requires 'bashly/commands/base'
requires 'bashly/library/base'
requires 'bashly/libraries/base'
requires 'bashly'
39 changes: 14 additions & 25 deletions lib/bashly/commands/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,63 +32,52 @@ class Add < Base
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"

def strings_command
add_lib Library::Strings.new
add_lib 'strings'
end

def lib_command
add_lib Library::Sample.new
add_lib 'lib'
end

def config_command
add_lib Library::Config.new
add_lib 'config'
end

def colors_command
add_lib Library::Colors.new
add_lib 'colors'
end

def yaml_command
add_lib Library::YAML.new
add_lib 'yaml'
end

def validations_command
add_lib Library::Validations.new
add_lib 'validations'
end

def comp_command
format = args['FORMAT']
output = args['OUTPUT']

case format
when "script"
path = output || "#{Settings.target_dir}/completions.bash"
add_lib Library::CompletionsScript.new(path)

when "function"
function = output || "send_completions"
path = "#{Settings.source_dir}/lib/#{function}.sh"
add_lib Library::CompletionsFunction.new(path, function: function)

when "yaml"
path = output || "#{Settings.target_dir}/completions.yml"
add_lib Library::CompletionsYAML.new(path)

else
raise Error, "Unrecognized format: #{format}"

when "script" then add_lib 'completions_script', output
when "function" then add_lib 'completions', output
when "yaml" then add_lib 'completions_yaml', output
else raise Error, "Unrecognized format: #{format}"
end

end

private

def add_lib(handler)
def add_lib(name, *args)
library = Bashly::Library.new name, *args
files_created = 0
handler.files.each do |file|
library.files.each do |file|
created = safe_write file[:path], file[:content]
files_created += 1 if created
end
message = handler.post_install_message
message = library.post_install_message
say "\n#{message}" if message and files_created > 0
end

Expand Down
30 changes: 14 additions & 16 deletions lib/bashly/commands/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,9 @@ def upgrade_libs
content = File.read file

if content =~ /\[@bashly-upgrade (.+)\]/
lib = $1

case lib
when "colors"
upgrade file, Library::Colors.new
when "config"
upgrade file, Library::Config.new
when "yaml"
upgrade file, Library::YAML.new
when "validations"
upgrade file, Library::Validations.new
when /completions (.+)/
upgrade file, Library::CompletionsFunction.new(file, function: $1)
end
args = $1.split ' '
library_name = args.shift
upgrade file, library_name, *args
end
end
end
Expand All @@ -57,8 +46,17 @@ def generated_files
Dir["#{Settings.source_dir}/**/*.*"].sort
end

def upgrade(existing_file, handler)
file = handler.files.select { |f| f[:path] == existing_file }.first
def upgrade(existing_file, library_name, *args)
if Library.exist? library_name
upgrade! existing_file, library_name, *args
else
quiet_say "!txtred!warning!txtrst! not upgrading !txtcyn!#{existing_file}!txtrst!, unknown library '#{library_name}'"
end
end

def upgrade!(existing_file, library_name, *args)
library = Bashly::Library.new library_name, *args
file = library.find_file existing_file

if file
File.deep_write file[:path], file[:content]
Expand Down
39 changes: 39 additions & 0 deletions lib/bashly/libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
colors:
files:
- source: "templates/lib/colors.sh"
target: "%{user_source_dir}/lib/colors.sh"

config:
files:
- source: "templates/lib/config.sh"
target: "%{user_source_dir}/lib/config.sh"

yaml:
files:
- source: "templates/lib/yaml.sh"
target: "%{user_source_dir}/lib/yaml.sh"

lib:
files:
- source: "templates/lib/sample_function.sh"
target: "%{user_source_dir}/lib/sample_function.sh"

strings:
files:
- source: "templates/strings.yml"
target: "%{user_source_dir}/bashly-strings.yml"

validations:
files:
- source: "templates/lib/validations/validate_dir_exists.sh"
target: "%{user_source_dir}/lib/validations/validate_dir_exists.sh"
- source: "templates/lib/validations/validate_file_exists.sh"
target: "%{user_source_dir}/lib/validations/validate_file_exists.sh"
- source: "templates/lib/validations/validate_integer.sh"
target: "%{user_source_dir}/lib/validations/validate_integer.sh"
- source: "templates/lib/validations/validate_not_empty.sh"
target: "%{user_source_dir}/lib/validations/validate_not_empty.sh"

completions: :CompletionsFunction
completions_script: :CompletionsScript
completions_yaml: :CompletionsYAML
19 changes: 19 additions & 0 deletions lib/bashly/libraries/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Bashly
module Libraries
class Base
attr_reader :args

def initialize(*args)
@args = args
end

def files
raise NotImplementedError, "Please implement #files"
end

def post_install_message
nil
end
end
end
end
14 changes: 14 additions & 0 deletions lib/bashly/libraries/completions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Bashly
module Libraries
class Completions < Base
protected
def command
@command ||= Script::Command.new config
end

def config
@config ||= Bashly::Config.new "#{Settings.source_dir}/bashly.yml"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Bashly
module Library
module Libraries
class CompletionsFunction < Completions
def file_content
def files
[
"# [@bashly-upgrade completions #{function_name}]",
command.completion_function(function_name)
].join "\n"
{
path: "#{Settings.source_dir}/lib/#{function_name}.sh",
content: completions_function_code(function_name)
}
]
end

def post_install_message
Expand All @@ -18,9 +20,19 @@ def post_install_message
EOF
end

private

def function_name
options[:function]
@function_name ||= args[0] || 'send_completions'
end

def completions_function_code(function_name)
[
"# [@bashly-upgrade completions #{function_name}]",
command.completion_function(function_name)
].join "\n"
end

end
end
end
29 changes: 29 additions & 0 deletions lib/bashly/libraries/completions_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Bashly
module Libraries
class CompletionsScript < Completions
def files
[
{
path: target_path,
content: command.completion_script
}
]
end

def post_install_message
<<~EOF
In order to enable completions, run:

!txtpur!$ source #{target_path}
EOF
end

private

def target_path
@target_path ||= args[0] || "#{Settings.target_dir}/completions.bash"
end

end
end
end
27 changes: 27 additions & 0 deletions lib/bashly/libraries/completions_yaml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Bashly
module Libraries
class CompletionsYAML < Completions
def files
[
{
path: target_path,
content: command.completion_data.to_yaml
}
]
end

def post_install_message
<<~EOF
This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem.
EOF
end

private

def target_path
@target_path ||= args[0] || "#{Settings.target_dir}/completions.yml"
end

end
end
end
63 changes: 63 additions & 0 deletions lib/bashly/library.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Bashly
class Library
class << self
def exist?(name)
config.has_key? name.to_s
end

def config
@config ||= YAML.load_file(config_path)
end

def config_path
@config_path ||= File.expand_path 'libraries.yml', __dir__
end
end

include AssetHelper
attr_reader :name, :args

def initialize(name, *args)
@name, @args = name.to_s, args
end

def files
if custom_handler
custom_handler.files

else
config['files'].map do |file|
{ path: file['target'] % target_file_args,
content: asset_content(file['source']) }
end
end
end

def post_install_message
if custom_handler
custom_handler.post_install_message
else
config['post_install_message']
end
end

def find_file(path)
files.select { |f| f[:path] == path }.first
end

private

def custom_handler
return nil unless config.is_a? Symbol
@custom_handler ||= Bashly::Libraries.const_get(config).new(*args)
end

def config
@config ||= self.class.config[name]
end

def target_file_args
{ user_source_dir: Settings.source_dir }
end
end
end
Loading