guard-cookstyle 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/guard-cookstyle.gemspec +1 -0
- data/lib/guard/cookstyle.rb +40 -81
- data/lib/guard/cookstyle/runner.rb +16 -129
- data/lib/guard/cookstyle/templates/Guardfile +1 -1
- data/lib/guard/cookstyle/version.rb +1 -1
- metadata +15 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: f959bd06a4864041ef9cbd6d243727ce97b5d79b
         | 
| 4 | 
            +
              data.tar.gz: 98aa10c88455e5f832ca8e00364b7f84b18906ed
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 4bde112d49d6224507022947e0ffc47fd2535cc36f02d6e9309ce816e49c6da536973d1ad36a67655b883d3729bc38d880701b431619d904d09c2a7741cd15c1
         | 
| 7 | 
            +
              data.tar.gz: 6d1cc236f2fdc71b011b844e310a7eb49c69e64975430ff4924113d14d687b3f6618b2f1ef7f93dd934c7a53a5855752ebdea1ae776cd5cd2ad7472308f65188
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/guard-cookstyle.gemspec
    CHANGED
    
    
    
        data/lib/guard/cookstyle.rb
    CHANGED
    
    | @@ -1,98 +1,57 @@ | |
| 1 1 | 
             
            require 'guard'
         | 
| 2 2 | 
             
            require 'guard/plugin'
         | 
| 3 | 
            +
            require 'guard/rubocop'
         | 
| 4 | 
            +
            require 'cookstyle'
         | 
| 3 5 | 
             
            require "guard/cookstyle/version"
         | 
| 4 6 | 
             
            require "guard/cookstyle/monkey_patch/listen"
         | 
| 5 7 |  | 
| 6 8 | 
             
            module Guard
         | 
| 7 | 
            -
              class Cookstyle <  | 
| 8 | 
            -
                 | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
                     | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
                   | 
| 25 | 
            -
                end
         | 
| 26 | 
            -
             | 
| 27 | 
            -
                def start
         | 
| 28 | 
            -
                  run_all if @options[:all_on_start]
         | 
| 29 | 
            -
                end
         | 
| 30 | 
            -
             | 
| 31 | 
            -
                def run_all
         | 
| 32 | 
            -
                  UI.info 'Inspecting Chef Cookbook style of all files'
         | 
| 33 | 
            -
                  inspect_with_cookstyle
         | 
| 34 | 
            -
                end
         | 
| 35 | 
            -
             | 
| 36 | 
            -
                def run_on_additions(paths)
         | 
| 37 | 
            -
                  run_partially(paths)
         | 
| 38 | 
            -
                end
         | 
| 39 | 
            -
             | 
| 40 | 
            -
                def run_on_modifications(paths)
         | 
| 41 | 
            -
                  run_partially(paths)
         | 
| 42 | 
            -
                end
         | 
| 43 | 
            -
             | 
| 44 | 
            -
                def reload
         | 
| 45 | 
            -
                  @failed_paths = []
         | 
| 46 | 
            -
                end
         | 
| 47 | 
            -
             | 
| 48 | 
            -
                private
         | 
| 49 | 
            -
             | 
| 50 | 
            -
                def run_partially(paths)
         | 
| 51 | 
            -
                  paths += @failed_paths if @options[:keep_failed]
         | 
| 52 | 
            -
                  paths = clean_paths(paths)
         | 
| 9 | 
            +
              class Cookstyle < ::Guard::RuboCop
         | 
| 10 | 
            +
                class_eval do
         | 
| 11 | 
            +
                  autoload :Runner, 'guard/cookstyle/runner'
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  def initialize(options = {})
         | 
| 14 | 
            +
                    super
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    @options = {
         | 
| 17 | 
            +
                      all_on_start: true,
         | 
| 18 | 
            +
                      keep_failed:  true,
         | 
| 19 | 
            +
                      notification: :failed,
         | 
| 20 | 
            +
                      cli: nil,
         | 
| 21 | 
            +
                      hide_stdout: false,
         | 
| 22 | 
            +
                      cookbook_dirs: %w[.]
         | 
| 23 | 
            +
                    }.merge(options)
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                    @failed_paths = []
         | 
| 26 | 
            +
                  end
         | 
| 53 27 |  | 
| 54 | 
            -
                   | 
| 28 | 
            +
                  def run_all
         | 
| 29 | 
            +
                    UI.info 'Inspecting Chef Cookbook style of all files'
         | 
