Skip to content

Commit

Permalink
refactor: extract luarocks cli functions to luarocks module
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored and vhyrro committed Oct 20, 2023
1 parent ea6672a commit dcbe3a2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
40 changes: 40 additions & 0 deletions lua/rocks/luarocks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---@mod rocks.luarocks
--
-- Copyright (C) 2023 NTBBloodbath
--
-- Version: 0.1.0
-- License: GPLv3
-- Created: 19 Jul 2023
-- Updated: 27 Aug 2023
-- Homepage: https://github.com/nvim-neorocks/rocks.nvim
-- Maintainers: NTBBloodbath <bloodbathalchemist@protonmail.com>, Vhyrro <vhyrro@gmail.com>
--
---@brief [[
--
-- Functions for interacting with the luarocks CLI
--
---@brief ]]

local luarocks = {}

local constants = require("rocks.constants")
local config = require("rocks.config")

---@param args string[] luarocks CLI arguments
---@param on_exit (function|nil) Called asynchronously when the luarocks command exits.
--- asynchronously. Receives SystemCompleted object, see return of SystemObj:wait().
---@param opts? SystemOpts
---@return vim.SystemObj
---@see vim.system
luarocks.cli = function(args, on_exit, opts)
local luarocks_cmd = vim.list_extend({
"luarocks",
"--lua-version=" .. constants.LUA_VERSION,
"--tree=" .. config.rocks_path,
}, args)
return vim.system(luarocks_cmd, opts, on_exit)
end

return luarocks

-- end of luarocks.lua
13 changes: 4 additions & 9 deletions lua/rocks/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local constants = require("rocks.constants")
local fs = require("rocks.fs")
local config = require("rocks.config")
local state = require("rocks.state")
local luarocks = require("rocks.luarocks")
local nio = require("nio")

local operations = {}
Expand All @@ -31,16 +32,13 @@ operations.install = function(name, version)
-- TODO(vhyrro): Input checking on name and version
local future = nio.control.future()
local install_cmd = {
"luarocks",
"--lua-version=" .. constants.LUA_VERSION,
"--tree=" .. config.rocks_path,
"install",
name,
}
if version then
table.insert(install_cmd, version)
end
vim.system(install_cmd, {}, function(obj)
luarocks.cli(install_cmd, function(obj)
if obj.code ~= 0 then
future.set_error(obj.stderr)
else
Expand All @@ -57,13 +55,10 @@ end
---@return nio.control.Future
operations.remove = function(name)
local future = nio.control.future()
vim.system({
"luarocks",
"--lua-version=" .. constants.LUA_VERSION,
"--tree=" .. config.rocks_path,
luarocks.cli({
"remove",
name,
}, {}, function(...)
}, function(...)
-- TODO: Raise an error with set_error on the future if something goes wrong
future.set(...)
end)
Expand Down
7 changes: 2 additions & 5 deletions lua/rocks/setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@

local setup = {}

local constants = require("rocks.constants")
local config = require("rocks.config")
local luarocks = require("rocks.luarocks")

local function bootstrap_install(name, version)
vim.system({
"luarocks",
"--lua-version=" .. constants.LUA_VERSION,
"--tree=" .. config.rocks_path,
luarocks.cli({
"install",
name,
version,
Expand Down
27 changes: 11 additions & 16 deletions lua/rocks/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

local state = {}

local constants = require("rocks.constants")
local config = require("rocks.config")
local luarocks = require("rocks.luarocks")
local nio = require("nio")

---@type fun(): {[string]: Rock}
Expand All @@ -29,14 +28,13 @@ state.installed_rocks = nio.create(function()

local future = nio.control.future()

vim.system(
{ "luarocks", "--lua-version=" .. constants.LUA_VERSION, "--tree=" .. config.rocks_path, "list", "--porcelain" },
{ text = true },
function(obj)
-- TODO: Error handling
future.set(obj.stdout)
end
)
luarocks.cli({
"list",
"--porcelain",
}, function(obj)
-- TODO: Error handling
future.set(obj.stdout)
end, { text = true })

local installed_rock_list = future.wait()

Expand All @@ -55,17 +53,14 @@ state.outdated_rocks = nio.create(function()

local future = nio.control.future()

vim.system({
"luarocks",
"--lua-version=" .. constants.LUA_VERSION,
"--tree=" .. config.rocks_path,
luarocks.cli({
"list",
"--porcelain",
"--outdated",
}, { text = true }, function(obj)
}, function(obj)
-- TODO: Error handling
future.set(obj.stdout)
end)
end, { text = true })

local installed_rock_list = future.wait()

Expand Down

0 comments on commit dcbe3a2

Please sign in to comment.