Skip to content

Commit

Permalink
Run rubocop --auto-correct
Browse files Browse the repository at this point in the history
Note that to as part of the auto-correct process Rubocop enabled Ruby's
frozen string literals.

This means that now to append to a string literal like we do when
creating the output message in this plugin we first need to unfreeze it,
which can be done by using the unary plus syntax:

```ruby
unfrozen_literal = +"abc"
```
  • Loading branch information
mokagio committed Oct 9, 2017
1 parent d2592c0 commit f9c5674
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 117 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
Expand Down
6 changes: 4 additions & 2 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

# A guardfile for making Danger Plugins
# For more info see https://github.com/guard/guard#readme

# To run, use `bundle exec guard`.

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
guard :rspec, cmd: 'bundle exec rspec' do
require 'guard/rspec/dsl'
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements
Expand Down
7 changes: 4 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

import 'ext/swiftlint/Rakefile'
require 'bundler/gem_tasks'
require "rspec/core/rake_task"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

Expand All @@ -10,5 +12,4 @@ task :spec do
Rake::Task['specs'].invoke
end

task :default => :spec

task default: :spec
1 change: 1 addition & 0 deletions bin/danger-swiftlint
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

if $PROGRAM_NAME == __FILE__
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
Expand Down
23 changes: 12 additions & 11 deletions danger-swiftlint.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding: utf-8

# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'version'
Expand All @@ -8,15 +10,15 @@ Gem::Specification.new do |spec|
spec.version = DangerSwiftlint::VERSION
spec.authors = ['Ash Furrow', 'David Grandinetti', 'Orta Therox', 'Thiago Felix', 'Giovanni Lodi']
spec.email = ['ash@ashfurrow.com', 'dbgrandi@gmail.com', 'orta.therox@gmail.com', 'thiago@thiagofelix.com', 'gio@mokacoding.com']
spec.description = %q{A Danger plugin for linting Swift with SwiftLint.}
spec.summary = %q{A Danger plugin for linting Swift with SwiftLint.}
spec.description = 'A Danger plugin for linting Swift with SwiftLint.'
spec.summary = 'A Danger plugin for linting Swift with SwiftLint.'
spec.homepage = 'https://github.com/ashfurrow/danger-swiftlint'
spec.license = 'MIT'

spec.files = `git ls-files`.split($/)
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.extensions = %w(ext/swiftlint/Rakefile)
spec.extensions = %w[ext/swiftlint/Rakefile]
spec.executables = ['danger-swiftlint']

spec.add_dependency 'danger'
Expand All @@ -27,19 +29,18 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.3'

# Testing support
spec.add_development_dependency "rspec", '~> 3.4'
spec.add_development_dependency 'rspec', '~> 3.4'

# Makes testing easy via `bundle exec guard`
spec.add_development_dependency "guard", '~> 2.14'
spec.add_development_dependency "guard-rspec", '~> 4.7'
spec.add_development_dependency 'guard', '~> 2.14'
spec.add_development_dependency 'guard-rspec', '~> 4.7'

# If you want to work on older builds of ruby
spec.add_development_dependency "listen", '3.0.7'
spec.add_development_dependency 'listen', '3.0.7'

# This gives you the chance to run a REPL inside your test
# via
# binding.pry
# This will stop test execution and let you inspect the results
spec.add_development_dependency "pry"

spec.add_development_dependency 'pry'
end
14 changes: 7 additions & 7 deletions ext/swiftlint/Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

require_relative '../../lib/version'

namespace :swiftlint do

desc "Download and install swiftlint tool"
desc 'Download and install swiftlint tool'
task :install do
REPO = "https://github.com/realm/SwiftLint"
VERSION = ENV["SWIFTLINT_VERSION"] || DangerSwiftlint::SWIFTLINT_VERSION
ASSET = "portable_swiftlint.zip"
REPO = 'https://github.com/realm/SwiftLint'
VERSION = ENV['SWIFTLINT_VERSION'] || DangerSwiftlint::SWIFTLINT_VERSION
ASSET = 'portable_swiftlint.zip'
URL = "#{REPO}/releases/download/#{VERSION}/#{ASSET}"
DESTINATION = File.expand_path(File.join(File.dirname(__FILE__), 'bin'))

