-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
110 lines (83 loc) · 3.29 KB
/
Cakefile
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
fs = require 'fs'
{ execSync } = require 'child_process'
luamin = require 'luamin'
cwd = __dirname.replace /\\/g, '/'
templateNames = [
'romllib'
'romlc',
'rossc' ]
build = 'build'
tmpFile = 'tmp.moon'
task 'build', 'Build the templates into a single, minified Lua script.', ->
timeTask ->
for templateName in templateNames
console.log "Starting build for #{templateName}"
buildTemplate("#{cwd}/src/#{templateName}.moon")
console.log "#{templateName} finished building\n"
for templateName in templateNames
task "build:#{templateName}", "Build the #{templateName} template into a single, minified Lua script.", ->
timeTask ->
buildTemplate("#{cwd}/src/#{templateName}.moon")
timeTask = (taskFn) ->
startTime = new Date().getTime()
taskFn()
endTime = new Date().getTime()
console.log "\nBuild completed in #{endTime - startTime}ms!"
buildTemplate = (templateLocation) ->
script = loadTemplate(templateLocation)
console.log 'Loaded template.'
script = fillTemplate(script)
console.log 'Template filled out.'
writeMoonScriptToTemporaryFile(script)
console.log "Wrote MoonScript to temporary file: #{tmpFile}."
script = compileMoonScript().toString()
console.log "Compiled temporary MoonScript file."
script = minifyLuaScript(script)
console.log 'Minified output lua script.'
createOutputDirectoryIfItDoesNotExist()
outFile = getOutputFilename(templateLocation) + '.lua'
writeLuaScriptToOutputDirectory(script, outFile)
console.log "Wrote lua script to file: #{build}/#{outFile}."
removeTemporaryMoonScriptFile()
console.log 'Removed temporary MoonScript file.'
loadTemplate = (templateLocation) ->
buffer = fs.readFileSync templateLocation
return replaceLineEndings buffer.toString()
replaceLineEndings = (code) ->
code = code.replace /\r\n/g, '\n'
return code.replace /\r/g, '\n'
fillTemplate = (template) ->
importPattern = /--\s*{{\s*TBSHTEMPLATE:IMPORT\s+([^\s]*)\s*}}/gi
return template.replace importPattern, (match, location) ->
buffer = fs.readFileSync "#{cwd}/src/#{location}"
code = replaceLineEndings buffer.toString()
code = extractSpecifiedCodeBlock code
return wrapInDoBlock code
extractSpecifiedCodeBlock = (fileContents) ->
specifiedCodeBlockPattern = /--\s*{{\s*TBSHTEMPLATE\s*:\s*BEGIN\s*}}\n([\w\W]*)\n--\s*{{\s*TBSHTEMPLATE\s*:\s*END\s*}}/gi
match = specifiedCodeBlockPattern.exec(fileContents)
if match == null
return ''
return match[1]
wrapInDoBlock = (code) ->
code = code.replace /\n/g, '\n\t'
return "do\n\t#{code}"
writeMoonScriptToTemporaryFile = (script) ->
fs.writeFileSync tmpFile, script
compileMoonScript = ->
execSync "moonc -p #{tmpFile}"
minifyLuaScript = (script) ->
luamin.minify script
createOutputDirectoryIfItDoesNotExist = ->
if !fs.existsSync build
fs.mkdirSync build
console.log "Created output directory: ./#{build}"
getOutputFilename = (templateLocation) ->
lastSlashIndex = templateLocation.lastIndexOf '/'
templateName = templateLocation.substring lastSlashIndex + 1
extensionIndex = templateName.indexOf '.'
return templateName.substring 0, extensionIndex
writeLuaScriptToOutputDirectory = (script, outFile) ->
fs.writeFileSync "#{build}/#{outFile}", script
removeTemporaryMoonScriptFile = ->
fs.unlinkSync tmpFile