| 30 | 
            +
                    inspect_with_cookstyle
         | 
| 31 | 
            +
                  end
         | 
| 55 32 |  | 
| 56 | 
            -
                   | 
| 57 | 
            -
                  UI.info "Inspecting Chef Cookbook style: #{displayed_paths.join(' ')}"
         | 
| 33 | 
            +
                  private
         | 
| 58 34 |  | 
| 59 | 
            -
                   | 
| 60 | 
            -
             | 
| 35 | 
            +
                  def run_partially(paths)
         | 
| 36 | 
            +
                    paths += @failed_paths if @options[:keep_failed]
         | 
| 37 | 
            +
                    paths = clean_paths(paths)
         | 
| 61 38 |  | 
| 62 | 
            -
             | 
| 63 | 
            -
                  runner = Runner.new(@options)
         | 
| 64 | 
            -
                  passed = runner.run(paths)
         | 
| 65 | 
            -
                  @failed_paths = runner.failed_paths
         | 
| 66 | 
            -
                  throw :task_has_failed unless passed
         | 
| 67 | 
            -
                rescue StandardError => error
         | 
| 68 | 
            -
                  UI.error 'The following exception occurred while running guard-cookstyle: ' \
         | 
| 69 | 
            -
                           "#{error.backtrace.first} #{error.message} (#{error.class.name})"
         | 
| 70 | 
            -
                end
         | 
| 39 | 
            +
                    return if paths.empty?
         | 
| 71 40 |  | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
                  paths.map! { |path| File.expand_path(path) }
         | 
| 75 | 
            -
                  paths.uniq!
         | 
| 76 | 
            -
                  paths.reject! do |path|
         | 
| 77 | 
            -
                    next true unless File.exist?(path)
         | 
| 78 | 
            -
                    included_in_other_path?(path, paths)
         | 
| 79 | 
            -
                  end
         | 
| 80 | 
            -
                  paths
         | 
| 81 | 
            -
                end
         | 
| 41 | 
            +
                    displayed_paths = paths.map { |path| smart_path(path) }
         | 
| 42 | 
            +
                    UI.info "Inspecting Chef Cookbook style: #{displayed_paths.join(' ')}"
         | 
| 82 43 |  | 
| 83 | 
            -
             | 
| 84 | 
            -
                  dir_paths = other_paths.select { |path| File.directory?(path) }
         | 
| 85 | 
            -
                  dir_paths.delete(target_path)
         | 
| 86 | 
            -
                  dir_paths.any? do |dir_path|
         | 
| 87 | 
            -
                    target_path.start_with?(dir_path)
         | 
| 44 | 
            +
                    inspect_with_cookstyle(paths)
         | 
| 88 45 | 
             
                  end
         | 
| 89 | 
            -
                end
         | 
| 90 46 |  | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
                     | 
| 94 | 
            -
             | 
| 95 | 
            -
                     | 
| 47 | 
            +
                  def inspect_with_cookstyle(paths = [])
         | 
| 48 | 
            +
                    runner = Runner.new(@options)
         | 
| 49 | 
            +
                    passed = runner.run(paths)
         | 
| 50 | 
            +
                    @failed_paths = runner.failed_paths
         | 
| 51 | 
            +
                    throw :task_has_failed unless passed
         | 
| 52 | 
            +
                  rescue StandardError => error
         | 
| 53 | 
            +
                    UI.error 'The following exception occurred while running guard-cookstyle: ' \
         | 
| 54 | 
            +
                            "#{error.backtrace.first} #{error.message} (#{error.class.name})"
         | 
| 96 55 | 
             
                  end
         | 
| 97 56 | 
             
                end
         | 
| 98 57 | 
             
              end
         | 
| @@ -1,146 +1,33 @@ | |
| 1 1 | 
             
            require 'json'
         | 
| 2 2 | 
             
            require 'cookstyle'
         | 
| 3 | 
            +
            require 'guard/rubocop/runner'
         | 
| 3 4 |  | 
| 4 5 | 
             
            module Guard
         | 
| 5 6 | 
             
              class Cookstyle
         | 
| 6 7 | 
             
                # This class runs `cookstyle` command, retrieves result and notifies.
         | 
| 7 8 | 
             
                # An instance of this class is intended to invoke `cookstyle` only once in its lifetime.
         | 
| 8 | 
            -
                class Runner
         | 
| 9 | 
            -
                   | 
| 10 | 
            -
                     | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
                  def run(paths = [])
         | 
| 14 | 
            -
                    command = build_command(paths)
         | 