Expand All @@ -16,9 +17,8 @@ namespace :swiftlint do
"curl -s -L #{URL} -o #{ASSET}",
"unzip -q #{ASSET} -d #{DESTINATION}",
"rm #{ASSET}"
].join(" && ")
].join(' && ')
end

end

task default: 'swiftlint:install'
18 changes: 9 additions & 9 deletions ext/swiftlint/swiftlint.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

# frozen_string_literal: true

class Swiftlint
def initialize(swiftlint_path=nil)
def initialize(swiftlint_path = nil)
@swiftlint_path = swiftlint_path
end

# Runs swiftlint
def run(cmd='lint', additional_swiftlint_args='', options={})
def run(cmd = 'lint', additional_swiftlint_args = '', options = {})
# change pwd before run swiftlint
if options.has_key? :pwd
Dir.chdir options.delete(:pwd)
end
Dir.chdir options.delete(:pwd) if options.key? :pwd

# run swiftlint with provided options
`#{swiftlint_path} #{cmd} #{swiftlint_arguments(options, additional_swiftlint_args)}`
Expand All @@ -27,18 +27,18 @@ def is_installed?

# Return swiftlint execution path
def swiftlint_path
@swiftlint_path or default_swiftlint_path
@swiftlint_path || default_swiftlint_path
end

private

# Parse options into shell arguments how swift expect it to be
# more information: https://github.com/Carthage/Commandant
# @param options (Hash) hash containing swiftlint options
def swiftlint_arguments (options, additional_swiftlint_args)
def swiftlint_arguments(options, additional_swiftlint_args)
(options.
# filter not null
select {|key, value| !value.nil?}.
reject { |_key, value| value.nil? }.
# map booleans arguments equal true
map { |key, value| value.is_a?(TrueClass) ? [key, ''] : [key, value] }.
# map booleans arguments equal false
Expand All @@ -48,7 +48,7 @@ def swiftlint_arguments (options, additional_swiftlint_args)
# prepend '--' into the argument
map { |key, value| ["--#{key}", value] }.
# reduce everything into a single string
reduce('') { |args, option| "#{args} #{option[0]} #{option[1]}"} +
reduce('') { |args, option| "#{args} #{option[0]} #{option[1]}" } +
" #{additional_swiftlint_args}").
# strip leading spaces
strip
Expand Down
69 changes: 35 additions & 34 deletions lib/danger_plugin.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

require 'find'
require 'yaml'
require 'shellwords'
require_relative '../ext/swiftlint/swiftlint'

module Danger

# Lint Swift files inside your projects.
# This is done using the [SwiftLint](https://github.com/realm/SwiftLint) tool.
# Results are passed out as a table in markdown.
Expand Down Expand Up @@ -39,9 +40,9 @@ class DangerSwiftlint < Plugin
# if nil, modified and added files from the diff will be used.
# @return [void]
#
def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_swiftlint_args: "")
def lint_files(files = nil, inline_mode: false, fail_on_error: false, additional_swiftlint_args: '')
# Fails if swiftlint isn't installed
raise "swiftlint is not installed" unless swiftlint.is_installed?
raise 'swiftlint is not installed' unless swiftlint.is_installed?

config = if config_file
File.expand_path(config_file)
Expand All @@ -51,7 +52,7 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s
nil
end
log "Using config file: #{config}"

dir_selected = directory ? File.expand_path(directory) : Dir.pwd
log "Swiftlint will be run from #{dir_selected}"

Expand Down Expand Up @@ -81,19 +82,19 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s

if inline_mode
# Reprt with inline comment
send_inline_comment(warnings, "warn")
send_inline_comment(errors, "fail")
send_inline_comment(warnings, 'warn')
send_inline_comment(errors, 'fail')
else
# Report if any warning or error
if warnings.count > 0 || errors.count > 0
message = "### SwiftLint found issues\n\n"
message = +"### SwiftLint found issues\n\n"
message << markdown_issues(warnings, 'Warnings') unless warnings.empty?
message << markdown_issues(errors, 'Errors') unless errors.empty?
markdown message

