-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.lua
361 lines (288 loc) · 9.62 KB
/
build.lua
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
--[[============================================================
--=
--= Build script
--=
--= Requires LuaFileSystem!
--= Preparing a release requires a lot more things!
--=
--=-------------------------------------------------------------
--=
--= ReFreezed Bitmap Font converter
--= by Marcus 'ReFreezed' Thunström
--=
--============================================================]]
io.stdout:setvbuf("no")
io.stderr:setvbuf("no")
collectgarbage("stop")
print("Building...")
local buildStartTime = os.clock()
local DIR_HERE = debug.getinfo(1, "S").source:match"^@(.+)":gsub("\\", "/"):gsub("/?[^/]+$", ""):gsub("^$", ".")
local pp = require"build.preprocess"
local lfs = require"lfs"
local devMode = false
local testMode = false
local doRelease = false
--
-- Parse args
--
if arg[1] == "dev" then
devMode = true
elseif arg[1] == "test" then
devMode = true
testMode = true
elseif arg[1] == "release" then
doRelease = true
elseif arg[1] then
error("Unknown mode '"..arg.."'.")
end
assert(not (devMode and doRelease))
--
-- Metaprogram stuff
--
local metaEnv = pp.metaEnvironment
metaEnv.DEV = devMode
metaEnv.TEST = testMode
metaEnv.lfs = lfs
local chunk = assert(loadfile"build/meta.lua")
setfenv(chunk, metaEnv)
chunk()
setmetatable(_G, {__index=metaEnv})
--
-- Build!
--
makeDirectory("srcgen")
for filename in lfs.dir("src") do
local pathIn = "src/"..filename
if filename:find"%.lua$" then
copyFile(pathIn, "srcgen/"..filename)
elseif filename:find"%.lua2p$" then
local pathOut = (
(filename == "conf.lua2p" and "conf.lua") or
(filename == "main.lua2p" and "main.lua") or
"srcgen/"..filename:gsub("%.lua2p$", ".lua")
)
pp.processFile{
pathIn = pathIn,
pathOut = pathOut,
pathMeta = pathOut:gsub("%.%w+$", ".meta%0"),
debug = false,
backtickStrings = true,
canOutputNil = false,
onError = function(err)
os.exit(1)
end,
}
end
end
printf("Build completed in %.3f seconds!", os.clock()-buildStartTime)
--
-- Prepare release
--
if doRelease then
print("Preparing release...")
local params = loadParams()
local outputDirWin64 = "output/win64/ReFreezed Bitmap Font converter"
local outputDirMacOs = "output/macOs/ReFreezed Bitmap Font converter"
local outputDirUniversal = "output/universal/ReFreezed Bitmap Font converter"
local values
do
local versionStr, major,minor,patch = getReleaseVersion()
values = {
exeName = "RbmfConverter",
exePath = outputDirWin64.."/RbmfConverter.exe",
iconPath = "temp/appIcon.ico",
appName = "ReFreezed Bitmap Font converter",
appNameShort = "RBMF converter", -- Should be less than 16 characters long.
appNameInternal = "ReFreezed Bitmap Font converter",
appVersion = versionStr,
appVersionMajor = major,
appVersionMinor = minor,
appVersionPatch = patch,
appIdentifier = "com.refreezed.rbmfconverter",
companyName = "",
copyright = os.date"Copyright %Y Marcus 'ReFreezed' Thunström",
lovePath = "temp/app.love",
loveExeDir = params.dirLoveWin64,
loveExePath = params.dirLoveWin64.."/lovec.exe",
loveAppDir = params.dirLoveMacOs,
loveAppPath = params.dirLoveMacOs.."/love.app",
versionInfoPath = "temp/appInfo.res",
}
end
makeDirectory("temp")
do
-- Create missing icon sizes.
for _, size in ipairs{--[[16,]]24,32,48,64,128--[[,256]]} do
executeRequired(params.pathMagick, {
"gfx/appIcon256.png",
"-resize", F("%dx%d", size, size),
F("gfx/appIcon%d.png", size),
})
end
-- Crush icon PNGs.
for _, size in ipairs{16,24,32,48,64,128,256} do
executeRequired(params.pathPngCrush, {
"-ow", -- Overwrite (must be first).
"-rem", "alla", -- Remove unnecessary chunks.
"-reduce", -- Lossless color reduction.
"-warn", -- No spam!
F("gfx/appIcon%d.png", size),
})
end
-- Create .ico.
writeFile("temp/icons.txt", ([[
gfx/appIcon16.png
gfx/appIcon24.png
gfx/appIcon32.png
gfx/appIcon48.png
gfx/appIcon64.png
gfx/appIcon128.png
gfx/appIcon256.png
]]):gsub("\t", ""))
executeRequired(params.pathMagick, {
"@temp/icons.txt",
values.iconPath,
})
end
-- Make love. <3
local filesToLove = {
"conf.lua",
"main.lua",
}
for _, dir in ipairs{"gfx","srcgen"} do
traverseDirectory(dir, function(path)
if isFile(path) then
local ext = path:match"[^.]+$"
if ext == "psd" then
-- print("- "..path)
else
-- print("+ "..path)
table.insert(filesToLove, path)
end
-- elseif path == ? then
-- return "ignore"
end
end)
end
os.remove(values.lovePath)
zipFiles(params, values.lovePath, filesToLove)
-- Windows.
local PATH_RC_LOG = "temp/robocopy.log"
os.remove(PATH_RC_LOG)
do
local outputDir = outputDirWin64
-- Compile resource file.
do
local contents = readFile("build/appInfoTemplate.rc") -- UTF-16 LE BOM encoded.
contents = templateToStringUtf16(params, contents, values)
writeFile("temp/appInfo.rc", contents)
executeRequired(params.pathRh, {
"-open", "temp/appInfo.rc",
"-save", values.versionInfoPath,
"-action", "compile",
"-log", "temp/rh.log", -- @Temp
-- "-log", "CONSOLE", -- Why doesn't this work? (And is it just in Sublime?)
})
end
do
local TEMPLATE_UPDATE_EXE = ([[
[FILENAMES]
Exe = "${loveExePath}"
SaveAs = "${exePath}"
Log = CONSOLE
[COMMANDS]
-delete ICONGROUP,,
-delete VERSIONINFO,,
-add "${versionInfoPath}", ,,
-add "${iconPath}", ICONGROUP,MAINICON,0
]]):gsub("\t+", "")
local contents = templateToString(TEMPLATE_UPDATE_EXE, values, toWindowsPath)
writeTextFile("temp/updateExe.rhs", contents)
end
-- Create base for install directory using robocopy.
-- Note: Because of robocopy's complex return codes we just trust that it's always successful.
-- https://blogs.technet.microsoft.com/deploymentguys/2008/06/16/robocopy-exit-codes/
execute("ROBOCOPY", {values.loveExeDir, outputDir, "/NOCOPY", "/PURGE", "/E", "/LOG+:"..PATH_RC_LOG})
execute("ROBOCOPY", {values.loveExeDir, outputDir, "*.dll", "/XF","OpenAL32.dll", "/E", "/LOG+:"..PATH_RC_LOG})
-- Create exe.
do
executeRequired(params.pathRh, {
"-script", "temp/updateExe.rhs",
})
local contentsLoveExe = readFile(values.exePath)
local contentsLove = readFile(values.lovePath)
local file = assert(io.open(values.exePath, "wb"))
file:write(contentsLoveExe)
file:write(contentsLove)
file:close()
end
-- Add remaining files.
do
copyFilesInDirectory("examples", outputDir.."/examples")
copyFile("build/Changelog.txt", outputDir.."/_Changelog.txt")
copyFile("build/README.txt", outputDir.."/_README.txt")
end
end
--[[ macOS.
do
local outputDir = outputDirMacOs
local contentsDir = F("%s/%s.app/Contents", outputDir, values.exeName)
-- Create base for install directory using robocopy.
makeDirectoryRecursive(contentsDir)
makeDirectory(contentsDir.."/Frameworks")
makeDirectory(contentsDir.."/MacOS")
makeDirectory(contentsDir.."/Resources")
execute("ROBOCOPY", {values.loveAppPath.."/Contents", contentsDir, "/NOCOPY", "/PURGE", "/E", "/LOG+:"..PATH_RC_LOG})
removeDirectoryRecursive(contentsDir.."/_CodeSignature")
execute("ROBOCOPY", {values.loveAppPath.."/Contents/Frameworks", contentsDir.."/Frameworks", "/E", "/LOG+:"..PATH_RC_LOG})
execute("ROBOCOPY", {values.loveAppPath.."/Contents/MacOS", contentsDir.."/MacOS", "/E", "/LOG+:"..PATH_RC_LOG})
-- Create .icns.
assert(createIcns?("temp/appIcon.icns", {
{path="gfx/appIcon16.png", size=16},
{path="gfx/appIcon32.png", size=32},
{path="gfx/appIcon64.png", size=64},
{path="gfx/appIcon128.png", size=128},
{path="gfx/appIcon256.png", size=256},
}))
-- Create other files.
local infoPlist = readFile("build/appInfoTemplate.plist")
infoPlist = infoPlist:gsub("<!%-%-.-%-%->", "") -- Remove comments.
infoPlist = infoPlist:gsub(">%s+", ">") -- Remove useless whitespace.
infoPlist = infoPlist:gsub("%s+<", "<") -- Remove useless whitespace.
infoPlist = templateToString(infoPlist, values, function(s)
return (s:gsub('[&<>"]', {
["&"] = "&",
["<"] = "<",
[">"] = ">",
['"'] = """,
}))
end)
writeFile(contentsDir.."/Info.plist", infoPlist)
writeFile(contentsDir.."/PkgInfo", "APPL????")
-- Add remaining files.
do
copyFilesInDirectory("examples", outputDir.."/examples")
copyFile(values.lovePath, contentsDir.."/Resources/Game.love")
copyFile("temp/appIcon.icns", contentsDir.."/Resources/AppIcon.icns")
copyFile("build/Changelog.txt", outputDir.."/_Changelog.txt")
copyFile("build/README.txt", outputDir.."/_README.txt")
end
end
--]]
-- Universal.
do
local outputDir = outputDirUniversal
removeDirectoryRecursive(outputDir)
makeDirectoryRecursive(outputDir)
copyFilesInDirectory("examples", outputDir.."/examples")
copyFile(values.lovePath, F("%s/%s.love", outputDir, values.exeName))
copyFile("build/Changelog.txt", F("%s/_Changelog.txt", outputDir))
copyFile("build/README.txt", F("%s/_README.txt", outputDir))
copyFile("build/README (universal).txt", F("%s/_README (universal).txt", outputDir))
end
-- Zip for distribution!
zipDirectory(params, "output/RbmfConverter_"..values.appVersion.."_win64.zip", "./"..outputDirWin64)
-- zipDirectory(params, "output/RbmfConverter_"..values.appVersion.."_macos.zip", "./"..outputDirMacOs)
zipDirectory(params, "output/RbmfConverter_"..values.appVersion.."_universal.zip", "./"..outputDirUniversal)
print("Release ready!")
end