class Pronto::StyleCop

Constants

STYLECOP_COMMAND

Public Class Methods

new(patches, commit = nil) click to toggle source
Calls superclass method
# File lib/pronto/style_cop.rb, line 12
def initialize(patches, commit = nil)
  super
  @config.extend(StyleCopConfig)
end

Public Instance Methods

inspect(patch) click to toggle source
# File lib/pronto/style_cop.rb, line 30
def inspect(patch)
  Parallel.map(definitions, in_processes: parallel) { |definition| run_stylecop(patch, definition) }
    .each_with_object(Set.new) { |violations, result| result.merge(violations) }
    .map do |violation|
    patch.added_lines
      .select { |line| line.new_lineno == violation[:line_number] }
      .map { |line| new_message(violation, line) }
  end
end
new_message(violation, line) click to toggle source
# File lib/pronto/style_cop.rb, line 40
def new_message(violation, line)
  path = line.patch.delta.new_file[:path]
  Message.new(path, line, :warning, "[#{violation[:rule_id]}] #{violation[:message]}", nil, self.class)
end
run() click to toggle source
# File lib/pronto/style_cop.rb, line 17
def run
  return [] unless @patches

  @patches.select { |patch| valid_patch?(patch) }
    .map { |patch| inspect(patch) }
    .flatten.compact
end
valid_patch?(patch) click to toggle source
# File lib/pronto/style_cop.rb, line 25
def valid_patch?(patch)
  return false unless patch.additions > 0
  csharp_file?(patch.new_file_full_path)
end

Private Instance Methods

cs_file?(path) click to toggle source
# File lib/pronto/style_cop.rb, line 55
def cs_file?(path)
  File.extname(path) == '.cs'
end
csharp_file?(path) click to toggle source
# File lib/pronto/style_cop.rb, line 51
def csharp_file?(path)
  cs_file?(path)
end
definitions() click to toggle source
# File lib/pronto/style_cop.rb, line 70
def definitions
  @definitions ||= @config.style_cop_definitions
end
git_repo_path() click to toggle source
# File lib/pronto/style_cop.rb, line 47
def git_repo_path
  @git_repo_path ||= Rugged::Repository.discover(File.expand_path(Dir.pwd)).workdir
end
parallel() click to toggle source
# File lib/pronto/style_cop.rb, line 74
def parallel
  @parallel ||= begin
    parallel = @config.style_cop_parallel
    parallel = nil if parallel < 0
    parallel
  end
end
parse_stylecop_violation(violation_file) click to toggle source
# File lib/pronto/style_cop.rb, line 105
def parse_stylecop_violation(violation_file)
  violations = Set.new
  File.open(violation_file) do |f|
    doc = REXML::Document.new(f)
    doc.elements.each('StyleCopViolations/Violation') do |violation|
      attributes = violation.attributes
      line_number = attributes['LineNumber'].to_i
      rule_id = attributes['RuleId']
      message = violation.text.strip
      violations.add(line_number: line_number, rule_id: rule_id, message: message)
    end
  end
  violations
end
run_stylecop(patch, definition) click to toggle source
# File lib/pronto/style_cop.rb, line 82
def run_stylecop(patch, definition)
  Dir.chdir(git_repo_path) do
    Tempfile.create do |f|
      file_path = patch.new_file_full_path.to_s
      opt = stylecop_options(definition)
      ret = `'#{STYLECOP_COMMAND}' #{opt.join(' ')} -cs '#{file_path}' -out '#{f.path}'`
      raise ret unless stylecop_success?($?)
      parse_stylecop_violation(f.path)
    end
  end
end
settings() click to toggle source
# File lib/pronto/style_cop.rb, line 59
def settings
  @settings ||= begin
    puts "deprecation environment variable 'STYLECOP_SETTINGS'" if ENV.key?('STYLECOP_SETTINGS')

    settigns = ENV.fetch('PRONTO_STYLECOP_SETTINGS', nil)
    settigns = ENV.fetch('STYLECOP_SETTINGS', nil) if settigns.nil? # deprecation
    settigns = './Settings.StyleCop' if settigns.nil? && File.exist?('./Settings.StyleCop')
    settigns
  end
end
stylecop_options(definition) click to toggle source
# File lib/pronto/style_cop.rb, line 94
def stylecop_options(definition)
  opt = []
  opt.push("-set '#{settings}'") unless settings.nil?
  opt.push("-flags '#{definition.join(',')}'") unless definition.nil? || definition.empty?
  opt
end
stylecop_success?(status) click to toggle source
# File lib/pronto/style_cop.rb, line 101
def stylecop_success?(status)
  status.success? || status.exitstatus == 2
end