# Fail Danger on errors
if fail_on_error && errors.count > 0
fail "Failed due to SwiftLint errors"
if fail_on_error && errors.count.positive?
fail 'Failed due to SwiftLint errors'
end
end
end
Expand All @@ -104,8 +105,8 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s
# @return [Array] swiftlint issues
def run_swiftlint(files, options, additional_swiftlint_args)
files
.map { |file| options.merge({path: file})}
.map { |full_options| swiftlint.lint(full_options, additional_swiftlint_args)}
.map { |file| options.merge(path: file) }
.map { |full_options| swiftlint.lint(full_options, additional_swiftlint_args) }
.reject { |s| s == '' }
.map { |s| JSON.parse(s).flatten }
.flatten
Expand All @@ -115,55 +116,55 @@ def run_swiftlint(files, options, additional_swiftlint_args)
# If files are not provided it will use git modifield and added files
#
# @return [Array] swift files
def find_swift_files(files=nil, excluded_paths=[], dir_selected)
def find_swift_files(files = nil, excluded_paths = [], dir_selected)
# Assign files to lint
files = files ? Dir.glob(files) : (git.modified_files - git.deleted_files) + git.added_files

# Filter files to lint
return files.
files.
# Ensure only swift files are selected
select { |file| file.end_with?('.swift') }.
# Make sure we don't fail when paths have spaces
map { |file| Shellwords.escape(file) }.
# Remove dups
uniq.
map { |file| File.expand_path(file) }.
uniq
.map { |file| File.expand_path(file) }.
# Ensure only files in the selected directory
select { |file| file.start_with?(dir_selected) }.
# Reject files excluded on configuration
reject { |file|
excluded_paths.any? { |excluded_path|
Find.find(excluded_path).
map { |excluded_file| Shellwords.escape(excluded_file) }.
include?(file)
}
}
reject do |file|
excluded_paths.any? do |excluded_path|
Find.find(excluded_path)
.map { |excluded_file| Shellwords.escape(excluded_file) }
.include?(file)
end
end
end

# Parses the configuration file and return the excluded files
#
# @return [Array] list of files excluded
def excluded_files_from_config(filepath)
config = if filepath
YAML.load_file(filepath)
else
{"excluded" => []}
YAML.load_file(filepath)
else
{ 'excluded' => [] }
end

excluded_paths = config['excluded'] || []

# Extract excluded paths
return excluded_paths.
map { |path| File.join(File.dirname(filepath), path) }.
map { |path| File.expand_path(path) }.
select { |path| File.exists?(path) || Dir.exists?(path) }
excluded_paths
.map { |path| File.join(File.dirname(filepath), path) }
.map { |path| File.expand_path(path) }
.select { |path| File.exist?(path) || Dir.exist?(path) }
end

# Create a markdown table from swiftlint issues
#
# @return [String]
def markdown_issues (results, heading)
message = "#### #{heading}\n\n"
def markdown_issues(results, heading)
message = +"#### #{heading}\n\n"

message << "File | Line | Reason |\n"
message << "| --- | ----- | ----- |\n"
Expand All @@ -182,11 +183,11 @@ def markdown_issues (results, heading)
# Send inline comment with danger's warn or fail method
#
# @return [void]
def send_inline_comment (results, method)
def send_inline_comment(results, method)
dir = "#{Dir.pwd}/"
results.each do |r|
filename = r['file'].gsub(dir, "")
send(method, r['reason'], file: filename, line: r['line'])
filename = r['file'].gsub(dir, '')
send(method, r['reason'], file: filename, line: r['line'])
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

module DangerSwiftlint
VERSION = "0.10.1".freeze
SWIFTLINT_VERSION = "0.20.1".freeze
VERSION = '0.10.0'
SWIFTLINT_VERSION = '0.20.1'
end
Loading

0 comments on commit f9c5674

Please sign in to comment.