forked from SwiftGen/SwiftGen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
executable file
·312 lines (268 loc) · 10.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/rake
require 'pathname'
require 'yaml'
require 'shellwords'
## [ Constants ] ##############################################################
WORKSPACE = 'SwiftGen'.freeze
SCHEME_NAME = 'swiftgen'.freeze
CONFIGURATION = 'Debug'.freeze
RELEASE_CONFIGURATION = 'Release'.freeze
POD_NAME = 'SwiftGen'.freeze
MIN_XCODE_VERSION = 9.0
BUILD_DIR = File.absolute_path('./build')
BIN_NAME = 'swiftgen'.freeze
TEMPLATES_SRC_DIR = 'templates'.freeze
## [ Utils ] ##################################################################
def path(str)
return nil if str.nil? || str.empty?
Pathname.new(str)
end
def defaults(args)
bindir = path(args.bindir) || (Pathname.new(BUILD_DIR) + 'swiftgen/bin')
fmkdir = path(args.fmkdir) || (bindir + '../lib')
tpldir = path(args.tpldir) || (bindir + '../templates')
[bindir, fmkdir, tpldir].map(&:expand_path)
end
## [ Test Output Generation ] #################################################
namespace :generate do
desc 'Generate Test Contexts'
task :contexts => 'xcode:build' do |task|
Utils.print_header 'Generating contexts...'
Utils.run(
%(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "SwiftGenKit - Generate Contexts" -configuration "#{CONFIGURATION}" test-without-building),
task,
xcrun: true,
formatter: :xcpretty
)
end
desc 'Generate Test Output'
task :output => 'xcode:build' do |task|
Utils.print_header 'Generating expected test output files...'
Utils.run(
%(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "Templates - Generate Output" -configuration "#{CONFIGURATION}" test-without-building),
task,
xcrun: true,
formatter: :xcpretty
)
end
end
## [ Output compilation ] #####################################################
MODULE_INPUT_PATH = 'Tests/Fixtures/CompilationEnvironment/Modules'.freeze
MODULE_OUTPUT_PATH = 'Tests/Fixtures/CompilationEnvironment'.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 '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/Fixtures/Generated/**/*.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
## [ Build Tasks ] ############################################################
namespace :cli do
desc "Build the CLI binary and its frameworks as an app bundle\n" \
"(in #{BUILD_DIR})"
task :build, %i[bindir tpldir] do |task, args|
(bindir, _, tpldir) = defaults(args)
tpl_rel_path = tpldir.relative_path_from(bindir)
Utils.print_header 'Building Binary'
plist_file = (Pathname.new(BUILD_DIR) + "Build/Products/#{RELEASE_CONFIGURATION}/swiftgen.app/Contents/Info.plist").to_s
Utils.run(
%(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "#{SCHEME_NAME}" -configuration "#{RELEASE_CONFIGURATION}") +
%( -derivedDataPath "#{BUILD_DIR}" TEMPLATE_PATH="#{tpl_rel_path}") +
%( SWIFTGEN_OTHER_LDFLAGS="-sectcreate __TEXT __info_plist #{plist_file.shellescape}"),
task, xcrun: true, formatter: :xcpretty
)
end
desc "Install the binary in $bindir, frameworks in $fmkdir, and templates in $tpldir\n" \
'(defaults $bindir=./build/swiftgen/bin/, $fmkdir=$bindir/../lib, $tpldir=$bindir/../templates'
task :install, %i[bindir fmkdir tpldir] => :build do |task, args|
(bindir, fmkdir, tpldir) = defaults(args)
generated_bundle_path = "#{BUILD_DIR}/Build/Products/#{RELEASE_CONFIGURATION}/swiftgen.app/Contents"
Utils.print_header "Installing binary in #{bindir}"
Utils.run([
%(mkdir -p "#{bindir}"),
%(cp -f "#{generated_bundle_path}/MacOS/swiftgen" "#{bindir}/#{BIN_NAME}")
], task, 'copy_binary')
Utils.print_header "Installing frameworks in #{fmkdir}"
Utils.run([
%(if [ -d "#{fmkdir}" ]; then rm -rf "#{fmkdir}"; fi),
%(mkdir -p "#{fmkdir}"),
%(cp -fR "#{generated_bundle_path}/Frameworks/" "#{fmkdir}")
], task, 'copy_frameworks')
Utils.print_header "Fixing binary's @rpath"
Utils.run([
%(install_name_tool -delete_rpath "@executable_path/../Frameworks" "#{bindir}/#{BIN_NAME}"),
%(install_name_tool -add_rpath "@executable_path/#{fmkdir.relative_path_from(bindir)}" "#{bindir}/#{BIN_NAME}")
], task, 'fix_rpath', xcrun: true)
Utils.print_header "Installing templates in #{tpldir}"
Utils.run([
%(mkdir -p "#{tpldir}"),
%(cp -r "#{TEMPLATES_SRC_DIR}/" "#{tpldir}")
], task, 'copy_templates')
Utils.print_info "Finished installing. Binary is available in: #{bindir}"
end
desc "Delete the build directory\n" \
"(#{BUILD_DIR})"
task :clean do
sh %(rm -fr #{BUILD_DIR})
end
end
task :default => 'cli:build'
## [ ChangeLog ] ##############################################################
namespace :changelog do
LINKS_SECTION_TITLE = 'Changes in other SwiftGen modules'.freeze
desc 'Add links to other CHANGELOGs in the topmost SwiftGen CHANGELOG entry'
task :links do
changelog = File.read('CHANGELOG.md')
abort('Links seems to already exist for latest version entry') if /^### (.*)/.match(changelog)[1] == LINKS_SECTION_TITLE
links = linked_changelogs(
stencilswiftkit: Utils.podfile_lock_version('StencilSwiftKit'),
stencil: Utils.podfile_lock_version('Stencil')
)
changelog.sub!(/^##[^#].*$\n/, "\\0\n#{links}")
File.write('CHANGELOG.md', changelog)
end
def linked_changelogs(swiftgenkit: nil, stencilswiftkit: nil, stencil: nil, templates: nil)
<<-LINKS.gsub(/^\s*\|/, '')
|### #{LINKS_SECTION_TITLE}
|
|* [StencilSwiftKit #{stencilswiftkit}](https://github.com/SwiftGen/StencilSwiftKit/blob/#{stencilswiftkit}/CHANGELOG.md)
|* [Stencil #{stencil}](https://github.com/kylef/Stencil/blob/#{stencil}/CHANGELOG.md)
LINKS
end
end
## [ Playground Resources ] ###################################################
namespace :playground do
task :clean do
sh 'rm -rf SwiftGen.playground/Resources'
sh 'mkdir SwiftGen.playground/Resources'
end
task :xcassets do
Utils.run(
%(actool --compile SwiftGen.playground/Resources --platform iphoneos --minimum-deployment-target 7.0 ) +
%(--output-format=human-readable-text Tests/Fixtures/Resources/XCAssets/Images.xcassets),
task,
xcrun: true
)
end
task :storyboard do
Utils.run(
%(ibtool --compile SwiftGen.playground/Resources/Wizard.storyboardc --flatten=NO ) +
%(Tests/Fixtures/Resources/Storyboards-iOS/Wizard.storyboard),
task,
xcrun: true
)
end
task :strings do
Utils.run(
%(plutil -convert binary1 -o SwiftGen.playground/Resources/Localizable.strings ) +
%(Tests/Fixtures/Resources/Strings/Localizable.strings),
task,
xcrun: true
)
end
desc "Regenerate all the Playground resources based on the test fixtures.\n" \
'This compiles the needed fixtures and place them in SwiftGen.playground/Resources'
task :resources => %w[clean xcassets storyboard strings]
end