This repository has been archived by the owner on Sep 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Rakefile
executable file
·200 lines (169 loc) · 5.7 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/rake
require 'English'
## [ Constants ] ##############################################################
WORKSPACE = 'Templates'.freeze
SCHEME_NAME = 'Tests'.freeze
CONFIGURATION = 'Debug'.freeze
MIN_XCODE_VERSION = '~> 9.0'.freeze
## [ Output compilation ] #####################################################
MODULE_INPUT_PATH = 'Fixtures/stub-env/Modules'.freeze
MODULE_OUTPUT_PATH = 'Fixtures/stub-env'.freeze
SDKS = {
macosx: 'x86_64-apple-macosx10.13',
iphoneos: 'arm64-apple-ios11.0',
watchos: 'armv7k-apple-watchos4.0',
appletvos: 'arm64-apple-tvos11.0'
}.freeze
TOOLCHAINS = {
swift3: {
version: 3,
module_path: "#{MODULE_OUTPUT_PATH}/swift3",
toolchain: 'com.apple.dt.toolchain.XcodeDefault'
},
swift4: {
version: 4,
module_path: "#{MODULE_OUTPUT_PATH}/swift4",
toolchain: 'com.apple.dt.toolchain.XcodeDefault'
}
}.freeze
namespace :output do
desc 'Generate Test Output'
task :generate => 'xcode:build' do |task|
Utils.print_header 'Generating expected test output files...'
Utils.run(
%(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "Generate Output" -configuration "#{CONFIGURATION}" test-without-building),
task,
xcrun: true,
formatter: :xcpretty
)
end
desc 'Compile modules'
task :modules do |task|
Utils.print_header 'Compile output modules'
# macOS
modules = %w[FadeSegue PrefsWindowController]
modules.each do |m|
Utils.print_info "Compiling module #{m}… (macos)"
compile_module(m, :macosx, task)
end
# iOS
modules = %w[CustomSegue LocationPicker SlackTextViewController]
modules.each do |m|
Utils.print_info "Compiling module #{m}… (ios)"
compile_module(m, :iphoneos, task)
end
# delete swiftdoc
Dir.glob("#{MODULE_OUTPUT_PATH}/*.swiftdoc").each do |f|
FileUtils.rm_rf(f)
end
end
desc 'Compile output'
task :compile => :modules do |task|
Utils.print_header 'Compiling template output files'
failures = []
Dir.glob('Tests/Expected/**/*.swift').each do |f|
Utils.print_info "Compiling #{f}…\n"
failures << f unless compile_file(f, task)
end
Utils.print_header 'Compilation report'
if failures.empty?
Utils.print_info 'All files compiled successfully!'
else
Utils.print_error 'The following files failed to compile'
failures.each { |f| Utils.print_error " - #{f}" }
end
exit failures.empty?
end
def compile_module(m, sdk, task)
target = SDKS[sdk]
subtask = File.basename(m, '.*')
commands = TOOLCHAINS.map do |_key, toolchain|
%(--toolchain #{toolchain[:toolchain]} -sdk #{sdk} swiftc -swift-version #{toolchain[:version]} ) +
%(-emit-module "#{MODULE_INPUT_PATH}/#{m}.swift" -module-name "#{m}" ) +
%(-emit-module-path "#{toolchain[:module_path]}" -target "#{target}")
end
Utils.run(commands, task, subtask, xcrun: true)
end
def toolchain(f)
if f.include?('swift3')
TOOLCHAINS[:swift3]
elsif f.include?('swift4')
TOOLCHAINS[:swift4]
end
end
def sdks(f)
if f.include?('iOS')
[:iphoneos]
elsif f.include?('macOS')
[:macosx]
else
%i[iphoneos macosx appletvos watchos]
end
end
def compile_file(f, task)
toolchain = toolchain(f)
if toolchain.nil?
puts "Unable to typecheck Swift 2 file #{f}"
return true
end
sdks = sdks(f)
defs = if f.include?('publicAccess')
["#{MODULE_OUTPUT_PATH}/PublicDefinitions.swift"]
else
defs = ["#{MODULE_OUTPUT_PATH}/Definitions.swift"]
end
defs << "#{MODULE_OUTPUT_PATH}/ExtraDefinitions.swift" if f.include?('extra-definitions')
commands = sdks.map do |sdk|
%(--toolchain #{toolchain[:toolchain]} -sdk #{sdk} swiftc -swift-version #{toolchain[:version]} ) +
%(-typecheck -target #{SDKS[sdk]} -I #{toolchain[:module_path]} #{defs.join(' ')} #{f})
end
subtask = File.basename(f, '.*')
begin
Utils.run(commands, task, subtask, xcrun: true)
return true
rescue
Utils.print_error "Failed to compile #{f}!"
return false
end
end
end
## [ Release a new version ] ##################################################
namespace :release do
desc 'Create a new release on GitHub'
task :new => [:check_versions, 'xcode:test', :create_tag, 'changelog:push_github_release']
desc 'Check if all versions from the podspecs and CHANGELOG match'
task :check_versions do
results = []
# Check if bundler is installed first, as we'll need it for the cocoapods task (and we prefer to fail early)
`which bundler`
results << Utils.table_result(
$CHILD_STATUS.success?,
'Bundler installed',
'Please install bundler using `gem install bundler` and run `bundle install` first.'
)
# Guess version to release
version = File.read('CHANGELOG.md').match(/^## (.*)$/)[1]
Utils.table_info('Top version in CHANGELOG.md', version)
# Check if entry present in CHANGELOG
changelog_entry = system(%(grep -q '^## #{Regexp.quote(version)}$' CHANGELOG.md))
results << Utils.table_result(
changelog_entry,
'CHANGELOG, Entry added',
"Please add an entry for #{version} in CHANGELOG.md"
)
changelog_master = system("grep -qi '^## Master' CHANGELOG.md")
results << Utils.table_result(
!changelog_master,
'CHANGELOG, No master',
'Please remove entry for master in CHANGELOG'
)
exit 1 unless results.all?
print "Release version #{version} [Y/n]? "
exit 2 unless STDIN.gets.chomp == 'Y'
end
task :create_tag do
version = File.read('CHANGELOG.md').match(/^## (.*)$/)[1]
`git tag #{version} && git push --tags`
end
end
task :default => 'xcode:test'