2023-09-26 14:44:58 +01:00
|
|
|
local Fs = require("@lune/fs")
|
|
|
|
|
|
|
|
local Darklua = require("cmd")
|
|
|
|
local FsUtils = require("../utils/fs")
|
|
|
|
local Codegen = require("codegen")
|
|
|
|
|
|
|
|
local Bundler = {}
|
2023-10-19 12:02:15 +01:00
|
|
|
export type BundlerExtended = typeof(Bundler.Prototype) & {
|
|
|
|
darkluaPath: string?,
|
|
|
|
darklua: Darklua.Darklua?,
|
|
|
|
config: { contents: string, path: string? },
|
|
|
|
}
|
|
|
|
export type DarkluaRules = Codegen.Rules
|
2023-09-26 14:44:58 +01:00
|
|
|
|
|
|
|
Bundler.Prototype = {}
|
|
|
|
Bundler.Interface = {}
|
|
|
|
Bundler.Type = "Bundler"
|
|
|
|
|
|
|
|
function Bundler.Prototype.Bundle(
|
|
|
|
self: BundlerExtended,
|
|
|
|
sourceKind: "code" | "path",
|
|
|
|
sourceInner: string
|
2023-10-19 12:02:15 +01:00
|
|
|
): { error: string?, processed: string? }
|
2023-09-26 14:44:58 +01:00
|
|
|
self.darklua = Darklua.new(self.darkluaPath)
|
|
|
|
|
|
|
|
if self.darklua:IsOk() then
|
2023-10-19 12:02:15 +01:00
|
|
|
return self.darklua:Process(sourceKind, sourceInner, self.config.path or (function()
|
2023-09-26 14:44:58 +01:00
|
|
|
local _, path = FsUtils.MakeTemp()
|
|
|
|
Fs.writeFile(
|
|
|
|
path,
|
|
|
|
Codegen({
|
|
|
|
generator = self.config.contents.generator,
|
|
|
|
rules = self.config.contents.rules,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return path
|
|
|
|
end)())
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
error = "constructed darklua instance was not OK",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function Bundler.Prototype.ToString(): string
|
|
|
|
return string.format("%s<%s>", Bundler.Type, "{}")
|
|
|
|
end
|
|
|
|
|
|
|
|
function Bundler.Interface.new(darkluaPath: string?, opts: Codegen.Options)
|
|
|
|
return setmetatable({
|
|
|
|
darkluaPath = darkluaPath,
|
|
|
|
darklua = nil,
|
|
|
|
config = {
|
|
|
|
contents = opts,
|
|
|
|
path = nil,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
__index = Bundler.Prototype,
|
|
|
|
__type = Bundler.Type,
|
|
|
|
__tostring = function()
|
|
|
|
return Bundler.Prototype.ToString()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
return Bundler.Interface
|