| 15 | 
            -
                    passed = system(*command)
         | 
| 16 | 
            -
             | 
| 17 | 
            -
                    case @options[:notification]
         | 
| 18 | 
            -
                    when :failed
         | 
| 19 | 
            -
                      notify(passed) unless passed
         | 
| 20 | 
            -
                    when true
         | 
| 21 | 
            -
                      notify(passed)
         | 
| 22 | 
            -
                    end
         | 
| 23 | 
            -
             | 
| 24 | 
            -
                    open_launchy_if_needed
         | 
| 25 | 
            -
             | 
| 26 | 
            -
                    passed
         | 
| 27 | 
            -
                  end
         | 
| 28 | 
            -
             | 
| 29 | 
            -
                  def build_command(paths)
         | 
| 30 | 
            -
                    command = ['cookstyle']
         | 
| 31 | 
            -
             | 
| 32 | 
            -
                    if should_add_default_formatter_for_console?
         | 
| 33 | 
            -
                      command.concat(%w[--format progress]) # Keep default formatter for console.
         | 
| 34 | 
            -
                    end
         | 
| 35 | 
            -
             | 
| 36 | 
            -
                    command.concat(['--config', ::Cookstyle.config])
         | 
| 37 | 
            -
                    command.concat(['--format', 'json', '--out', json_file_path])
         | 
| 38 | 
            -
                    command << '--force-exclusion'
         | 
| 39 | 
            -
                    command.concat(args_specified_by_user)
         | 
| 9 | 
            +
                class Runner < ::Guard::RuboCop::Runner
         | 
| 10 | 
            +
                  class_eval do
         | 
| 11 | 
            +
                    def build_command(paths)
         | 
| 12 | 
            +
                      command = ['rubocop']
         | 
| 40 13 |  | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
                    else
         | 
| 44 | 
            -
                      command.concat(@options[:cookbook_dirs])
         | 
| 45 | 
            -
                    end
         | 
| 46 | 
            -
                    command
         | 
| 47 | 
            -
                  end
         | 
| 48 | 
            -
             | 
| 49 | 
            -
                  def should_add_default_formatter_for_console?
         | 
| 50 | 
            -
                    !@options[:hide_stdout] && !include_formatter_for_console?(args_specified_by_user)
         | 
| 51 | 
            -
                  end
         | 
| 52 | 
            -
             | 
| 53 | 
            -
                  def args_specified_by_user
         | 
| 54 | 
            -
                    @args_specified_by_user ||= begin
         | 
| 55 | 
            -
                      args = @options[:cli]
         | 
| 56 | 
            -
                      case args
         | 
| 57 | 
            -
                      when Array    then args
         | 
| 58 | 
            -
                      when String   then args.shellsplit
         | 
| 59 | 
            -
                      when NilClass then []
         | 
| 60 | 
            -
                      else raise ArgumentError, ':cli option must be either an array or string'
         | 
| 14 | 
            +
                      if should_add_default_formatter_for_console?
         | 
| 15 | 
            +
                        command.concat(%w[--format progress]) # Keep default formatter for console.
         | 
| 61 16 | 
             
                      end
         | 
| 62 | 
            -
                    end
         | 
| 63 | 
            -
                  end
         | 
| 64 | 
            -
             | 
| 65 | 
            -
                  def include_formatter_for_console?(cli_args)
         | 
| 66 | 
            -
                    index = -1
         | 
| 67 | 
            -
                    formatter_args = cli_args.group_by do |arg|
         | 
| 68 | 
            -
                      index += 1 if arg == '--format' || arg.start_with?('-f')
         | 
| 69 | 
            -
                      index
         | 
| 70 | 
            -
                    end
         | 
| 71 | 
            -
                    formatter_args.delete(-1)
         | 
| 72 17 |  | 
| 73 | 
            -
             | 
| 74 | 
            -
                       | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 18 | 
            +
                      command.concat(['-r', 'cookstyle'])
         | 
| 19 | 
            +
                      command.concat(['--format', 'json', '--out', json_file_path])
         | 
| 20 | 
            +
                      command << '--force-exclusion'
         | 
| 21 | 
            +
                      command.concat(args_specified_by_user)
         | 
| 77 22 |  | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
                       | 
| 81 | 
            -
             | 
| 82 | 
            -
                      tempfile = Tempfile.new(basename)
         | 
| 83 | 
            -
                      tempfile.close
         | 
| 84 | 
            -
                      tempfile.path
         | 
