feat: provide user facing API

This commit is contained in:
Erica Marigold 2023-10-19 04:02:15 -07:00
parent 3ab2569e09
commit 8f45fc05f8
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
13 changed files with 196 additions and 23 deletions

2
.gitignore vendored
View file

@ -3,4 +3,4 @@
.wax-tmp
# Compiled outputs
terracotta.luau
dist/

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "clap"]
path = clap
url = https://github.com/0x5eal/clap.luau.git

View file

@ -51,6 +51,10 @@ local BoxifyOutput = function(msg: string)
end
end
if not fs.isDir("dist/") then
fs.writeDir("dist/")
end
print(`{WAX_PREFIX_FORMAT("info", "[codegen]")} Generating darklua config file at darklua.config.json`)
fs.writeFile(
".darklua.json5",
@ -85,10 +89,15 @@ local loader = task.spawn(function()
end)
task.spawn(function()
local child = spawn(
"lune",
{ "wax", "bundle", "input=default.project.json", "verbose=true", "minify=true", "output=terracotta.luau" }
)
local child = spawn("lune", {
"wax",
"bundle",
"input=default.project.json",
"verbose=true",
"minify=true",
"ci-mode=true",
"output=dist/terracotta.luau",
})
task.cancel(loader)
print("\b \n")

View file

@ -1,7 +1,7 @@
{
"luau-lsp.require.mode": "relativeToFile",
"luau-lsp.require.directoryAliases": {
"@lune/": "~/.lune/.typedefs/0.7.7/"
"@lune/": "~/.lune/.typedefs/0.7.8/"
},
"editor.formatOnSave": true
}

1
clap Submodule

@ -0,0 +1 @@
Subproject commit be0dd886262ad7df5e06edc11daa38a19e98a1dd

10
cmd/init.luau Normal file
View file

@ -0,0 +1,10 @@
--> Placeholder file!
local clap = require("../clap/src/clap")
local Command = clap.Command
local Opt = clap.Opt
Command.new("terracotta", true)
:afterHelp("For more help, refer to the documentation.")
:author("Seal Gang (0x5eal), Erica Marigold (DevComp)")

View file

@ -5,9 +5,12 @@ local FsUtils = require("../utils/fs")
local Codegen = require("codegen")
local Bundler = {}
type BundlerExtended =
typeof(Bundler.Prototype)
& { darkluaPath: string?, darklua: Darklua.Darklua?, config: { contents: string, path: string? } }
export type BundlerExtended = typeof(Bundler.Prototype) & {
darkluaPath: string?,
darklua: Darklua.Darklua?,
config: { contents: string, path: string? },
}
export type DarkluaRules = Codegen.Rules
Bundler.Prototype = {}
Bundler.Interface = {}
@ -17,11 +20,11 @@ function Bundler.Prototype.Bundle(
self: BundlerExtended,
sourceKind: "code" | "path",
sourceInner: string
): { error: string?, bundled: string? }
): { error: string?, processed: string? }
self.darklua = Darklua.new(self.darkluaPath)
if self.darklua:IsOk() then
return self.darklua:Process("code", sourceInner, self.config.path or (function()
return self.darklua:Process(sourceKind, sourceInner, self.config.path or (function()
local _, path = FsUtils.MakeTemp()
Fs.writeFile(
path,

13
darklua/init.luau Normal file
View file

@ -0,0 +1,13 @@
local Bundler = require("bundler")
local Minifier = require("minifier")
export type Bundler = Bundler.BundlerExtended
export type Minifier = Minifier.MinfierExtended
local Darklua = {
bundler = Bundler,
minifier = Minifier,
__cmd = require("cmd"),
}
return Darklua

View file

@ -1,7 +1,7 @@
local Darklua = require("cmd")
local Minifier = {}
type MinfierExtended = typeof(Minifier.Prototype) & { darkluaPath: string?, darklua: Darklua.Darklua? }
export type MinfierExtended = typeof(Minifier.Prototype) & { darkluaPath: string?, darklua: Darklua.Darklua? }
Minifier.Prototype = {}
Minifier.Interface = {}

View file

@ -1 +1,46 @@
{"name":"terracotta","className":"Folder","filePaths":["default.project.json"],"children":[{"name":"darklua","className":"Folder","children":[{"name":"bundler","className":"ModuleScript","filePaths":["darklua/bundler.luau"]},{"name":"cmd","className":"ModuleScript","filePaths":["darklua/cmd.luau"]},{"name":"codegen","className":"ModuleScript","filePaths":["darklua/codegen.luau"]},{"name":"minifier","className":"ModuleScript","filePaths":["darklua/minifier.luau"]}]},{"name":"terracotta","className":"Folder"}]}
{
"name": "terracotta",
"className": "Folder",
"filePaths": ["default.project.json"],
"children": [
{
"name": "darklua",
"className": "ModuleScript",
"filePaths": ["darklua/init.luau"],
"children": [
{
"name": "bundler",
"className": "ModuleScript",
"filePaths": ["darklua/bundler.luau"]
},
{
"name": "cmd",
"className": "ModuleScript",
"filePaths": ["darklua/cmd.luau"]
},
{
"name": "codegen",
"className": "ModuleScript",
"filePaths": ["darklua/codegen.luau"]
},
{
"name": "minifier",
"className": "ModuleScript",
"filePaths": ["darklua/minifier.luau"]
}
]
},
{
"name": "terracotta",
"className": "ModuleScript",
"filePaths": ["src/init.luau"],
"children": [
{
"name": "terracotta",
"className": "ModuleScript",
"filePaths": ["src/terracotta.luau"]
}
]
}
]
}

19
src/init.luau Normal file
View file

@ -0,0 +1,19 @@
local Fs = require("@lune/fs")
local Terracotta = require("terracotta")
Terracotta.Build({
entryPoints = { "utils/fs.luau", "darklua/cmd.luau" },
bundle = true,
})
-- OR --
local terracotta = Terracotta.new()
local outputs = terracotta:Build({
entryPoints = { "utils/fs.luau", "darklua/cmd.luau" },
bundle = true,
})
Fs.writeFile("darklua.luau", outputs["darklua/cmd.luau"])

70
src/terracotta.luau Normal file
View file

@ -0,0 +1,70 @@
local Darklua = require("../darklua")
local Codegen = require("../darklua/codegen")
local Terracotta = {}
Terracotta.Interface = {}
Terracotta.Prototype = {}
type DarkluaRules = Codegen.Rules
export type Terracotta = typeof(Terracotta) & {
bundler: Darklua.Bundler,
minifier: Darklua.Minifier,
}
function Terracotta.Prototype.Build(
self: Terracotta?,
opts: {
entryPoints: { string },
bundle: boolean?,
outFile: string?,
rules: { DarkluaRules }?,
}
)
local bundler: Darklua.Bundler
if self ~= nil and self.bundler ~= nil then
bundler = self.bundler
else
bundler = Darklua.bundler.new(nil, {
generator = "dense",
sources = opts.entryPoints,
outFile = opts.outFile,
requireMode = (opts.bundle and "path") or nil,
rules = opts.rules or {
"remove_comments",
"rename_variables",
"remove_spaces",
},
})
end
local builtOuts = {}
for _, path in opts.entryPoints do
local out = bundler:Bundle("path", path)
if out.error ~= nil then
error(out.error)
end
builtOuts[path] = out.processed
end
return builtOuts
end
function Terracotta.Interface.new()
return setmetatable({
minifier = nil,
bundler = nil,
}, {
__index = Terracotta.Prototype,
})
end
function Terracotta.Interface.Build(opts: { bundle: boolean?, entryPoints: { string }, outFile: string? })
return Terracotta.Prototype.Build(nil, opts)
end
return Terracotta.Interface