mirror of
https://github.com/pesde-pkg/tooling.git
synced 2025-01-10 08:19:10 +00:00
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:
parent
c69a7417a0
commit
f377116271
1 changed files with 25 additions and 36 deletions
|
@ -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
|
end
|
||||||
|
|
||||||
local function makeCondPause(yes: boolean?)
|
return {
|
||||||
return function()
|
next = makeConditional(bar.nextStage),
|
||||||
if yes then
|
pause = makeConditional(bar.pause),
|
||||||
bar:pause()
|
resume = makeConditional(bar.resume),
|
||||||
end
|
}
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function makeCondResume(yes: boolean?)
|
_G.interactive = false
|
||||||
return function()
|
function installTool(tool: ToolId, installPath: pathfs.Path)
|
||||||
if yes then
|
local barFns = makeConditionalBar()
|
||||||
bar:resume()
|
barFns.next(bar)
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue