CSSharpTemplate/lune/manager/setup.luau
marked e517b193e5
All checks were successful
Build / Build (push) Successful in 39s
Initial push
2025-04-05 18:27:50 +02:00

69 lines
1.9 KiB
Text

--!strict
local fs = require("@lune/fs")
local common = require("./common")
local progress_bar = require("@pkg/progress")
local result = require("@pkg/result")
local function setup(name: string, force: boolean): result.Identity<nil>
local progress = progress_bar
.new()
:with_stage("init", "Initializing")
:with_stage("prepare", "Cleaning up defaults")
:with_stage("modify", "Replacing default names")
:with_stage("generate", "Generating .sln")
progress:start() -- init
if not fs.metadata(`{common.default_name}.sln`).exists and not force then
progress:stop()
return result(false, "Aborting -- the template is already set up.")
end
progress:next_stage() -- prepare
fs.removeFile(`{common.default_name}.sln`)
fs.removeFile(`PROJECT-SETUP.md`)
progress:next_stage() -- modify
fs.move(`src/{common.default_name}.cs`, `src/{name}.cs`)
fs.move(`src/{common.default_name}.csproj`, `src/{name}.csproj`)
do
local main_src = fs.readFile(`src/{name}.cs`)
local edited_src = string.gsub(main_src, common.default_name, name)
fs.writeFile(`src/{name}.cs`, edited_src)
end
progress:next_stage() -- generate
do
local res = common.spawn(`dotnet new sln --name {name}`)
if not res.ok then
return result(false, `Failed to create new sln:\n{res.err}`)
end
end
do
local res = common.spawn("dotnet sln add src")
if not res.ok then
return result(false, `Failed to add src to sln:\n{res.err}`)
end
end
do
local sln_src = fs.readFile(`{name}.sln`)
local by_line = string.split(sln_src, "\n")
for _ = 1, 3 do
table.remove(by_line, 3)
end
local concated = table.concat(by_line, "\n")
fs.writeFile(`{name}.sln`, concated)
end
progress:stop() -- finish
return result(true, nil)
end
return setup