refactor(lib): minor structure improvements for progress bar

* Made bar related conditional operations get constructed by a
  `makeCondBar` which returns a table of functions to conditionally handle
  a progress bar.
* Moved progress bar boolean out of function args for `installTool` into
  global state.
This commit is contained in:
Erica Marigold 2024-12-13 16:34:36 +00:00
parent c69a7417a0
commit f377116271
Signed by: DevComp
GPG key ID: 429EF1C337871656

View file

@ -113,35 +113,24 @@ function runTool(tool: ToolId | pathfs.Path): number
}).code }).code
end end
local function makeCondInc(yes: boolean?) local function makeConditionalBar()
return function() local function makeConditional(fn: (ProgressBar.ProgressBarImpl) -> ())
if yes then return function(bar: ProgressBar.ProgressBarImpl)
bar:nextStage() local _ = _G.interactive and task.spawn(fn, bar)
end end
end end
return {
next = makeConditional(bar.nextStage),
pause = makeConditional(bar.pause),
resume = makeConditional(bar.resume),
}
end end
local function makeCondPause(yes: boolean?) _G.interactive = false
return function() function installTool(tool: ToolId, installPath: pathfs.Path)
if yes then local barFns = makeConditionalBar()
bar:pause() barFns.next(bar)
end
end
end
local function makeCondResume(yes: boolean?)
return function()
if yes then
bar:resume()
end
end
end
function installTool(tool: ToolId, installPath: pathfs.Path, interactive: boolean?)
local next = makeCondInc(interactive)
local pause = makeCondPause(interactive)
local resume = makeCondResume(interactive)
next()
local toolAlias = toolAliasOrDefault(tool) local toolAlias = toolAliasOrDefault(tool)
local client = Github.new( local client = Github.new(
@ -153,7 +142,7 @@ function installTool(tool: ToolId, installPath: pathfs.Path, interactive: boolea
}) :: Option<Github.Config> }) :: Option<Github.Config>
) )
next() barFns.next(bar)
local releases = client:queueTransactions({ "FetchReleases" })[1]:unwrap() :: GithubReleases local releases = client:queueTransactions({ "FetchReleases" })[1]:unwrap() :: GithubReleases
local assets = tool.version:match({ local assets = tool.version:match({
Some = function(version: string) Some = function(version: string)
@ -163,7 +152,7 @@ function installTool(tool: ToolId, installPath: pathfs.Path, interactive: boolea
end end
end end
pause() barFns.pause(bar)
return error(`No release found for version {version}`) return error(`No release found for version {version}`)
end, end,
@ -172,7 +161,7 @@ function installTool(tool: ToolId, installPath: pathfs.Path, interactive: boolea
end, end,
}) })
next() barFns.next(bar)
-- TODO: Use index type fn in solver v2 -- TODO: Use index type fn in solver v2
local matchingAsset: { local matchingAsset: {
name: string, name: string,
@ -199,12 +188,12 @@ function installTool(tool: ToolId, installPath: pathfs.Path, interactive: boolea
local binaryPath: pathfs.Path local binaryPath: pathfs.Path
if matchingAsset == nil then if matchingAsset == nil then
pause() barFns.pause(bar)
warn("Pesde could not find a matching binary for your system") warn("Pesde could not find a matching binary for your system")
warn("Will now attempt to download all binaries and find a matching one") warn("Will now attempt to download all binaries and find a matching one")
resume() barFns.resume(bar)
next() barFns.next(bar)
for _, asset in assets do for _, asset in assets do
local decompressedPath = downloadAndDecompress(asset) local decompressedPath = downloadAndDecompress(asset)
@ -225,12 +214,12 @@ function installTool(tool: ToolId, installPath: pathfs.Path, interactive: boolea
local decompressedPath = downloadAndDecompress(matchingAsset):unwrap() local decompressedPath = downloadAndDecompress(matchingAsset):unwrap()
binaryPath = decompressedPath:join(aliasPath) binaryPath = decompressedPath:join(aliasPath)
if not pathfs.isFile(binaryPath) then if not pathfs.isFile(binaryPath) then
pause() barFns.pause(bar)
error(`No matching binary found in {decompressedPath}`) error(`No matching binary found in {decompressedPath}`)
end end
end end
next() barFns.next(bar)
-- Maintain multiple versions of a tool, and avoid downloading -- Maintain multiple versions of a tool, and avoid downloading
-- the binary for a version again if it's already there -- the binary for a version again if it's already there
local toolDir = Option.from(installPath:parent()):unwrap() local toolDir = Option.from(installPath:parent()):unwrap()
@ -273,7 +262,7 @@ end
type LibExports = { type LibExports = {
runTool: (pathfs.Path | ToolId) -> number, runTool: (pathfs.Path | ToolId) -> number,
installTool: (ToolId, pathfs.Path, boolean?) -> never, installTool: (ToolId, pathfs.Path) -> never,
} }
type LibExportsImpl = typeof(setmetatable( type LibExportsImpl = typeof(setmetatable(
{} :: LibExports, {} :: LibExports,
@ -287,6 +276,7 @@ return setmetatable(
} :: LibExports, } :: LibExports,
{ {
__call = function(lib: LibExportsImpl, tool: string, pesdeRoot: string?): number __call = function(lib: LibExportsImpl, tool: string, pesdeRoot: string?): number
_G.interactive = true
local repo, version = string.match(tool, "([^@]+)@?(.*)") local repo, version = string.match(tool, "([^@]+)@?(.*)")
if repo == nil or version == nil then if repo == nil or version == nil then
stdio.ewrite(`{ERROR_PREFIX} Invalid tool provided\n`) stdio.ewrite(`{ERROR_PREFIX} Invalid tool provided\n`)
@ -339,8 +329,7 @@ return setmetatable(
repo = repo, repo = repo,
version = Option.Some(versionOrDefault), version = Option.Some(versionOrDefault),
} :: ToolId, } :: ToolId,
toolInstallPath, toolInstallPath
true
) )
if not ok then if not ok then