This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
484 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,43 @@ | ||
--- Tasks for the lexer | ||
-- @module lexer.Tasks | ||
|
||
local function MinifyFile(inputFile, outputFile) | ||
local input = fs.open(inputFile, "r") | ||
local contents = input.readAll() | ||
input.close() | ||
|
||
local contents = Rebuild.Minify(Parse.ParseLua(Parse.LexLua(contents))) | ||
|
||
local result = fs.open(outputFile, "w") | ||
result.write(contents) | ||
result.close() | ||
end | ||
|
||
|
||
--- A task that minifies a source file | ||
-- @tparam string name Name of the task | ||
-- @tparam string inputFile The input file | ||
-- @tparam string outputFile The file to save to | ||
-- @tparam table taskDepends A list of @{Task.Task|tasks} this task requires | ||
-- @treturn Task.TaskRunner The task runner (for chaining) | ||
-- @see Task.TaskRunner | ||
function Task.TaskRunner:Minify(name, inputFile, outputFile, taskDepends) | ||
-- @treturn Runner.Runner The task runner (for chaining) | ||
-- @see Runner.Runner | ||
function Runner.Runner:Minify(name, inputFile, outputFile, taskDepends) | ||
return self:AddTask(name, taskDepends, function() | ||
local input = fs.open(inputFile, "r") | ||
local contents = input.readAll() | ||
input.close() | ||
|
||
local contents = Rebuild.Minify(Parse.ParseLua(Parse.LexLua(contents))) | ||
MinifyFile(inputFile, outputFile) | ||
end) | ||
:Description("Minifies '" .. fs.getName(inputFile) .. "'' into '" .. fs.getName(outputFile) .. "'") | ||
:Requires(inputFile) | ||
:Produces(outputFile) | ||
end | ||
|
||
local result = fs.open(outputFile, "w") | ||
result.write(contents) | ||
result.close() | ||
end):Description("Minifies '" .. fs.getName(inputFile) .. "'' into '" .. fs.getName(outputFile) .. "'") | ||
--- A task that minifies to a pattern instead | ||
-- @tparam string name Name of the task | ||
-- @tparam string inputPattern The pattern to read in | ||
-- @tparam string outputPattern The pattern to produce | ||
-- @treturn Runner.Runner The task runner (for chaining) | ||
function Runner.Runner:MinifyAll(name, inputPattern, outputPattern) | ||
name = name or "_minify" | ||
return self:AddTask(name, {}, MinifyFile) | ||
:Description("Minifies files") | ||
:Maps(inputPattern or "wild:*.lua", outputPattern or "wild:*.min.lua") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
--- Manages the running of tasks | ||
-- @module tasks.Context | ||
|
||
--- Holds task contexts | ||
-- @type Context | ||
local Context = {} | ||
|
||
function Context:DoRequire(path, quite) | ||
if self.filesProduced[path] then return true end | ||
|
||
-- Check for normal files | ||
local task = self.producesCache[path] | ||
if task then | ||
self.filesProduced[path] = true | ||
return self:Run(task) | ||
end | ||
|
||
-- Check for file mapping | ||
task = self.normalMapsCache[path] | ||
local from, name | ||
local to = path | ||
if task then | ||
self.filesProduced[path] = true | ||
|
||
-- Convert task.Pattern.From to path | ||
-- (which should be task.Pattern.To) | ||
name = task.Name | ||
from = task.Pattern.From | ||
end | ||
|
||
for match, data in pairs(self.patternMapsCache) do | ||
if path:match(match) then | ||
-- Run task, replacing match with the replacement pattern | ||
name = data.Name | ||
from = path:gsub(match, data.Pattern.From) | ||
break | ||
end | ||
end | ||
|
||
if name then | ||
local canCreate = self:DoRequire(from, quite) | ||
if not canCreate and not fs.exists(from) then | ||
if not quite then | ||
Utils.PrintError("Cannot find '" .. from .. "'") | ||
end | ||
return false | ||
end | ||
|
||
return self:Run(name, from, to) | ||
end | ||
|
||
|
||
self.filesProduced[path] = true | ||
if not quite then | ||
Utils.PrintError("Cannot find a task matching '" .. path .. "'") | ||
end | ||
return false | ||
end | ||
|
||
--- Run a task | ||
-- @tparam string|Task.Task task The name of the task or a Task object | ||
-- @param ... The arguments to pass to it | ||
-- @treturn boolean Success in running the task? | ||
function Context:Run(name, ...) | ||
if self.ran[task] then return true end | ||
|
||
local task | ||
if type(name) == "string" then | ||
task = self.tasks[name] | ||
|
||
if not task then | ||
Utils.PrintError("Cannot find a task called '" .. name .."'") | ||
return false | ||
end | ||
elseif not task or not task.Run then | ||
Utils.PrintError("Cannot call task as it has no 'Run' method") | ||
return false | ||
end | ||
|
||
self.ran[task] = true | ||
return task:Run(self, ...) | ||
end | ||
|
||
--- Start the task process | ||
-- @tparam string The name of the task (Optional) | ||
-- @tretrn boolean Success in running the task? | ||
function Context:Start(name) | ||
local task | ||
if name then | ||
task = self.tasks[name] | ||
else | ||
task = self.default | ||
name = "<default>" | ||
end | ||
|
||
if not task then | ||
Utils.PrintError("Cannot find a task called '" .. name .. "'") | ||
return false | ||
end | ||
|
||
return task:Run(self) | ||
end | ||
|
||
--- Build a cache of tasks | ||
-- This is used to speed up finding file based tasks | ||
-- @treturn Context The current context | ||
function Context:BuildCache() | ||
local producesCache = {} | ||
local patternMapsCache = {} | ||
local normalMapsCache = {} | ||
|
||
self.producesCache = producesCache | ||
self.patternMapsCache = patternMapsCache | ||
self.normalMapsCache = normalMapsCache | ||
|
||
for name, task in pairs(self.tasks) do | ||
local produces = task.produces | ||
if produces then | ||
for _, file in ipairs(produces) do | ||
local existing = producesCache[file] | ||
if existing then | ||
error(string.format("Both '%s' and '%s' produces '%s'", existing, name, file)) | ||
end | ||
producesCache[file] = name | ||
end | ||
end | ||
|
||
local maps = task.maps | ||
if maps then | ||
for _, pattern in ipairs(maps) do | ||
-- We store two separate caches for each of them | ||
local toMap = (pattern.Type == "Pattern" and patternMapsCache or normalMapsCache) | ||
local match = pattern.To | ||
local existing = toMap[match] | ||
if existing then | ||
error(string.format("Both '%s' and '%s' match '%s'", existing, name, match)) | ||
end | ||
toMap[match] = {Name = name, Pattern = pattern} | ||
end | ||
end | ||
end | ||
|
||
return self | ||
end | ||
|
||
local function Factory(runner) | ||
return setmetatable({ | ||
ran = {}, -- List of task already run | ||
filesProduced = {}, | ||
tasks = runner.tasks, | ||
default = runner.default, | ||
|
||
Traceback = runner.Traceback, | ||
ShowTime = runner.ShowTime, | ||
}, {__index = Context}):BuildCache() | ||
end | ||
|
||
--- @export | ||
return { | ||
Factory = Factory, | ||
Context = Context, | ||
} |
Oops, something went wrong.