diff --git a/toolchainlib/src/init.luau b/toolchainlib/src/init.luau index 55bb92b..a361fdd 100644 --- a/toolchainlib/src/init.luau +++ b/toolchainlib/src/init.luau @@ -11,7 +11,6 @@ local net = require("@lune/net") local process = require("@lune/process") local stdio = require("@lune/stdio") local serde = require("@lune/serde") -local task = require("@lune/task") local pathfs = require("../lune_packages/pathfs") local dirs = require("../lune_packages/dirs") @@ -116,21 +115,19 @@ end local function makeConditionalBar() local function makeConditional(fn: (ProgressBar.ProgressBarImpl) -> ()) return function(bar: ProgressBar.ProgressBarImpl) - local _ = _G.interactive and task.spawn(fn, bar) + local _ = _G.interactive and fn(bar) end end return { next = makeConditional(bar.nextStage), - pause = makeConditional(bar.pause), - resume = makeConditional(bar.resume), + stop = makeConditional(bar.stop), } end _G.interactive = false -function installTool(tool: ToolId, installPath: pathfs.Path) +function installTool(tool: ToolId, installPath: pathfs.Path): number local barFns = makeConditionalBar() - barFns.next(bar) local toolAlias = toolAliasOrDefault(tool) local client = Github.new( @@ -142,7 +139,7 @@ function installTool(tool: ToolId, installPath: pathfs.Path) }) :: Option ) - barFns.next(bar) + barFns.next(bar) -- fetch local releases = client:queueTransactions({ "FetchReleases" })[1]:unwrap() :: GithubReleases local assets = tool.version:match({ Some = function(version: string) @@ -152,7 +149,7 @@ function installTool(tool: ToolId, installPath: pathfs.Path) end end - barFns.pause(bar) + barFns.stop(bar) return error(`No release found for version {version}`) end, @@ -161,7 +158,7 @@ function installTool(tool: ToolId, installPath: pathfs.Path) end, }) - barFns.next(bar) + barFns.next(bar) -- locate -- TODO: Use index type fn in solver v2 local matchingAsset: { name: string, @@ -186,14 +183,14 @@ function installTool(tool: ToolId, installPath: pathfs.Path) end end + barFns.next(bar) -- download + -- task.delay(0.1, barFns.next, bar) -- download + local binaryPath: pathfs.Path if matchingAsset == nil then - barFns.pause(bar) + stdio.write("\x1b[2K\x1b[0G") warn("Pesde could not find a matching binary for your system") warn("Will now attempt to download all binaries and find a matching one") - barFns.resume(bar) - - barFns.next(bar) for _, asset in assets do local decompressedPath = downloadAndDecompress(asset) @@ -214,12 +211,13 @@ function installTool(tool: ToolId, installPath: pathfs.Path) local decompressedPath = downloadAndDecompress(matchingAsset):unwrap() binaryPath = decompressedPath:join(aliasPath) if not pathfs.isFile(binaryPath) then - barFns.pause(bar) + barFns.stop(bar) error(`No matching binary found in {decompressedPath}`) end end - barFns.next(bar) + barFns.next(bar) -- install + -- Maintain multiple versions of a tool, and avoid downloading -- the binary for a version again if it's already there local toolDir = Option.from(installPath:parent()):unwrap() @@ -250,14 +248,11 @@ function installTool(tool: ToolId, installPath: pathfs.Path) -- ... ]] - -- Stop the progress bar and clean it up from stdout - bar:stop() - task.cancel(bar.thread :: thread) - stdio.write("\x1b[2K\x1b[0G") + barFns.stop(bar) -- NOTE: This is equivalent to `0o755` or `rwxr-xr-x` chmod(installPath, 0b10101010011) - runTool(installPath) + return runTool(installPath) end type LibExports = { @@ -321,7 +316,7 @@ return setmetatable( return lib.runTool(toolInstallPath) end - bar:start() + bar:start() -- init local ok, err = pcall( lib.installTool, { diff --git a/toolchainlib/src/utils/progress.luau b/toolchainlib/src/utils/progress.luau index 764d9df..4f79617 100644 --- a/toolchainlib/src/utils/progress.luau +++ b/toolchainlib/src/utils/progress.luau @@ -52,11 +52,6 @@ function ProgressBar.start(self: ProgressBarImpl): () self.thread = task.spawn(function() while not self.finished do - if self.currentStageIndex == #self.stages then - self:stop() - break - end - for _, spinner in SPINNERS do local stage = self.stages[self.currentStageIndex] stdio.write( @@ -69,21 +64,14 @@ function ProgressBar.start(self: ProgressBarImpl): () task.wait(0.1) end end - - stdio.write("\n") end) end function ProgressBar.stop(self: ProgressBarImpl): () + -- Trigger upvalue, kill thread and clean progress bar remnant self.finished = true -end - -function ProgressBar.pause(self: ProgressBarImpl): () - local _ = self.thread and coroutine.yield(self.thread) -end - -function ProgressBar.resume(self: ProgressBarImpl): () - local _ = self.thread and coroutine.resume(self.thread) + task.cancel(self.thread :: thread) + stdio.write("\x1b[2K\x1b[0G") end function ProgressBar.nextStage(self: ProgressBarImpl): ()