From 2fce4e35116f44f6f00dbb067516403a93bf8208 Mon Sep 17 00:00:00 2001 From: Damian Senn Date: Sun, 11 Aug 2024 19:55:37 +0200 Subject: [PATCH] refactor: lazy imports --- lua/gh-actions/git.lua | 19 +++++++++++-------- lua/gh-actions/github.lua | 12 ++++++++---- lua/gh-actions/health.lua | 14 +++++++------- lua/gh-actions/init.lua | 38 ++++++++++++++++++++++---------------- lua/gh-actions/utils.lua | 19 +------------------ lua/gh-actions/yaml.lua | 28 ++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 lua/gh-actions/yaml.lua diff --git a/lua/gh-actions/git.lua b/lua/gh-actions/git.lua index 3424f8d..acae0ef 100644 --- a/lua/gh-actions/git.lua +++ b/lua/gh-actions/git.lua @@ -1,28 +1,31 @@ -local job = require('plenary.job') - local M = {} +---@param job Job +local function create_job(job) + return require('plenary.job').job:new(job) +end + function M.get_current_branch() - local gitJob = job:new { + local job = create_job { command = 'git', args = { 'branch', '--show-current' }, } - gitJob:sync() + job:sync() - return table.concat(gitJob:result(), '') + return table.concat(job:result(), '') end function M.get_default_branch() - local gitJob = job:new { + local job = create_job { command = 'git', args = { 'remote', 'show', 'origin' }, } - gitJob:sync() + job:sync() -- luacheck: ignore - for match in table.concat(gitJob:result(), ''):gmatch('HEAD branch: (%a+)') do + for match in table.concat(job:result(), ''):gmatch('HEAD branch: (%a+)') do return match end diff --git a/lua/gh-actions/github.lua b/lua/gh-actions/github.lua index 5f28e52..e37ba28 100644 --- a/lua/gh-actions/github.lua +++ b/lua/gh-actions/github.lua @@ -1,7 +1,3 @@ -local curl = require('plenary.curl') -local job = require('plenary.job') -local utils = require('gh-actions.utils') - local M = {} ---@param str string @@ -15,6 +11,7 @@ local function strip_git_suffix(str) end function M.get_current_repository() + local job = require('plenary.job') local origin_url_job = job:new { command = 'git', args = { @@ -74,6 +71,10 @@ end ---@param path string ---@param opts? table function M.fetch(server, path, opts) + vim.validate('server', server, 'string') + vim.validate('path', path, 'string') + vim.validate('opts', opts, 'table', true) + opts = opts or {} opts.callback = opts.callback and vim.schedule_wrap(opts.callback) @@ -82,6 +83,8 @@ function M.fetch(server, path, opts) url = string.format('https://%s/api/v3%s', server, path) end + local curl = require('plenary.curl') + return curl[opts.method or 'get']( url, vim.tbl_deep_extend('force', opts, { @@ -297,6 +300,7 @@ end function M.get_workflow_config(path) path = vim.fn.expand(path) + local utils = require('gh-actions.utils') local workflow_yaml = utils.read_file(path) or '' local config = { on = { diff --git a/lua/gh-actions/health.lua b/lua/gh-actions/health.lua index 00a8f33..b104961 100644 --- a/lua/gh-actions/health.lua +++ b/lua/gh-actions/health.lua @@ -1,13 +1,13 @@ -local health = vim.health or require('health') - -local start = health.start or health.report_start -local ok = health.ok or health.report_ok -local warn = health.warn or health.report_warn -local error = health.error or health.report_error - local M = {} function M.check() + local health = vim.health or require('health') + + local start = health.start or health.report_start + local ok = health.ok or health.report_ok + local warn = health.warn or health.report_warn + local error = health.error or health.report_error + start('Checking ability to parse yaml files') local has_rust_module = pcall(require, 'gh-actions.rust') diff --git a/lua/gh-actions/init.lua b/lua/gh-actions/init.lua index ec144fb..b533cf5 100644 --- a/lua/gh-actions/init.lua +++ b/lua/gh-actions/init.lua @@ -1,13 +1,3 @@ -local Input = require('nui.input') -local Menu = require('nui.menu') -local event = require('nui.utils.autocmd').event -local Config = require('gh-actions.config') -local store = require('gh-actions.store') -local git = require('gh-actions.git') -local gh = require('gh-actions.github') -local ui = require('gh-actions.ui') -local utils = require('gh-actions.utils') - local M = { setup_called = false, init_root = '', @@ -23,8 +13,8 @@ function M.setup(opts) M.setup_called = true - Config.setup(opts) - ui.setup() + require('gh-actions.config').setup(opts) + require('gh-actions.ui').setup() vim.api.nvim_create_user_command('GhActions', M.open, {}) end @@ -36,6 +26,8 @@ end --TODO Maybe send lsp progress events when fetching, to interact -- with fidget.nvim local function fetch_data() + local gh = require('gh-actions.github') + local store = require('gh-actions.store') local server, repo = gh.get_current_repository() store.update_state(function(state) @@ -53,6 +45,7 @@ local function fetch_data() gh.get_repository_workflow_runs(server, repo, 100, { callback = function(workflow_runs) + local utils = require('gh-actions.utils') local old_workflow_runs = store.get_state().workflow_runs store.update_state(function(state) @@ -90,6 +83,7 @@ local WORKFLOW_CONFIG_CACHE_TTL_S = 10 ---TODO We should run this after fetching the workflows instead of within the state update event ---@param state GhActionsState function M.update_workflow_configs(state) + local gh = require('gh-actions.github') local n = now() for _, workflow in ipairs(state.workflows) do @@ -108,6 +102,8 @@ end ---@param opts { prompt: string, title: string, default_value: string, on_submit: fun(value: string) } local function text(opts) + local Input = require('nui.input') + return Input({ relative = 'editor', position = '50%', @@ -127,6 +123,7 @@ end ---@param opts { prompt: string, title: string, options: string[], on_submit: fun(value: { text: string }) } local function menu(opts) + local Menu = require('nui.menu') local lines = { Menu.separator(opts.prompt) } for _, option in ipairs(opts.options) do @@ -156,6 +153,10 @@ local function menu(opts) end function M.open() + local ui = require('gh-actions.ui') + local store = require('gh-actions.store') + local utils = require('gh-actions.utils') + ui.open() ui.split:map('n', 'q', M.close, { noremap = true }) @@ -191,6 +192,7 @@ function M.open() -- TODO Move this into its own module, ui? ui.split:map('n', 'd', function() + local gh = require('gh-actions.github') local workflow = ui.get_workflow() if workflow then @@ -199,9 +201,9 @@ function M.open() -- TODO should we get current ref instead or show an input with the -- default branch or current ref preselected? - local default_branch = git.get_default_branch() - - local workflow_config = utils.read_yaml_file(workflow.path) + local default_branch = require('gh-actions.git').get_default_branch() + local workflow_config = + require('gh-actions.yaml').read_yaml_file(workflow.path) if not workflow_config or not workflow_config.on.workflow_dispatch then return @@ -213,6 +215,7 @@ function M.open() inputs = workflow_config.on.workflow_dispatch.inputs end + local event = require('nui.utils.autocmd').event local questions = {} local i = 0 local input_values = vim.empty_dict() @@ -304,7 +307,7 @@ function M.open() M.timer = vim.loop.new_timer() M.timer:start( 0, - Config.options.refresh_interval * 1000, + require('gh-actions.config').options.refresh_interval * 1000, vim.schedule_wrap(fetch_data) ) @@ -313,6 +316,9 @@ function M.open() end function M.close() + local ui = require('gh-actions.ui') + local store = require('gh-actions.store') + ui.close() M.timer:stop() M.timer:close() diff --git a/lua/gh-actions/utils.lua b/lua/gh-actions/utils.lua index fa3c800..f73aa36 100644 --- a/lua/gh-actions/utils.lua +++ b/lua/gh-actions/utils.lua @@ -1,5 +1,4 @@ local stringUtils = require('gh-actions.utils.string') -local has_rust_module, rust = pcall(require, 'gh-actions.rust') local M = { string = stringUtils, @@ -45,22 +44,6 @@ function M.read_file(path) return content end -function M.read_yaml_file(path) - local yamlstr = M.read_file(path) - - return M.parse_yaml(yamlstr) -end - ----@param yamlstr string|nil ----@return table|nil -function M.parse_yaml(yamlstr) - if has_rust_module then - return rust.parse_yaml(yamlstr or '') - else - return vim.json.decode(vim.fn.system({'yq', '-o', 'json'}, yamlstr) or '') - end -end - ---@generic V ---@param fn fun(arg1: V): any ---@param tbl table @@ -158,7 +141,7 @@ end function M.is_nil(value) return value == nil or value == vim.NIL - or (has_rust_module and value == rust.NIL) + or require('gh-actions.yaml').is_yaml_nil(value) end return M diff --git a/lua/gh-actions/yaml.lua b/lua/gh-actions/yaml.lua new file mode 100644 index 0000000..478aa2b --- /dev/null +++ b/lua/gh-actions/yaml.lua @@ -0,0 +1,28 @@ +local utils = require('gh-actions.utils') +local has_rust_module, rust = pcall(require, 'gh-actions.rust') + +local M = {} + +---@param path string +---@return table|nil +function M.read_yaml_file(path) + local yamlstr = utils.read_file(path) + + return M.parse_yaml(yamlstr) +end + +---@param yamlstr string|nil +---@return table|nil +function M.parse_yaml(yamlstr) + if has_rust_module then + return rust.parse_yaml(yamlstr or '') + else + return vim.json.decode(vim.fn.system('yq', yamlstr)) + end +end + +function M.is_yaml_nil(value) + return has_rust_module and value == rust.NIL +end + +return M