dymokomi/luced-3d
Source of release 0.11.8
0.11.8 / src / script_build.luc
## A Script node's code as a Luce Base tool package that luc builds: the node's
## code byte for byte as src/script.lucb (so the compiler's lines are the
## editor's), a generated main that serves cooks (luce-geocore's script.serve),
## and a package.prisma naming luce-geocore, luce-std and the node's packages.
## Each build lives in ~/.luce/scripts/build/<key>, the key covering the code,
## the packages and their lock, the toolchain, the target and luce-geocore, so
## an unchanged script never builds twice. Which packages it builds against is
## script_packages.luc's: the editor's own, so none comes in twice.
from luce_std import files, paths, process
from luce_std.process import Command
from luce_std.wake import Waker
from luce_geocore.script_host import geocore_requirement
from script_support import text_key, keep_tool
from script_packages import Dependency, dependencies, duplicate_hint
import platform
## Where Script builds and runs live: ~/.luce/scripts (LUC_HOME's when set).
pub func scripts_home() -> str!:
if let home = process.variable("LUC_HOME"):
if home != "":
return paths.join(home, "scripts")
return paths.join(paths.join(files.home_directory(), ".luce"), "scripts")
## A tool from the Luce toolchain: luc or luce-base, from the environment
## (LUC, LUCE_BASE), else the first on PATH (the toolchain the user runs),
## else where the installer puts it.
pub func tool(name: str) -> str!:
let variable = "LUC" if name == "luc" else "LUCE_BASE"
if let given = process.variable(variable):
if given != "":
return given
let file = name + (".exe" if platform.windows else "")
let places: list[str] = []
if let search = process.variable("PATH"):
for place in search.split(";" if platform.windows else ":"):
if place != "":
places.append(place)
let home = files.home_directory()
places.append(paths.join(paths.join(paths.join(paths.join(home, "AppData"), "Local"), "luce"), "bin"))
places.append(paths.join(paths.join(home, ".local"), "bin"))
places.append(paths.join(paths.join(paths.join(home, ".local"), "luce"), "bin"))
for place in places:
let candidate = paths.join(place, file)
let kind = files.metadata(candidate).kind catch failure:
continue
if kind == files.FileKind.regular:
return candidate
return name
## A build of one script. The tool is kept under its key (`folder`); it is
## built in a working project (`work`) a node keeps for one set of
## dependencies, so an edit rebuilds incrementally (luce-base reuses the
## unchanged pieces in the project's build folder) and only the tool is copied
## out under the new key.
pub class BuildPlan:
pub let key: str
pub let folder: str
pub let work: str
pub let program: str
pub let manifest: str
pub let code: str
pub let lock: str
## What a build failing on a package found twice adds to its message.
pub var hint: str = ""
pub func init(self, key: str, folder: str, work: str, manifest: str, code: str, lock: str) -> unit!:
self.key = key
self.folder = folder
self.work = work
self.program = paths.join(folder, executable())
self.manifest = manifest
self.code = code
self.lock = lock
## Whether every dependency comes from the registry (so the key alone says
## the build is current: checkouts can change under it).
pub func pinned(self) -> bool:
return not self.manifest.contains("str path =")
## Write the working project (its build folder kept).
pub func write(self) -> unit!:
files.ensure_directory(paths.join(self.work, "src"))
write_changed(paths.join(self.work, "package.prisma"), self.manifest)
write_changed(paths.join(paths.join(self.work, "src"), "script.lucb"), self.code)
write_changed(paths.join(paths.join(self.work, "src"), "main.lucb"), main_text())
if self.lock != "":
write_changed(paths.join(self.work, "luc.lock"), self.lock)
## Start `luc build --release` in the working project.
## Start luc building it; `waker` is woken as its output arrives and once
## it ends.
pub func start(self, waker: Waker? = none) -> Command!:
self.write()
return Command(tool("luc"), ["build", "--release"], directory = self.work, output_limit = 4194304, waker = waker)
## Keep the tool the working project built under the key.
pub func keep(self) -> unit!:
files.ensure_directory(self.folder)
# luc names the tool after its package.
let built = paths.join(paths.join(self.work, "build"), "luced-script.exe" if platform.windows else "luced-script")
keep_tool(built, self.program)
## The tool's file name on this system.
func executable() -> str:
return "script.exe" if platform.windows else "script"
## The plan for `code` with `packages` and `lock` for node `node`, in a
## project in `folder`; `versions` names the compiler versions and `editor` is
## the editor's packages (script_packages.editor_packages). A `checking`
## plan's project is the package set's check project (script_check.luc).
pub func plan(node: int, code: str, packages: str, lock: str, folder: str, versions: str, editor: map[str, str], checking: bool = false) -> BuildPlan!:
let needed = dependencies(packages, folder, editor)
let manifest = manifest_text(needed)
let target = "windows" if platform.windows else ("macos" if platform.macos else "linux")
let setting = text_key(f"{manifest}\n--\n{lock}\n--\n{versions}\n--\n{target}\n--\n{geocore_requirement}")
let key = text_key(f"{code}\n--\n{setting}")
let home = scripts_home()
let work = paths.join(paths.join(home, "check"), setting) if checking else paths.join(paths.join(home, "work"), f"{setting}-{node}")
let made = BuildPlan(key, paths.join(paths.join(home, "build"), key), work, manifest, code, lock)
made.hint = duplicate_hint(needed)
return made
func manifest_text(needed: list[Dependency]) -> str:
var text = "#prisma 4.0\ndef package \"luced-script\" {\n str owner = \"luced\"\n str version = \"0.1.0\"\n str kind = \"tool\"\n str language = \"luce-base\"\n str description = \"A luced-3d Script node.\"\n str entry = \"src/main.lucb\"\n"
for d in needed:
text = text + d.block()
return text + "}\n"
## The generated main: the node's cook, served.
func main_text() -> str:
return "## Generated by luced-3d: a Script node's tool. The node's code is script.lucb.\nfrom luce_geocore.script import serve\nimport script\n\npub func main(arguments: str[]) -> i32!:\n return try serve(arguments, script.cook)\n"
## Write `text` to `path` unless it already holds it (an unchanged file keeps
## its time, so luc's incremental build sees no change).
func write_changed(path: str, text: str) -> unit!:
let old = files.read_text(path) catch failure:
recover ""
if old != text:
files.write_text(path, text)
## The toolchain's versions, for the build key ("luc 0.24.0 / luce-base 0.39.0").
pub func toolchain() -> str!:
var text = ""
for name in ["luc", "luce-base"]:
let command = Command(tool(name), ["--version"], output_limit = 65536)
_ = command.wait() catch failure:
recover 0
text = text + command.output().trim() + " / "
command.close()
return text
## A build's compiler output as the node's failure: the first error in the
## node's code as "line L, column C: message" (the editor underlines it),
## else the first error line as it is, with `hint` when a package came in
## twice.
pub func build_failure(output: str, hint: str = "") -> str:
var first = ""
for line in output.split("\n"):
let trimmed = line.trim()
if trimmed == "" or trimmed.starts_with("compiling"):
continue
if let place = code_place(trimmed, "script.lucb:"):
return place
if first == "" and (trimmed.contains("error") or trimmed.starts_with("luce-base:") or trimmed.starts_with("luc:")):
first = trimmed
let twice = hint if output.contains("same canonical module name") else ""
return "the script did not build: " + (first if first != "" else output.trim()) + twice
## "line L, column C: message" for a compiler or trap line naming
## `file:L:C: message`, else none.
pub func code_place(line: str, file: str) -> str?:
let at = line.index_of(file) else return none
let rest = line[at + file.byte_count..]
let parts = rest.split(":")
if parts.length < 3:
return none
let row = int(parts[0]) catch failure:
return none
let column = int(parts[1]) catch failure:
return none
let message = rest[parts[0].byte_count + parts[1].byte_count + 2..].trim()
return f"line {row}, column {column}: {message}"