Skip to content

Commit

Permalink
refactor: lazy imports
Browse files Browse the repository at this point in the history
  • Loading branch information
topaxi committed Aug 11, 2024
1 parent 2bc7957 commit 2fce4e3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 53 deletions.
19 changes: 11 additions & 8 deletions lua/gh-actions/git.lua
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 8 additions & 4 deletions lua/gh-actions/github.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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)

Expand All @@ -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, {
Expand Down Expand Up @@ -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 = {
Expand Down
14 changes: 7 additions & 7 deletions lua/gh-actions/health.lua
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
38 changes: 22 additions & 16 deletions lua/gh-actions/init.lua
Original file line number Diff line number Diff line change
@@ -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 = '',
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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%',
Expand All @@ -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
Expand Down Expand Up @@ -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 })

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
)

Expand All @@ -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()
Expand Down
19 changes: 1 addition & 18 deletions lua/gh-actions/utils.lua
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<any, V>
Expand Down Expand Up @@ -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
28 changes: 28 additions & 0 deletions lua/gh-actions/yaml.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2fce4e3

Please sign in to comment.