From 22552c0f80514e39ccc2c7564ee26c58ed21e861 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sun, 8 Dec 2024 11:09:53 +0000 Subject: [PATCH] refactor: restructure to use script package system General restructuring ===================== * Restructures to workspace with script packages for each tool choice. * Include build script to generate package for every supported tool choice in library into the `.pesde/` directory. * Remove testing .lune scripts for Rojo. Manifest changes ================ * Exclude `.spec.luau` test files for library package. * Include .pesde directory for library package. * Add pathfs dev-dependency. * Include metadata for build script package codegen. * Use new git URL for index repo. * Setup workspace including top level itself & tool choice script packages. These changes require an unstable version of pesde: https://github.com/pesde-pkg/pesde/commit/2b2d280fe0faf53248b223980ff54c474d41f57d. --- .gitignore | 7 +- .lune/build.luau | 164 ++++++++++++++++++++++++ .lune/lib/manifest.luau | 71 ++++++++++ .lune/roblox_sync_config_generator.luau | 19 --- .lune/sourcemap_generator.luau | 21 --- .pesde/.gitkeep | 0 .vscode/settings.json | 3 +- pesde.lock | 26 +++- pesde.toml | 30 +++-- src/init.luau | 2 +- 10 files changed, 291 insertions(+), 52 deletions(-) create mode 100644 .lune/build.luau create mode 100644 .lune/lib/manifest.luau delete mode 100644 .lune/roblox_sync_config_generator.luau delete mode 100644 .lune/sourcemap_generator.luau create mode 100644 .pesde/.gitkeep diff --git a/.gitignore b/.gitignore index a6f599a..e57fe2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ +# Pesde packages *_packages/ -pesde.lock \ No newline at end of file +pesde.lock + +# Generated scripts +.pesde/* +!.pesde/.gitkeep diff --git a/.lune/build.luau b/.lune/build.luau new file mode 100644 index 0000000..94652b6 --- /dev/null +++ b/.lune/build.luau @@ -0,0 +1,164 @@ +--> Generates sync config and sourcemap scripts for supported tools + +local process = require("@lune/process") +local serde = require("@lune/serde") +local stdio = require("@lune/stdio") +local task = require("@lune/task") + +local lib = require("../src") +local manifest = require("./lib/manifest") +local pathfs = require("../lune_packages/pathfs") + +type ToolChoice = "rojo" +type ManifestExt = { + scripts: { + [ToolChoice]: { + version: string, + tool_dependencies: { { [string]: manifest.DependencySpecifier } }, + }, + }, +} + +local SCRIPTS_DIR = pathfs.getAbsolutePathOf(pathfs.Path.from(".pesde")) +local MANIFEST = manifest(nil, (nil :: any) :: { meta: ManifestExt }) +local SCRIPTS = { + syncConfigGenerator = [[local process = require("@lune/process") + +local args = table.clone(process.args) +local ok, _ = +require("./lune_packages/core").generators.%s.syncConfig(table.remove(args, 1), args, { writeToFile = true }) + +process.exit(tonumber(ok))]], + + sourcemapGenerator = [[local process = require("@lune/process") + +return process.exit( + tonumber(require("./lune_packages/core").generators.%s.sourcemap(process.args[1])) +)]], +} + +local function logPrefix(type: "error" | "info") + local statusColor: stdio.Color = if type == "error" + then "red" + elseif type == "info" then "green" + else error(`Invalid type: {type}`) + + return `pesde::{stdio.style("bold")}{stdio.color(statusColor)}{type}{stdio.color("reset")}` +end + +local INFO_PREFIX = `[{logPrefix("info")}]` +local _ERROR_PREFIX = `[{logPrefix("error")}]` + +local function installDeps(): number + local STDOUT_LINE_PREFIX = `[pesde::{logPrefix("info")}]` + local PESDE_ERROR_PREFIX = `[pesde::{logPrefix("error")}]` + local SPINNER_STATES = { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' } + + stdio.write(`{STDOUT_LINE_PREFIX} Installing dependencies with pesde`) + + -- Spawn our thread to display the spinner + local spinnerThread = task.spawn(function() + -- Hide the cursor + stdio.write("\x1b[?25l") + + -- Display the spinner + while true do + for _, state in SPINNER_STATES do + stdio.write(` {state}`) + stdio.write( + -- Moves the cursor back 1 spot and clears everything after + "\x1b[2D\x1b[0K" + ) + end + end + end) + + -- `process_spawn` is an async rust function, so tokio spawns a hardware + -- thread and mlua will yield the current thread, allowing for `spinnerThread` + -- to continue executing + local child = process.spawn("pesde", { "install" }, { + stdio = "default", + }) + + -- If we reach this point, that means mlua resumed the current thread and + -- `process_spawn` returned or errored. We can now close `spinnerThread` and + -- clean up + + task.cancel(spinnerThread) + stdio.write( + -- Clear the current line, move cursor back to its beginning and + -- show it + "\x1b[2K\x1b[1G\x1b[?25h" + ) + + if not child.ok then + stdio.ewrite(`{PESDE_ERROR_PREFIX} Failed to install dependencies with pesde, error:\n`) + stdio.ewrite(child.stderr) + end + + stdio.write(`{STDOUT_LINE_PREFIX} Installed dependencies with pesde successfully\n`) + + return child.code +end + +for tool, generators in lib.generators do + local startTime = os.clock() + + -- For each tool, we generate a respective manifests and scripts + local toolChoice = tool :: ToolChoice + local toolScriptsDir = SCRIPTS_DIR:join(toolChoice) + local toolMeta = MANIFEST.meta.scripts[toolChoice] + + if not pathfs.isDir(toolScriptsDir) then + pathfs.writeDir(toolScriptsDir) + end + + -- Define the manifest for the tool + local toolManifest: manifest.PesdeManifest = { + name = `pesde/scripts_{toolChoice}`, + version = toolMeta.version, + description = `Scripts for {toolChoice}-based Roblox projects`, + authors = { + "dai (https://www.daimond113.com/)", + "Erica Marigold ", + }, + repository = `https://github.com/pesde-pkg/scripts/tree/master/.pesde/{toolChoice}`, + license = "MIT", + + target = { + environment = "lune", + scripts = { + roblox_sync_config_generator = "roblox_sync_config_generator.luau", + sourcemap_generator = "sourcemap_generator.luau", + }, + }, + + peer_dependencies = toolMeta.tool_dependencies, + dependencies = { + core = { workspace = "pesde/scripts_core", version = "^" }, + }, + + indices = { + default = "https://github.com/pesde-pkg/index", + }, + } + + -- Format the scripts for the tool + local syncConfigGeneratorScript, sourcemapGeneratorScript = + string.format(SCRIPTS.syncConfigGenerator, toolChoice), string.format(SCRIPTS.sourcemapGenerator, toolChoice) + + -- Finally, write all the generated files + pathfs.writeFile(toolScriptsDir:join("pesde.toml"), serde.encode("toml", toolManifest, true)) + pathfs.writeFile(toolScriptsDir:join("roblox_sync_config_generator.luau"), syncConfigGeneratorScript) + pathfs.writeFile(toolScriptsDir:join("sourcemap_generator.luau"), sourcemapGeneratorScript) + + stdio.write( + `{INFO_PREFIX} Generated script project for tool {toolChoice} [{stdio.style("dim")}{string.format( + "%.2fs", + os.clock() - startTime + )}{stdio.style("reset")}]\n` + ) + + -- Now we install the dependencies for the newly created projects + process.exit(installDeps()) +end diff --git a/.lune/lib/manifest.luau b/.lune/lib/manifest.luau new file mode 100644 index 0000000..b2f1ede --- /dev/null +++ b/.lune/lib/manifest.luau @@ -0,0 +1,71 @@ +local fs = require("@lune/fs") +local serde = require("@lune/serde") + +export type SPDXLicense = + "MIT" + | "Apache-2.0" + | "BSD-2-Clause" + | "BSD-3-Clause" + | "GPL-2.0" + | "GPL-3.0" + | "LGPL-2.1" + | "LGPL-3.0" + | "MPL-2.0" + | "ISC" + | "Unlicense" + | "WTFPL" + | "Zlib" + | "CC0-1.0" + | "CC-BY-4.0" + | "CC-BY-SA-4.0" + | "BSL-1.0" + | "EPL-2.0" + | "AGPL-3.0" + +export type DependencySpecifier = (( + { name: string, version: string, index: string? } + | { workspace: string, version: string } + | { repo: string, rev: string, path: string? } +) & { + target: string?, +}) | { wally: string, version: string, index: string? } + +export type PackageTarget = { + environment: "luau" | "lune" | "roblox" | "roblox_server", + lib: string, +} | ({ environment: "luau" | "lune" } & ({ + bin: string, +} | { + scripts: { [string]: string }, +})) + +export type PesdeManifest = { + name: string, + version: string, + description: string?, + license: SPDXLicense?, + authors: { string }?, + repository: string?, + private: boolean?, + includes: { string }?, + pesde_version: string?, + workspace_members: { string }?, + target: PackageTarget, + build_files: { string }?, + scripts: { [string]: string }?, + indices: { [string]: string }, + wally_indices: { [string]: string }?, + overrides: { [string]: DependencySpecifier }?, + patches: { [string]: { [string]: string } }?, + place: { [string]: string }?, + dependencies: { [string]: DependencySpecifier }?, + peer_dependencies: { [string]: DependencySpecifier }?, + dev_dependencies: { [string]: DependencySpecifier }?, +} & T + +return function(path: string?, _phantom: T): PesdeManifest + local manifestContents = fs.readFile(path or "pesde.toml") + local decoded = serde.decode("toml", manifestContents) + + return decoded :: PesdeManifest +end diff --git a/.lune/roblox_sync_config_generator.luau b/.lune/roblox_sync_config_generator.luau deleted file mode 100644 index 13d1fca..0000000 --- a/.lune/roblox_sync_config_generator.luau +++ /dev/null @@ -1,19 +0,0 @@ ---> Generates a Rojo sync config from a list of input files - -local function enter(fn: (args: { string }) -> number?): never - local process = require("@lune/process") - local stdio = require("@lune/stdio") - - local startTime = os.clock() - local exitCode = fn(table.clone(process.args)) - - stdio.write(`done in {stdio.style("dim")}{string.format("%.2fs", os.clock() - startTime)}{stdio.style("reset")}!\n`) - - return process.exit(exitCode) -end - -return enter(function(args: { string }): number? - local ok, _ = require("../src").generators.rojo.syncConfig(table.remove(args, 1), args, { writeToFile = true }) - - return tonumber(ok) -end) diff --git a/.lune/sourcemap_generator.luau b/.lune/sourcemap_generator.luau deleted file mode 100644 index e98a3ef..0000000 --- a/.lune/sourcemap_generator.luau +++ /dev/null @@ -1,21 +0,0 @@ ---> Generates a Rojo sourcemap for a provided project directory - -local function enter(fn: (args: { string }) -> number?): never - local process = require("@lune/process") - local stdio = require("@lune/stdio") - - local startTime = os.clock() - local exitCode = fn(table.clone(process.args)) - - stdio.ewrite( - `\ndone in {stdio.style("dim")}{string.format("%.2fs", os.clock() - startTime)}{stdio.style("reset")}!\n` - ) - - return process.exit(exitCode) -end - -return enter(function(args: { string }): number? - return tonumber(require("../src").generators.rojo.sourcemap( - args[1] - )) -end) \ No newline at end of file diff --git a/.pesde/.gitkeep b/.pesde/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/.vscode/settings.json b/.vscode/settings.json index 17eb63b..afb4651 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,6 @@ "luau-lsp.require.mode": "relativeToFile", "luau-lsp.require.directoryAliases": { "@lune/": "~/.lune/.typedefs/0.8.9/" - } + }, + "stylua.targetReleaseVersion": "latest" } \ No newline at end of file diff --git a/pesde.lock b/pesde.lock index 29244a6..d9a7edf 100644 --- a/pesde.lock +++ b/pesde.lock @@ -1,7 +1,13 @@ -name = "pesde/scripts" +name = "pesde/scripts_core" version = "0.1.0" target = "lune" +[workspace."pesde/scripts_core"] +lune = "" + +[workspace."pesde/scripts_rojo"] +lune = ".pesde/rojo" + [graph."itsfrank/frktest"."0.0.2 lune"] direct = ["frktest", { name = "itsfrank/frktest", version = "^0.0.2" }, "dev"] resolved_ty = "dev" @@ -19,3 +25,21 @@ index_url = "https://github.com/daimond113/pesde-index" [graph."itsfrank/frktest"."0.0.2 lune".pkg_ref.target] environment = "lune" lib = "src/_pesde_init.luau" + +[graph."jiwonz/pathfs"."0.1.0 lune"] +direct = ["pathfs", { name = "jiwonz/pathfs", version = "^0.1.0" }, "dev"] +resolved_ty = "dev" + +[graph."jiwonz/pathfs"."0.1.0 lune".target] +environment = "lune" +lib = "init.luau" + +[graph."jiwonz/pathfs"."0.1.0 lune".pkg_ref] +ref_ty = "pesde" +name = "jiwonz/pathfs" +version = "0.1.0" +index_url = "https://github.com/daimond113/pesde-index" + +[graph."jiwonz/pathfs"."0.1.0 lune".pkg_ref.target] +environment = "lune" +lib = "init.luau" diff --git a/pesde.toml b/pesde.toml index 77d667c..a4daa34 100644 --- a/pesde.toml +++ b/pesde.toml @@ -1,21 +1,35 @@ -name = "pesde/scripts" +name = "pesde/scripts_core" version = "0.1.0" pesde_version = "0.5.0-rc.14" description = "Scripts and other utilities for use with pesde" -authors = ["dai (https://www.daimond113.com/)", "Erica Marigold "] +authors = [ + "dai (https://www.daimond113.com/)", + "Erica Marigold ", +] repository = "https://github.com/pesde-pkg/scripts" license = "MIT" -includes = ["src/**/*.luau", "pesde.toml", "README.md", "LICENSE.md"] +includes = [ + "src/**/*.luau", + "!src/**/*.spec.luau", + ".pesde", + "pesde.toml", + "README.md", + "LICENSE.md", +] -[meta] -scripts_root = ".lune" +workspace_members = [".", ".pesde/rojo"] + +[meta.scripts.rojo] +version = "0.1.0" +tool_dependencies = { rojo = { name = "pesde/rojo", version = "^7.4.4" } } [target] environment = "lune" lib = "src/init.luau" -[indices] -default = "https://github.com/daimond113/pesde-index" - [dev_dependencies] frktest = { name = "itsfrank/frktest", version = "^0.0.2" } +pathfs = { name = "jiwonz/pathfs", version = "^0.1.0" } + +[indices] +default = "https://github.com/pesde-pkg/index" diff --git a/src/init.luau b/src/init.luau index cf48aa9..af8ea3f 100644 --- a/src/init.luau +++ b/src/init.luau @@ -5,4 +5,4 @@ return { syncConfig = require("./generators/rojo/sync_config"), }, }, -} +} \ No newline at end of file