diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/plugin_manager/init.lua | 84 | ||||
-rw-r--r-- | plugins/plugin_manager/plugin_view.lua | 42 |
2 files changed, 79 insertions, 47 deletions
diff --git a/plugins/plugin_manager/init.lua b/plugins/plugin_manager/init.lua index 7a9a494..fe4cba7 100644 --- a/plugins/plugin_manager/init.lua +++ b/plugins/plugin_manager/init.lua @@ -32,7 +32,7 @@ config.plugins.plugin_manager = common.merge({ package.path = package.path .. ";" .. USERDIR .. "/libraries/?.lua" .. ";" .. USERDIR .. "/libraries/?/init.lua" .. ";" .. DATADIR .. "/libraries/?.lua" .. ";" .. DATADIR .. "/libraries/?/init.lua" if not config.plugins.plugin_manager.lpm_binary_path then - local paths = { + local paths = { DATADIR .. PATHSEP .. "plugins" .. PATHSEP .. "plugin_manager" .. PATHSEP .. config.plugins.plugin_manager.lpm_binary_name, USERDIR .. PATHSEP .. "plugins" .. PATHSEP .. "plugin_manager" .. PATHSEP .. config.plugins.plugin_manager.lpm_binary_name, DATADIR .. PATHSEP .. "plugins" .. PATHSEP .. "plugin_manager" .. PATHSEP .. "lpm" .. binary_extension, @@ -46,9 +46,9 @@ if not config.plugins.plugin_manager.lpm_binary_path then s = e + 1 end for i, path in ipairs(paths) do - if system.get_file_info(path) then - config.plugins.plugin_manager.lpm_binary_path = path - break + if system.get_file_info(path) then + config.plugins.plugin_manager.lpm_binary_path = path + break end end end @@ -67,16 +67,27 @@ local function join(joiner, t) local s = "" for i,v in ipairs(t) do if i > 1 the local running_processes = {} -local function run(cmd) +local function extract_progress(chunk) + local newline = chunk:find("\n") + if not newline then return nil, chunk end + if #chunk == newline then + if chunk:find("^{\"progress\"") then return chunk, "" end + return nil, chunk + end + return chunk:sub(1, newline - 1), chunk:sub(newline + 1) +end + +local function run(cmd, progress) table.insert(cmd, 1, config.plugins.plugin_manager.lpm_binary_path) table.insert(cmd, "--json") - table.insert(cmd, "--mod-version=" .. MOD_VERSION) + table.insert(cmd, "--mod-version=" .. MOD_VERSION_MAJOR) table.insert(cmd, "--quiet") + table.insert(cmd, "--progress") table.insert(cmd, "--userdir=" .. USERDIR) table.insert(cmd, "--datadir=" .. DATADIR) table.insert(cmd, "--binary=" .. EXEFILE) table.insert(cmd, "--assume-yes") - if config.plugins.plugin_manager.ssl_certs then table.insert(cmd, "--ssl_certs") table.insert(cmd, config.plugins.plugin_manager.ssl_certs) end + if config.plugins.plugin_manager.ssl_certs then table.insert(cmd, "--ssl_certs") table.insert(cmd, config.plugins.plugin_manager.ssl_certs) end if config.plugins.plugin_manager.force then table.insert(cmd, "--force") end local proc = process.start(cmd) if config.plugins.plugin_manager.debug then for i, v in ipairs(cmd) do io.stdout:write((i > 1 and " " or "") .. v) end io.stdout:write("\n") io.stdout:flush() end @@ -84,7 +95,7 @@ local function run(cmd) table.insert(running_processes, { proc, promise, "" }) if #running_processes == 1 then core.add_thread(function() - while #running_processes > 0 do + while #running_processes > 0 do local still_running_processes = {} local has_chunk = false local i = 1 @@ -95,8 +106,15 @@ local function run(cmd) local chunk = v[1]:read_stdout(2048) if config.plugins.plugin_manager.debug and chunk ~= nil then io.stdout:write(chunk) io.stdout:flush() end if chunk and v[1]:running() and #chunk == 0 then break end - if chunk ~= nil and #chunk > 0 then - v[3] = v[3] .. chunk + if chunk ~= nil and #chunk > 0 then + v[3] = v[3] .. chunk + local progress_line + progress_line, v[3] = extract_progress(v[3]) + if progress and progress_line then + print(chunk) + progress_line = json.decode(progress_line) + progress(progress_line.progress) + end has_chunk = true else still_running = false @@ -116,7 +134,7 @@ local function run(cmd) i = i + 1 end running_processes = still_running_processes - coroutine.yield(has_chunk and 0.001 or 0.1) + coroutine.yield(has_chunk and 0.001 or 0.05) end end) end @@ -124,9 +142,9 @@ local function run(cmd) end -function PluginManager:refresh() +function PluginManager:refresh(progress) local prom = Promise.new() - run({ "list" }):done(function(addons) + run({ "list" }, progress):done(function(addons) self.addons = json.decode(addons)["addons"] table.sort(self.addons, function(a,b) return a.id < b.id end) self.valid_addons = {} @@ -144,14 +162,14 @@ function PluginManager:refresh() self.repositories = json.decode(repositories)["repositories"] end) end) - return prom + return prom end function PluginManager:get_addons() local prom = Promise.new() - if self.addons then - prom:resolve(self.addons) + if self.addons then + prom:resolve(self.addons) else self:refresh():done(function() prom:resolve(self.addons) @@ -189,7 +207,7 @@ function PluginManager:get_addon(name_and_version) end local match = false for i, addon in ipairs(PluginManager.addons) do - if not addon.mod_version or tostring(addon.mod_version) == tostring(MOD_VERSION) and (addon.version == version or version == nil) then + if not addon.mod_version or tostring(addon.mod_version) == tostring(MOD_VERSION_MAJOR) and (addon.version == version or version == nil) then promise:resolve(addon) match = true break @@ -204,20 +222,20 @@ PluginManager.promise = Promise PluginManager.view = require "plugins.plugin_manager.plugin_view" command.add(nil, { - ["plugin-manager:install"] = function() + ["plugin-manager:install"] = function() PluginManager:get_addons() - core.command_view:enter("Enter plugin name", - function(name) + core.command_view:enter("Enter plugin name", + function(name) PluginManager:get_addon(name):done(function(addon) core.log("Attempting to install plugin " .. name .. "...") PluginManager:install(addon):done(function() core.log("Successfully installed plugin " .. addon.id .. ".") - end) + end) end):fail(function() core.error("Unknown plugin " .. name .. ".") end) - end, - function(text) + end, + function(text) local items = {} if not PluginManager.addons then return end for i, addon in ipairs(PluginManager.addons) do @@ -229,20 +247,20 @@ command.add(nil, { end ) end, - ["plugin-manager:uninstall"] = function() + ["plugin-manager:uninstall"] = function() PluginManager:get_addons() core.command_view:enter("Enter plugin name", - function(name) + function(name) PluginManager:get_addon(name):done(function(addon) core.log("Attempting to uninstall plugin " .. addon.id .. "...") PluginManager:install(addon):done(function() core.log("Successfully uninstalled plugin " .. addon.id .. ".") - end) + end) end):fail(function() core.error("Unknown plugin " .. name .. ".") end) - end, - function(text) + end, + function(text) local items = {} if not PluginManager.addons then return end for i, addon in ipairs(PluginManager.addons) do @@ -256,7 +274,7 @@ command.add(nil, { end, ["plugin-manager:add-repository"] = function() core.command_view:enter("Enter repository url", - function(url) + function(url) PluginManager:add(url):done(function() core.log("Successfully added repository " .. url .. ".") end) @@ -266,19 +284,19 @@ command.add(nil, { ["plugin-manager:remove-repository"] = function() PluginManager:get_plugins() core.command_view:enter("Enter repository url", - function(url) + function(url) PluginManager:remove(url):done(function() core.log("Successfully removed repository " .. url .. ".") end) - end, - function(text) + end, + function(text) local items = {} if PluginManager.repositories then for i,v in ipairs(PluginManager.repositories) do table.insert(items, v.remote .. ":" .. (v.commit or v.branch)) end end - return common.fuzzy_match(items, text) + return common.fuzzy_match(items, text) end ) end, diff --git a/plugins/plugin_manager/plugin_view.lua b/plugins/plugin_manager/plugin_view.lua index a7d5ec7..aa46ea1 100644 --- a/plugins/plugin_manager/plugin_view.lua +++ b/plugins/plugin_manager/plugin_view.lua @@ -32,6 +32,7 @@ PluginView.menu:register(nil, { function PluginView:new() PluginView.super.new(self) self.scrollable = true + self.progress = nil self.show_incompatible_plugins = false self.plugin_table_columns = { "ID", "Name", "Version", "Type", "Modversion", "Status", "Tags", "Author", "Description" } self.hovered_plugin = nil @@ -40,7 +41,11 @@ function PluginView:new() self.selected_plugin_idx = nil self.initialized = false self.plugin_manager = require "plugins.plugin_manager" - self.plugin_manager:refresh():done(function() + self.plugin_manager:refresh(function(progress) + self.progress = progress + core.redraw = true + print("PROG", self.progress) + end):done(function() self.initialized = true self:refresh() end) @@ -104,7 +109,7 @@ function PluginView:refresh() end for i, plugin in ipairs(self:get_plugins()) do local t = { get_plugin_text(plugin) } - for j = 1, #self.widths do + for j = 1, #self.widths do self.widths[j] = math.max(style.font:get_width(t[j] or ""), self.widths[j]) end end @@ -131,14 +136,23 @@ end function PluginView:draw() self:draw_background(style.background) - if not self.initialized then - common.draw_text(style.big_font, style.dim, "Loading...", "center", self.position.x, self.position.y, self.size.x, self.size.y) - return - end - local th = style.font:get_height() local lh = th + style.padding.y + if not self.initialized then + common.draw_text(style.big_font, style.dim, "Loading...", "center", self.position.x, self.position.y, self.size.x, self.size.y) + local width = self.size.x / 2 + local offset_y = self.size.y / 2 + if self.progress then + common.draw_text(style.font, style.dim, self.progress.label, "center", self.position.x, self.position.y + offset_y + lh, self.size.x, lh) + renderer.draw_rect(self.position.x + (self.size.x / 2) - (width / 2), self.position.y + self.size.y / 2 + (lh * 2), width, lh, style.line_highlight) + renderer.draw_rect(self.position.x + (self.size.x / 2) - (width / 2), self.position.y + self.size.y / 2 + (lh * 2), width * self.progress.percent, lh, style.caret) + -- common.draw_text(style.font, style.dim, string.format("[%03d%%] %s", math.floor(self.progress.percent * 100), self.progress.label), "center", self.position.x, self.position.y + (lh * 2), self.size.x, self.size.y) + end + return + end + + local ox, oy = self:get_content_offset() core.push_clip_rect(self.position.x, self.position.y, self.size.x, self.size.y) local x, y = ox + style.padding.x, oy @@ -150,7 +164,7 @@ function PluginView:draw() for i, plugin in ipairs(self:get_plugins()) do local x, y = ox, oy if y + lh >= self.position.y and y <= self.position.y + self.size.y then - if plugin == self.selected_plugin then + if plugin == self.selected_plugin then renderer.draw_rect(x, y, self.size.x, lh, style.dim) elseif plugin == self.hovered_plugin then renderer.draw_rect(x, y, self.size.x, lh, style.line_highlight) @@ -158,7 +172,7 @@ function PluginView:draw() x = x + style.padding.x for j, v in ipairs({ get_plugin_text(plugin) }) do local color = (plugin.status == "installed" or plugin.status == "bundled" or plugin.status == "orphan") and style.good or - (plugin.status == "core" and style.warn or + (plugin.status == "core" and style.warn or (plugin.status == "special" and style.modified or style.text) ) if self.loading then color = mul(color, style.dim) end @@ -198,8 +212,8 @@ function PluginView:reinstall(plugin) end command.add(PluginView, { - ["plugin-manager:select"] = function(x, y) - plugin_view.selected_plugin, plugin_view.selected_plugin_idx = plugin_view.hovered_plugin, plugin_view.hovered_plugin_idx + ["plugin-manager:select"] = function(x, y) + plugin_view.selected_plugin, plugin_view.selected_plugin_idx = plugin_view.hovered_plugin, plugin_view.hovered_plugin_idx end, ["plugin-manager:select-prev"] = function() local plugins = plugin_view:get_plugins() @@ -211,8 +225,8 @@ command.add(PluginView, { if plugin_view.selected_plugin_idx < #plugins then plugin_view.selected_plugin_idx = plugin_view.selected_plugin_idx + 1 end plugin_view.selected_plugin = plugins[plugin_view.selected_plugin_idx] end, - ["plugin-manager:select"] = function(x, y) - plugin_view.selected_plugin, plugin_view.selected_plugin_idx = plugin_view.hovered_plugin, plugin_view.hovered_plugin_idx + ["plugin-manager:select"] = function(x, y) + plugin_view.selected_plugin, plugin_view.selected_plugin_idx = plugin_view.hovered_plugin, plugin_view.hovered_plugin_idx end, }) command.add(function() @@ -239,7 +253,7 @@ end, { command.add(function() return core.active_view and core.active_view:is(PluginView) and plugin_view.hovered_plugin end, { - ["plugin-manager:view-source-hovered"] = function() + ["plugin-manager:view-source-hovered"] = function() local directory = plugin_view.hovered_plugin.type == "library" and "libraries" or "plugins" local opened = false for i, path in ipairs({ plugin_view.hovered_plugin.path, plugin_view.hovered_plugin.path .. PATHSEP .. "init.lua" }) do |