Skip to content

Commit

Permalink
rubocop --autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed Aug 24, 2017
1 parent e3eee22 commit a2e321d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 72 deletions.
23 changes: 13 additions & 10 deletions common/rakelib/changelog.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace :changelog do
desc 'Add the empty CHANGELOG entries after a new release'
task :reset do |task|
task :reset do
changelog = File.read('CHANGELOG.md')
abort('A Master entry already exists') if changelog =~ /^##\s*Master$/
changelog.sub!(/^##[^#]/, "#{header}\\0")
File.write('CHANGELOG.md', changelog)
end

def header
return <<-HEADER.gsub(/^\s*\|/,'')
<<-HEADER.gsub(/^\s*\|/, '')
|## Master
|
|### Bug Fixes
Expand All @@ -34,19 +34,22 @@ namespace :changelog do
end

desc 'Check if links to issues and PRs use matching numbers between text & link'
task :check do |task|
task :check do
current_repo = File.basename(`git remote get-url origin`.chomp, '.git').freeze
slug_re = '([a-zA-Z]*/[a-zA-Z]*)'
links = %r{\[#{slug_re}?\#([0-9]+)\]\(https://github.com/#{slug_re}/(issues|pull)/([0-9]+)\)}
all_wrong_links = []
File.readlines('CHANGELOG.md').each_with_index do |line, idx|
wrong_links = line.scan(links)
.reject do |m|
(slug, num, url_slug, url_num) = [m[0] || "SwiftGen/#{current_repo}", m[1], m[2], m[4]]
(slug == url_slug) && (num == url_num)
end.map do |m|
" - Line #{idx+1}, link text is #{m[0]}##{m[1]} but links points to #{m[2]}##{m[4]}"
end
.reject do |m|
slug = m[0] || "SwiftGen/#{current_repo}"
num = m[1]
url_slug = m[2]
url_num = m[4]
(slug == url_slug) && (num == url_num)
end.map do |m|
" - Line #{idx + 1}, link text is #{m[0]}##{m[1]} but links points to #{m[2]}##{m[4]}"
end
all_wrong_links.concat(wrong_links)
end
if all_wrong_links.empty?
Expand All @@ -65,6 +68,6 @@ namespace :changelog do
body = Utils.top_changelog_entry

repo_name = File.basename(`git remote get-url origin`.chomp, '.git').freeze
client.create_release("SwiftGen/#{repo_name}", tag, :body => body)
client.create_release("SwiftGen/#{repo_name}", tag, body: body)
end
end
32 changes: 16 additions & 16 deletions common/rakelib/lint.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# - WORKSPACE

namespace :lint do
SWIFTLINT_VERSION = '0.19.0'
SWIFTLINT_VERSION = '0.19.0'.freeze

desc 'Install swiftlint'
task :install do |task|
next if check_version()
next if check_version

if not system('tty >/dev/null')
unless system('tty >/dev/null')
puts "warning: Unable to install SwiftLint #{SWIFTLINT_VERSION} without a terminal. Please run 'bundle exec rake lint:install' from a terminal."
next
end
Expand All @@ -17,39 +17,39 @@ namespace :lint do
tmppath = '/tmp/SwiftLint.pkg'

Utils.run([
"curl -Lo #{tmppath} #{url}",
"sudo installer -pkg #{tmppath} -target /"
], task)
"curl -Lo #{tmppath} #{url}",
"sudo installer -pkg #{tmppath} -target /"
], task)
end

if File.directory?('Sources')
desc 'Lint the code'
task :code => :install do |task|
Utils.print_header 'Linting the code'
Utils.run(%Q(swiftlint lint --no-cache --strict --path Sources), task)
Utils.run(%(swiftlint lint --no-cache --strict --path Sources), task)
end
end

desc 'Lint the tests'
task :tests => :install do |task|
Utils.print_header 'Linting the unit test code'
Utils.run(%Q(swiftlint lint --no-cache --strict --path "Tests/#{WORKSPACE}Tests"), task)
Utils.run(%(swiftlint lint --no-cache --strict --path "Tests/#{WORKSPACE}Tests"), task)
end

if File.directory?('Tests/Expected')
desc 'Lint the output'
task :output => :install do |task|
Utils.print_header 'Linting the template output code'
Utils.run(%Q(swiftlint lint --no-cache --strict --path Tests/Expected), task)
Utils.run(%(swiftlint lint --no-cache --strict --path Tests/Expected), task)
end
end

def check_version
return false if not system('which swiftlint > /dev/null')
return false unless system('which swiftlint > /dev/null')

current = `swiftlint version`.chomp.split('.').map { |e| e.to_i }
required = SWIFTLINT_VERSION.chomp.split('.').map { |e| e.to_i }
current = `swiftlint version`.chomp.split('.').map(&:to_i)
required = SWIFTLINT_VERSION.chomp.split('.').map(&:to_i)

return (current <=> required) >= 0
(current <=> required) >= 0
end
end
2 changes: 1 addition & 1 deletion common/rakelib/pod.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if defined?(POD_NAME) && File.file?("#{POD_NAME}.podspec")
desc 'Lint the Pod'
task :lint do |task|
Utils.print_header 'Linting the pod spec'
Utils.run(%Q(bundle exec pod lib lint "#{POD_NAME}.podspec" --quick), task)
Utils.run(%(bundle exec pod lib lint "#{POD_NAME}.podspec" --quick), task)
end
end
end
2 changes: 1 addition & 1 deletion common/rakelib/spm.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# none

if File.file?('Package.swift')
namespace :spm do
namespace :spm do
desc 'Build using SPM'
task :build do |task|
Utils.print_header 'Compile using SPM'
Expand Down
83 changes: 41 additions & 42 deletions common/rakelib/utils.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class Utils
## [ Run commands ] #########################################################

# formatter types
:xcpretty # pass through xcpretty and store in artifacts
:raw # store in artifacts
:to_string # run using backticks and return output
# :xcpretty : through xcpretty and store in artifacts
# :raw : store in artifacts
# :to_string : run using backticks and return output

# run a command using xcrun and xcpretty if applicable
def self.run(cmd, task, subtask = '', xcrun: false, formatter: :raw)
commands = xcrun ? [*cmd].map { |cmd|
commands = xcrun ? [*cmd].map do |cmd|
"#{@@version_select} xcrun #{cmd}"
} : [*cmd]
end : [*cmd]

case formatter
when :xcpretty then xcpretty(commands, task, subtask)
Expand All @@ -32,7 +32,7 @@ class Utils
## [ Convenience Helpers ] ##################################################

def self.podspec_version(file = '*')
JSON.parse(`bundle exec pod ipc spec #{file}.podspec`)["version"]
JSON.parse(`bundle exec pod ipc spec #{file}.podspec`)['version']
end

def self.podfile_lock_version(pod)
Expand All @@ -46,16 +46,16 @@ class Utils
token = File.exist?('.apitoken') && File.read('.apitoken')
token ||= File.exist?('../.apitoken') && File.read('../.apitoken')
Utils.print_error('No .apitoken file found') unless token
Octokit::Client.new(:access_token => token)
Octokit::Client.new(access_token: token)
end

def self.top_changelog_version(changelog_file = 'CHANGELOG.md')
`grep -m 1 '^## ' "#{changelog_file}" | sed 's/## //'`.strip
end

def self.top_changelog_entry(changelog_file = 'CHANGELOG.md')
tag = self.top_changelog_version
`sed -n /'^## #{tag}$'/,/'^## '/p "#{changelog_file}"`.gsub(/^## .*$/,'').strip
tag = top_changelog_version
`sed -n /'^## #{tag}$'/,/'^## '/p "#{changelog_file}"`.gsub(/^## .*$/, '').strip
end

## [ Print info/errors ] ####################################################
Expand Down Expand Up @@ -90,12 +90,11 @@ class Utils
result
end


## [ Private helper functions ] ##################################################

# run a command, pipe output through 'xcpretty' and store the output in CI artifacts
def self.xcpretty(cmd, task, subtask)
name = (task.name + (subtask.empty? ? '' : "_#{subtask}")).gsub(/[:-]/, "_")
name = (task.name + (subtask.empty? ? '' : "_#{subtask}")).gsub(/[:-]/, '_')
command = [*cmd].join(' && ')

if ENV['CI']
Expand All @@ -110,7 +109,7 @@ class Utils

# run a command and store the output in CI artifacts
def self.plain(cmd, task, subtask)
name = (task.name + (subtask.empty? ? '' : "_#{subtask}")).gsub(/[:-]/, "_")
name = (task.name + (subtask.empty? ? '' : "_#{subtask}")).gsub(/[:-]/, '_')
command = [*cmd].join(' && ')

if ENV['CI']
Expand All @@ -128,22 +127,22 @@ class Utils
private_class_method :version_select

def self.compute_developer_dir(min_version)
# if current Xcode already fulfills min version don't force DEVELOPER_DIR=
# if current Xcode already fulfills min version don't force DEVELOPER_DIR=...
current_xcode = Pathname.new(`xcode-select -p`).parent.parent
current_xcode_version = `mdls -name kMDItemVersion -raw #{current_xcode}`.chomp
return "" if current_xcode_version.to_f >= min_version
return '' if current_xcode_version.to_f >= min_version

# Get all available Xcodes, order by version, get the latest one
xcodes = `mdfind "kMDItemCFBundleIdentifier = 'com.apple.dt.Xcode'"`.chomp.split("\n")
versions = xcodes.map { |path| { :vers => `mdls -name kMDItemVersion -raw "#{path}"`, :path => path } }
latest_xcode = versions.sort { |p1, p2| p1[:vers] <=> p2[:vers] }.last
versions = xcodes.map { |path| { vers: `mdls -name kMDItemVersion -raw "#{path}"`, path: path } }
latest_xcode = versions.sort_by { |a| a[:vers] }.last

# Check if it's at least the right version
unless latest_xcode[:vers].to_f >= min_version
raise "\n[!!!] SwiftGen requires Xcode #{MIN_XCODE_VERSION}, but we were not able to find it. If it's already installed update your Spotlight index with 'mdimport /Applications/Xcode*'\n\n"
end

return %Q(DEVELOPER_DIR="#{latest_xcode[:path]}/Contents/Developer")
%(DEVELOPER_DIR="#{latest_xcode[:path]}/Contents/Developer")
end
private_class_method :compute_developer_dir
end
Expand All @@ -152,37 +151,37 @@ class String
# colorization
FORMATTING = {
# text styling
:bold => 1,
:faint => 2,
:italic => 3,
:underline => 4,
bold: 1,
faint: 2,
italic: 3,
underline: 4,
# foreground colors
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
black: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
white: 37,
# background colors
:bg_black => 40,
:bg_red => 41,
:bg_green => 42,
:bg_yellow => 43,
:bg_blue => 44,
:bg_magenta => 45,
:bg_cyan => 46,
:bg_white => 47
}
bg_black: 40,
bg_red: 41,
bg_green: 42,
bg_yellow: 43,
bg_blue: 44,
bg_magenta: 45,
bg_cyan: 46,
bg_white: 47
}.freeze

# only enable formatting if terminal supports it
if `tput colors`.chomp.to_i >= 8
def format(*styles)
styles.reduce("") { |r, s| r << "\e[#{FORMATTING[s]}m" } << "#{self}\e[0m"
def format(*styles)
styles.reduce('') { |r, s| r << "\e[#{FORMATTING[s]}m" } << "#{self}\e[0m"
end
else
def format(*styles)
def format(*_styles)
self
end
end
Expand Down
4 changes: 2 additions & 2 deletions common/rakelib/xcode.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace :xcode do
desc 'Build using Xcode'
task :build do |task|
Utils.print_header 'Compile using Xcode'
Utils.run(%Q(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "#{SCHEME_NAME}" -configuration "#{CONFIGURATION}" build-for-testing), task, xcrun: true, formatter: :xcpretty)
Utils.run(%(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "#{SCHEME_NAME}" -configuration "#{CONFIGURATION}" build-for-testing), task, xcrun: true, formatter: :xcpretty)
end

desc 'Run Xcode Unit Tests'
task :test => :build do |task|
Utils.print_header 'Run the unit tests using Xcode'
Utils.run(%Q(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "#{SCHEME_NAME}" -configuration "#{CONFIGURATION}" test-without-building), task, xcrun: true, formatter: :xcpretty)
Utils.run(%(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "#{SCHEME_NAME}" -configuration "#{CONFIGURATION}" test-without-building), task, xcrun: true, formatter: :xcpretty)
end
end

0 comments on commit a2e321d

Please sign in to comment.