| 85 | 
            -
                    end
         | 
| 86 | 
            -
                  end
         | 
| 87 | 
            -
             | 
| 88 | 
            -
                  def result
         | 
| 89 | 
            -
                    @result ||= begin
         | 
| 90 | 
            -
                      File.open(json_file_path) do |file|
         | 
| 91 | 
            -
                        # Rubinius 2.0.0.rc1 does not support `JSON.load` with 3 args.
         | 
| 92 | 
            -
                        JSON.parse(file.read, symbolize_names: true)
         | 
| 23 | 
            +
                      if paths.any?
         | 
| 24 | 
            +
                        command.concat(paths)
         | 
| 25 | 
            +
                      else
         | 
| 26 | 
            +
                        command.concat(@options[:cookbook_dirs])
         | 
| 93 27 | 
             
                      end
         | 
| 28 | 
            +
                      command
         | 
| 94 29 | 
             
                    end
         | 
| 95 30 | 
             
                  end
         | 
| 96 | 
            -
             | 
| 97 | 
            -
                  def notify(passed)
         | 
| 98 | 
            -
                    image = passed ? :success : :failed
         | 
| 99 | 
            -
                    Notifier.notify(summary_text, title: 'Cookstyle results', image: image)
         | 
| 100 | 
            -
                  end
         | 
| 101 | 
            -
             | 
| 102 | 
            -
                  def summary_text
         | 
| 103 | 
            -
                    summary = result[:summary]
         | 
| 104 | 
            -
             | 
| 105 | 
            -
                    text = pluralize(summary[:inspected_file_count], 'file')
         | 
| 106 | 
            -
                    text << ' inspected, '
         | 
| 107 | 
            -
             | 
| 108 | 
            -
                    offense_count = summary[:offense_count] || summary[:offence_count]
         | 
| 109 | 
            -
                    text << pluralize(offense_count, 'offense', no_for_zero: true)
         | 
| 110 | 
            -
                    text << ' detected'
         | 
| 111 | 
            -
                  end
         | 
| 112 | 
            -
             | 
| 113 | 
            -
                  def failed_paths
         | 
| 114 | 
            -
                    failed_files = result[:files].reject do |file|
         | 
| 115 | 
            -
                      offenses = file[:offenses] || file[:offences]
         | 
| 116 | 
            -
                      offenses.empty?
         | 
| 117 | 
            -
                    end
         | 
| 118 | 
            -
                    failed_files.map do |file|
         | 
| 119 | 
            -
                      file[:path]
         | 
| 120 | 
            -
                    end
         | 
| 121 | 
            -
                  end
         | 
| 122 | 
            -
             | 
| 123 | 
            -
                  def pluralize(number, thing, options = {})
         | 
| 124 | 
            -
                    text = ''
         | 
| 125 | 
            -
             | 
| 126 | 
            -
                    if number.zero? && options[:no_for_zero]
         | 
| 127 | 
            -
                      text = 'no'
         | 
| 128 | 
            -
                    else
         | 
| 129 | 
            -
                      text << number.to_s
         | 
| 130 | 
            -
                    end
         | 
| 131 | 
            -
             | 
| 132 | 
            -
                    text << " #{thing}"
         | 
| 133 | 
            -
                    text << 's' unless number == 1
         | 
| 134 | 
            -
             | 
| 135 | 
            -
                    text
         | 
| 136 | 
            -
                  end
         | 
| 137 | 
            -
             | 
| 138 | 
            -
                  def open_launchy_if_needed
         | 
| 139 | 
            -
                    return unless (output_path = @options[:launchy])
         | 
| 140 | 
            -
                    return unless File.exist?(output_path)
         | 
| 141 | 
            -
                    require 'launchy'
         | 
| 142 | 
            -
                    ::Launchy.open(output_path)
         | 
| 143 | 
            -
                  end
         | 
| 144 31 | 
             
                end
         | 
| 145 32 | 
             
              end
         | 
| 146 33 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: guard-cookstyle
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.3.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - sawanoboly
         | 
| @@ -24,6 +24,20 @@ dependencies: | |
| 24 24 | 
             
                - - ">="
         | 
| 25 25 | 
             
                  - !ruby/object:Gem::Version
         | 
| 26 26 | 
             
                    version: '0'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: guard-rubocop
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :runtime
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 27 41 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 28 42 | 
             
              name: cookstyle
         | 
| 29 43 | 
             
              requirement: !ruby/object:Gem::Requirement
         |