Sha256: ba6ba90e96cfde22a7e4003c2e8d625c38b36fd33d4f09f3d41952eed46f4acc

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

class IBLinterRunner
  def initialize(binary_path, execute_command)
    @binary_path = binary_path
    @execute_command = execute_command
  end

  def run(command)
    `#{command}`
  end

  def lint(path, options)
    command = lint_command(options)
    Dir.chdir path
    JSON.parse(run(command))
  end

  def lint_command(options)
    executable = @execute_command
    executable ||= @binary_path.nil? ? "iblinter" : File.absolute_path(@binary_path)
    "#{executable} lint #{arguments(options.merge(reporter: 'json'))}"
  end

  # Parse options into shell arguments.
  # Reference  https://github.com/ashfurrow/danger-ruby-swiftlint/blob/0af6d5aff38dc666352ea3750266fb7630d88bdd/ext/swiftlint/swiftlint.rb#L38
  def arguments(options)
    options.
      reject { |_key, value| value.nil? }.
      map { |key, value| value.kind_of?(TrueClass) ? [key, ""] : [key, value] }.
      # map booleans arguments equal false
      map { |key, value| value.kind_of?(FalseClass) ? ["no-#{key}", ""] : [key, value] }.
      # replace underscore by hyphen
      map { |key, value| [key.to_s.tr("_", "-"), value] }.
      # prepend "--" into the argument
      map { |key, value| ["--#{key}", value] }.
      # reduce everything into a single string
      reduce("") { |args, option| "#{args} #{option[0]} #{option[1]}" }.
      # strip leading spaces
      strip
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danger-iblinter-0.0.5 lib/iblinter/iblinter.rb