From b839376a507e37a048ea2df53127ed0884310f60 Mon Sep 17 00:00:00 2001 From: elijah Date: Mon, 24 Nov 2014 22:45:27 -0800 Subject: moved core_ext and lib_ext under leap_cli --- bin/leap | 2 +- lib/core_ext/boolean.rb | 14 --- lib/core_ext/hash.rb | 35 ------- lib/core_ext/json.rb | 42 --------- lib/core_ext/nil.rb | 5 - lib/core_ext/string.rb | 14 --- lib/core_ext/yaml.rb | 29 ------ lib/leap_cli.rb | 16 ++-- lib/leap_cli/commands/deploy.rb | 4 +- lib/leap_cli/core_ext/boolean.rb | 14 +++ lib/leap_cli/core_ext/hash.rb | 35 +++++++ lib/leap_cli/core_ext/json.rb | 42 +++++++++ lib/leap_cli/core_ext/nil.rb | 5 + lib/leap_cli/core_ext/string.rb | 14 +++ lib/leap_cli/core_ext/yaml.rb | 29 ++++++ lib/leap_cli/lib_ext/capistrano_connections.rb | 16 ++++ lib/leap_cli/lib_ext/gli.rb | 52 +++++++++++ lib/leap_cli/override/json.rb | 11 +++ lib/leap_cli/util/remote_command.rb | 2 +- lib/lib_ext/capistrano_connections.rb | 16 ---- lib/lib_ext/gli.rb | 52 ----------- lib/lib_ext/markdown_document_listener.rb | 122 ------------------------- lib/override/json.rb | 11 --- 23 files changed, 231 insertions(+), 351 deletions(-) delete mode 100644 lib/core_ext/boolean.rb delete mode 100644 lib/core_ext/hash.rb delete mode 100644 lib/core_ext/json.rb delete mode 100644 lib/core_ext/nil.rb delete mode 100644 lib/core_ext/string.rb delete mode 100644 lib/core_ext/yaml.rb create mode 100644 lib/leap_cli/core_ext/boolean.rb create mode 100644 lib/leap_cli/core_ext/hash.rb create mode 100644 lib/leap_cli/core_ext/json.rb create mode 100644 lib/leap_cli/core_ext/nil.rb create mode 100644 lib/leap_cli/core_ext/string.rb create mode 100644 lib/leap_cli/core_ext/yaml.rb create mode 100644 lib/leap_cli/lib_ext/capistrano_connections.rb create mode 100644 lib/leap_cli/lib_ext/gli.rb create mode 100644 lib/leap_cli/override/json.rb delete mode 100644 lib/lib_ext/capistrano_connections.rb delete mode 100644 lib/lib_ext/gli.rb delete mode 100644 lib/lib_ext/markdown_document_listener.rb delete mode 100644 lib/override/json.rb diff --git a/bin/leap b/bin/leap index c39da1c..512201c 100755 --- a/bin/leap +++ b/bin/leap @@ -29,7 +29,7 @@ end require 'gli' require 'highline' require 'forwardable' -require 'lib_ext/gli' # our custom extensions to gli +require 'leap_cli/lib_ext/gli' # our custom extensions to gli # # Typically, GLI and Highline methods are loaded into the global namespace. diff --git a/lib/core_ext/boolean.rb b/lib/core_ext/boolean.rb deleted file mode 100644 index 9b617b2..0000000 --- a/lib/core_ext/boolean.rb +++ /dev/null @@ -1,14 +0,0 @@ -# -# make is_a?(Boolean) possible. -# - -module Boolean -end - -class TrueClass - include Boolean -end - -class FalseClass - include Boolean -end \ No newline at end of file diff --git a/lib/core_ext/hash.rb b/lib/core_ext/hash.rb deleted file mode 100644 index 7df33b2..0000000 --- a/lib/core_ext/hash.rb +++ /dev/null @@ -1,35 +0,0 @@ -class Hash - - ## - ## CONVERTING - ## - - # - # convert self into a hash, but only include the specified keys - # - def pick(*keys) - keys.map(&:to_s).inject({}) do |hsh, key| - if has_key?(key) - hsh[key] = self[key] - end - hsh - end - end - - # - # recursive merging (aka deep merge) - # taken from ActiveSupport::CoreExtensions::Hash::DeepMerge - # - def deep_merge(other_hash) - self.merge(other_hash) do |key, oldval, newval| - oldval = oldval.to_hash if oldval.respond_to?(:to_hash) - newval = newval.to_hash if newval.respond_to?(:to_hash) - oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval - end - end - - def deep_merge!(other_hash) - replace(deep_merge(other_hash)) - end - -end diff --git a/lib/core_ext/json.rb b/lib/core_ext/json.rb deleted file mode 100644 index 1a82bd9..0000000 --- a/lib/core_ext/json.rb +++ /dev/null @@ -1,42 +0,0 @@ -module JSON - # - # Output JSON from ruby objects in such a manner that all the hashes and arrays are output in alphanumeric sorted order. - # This is required so that our generated configs don't throw puppet or git for a tizzy fit. - # - # Beware: some hacky stuff ahead. - # - # This relies on the pure ruby implementation of JSON.generate (i.e. require 'json/pure') - # see https://github.com/flori/json/blob/master/lib/json/pure/generator.rb - # - # The Oj way that we are not using: Oj.dump(obj, :mode => :compat, :indent => 2) - # - def self.sorted_generate(obj) - # modify hash and array - Array.class_eval do - alias_method :each_without_sort, :each - def each(&block) - sorted = sort {|a,b| a.to_s <=> b.to_s } - for i in 0..(sorted.length-1) do - yield sorted[i] - end - end - end - Hash.class_eval do - alias_method :each_without_sort, :each - def each(&block) - self.keys.each do |key| - yield key, self.fetch(key) # fetch is used so we don't trigger Config::Object auto-eval - end - end - end - - # generate json - json_str = JSON.pretty_generate(obj) - - # restore hash and array - Hash.class_eval {alias_method :each, :each_without_sort} - Array.class_eval {alias_method :each, :each_without_sort} - - return json_str - end -end diff --git a/lib/core_ext/nil.rb b/lib/core_ext/nil.rb deleted file mode 100644 index 05ca98f..0000000 --- a/lib/core_ext/nil.rb +++ /dev/null @@ -1,5 +0,0 @@ -class NilClass - def any? - false - end -end \ No newline at end of file diff --git a/lib/core_ext/string.rb b/lib/core_ext/string.rb deleted file mode 100644 index 07af8e5..0000000 --- a/lib/core_ext/string.rb +++ /dev/null @@ -1,14 +0,0 @@ -# -# make ruby 1.9 act more like ruby 1.8 -# -unless String.method_defined?(:to_a) - class String - def to_a; [self]; end - end -end - -unless String.method_defined?(:any?) - class String - def any?; self.chars.any?; end - end -end diff --git a/lib/core_ext/yaml.rb b/lib/core_ext/yaml.rb deleted file mode 100644 index bb0b5c9..0000000 --- a/lib/core_ext/yaml.rb +++ /dev/null @@ -1,29 +0,0 @@ -class Object - # - # ya2yaml will output hash keys in sorted order, but it outputs arrays - # in natural order. This new method, sorted_ya2yaml(), is the same as - # ya2yaml but ensures that arrays are sorted. - # - # This is important so that the .yaml files don't change each time you recompile. - # - # see https://github.com/afunai/ya2yaml/blob/master/lib/ya2yaml.rb - # - def sorted_ya2yaml(options = {}) - # modify array - Array.class_eval do - alias_method :collect_without_sort, :collect - def collect(&block) - sorted = sort {|a,b| a.to_s <=> b.to_s} - sorted.collect_without_sort(&block) - end - end - - # generate yaml - yaml_str = self.ya2yaml(options) - - # restore array - Array.class_eval {alias_method :collect, :collect_without_sort} - - return yaml_str - end -end diff --git a/lib/leap_cli.rb b/lib/leap_cli.rb index 557350b..f07fd25 100644 --- a/lib/leap_cli.rb +++ b/lib/leap_cli.rb @@ -5,8 +5,8 @@ end $ruby_version = RUBY_VERSION.split('.').collect{ |i| i.to_i }.extend(Comparable) -# ensure leap_cli/lib/overrides has the highest priority -$:.unshift(File.expand_path('../override',__FILE__)) +# ensure lib/leap_cli/overrides has the highest priority +$:.unshift(File.expand_path('../leap_cli/override',__FILE__)) # for a few gems, things will break if using earlier versions. # enforce the compatible versions here: @@ -20,12 +20,12 @@ require 'leap_cli/version' require 'leap_cli/exceptions' require 'leap_cli/leapfile' -require 'core_ext/hash' -require 'core_ext/boolean' -require 'core_ext/nil' -require 'core_ext/string' -require 'core_ext/json' -require 'core_ext/yaml' +require 'leap_cli/core_ext/hash' +require 'leap_cli/core_ext/boolean' +require 'leap_cli/core_ext/nil' +require 'leap_cli/core_ext/string' +require 'leap_cli/core_ext/json' +require 'leap_cli/core_ext/yaml' require 'leap_cli/log' require 'leap_cli/path' diff --git a/lib/leap_cli/commands/deploy.rb b/lib/leap_cli/commands/deploy.rb index 6589837..90807db 100644 --- a/lib/leap_cli/commands/deploy.rb +++ b/lib/leap_cli/commands/deploy.rb @@ -73,7 +73,9 @@ module LeapCli end end end - + if !Util.exit_status.nil? && Util.exit_status != 0 + log :warning, "puppet did not finish successfully." + end end end diff --git a/lib/leap_cli/core_ext/boolean.rb b/lib/leap_cli/core_ext/boolean.rb new file mode 100644 index 0000000..9b617b2 --- /dev/null +++ b/lib/leap_cli/core_ext/boolean.rb @@ -0,0 +1,14 @@ +# +# make is_a?(Boolean) possible. +# + +module Boolean +end + +class TrueClass + include Boolean +end + +class FalseClass + include Boolean +end \ No newline at end of file diff --git a/lib/leap_cli/core_ext/hash.rb b/lib/leap_cli/core_ext/hash.rb new file mode 100644 index 0000000..7df33b2 --- /dev/null +++ b/lib/leap_cli/core_ext/hash.rb @@ -0,0 +1,35 @@ +class Hash + + ## + ## CONVERTING + ## + + # + # convert self into a hash, but only include the specified keys + # + def pick(*keys) + keys.map(&:to_s).inject({}) do |hsh, key| + if has_key?(key) + hsh[key] = self[key] + end + hsh + end + end + + # + # recursive merging (aka deep merge) + # taken from ActiveSupport::CoreExtensions::Hash::DeepMerge + # + def deep_merge(other_hash) + self.merge(other_hash) do |key, oldval, newval| + oldval = oldval.to_hash if oldval.respond_to?(:to_hash) + newval = newval.to_hash if newval.respond_to?(:to_hash) + oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval + end + end + + def deep_merge!(other_hash) + replace(deep_merge(other_hash)) + end + +end diff --git a/lib/leap_cli/core_ext/json.rb b/lib/leap_cli/core_ext/json.rb new file mode 100644 index 0000000..1a82bd9 --- /dev/null +++ b/lib/leap_cli/core_ext/json.rb @@ -0,0 +1,42 @@ +module JSON + # + # Output JSON from ruby objects in such a manner that all the hashes and arrays are output in alphanumeric sorted order. + # This is required so that our generated configs don't throw puppet or git for a tizzy fit. + # + # Beware: some hacky stuff ahead. + # + # This relies on the pure ruby implementation of JSON.generate (i.e. require 'json/pure') + # see https://github.com/flori/json/blob/master/lib/json/pure/generator.rb + # + # The Oj way that we are not using: Oj.dump(obj, :mode => :compat, :indent => 2) + # + def self.sorted_generate(obj) + # modify hash and array + Array.class_eval do + alias_method :each_without_sort, :each + def each(&block) + sorted = sort {|a,b| a.to_s <=> b.to_s } + for i in 0..(sorted.length-1) do + yield sorted[i] + end + end + end + Hash.class_eval do + alias_method :each_without_sort, :each + def each(&block) + self.keys.each do |key| + yield key, self.fetch(key) # fetch is used so we don't trigger Config::Object auto-eval + end + end + end + + # generate json + json_str = JSON.pretty_generate(obj) + + # restore hash and array + Hash.class_eval {alias_method :each, :each_without_sort} + Array.class_eval {alias_method :each, :each_without_sort} + + return json_str + end +end diff --git a/lib/leap_cli/core_ext/nil.rb b/lib/leap_cli/core_ext/nil.rb new file mode 100644 index 0000000..05ca98f --- /dev/null +++ b/lib/leap_cli/core_ext/nil.rb @@ -0,0 +1,5 @@ +class NilClass + def any? + false + end +end \ No newline at end of file diff --git a/lib/leap_cli/core_ext/string.rb b/lib/leap_cli/core_ext/string.rb new file mode 100644 index 0000000..07af8e5 --- /dev/null +++ b/lib/leap_cli/core_ext/string.rb @@ -0,0 +1,14 @@ +# +# make ruby 1.9 act more like ruby 1.8 +# +unless String.method_defined?(:to_a) + class String + def to_a; [self]; end + end +end + +unless String.method_defined?(:any?) + class String + def any?; self.chars.any?; end + end +end diff --git a/lib/leap_cli/core_ext/yaml.rb b/lib/leap_cli/core_ext/yaml.rb new file mode 100644 index 0000000..bb0b5c9 --- /dev/null +++ b/lib/leap_cli/core_ext/yaml.rb @@ -0,0 +1,29 @@ +class Object + # + # ya2yaml will output hash keys in sorted order, but it outputs arrays + # in natural order. This new method, sorted_ya2yaml(), is the same as + # ya2yaml but ensures that arrays are sorted. + # + # This is important so that the .yaml files don't change each time you recompile. + # + # see https://github.com/afunai/ya2yaml/blob/master/lib/ya2yaml.rb + # + def sorted_ya2yaml(options = {}) + # modify array + Array.class_eval do + alias_method :collect_without_sort, :collect + def collect(&block) + sorted = sort {|a,b| a.to_s <=> b.to_s} + sorted.collect_without_sort(&block) + end + end + + # generate yaml + yaml_str = self.ya2yaml(options) + + # restore array + Array.class_eval {alias_method :collect, :collect_without_sort} + + return yaml_str + end +end diff --git a/lib/leap_cli/lib_ext/capistrano_connections.rb b/lib/leap_cli/lib_ext/capistrano_connections.rb new file mode 100644 index 0000000..c46455f --- /dev/null +++ b/lib/leap_cli/lib_ext/capistrano_connections.rb @@ -0,0 +1,16 @@ +module Capistrano + class Configuration + module Connections + def failed!(server) + @failure_callback.call(server) if @failure_callback + Thread.current[:failed_sessions] << server + end + + def call_on_failure(&block) + @failure_callback = block + end + end + end +end + + diff --git a/lib/leap_cli/lib_ext/gli.rb b/lib/leap_cli/lib_ext/gli.rb new file mode 100644 index 0000000..f9b03be --- /dev/null +++ b/lib/leap_cli/lib_ext/gli.rb @@ -0,0 +1,52 @@ +# +# print subcommands indented in the main global help screen +# + +module GLI + module Commands + module HelpModules + class GlobalHelpFormat + SUB_CMD_INDENT = " " + def format + program_desc = @app.program_desc + program_long_desc = @app.program_long_desc + if program_long_desc + wrapper = @wrapper_class.new(Terminal.instance.size[0],4) + program_long_desc = "\n #{wrapper.wrap(program_long_desc)}\n\n" if program_long_desc + else + program_long_desc = "\n" + end + + # build a list of commands, sort them so the commands with subcommands are at the bottom + commands = @sorter.call(@app.commands_declaration_order.reject(&:nodoc)).sort do |a,b| + if a.commands.any? && b.commands.any?; a.name.to_s <=> b.name.to_s + elsif a.commands.any?; 1 + elsif b.commands.any?; -1 + else; a.name.to_s <=> b.name.to_s + end + end + + # build a list of command info ([name, description]), including subcommands if appropriate + command_info_list = [] + commands.each do |command| + name = [command.name, Array(command.aliases)].flatten.join(', ') + command_info_list << [name, command.description] + if command.commands.any? + @sorter.call(command.commands_declaration_order).each do |cmd| + command_info_list << [SUB_CMD_INDENT + command.name.to_s + " " + cmd.names, cmd.description + (command.get_default_command == cmd.name ? " (default)" : "")] + end + end + end + + # display + command_formatter = ListFormatter.new(command_info_list, @wrapper_class) + stringio = StringIO.new + command_formatter.output(stringio) + commands = stringio.string + global_option_descriptions = OptionsFormatter.new(global_flags_and_switches, @sorter, @wrapper_class).format + GLOBAL_HELP.result(binding) + end + end + end + end +end diff --git a/lib/leap_cli/override/json.rb b/lib/leap_cli/override/json.rb new file mode 100644 index 0000000..a7ae328 --- /dev/null +++ b/lib/leap_cli/override/json.rb @@ -0,0 +1,11 @@ +# +# This exists solely to prevent other gems we depend on from +# importing json/ext (e.g. require 'json'). +# +# If json/ext is imported, json/pure cannot work, and we heavily +# rely on the specific behavior of json/pure. +# +# This trick only works if this directory is early in the +# include path. +# +require 'json/pure' \ No newline at end of file diff --git a/lib/leap_cli/util/remote_command.rb b/lib/leap_cli/util/remote_command.rb index 16d2b22..10a5ca8 100644 --- a/lib/leap_cli/util/remote_command.rb +++ b/lib/leap_cli/util/remote_command.rb @@ -120,7 +120,7 @@ module LeapCli; module Util; module RemoteCommand @capistrano_enabled ||= begin require 'capistrano' require 'capistrano/cli' - require 'lib_ext/capistrano_connections' + require 'leap_cli/lib_ext/capistrano_connections' require 'leap_cli/remote/leap_plugin' require 'leap_cli/remote/puppet_plugin' require 'leap_cli/remote/rsync_plugin' diff --git a/lib/lib_ext/capistrano_connections.rb b/lib/lib_ext/capistrano_connections.rb deleted file mode 100644 index c46455f..0000000 --- a/lib/lib_ext/capistrano_connections.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Capistrano - class Configuration - module Connections - def failed!(server) - @failure_callback.call(server) if @failure_callback - Thread.current[:failed_sessions] << server - end - - def call_on_failure(&block) - @failure_callback = block - end - end - end -end - - diff --git a/lib/lib_ext/gli.rb b/lib/lib_ext/gli.rb deleted file mode 100644 index f9b03be..0000000 --- a/lib/lib_ext/gli.rb +++ /dev/null @@ -1,52 +0,0 @@ -# -# print subcommands indented in the main global help screen -# - -module GLI - module Commands - module HelpModules - class GlobalHelpFormat - SUB_CMD_INDENT = " " - def format - program_desc = @app.program_desc - program_long_desc = @app.program_long_desc - if program_long_desc - wrapper = @wrapper_class.new(Terminal.instance.size[0],4) - program_long_desc = "\n #{wrapper.wrap(program_long_desc)}\n\n" if program_long_desc - else - program_long_desc = "\n" - end - - # build a list of commands, sort them so the commands with subcommands are at the bottom - commands = @sorter.call(@app.commands_declaration_order.reject(&:nodoc)).sort do |a,b| - if a.commands.any? && b.commands.any?; a.name.to_s <=> b.name.to_s - elsif a.commands.any?; 1 - elsif b.commands.any?; -1 - else; a.name.to_s <=> b.name.to_s - end - end - - # build a list of command info ([name, description]), including subcommands if appropriate - command_info_list = [] - commands.each do |command| - name = [command.name, Array(command.aliases)].flatten.join(', ') - command_info_list << [name, command.description] - if command.commands.any? - @sorter.call(command.commands_declaration_order).each do |cmd| - command_info_list << [SUB_CMD_INDENT + command.name.to_s + " " + cmd.names, cmd.description + (command.get_default_command == cmd.name ? " (default)" : "")] - end - end - end - - # display - command_formatter = ListFormatter.new(command_info_list, @wrapper_class) - stringio = StringIO.new - command_formatter.output(stringio) - commands = stringio.string - global_option_descriptions = OptionsFormatter.new(global_flags_and_switches, @sorter, @wrapper_class).format - GLOBAL_HELP.result(binding) - end - end - end - end -end diff --git a/lib/lib_ext/markdown_document_listener.rb b/lib/lib_ext/markdown_document_listener.rb deleted file mode 100644 index 55026e9..0000000 --- a/lib/lib_ext/markdown_document_listener.rb +++ /dev/null @@ -1,122 +0,0 @@ -require 'stringio' -require 'gli/commands/help_modules/arg_name_formatter' - -# -# adaption of RdocDocumentListener to use Markdown -# see http://rtomayko.github.com/ronn/ronn-format.7 -# - -module GLI - module Commands - class MarkdownDocumentListener - - def initialize(global_options,options,arguments) - @io = STDOUT #File.new(File.basename($0) + ".rdoc",'w') - @nest = '' - @arg_name_formatter = GLI::Commands::HelpModules::ArgNameFormatter.new - end - - def beginning - end - - # Called when processing has completed - def ending - #@io.close - end - - # Gives you the program description - def program_desc(desc) - @io.puts "== #{File.basename($0)} - #{desc}" - @io.puts - end - - def program_long_desc(desc) - @io.puts desc - @io.puts - end - - # Gives you the program version - def version(version) - @io.puts "v#{version}" - @io.puts - end - - def options - if @nest.size == 0 - @io.puts "=== Global Options" - else - @io.puts "#{@nest}=== Options" - end - end - - # Gives you a flag in the current context - def flag(name,aliases,desc,long_desc,default_value,arg_name,must_match,type) - invocations = ([name] + Array(aliases)).map { |_| add_dashes(_) }.join('|') - usage = "#{invocations} #{arg_name || 'arg'}" - @io.puts "#{@nest}=== #{usage}" - @io.puts - @io.puts String(desc).strip - @io.puts - @io.puts "[Default Value] #{default_value || 'None'}" - @io.puts "[Must Match] #{must_match.to_s}" unless must_match.nil? - @io.puts String(long_desc).strip - @io.puts - end - - # Gives you a switch in the current context - def switch(name,aliases,desc,long_desc,negetable) - if negetable - name = "[no-]#{name}" if name.to_s.length > 1 - aliases = aliases.map { |_| _.to_s.length > 1 ? "[no-]#{_}" : _ } - end - invocations = ([name] + aliases).map { |_| add_dashes(_) }.join('|') - @io.puts "#{@nest}=== #{invocations}" - @io.puts String(desc).strip - @io.puts - @io.puts String(long_desc).strip - @io.puts - end - - def end_options - end - - def commands - @io.puts "#{@nest}=== Commands" - @nest = "#{@nest}=" - end - - # Gives you a command in the current context and creates a new context of this command - def command(name,aliases,desc,long_desc,arg_name,arg_options) - @io.puts "#{@nest}=== Command: #{([name] + aliases).join('|')} #{@arg_name_formatter.format(arg_name,arg_options)}" - @io.puts String(desc).strip - @io.puts - @io.puts String(long_desc).strip - @nest = "#{@nest}=" - end - - # Ends a command, and "pops" you back up one context - def end_command(name) - @nest.gsub!(/=$/,'') - end - - # Gives you the name of the current command in the current context - def default_command(name) - @io.puts "[Default Command] #{name}" unless name.nil? - end - - def end_commands - @nest.gsub!(/=$/,'') - end - - private - - def add_dashes(name) - name = "-#{name}" - name = "-#{name}" if name.length > 2 - name - end - - - end - end -end diff --git a/lib/override/json.rb b/lib/override/json.rb deleted file mode 100644 index a7ae328..0000000 --- a/lib/override/json.rb +++ /dev/null @@ -1,11 +0,0 @@ -# -# This exists solely to prevent other gems we depend on from -# importing json/ext (e.g. require 'json'). -# -# If json/ext is imported, json/pure cannot work, and we heavily -# rely on the specific behavior of json/pure. -# -# This trick only works if this directory is early in the -# include path. -# -require 'json/pure' \ No newline at end of file -- cgit v1.2.3