Sha256: 905b96476d85ee6263a198e531e860e331e99345713b8752cb277f12eb97116d

Contents?: true

Size: 1.21 KB

Versions: 29

Compression:

Stored size: 1.21 KB

Contents

require 'json'
require 'open3'
require 'shellwords'

module NdrDevSupport
  module Rubocop
    # This class filters the Rubocop report of a file
    # to only the given lines.
    class Executor
      class << self
        # Use RuboCop to produce a list of all files that should be scanned.
        def target_files
          @target_files ||= `rubocop -L 2>/dev/null`.each_line.map(&:strip)
        end
      end

      def initialize(filenames)
        @filenames = Executor.target_files & filenames

        check_ruby_syntax
      end

      def offenses_by_file
        return [] if @filenames.empty?

        output = JSON.parse(`rubocop --format json #{escaped_paths.join(' ')} 2>/dev/null`)

        output['files'].each_with_object({}) do |file_output, result|
          result[file_output['path']] = file_output['offenses']
        end
      end

      private

      def escaped_paths
        @escaped_paths ||= @filenames.map { |path| Shellwords.escape(path) }
      end

      def check_ruby_syntax
        escaped_paths.each do |path|
          stdout_and_err_str, status = Open3.capture2e("ruby -c #{path}")
          next if status.exitstatus.zero?

          raise stdout_and_err_str
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
ndr_dev_support-6.1.0 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-6.0.4 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-6.0.3 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-6.0.2 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-6.0.1 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-6.0.0 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-5.10.2 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-5.10.1 lib/ndr_dev_support/rubocop/executor.rb
ndr_dev_support-5.10.0 lib/ndr_dev_support/rubocop/executor.rb