Sha256: 20c0f59d21107adedbe64102c9925a3060e8d2249da7291efa04443061215b59

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

require "logger"

module Danger
  class SwiftFormat
    def initialize(path = nil)
      @path = path || "swiftformat"
    end

    def installed?
      Cmd.run([@path, "--version"])
    end

    def check_format(files, additional_args = "")
      cmd = [@path] + files
      cmd << additional_args.split unless additional_args.nil? || additional_args.empty?
      cmd << %w(--dryrun --verbose)
      output = Cmd.run(cmd.flatten)
      raise "error running swiftformat: empty output" if output.empty?
      process(output)
    end

    private

    def process(output)
      {
          errors: errors(output),
          stats: {
              run_time: run_time(output)
          }
      }
    end

    ERRORS_REGEX = /rules applied:(.*)\n.*updated (.*)$/

    def errors(output)
      errors = []
      output.scan(ERRORS_REGEX) do |match|
        next if match.count < 2
        errors << {
            file: match[1],
            rules: match[0].split(",").map(&:strip)
        }
      end
      errors
    end

    RUNTIME_REGEX = /.*swiftformat completed.*(.+\..+)s/

    def run_time(output)
      if RUNTIME_REGEX.match(output)
        RUNTIME_REGEX.match(output)[1]
      else
        logger = Logger.new(STDERR)
        logger.error("Invalid run_time output: #{output}")
        "-1"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
danger-swiftformat-0.3.5 lib/swiftformat/swiftformat.rb
danger-swiftformat-0.3.4 lib/swiftformat/swiftformat.rb
danger-swiftformat-0.3.3 lib/swiftformat/swiftformat.rb
danger-swiftformat-0.3.2 lib/swiftformat/swiftformat.rb
danger-swiftformat-0.3.1 lib/swiftformat/swiftformat.rb