jecs-nightly/src/sync.luau

116 lines
3.4 KiB
Text

--!strict
local datetime = require("@lune/datetime")
local fs = require("@lune/fs")
local serde = require("@lune/serde")
local stdio = require("@lune/stdio")
local progress_bar = require("./util/progress")
local result = require("./util/result")
local shared = require("./shared")
local types = require("./types")
--- Synchronizes the required files from the jecs main branch.
local function sync(to: string): result.Identity<boolean>
local now = datetime.now()
local begin = os.clock()
local progress = progress_bar
.new()
:withStage("init", "Initializing")
:withStage("fetch", "Fetching latest files")
:withStage("save", "Saving files")
:withStage("version", "Pulling latest jecs version")
:withStage("metadata", "Writing build metadata")
progress:start() -- init
if not fs.metadata(to).exists then
fs.writeDir(to)
end
progress:nextStage() -- fetch
local includes = {
"jecs.luau",
"README.md",
"CHANGELOG.md",
"LICENSE",
".luaurc",
}
local sources = {}
for _, file in includes do
local contents = shared.fetch_raw(file)
if not contents.ok then
progress:stop()
stdio.ewrite(`🔥 Couldn't get the latest source for {file}:\n{contents.err}\n`)
return result(false, `Couldn't get the latest source for {file}.`)
end
sources[file] = contents.val
end
progress:nextStage() -- save
local sources_modified = {}
local any_changed = false
for file, contents in sources do
local res = shared.save_if_diff(`{to}/{file}`, contents)
if res.ok then
any_changed = true
table.insert(sources_modified, file)
end
end
if not any_changed then
progress:stop()
local took = shared.round_to((os.clock() - begin) * 1_000, 2)
print(
`🕛 Finished synchronizing, no changes since latest source {stdio.style("dim")}(took {took}ms){stdio.style(
"reset"
)}`
)
return result(true, false)
end
local project_json = {
name = "jecs-nightly",
tree = {
["$path"] = "jecs.luau",
},
}
local encoded_project_json = serde.encode("json", project_json)
fs.writeFile(`{to}/default.project.json`, encoded_project_json)
progress:nextStage() -- version
local wally_contents = shared.fetch_raw("wally.toml")
if not wally_contents.ok then
progress:stop()
stdio.ewrite(`🔥 Couldn't get the jecs wally manifest:\n{wally_contents.err}\n`)
return result(false, `Couldn't get the jecs wally manifest:\n{wally_contents.err}`)
end
local parsed: types.WallyManifest = serde.decode("toml", wally_contents.val)
progress:nextStage() -- metadata
local version = `{parsed.package.version}-nightly.{shared.iso_date_light(now)}`
local metadata = {
version = version,
modified = sources_modified,
}
local encoded_metadata = serde.encode("toml", metadata)
fs.writeFile(`{to}/build.txt`, encoded_metadata)
progress:stop() -- finish
local took = shared.round_to((os.clock() - begin) * 1_000, 2)
print(`🪨 Finished synchronizing {stdio.style("dim")}(took {took}ms){stdio.style("reset")}`)
print(`Changed files:`)
print(sources_modified)
return result(true, true)
end
return sync