1- SWIFTLINT_VERSION = "0.41.0"
2- XCODE_WORKSPACE = "Simplenote.xcworkspace"
3- XCODE_SCHEME = "Simplenote"
4- XCODE_CONFIGURATION = "Debug"
1+ # frozen_string_literal: true
2+
3+ require 'English'
4+ SWIFTLINT_VERSION = '0.41.0'
5+ XCODE_WORKSPACE = 'Simplenote.xcworkspace'
6+ XCODE_SCHEME = 'Simplenote'
7+ XCODE_CONFIGURATION = 'Debug'
58
69require 'fileutils'
710require 'tmpdir'
811require 'rake/clean'
912require 'yaml'
1013require 'digest'
11- PROJECT_DIR = File . expand_path ( File . dirname ( __FILE__ ) )
14+ PROJECT_DIR = __dir__
1215
1316task default : %w[ test ]
1417
15- desc " Install required dependencies"
16- task : dependencies => %w[ dependencies:check ]
18+ desc ' Install required dependencies'
19+ task dependencies : %w[ dependencies:check ]
1720
1821namespace :dependencies do
19- task : check => %w[ bundler:check bundle:check pod:check lint:check ]
22+ task check : %w[ bundler:check bundle:check pod:check lint:check ]
2023
2124 namespace :bundler do
2225 task :check do
23- unless command? ( "bundler" )
24- Rake ::Task [ "dependencies:bundler:install" ] . invoke
25- end
26+ Rake ::Task [ 'dependencies:bundler:install' ] . invoke unless command? ( 'bundler' )
2627 end
2728
2829 task :install do
29- puts " Bundler not found in PATH, installing to vendor"
30+ puts ' Bundler not found in PATH, installing to vendor'
3031 ENV [ 'GEM_HOME' ] = File . join ( PROJECT_DIR , 'vendor' , 'gems' )
31- ENV [ 'PATH' ] = File . join ( PROJECT_DIR , 'vendor' , 'gems' , 'bin' ) + ":#{ ENV [ 'PATH' ] } "
32- sh " gem install bundler" unless command? ( " bundler" )
32+ ENV [ 'PATH' ] = File . join ( PROJECT_DIR , 'vendor' , 'gems' , 'bin' ) + ":#{ ENV . fetch ( 'PATH' , nil ) } "
33+ sh ' gem install bundler' unless command? ( ' bundler' )
3334 end
34- CLOBBER << " vendor/gems"
35+ CLOBBER << ' vendor/gems'
3536 end
3637
3738 namespace :bundle do
3839 task :check do
39- sh " bundle check --path=${BUNDLE_PATH:-vendor/bundle} > /dev/null" , verbose : false do |ok , res |
40+ sh ' bundle check --path=${BUNDLE_PATH:-vendor/bundle} > /dev/null' , verbose : false do |ok , _res |
4041 next if ok
42+
4143 # bundle check exits with a non zero code if install is needed
42- dependency_failed ( " Bundler" )
43- Rake ::Task [ " dependencies:bundle:install" ] . invoke
44+ dependency_failed ( ' Bundler' )
45+ Rake ::Task [ ' dependencies:bundle:install' ] . invoke
4446 end
4547 end
4648
4749 task :install do
48- fold ( " install.bundler" ) do
49- sh " bundle install --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle}"
50+ fold ( ' install.bundler' ) do
51+ sh ' bundle install --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle}'
5052 end
5153 end
52- CLOBBER << " vendor/bundle"
53- CLOBBER << " .bundle"
54+ CLOBBER << ' vendor/bundle'
55+ CLOBBER << ' .bundle'
5456 end
5557
5658 namespace :pod do
5759 task :check do
5860 unless podfile_locked? && lockfiles_match?
59- dependency_failed ( " CocoaPods" )
60- Rake ::Task [ " dependencies:pod:install" ] . invoke
61+ dependency_failed ( ' CocoaPods' )
62+ Rake ::Task [ ' dependencies:pod:install' ] . invoke
6163 end
6264 end
6365
6466 task :install do
65- fold ( " install.cocoapds" ) do
67+ fold ( ' install.cocoapds' ) do
6668 pod %w[ install ]
6769 end
6870 end
6971
7072 task :clean do
71- fold ( " clean.cocoapds" ) do
73+ fold ( ' clean.cocoapds' ) do
7274 FileUtils . rm_rf ( 'Pods' )
7375 end
7476 end
75- CLOBBER << " Pods"
77+ CLOBBER << ' Pods'
7678 end
7779
7880 namespace :lint do
79-
8081 task :check do
8182 if swiftlint_needs_install
82- dependency_failed ( " SwiftLint" )
83- Rake ::Task [ " dependencies:lint:install" ] . invoke
83+ dependency_failed ( ' SwiftLint' )
84+ Rake ::Task [ ' dependencies:lint:install' ] . invoke
8485 end
8586 end
8687
8788 task :install do
88- fold ( " install.swiftlint" ) do
89+ fold ( ' install.swiftlint' ) do
8990 puts "Installing SwiftLint #{ SWIFTLINT_VERSION } into #{ swiftlint_path } "
9091 Dir . mktmpdir do |tmpdir |
9192 # Try first using a binary release
@@ -100,93 +101,94 @@ namespace :dependencies do
100101 sh "git clone --quiet https://github.com/realm/SwiftLint.git #{ tmpdir } "
101102 Dir . chdir ( tmpdir ) do
102103 sh "git checkout --quiet #{ SWIFTLINT_VERSION } "
103- sh " git submodule --quiet update --init --recursive"
104- FileUtils . remove_entry_secure ( swiftlint_path ) if Dir . exist? ( swiftlint_path )
104+ sh ' git submodule --quiet update --init --recursive'
105+ FileUtils . rm_rf ( swiftlint_path )
105106 FileUtils . mkdir_p ( swiftlint_path )
106107 sh "make prefix_install PREFIX='#{ swiftlint_path } '"
107108 end
108109 end
109110 end
110111 end
111112 end
112- CLOBBER << " vendor/swiftlint"
113+ CLOBBER << ' vendor/swiftlint'
113114 end
114-
115115end
116116
117- CLOBBER << " vendor"
117+ CLOBBER << ' vendor'
118118
119119desc "Build #{ XCODE_SCHEME } "
120- task : build => [ :dependencies ] do
120+ task build : [ :dependencies ] do
121121 xcodebuild ( :build )
122122end
123123
124124desc "Profile build #{ XCODE_SCHEME } "
125- task : buildprofile => [ :dependencies ] do
126- ENV [ " verbose" ] = "1"
125+ task buildprofile : [ :dependencies ] do
126+ ENV [ ' verbose' ] = '1'
127127 xcodebuild ( :build , "OTHER_SWIFT_FLAGS='-Xfrontend -debug-time-compilation -Xfrontend -debug-time-expression-type-checking'" )
128128end
129129
130- task : timed_build => [ :clean ] do
130+ task timed_build : [ :clean ] do
131131 require 'benchmark'
132132 time = Benchmark . measure do
133- Rake ::Task [ " build" ] . invoke
133+ Rake ::Task [ ' build' ] . invoke
134134 end
135135 puts "CPU Time: #{ time . total } "
136136 puts "Wall Time: #{ time . real } "
137137end
138138
139- desc " Run test suite"
140- task : test => [ :dependencies ] do
139+ desc ' Run test suite'
140+ task test : [ :dependencies ] do
141141 xcodebuild ( :build , :test )
142142end
143143
144- desc " Remove any temporary products"
144+ desc ' Remove any temporary products'
145145task :clean do
146146 xcodebuild ( :clean )
147147end
148148
149- desc " Checks the source for style errors"
150- task : lint => %w[ dependencies:lint:check ] do
149+ desc ' Checks the source for style errors'
150+ task lint : %w[ dependencies:lint:check ] do
151151 swiftlint %w[ lint --quiet ]
152152end
153153
154154namespace :lint do
155- desc " Automatically corrects style errors where possible"
156- task : autocorrect => %w[ dependencies:lint:check ] do
155+ desc ' Automatically corrects style errors where possible'
156+ task autocorrect : %w[ dependencies:lint:check ] do
157157 swiftlint %w[ autocorrect ]
158158 end
159159end
160160
161161namespace :git do
162162 hooks = %w[ pre-commit post-checkout post-merge ]
163163
164- desc " Install git hooks"
164+ desc ' Install git hooks'
165165 task :install_hooks do
166166 hooks . each do |hook |
167167 target = hook_target ( hook )
168168 source = hook_source ( hook )
169169 backup = hook_backup ( hook )
170170
171- next if File . symlink? ( target ) and File . readlink ( target ) == source
172- next if File . file? ( target ) and File . identical? ( target , source )
171+ next if File . symlink? ( target ) && ( File . readlink ( target ) == source )
172+ next if File . file? ( target ) && File . identical? ( target , source )
173+
173174 if File . exist? ( target )
174175 puts "Existing hook for #{ hook } . Creating backup at #{ target } -> #{ backup } "
175- FileUtils . mv ( target , backup , : force => true )
176+ FileUtils . mv ( target , backup , force : true )
176177 end
177178 FileUtils . ln_s ( source , target )
178179 puts "Installed #{ hook } hook"
179180 end
180181 end
181182
182- desc " Uninstall git hooks"
183+ desc ' Uninstall git hooks'
183184 task :uninstall_hooks do
184185 hooks . each do |hook |
185186 target = hook_target ( hook )
186187 source = hook_source ( hook )
187188 backup = hook_backup ( hook )
188189
189- next unless File . symlink? ( target ) and File . readlink ( target ) == source
190+ next unless File . symlink? ( target ) && ( File . readlink ( target ) == source )
191+
190192 puts "Removing hook for #{ hook } "
191193 File . unlink ( target )
192194 if File . exist? ( backup )
@@ -210,12 +212,10 @@ namespace :git do
210212end
211213
212214namespace :git do
213- task :pre_commit => %[dependencies:lint:check] do
214- begin
215- swiftlint %w[ lint --quiet --strict ]
216- rescue
217- exit $?. exitstatus
218- end
215+ task pre_commit : %(dependencies:lint:check) do
216+ swiftlint %w[ lint --quiet --strict ]
217+ rescue StandardError
218+ exit $CHILD_STATUS. exitstatus
219219 end
220220
221221 task :post_merge do
@@ -227,19 +227,14 @@ namespace :git do
227227 end
228228end
229229
230- desc " Open the project in Xcode"
231- task : xcode => [ :dependencies ] do
230+ desc ' Open the project in Xcode'
231+ task xcode : [ :dependencies ] do
232232 sh "open #{ XCODE_WORKSPACE } "
233233end
234234
235- def fold ( label , & block )
236- puts "travis_fold:start: #{ label } " if is_travis?
235+ def fold ( label )
236+ puts "--- #{ label } " if ENV [ 'BUILDKITE' ]
237237 yield
238- puts "travis_fold:end:#{ label } " if is_travis?
239- end
240-
241- def is_travis?
242- return ENV [ "TRAVIS" ] != nil
243238end
244239
245240def pod ( args )
@@ -252,14 +247,14 @@ def lockfiles_match?
252247end
253248
254249def podfile_locked?
255- podfile_checksum = Digest ::SHA1 . file ( " Podfile" )
256- lockfile_checksum = YAML . load ( File . read ( " Podfile.lock" ) ) [ " PODFILE CHECKSUM" ]
250+ podfile_checksum = Digest ::SHA1 . file ( ' Podfile' )
251+ lockfile_checksum = YAML . load_file ( ' Podfile.lock' ) [ ' PODFILE CHECKSUM' ]
257252
258253 podfile_checksum == lockfile_checksum
259254end
260255
261256def swiftlint_path
262- "#{ PROJECT_DIR } /vendor/swiftlint"
257+ "#{ PROJECT_DIR } /vendor/swiftlint"
263258end
264259
265260def swiftlint ( args )
@@ -268,25 +263,26 @@ def swiftlint(args)
268263end
269264
270265def swiftlint_bin
271- "#{ swiftlint_path } /bin/swiftlint"
266+ "#{ swiftlint_path } /bin/swiftlint"
272267end
273268
274269def swiftlint_needs_install
275270 return true unless File . exist? ( swiftlint_bin )
271+
276272 installed_version = `"#{ swiftlint_bin } " version` . chomp
277- return ( installed_version != SWIFTLINT_VERSION )
273+ ( installed_version != SWIFTLINT_VERSION )
278274end
279275
280276def xcodebuild ( *build_cmds )
281- cmd = " xcodebuild"
277+ cmd = ' xcodebuild'
282278 cmd += " -destination 'platform=iOS Simulator,name=iPhone 6s'"
283- cmd += " -sdk iphonesimulator"
279+ cmd += ' -sdk iphonesimulator'
284280 cmd += " -workspace #{ XCODE_WORKSPACE } "
285281 cmd += " -scheme #{ XCODE_SCHEME } "
286282 cmd += " -configuration #{ xcode_configuration } "
287- cmd += " "
288- cmd += build_cmds . map ( &:to_s ) . join ( " " )
289- cmd += " | bundle exec xcpretty -f `bundle exec xcpretty-travis-formatter` && exit ${PIPESTATUS[0]}" unless ENV [ 'verbose' ]
283+ cmd += ' '
284+ cmd += build_cmds . map ( &:to_s ) . join ( ' ' )
285+ cmd += ' | bundle exec xcpretty -f `bundle exec xcpretty-travis-formatter` && exit ${PIPESTATUS[0]}' unless ENV [ 'verbose' ]
290286 sh ( cmd )
291287end
292288
@@ -297,22 +293,23 @@ end
297293def command? ( command )
298294 system ( "which #{ command } > /dev/null 2>&1" )
299295end
296+
300297def dependency_failed ( component )
301298 msg = "#{ component } dependencies missing or outdated. "
302299 if ENV [ 'DRY_RUN' ]
303- msg += " Run rake dependencies to install them."
304- fail msg
300+ msg += ' Run rake dependencies to install them.'
301+ raise msg
305302 else
306- msg += " Installing..."
303+ msg += ' Installing...'
307304 puts msg
308305 end
309306end
310307
311308def check_dependencies_hook
312- ENV [ 'DRY_RUN' ] = "1"
309+ ENV [ 'DRY_RUN' ] = '1'
313310 begin
314311 Rake ::Task [ 'dependencies' ] . invoke
315- rescue Exception => e
312+ rescue StandardError => e
316313 puts e . message
317314 exit 1
318315 end
0 commit comments