Skip to content

Commit

Permalink
feat: first PoC testing version, can only install and remove (automat…
Browse files Browse the repository at this point in the history
…ically)
  • Loading branch information
NTBBloodbath committed Jul 5, 2023
1 parent 45a12e7 commit 7afdffa
Show file tree
Hide file tree
Showing 6 changed files with 529 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lua/rocks/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--- config.lua --- rocks.nvim config module
--
-- Copyright (C) 2023 NTBBloodbath
--
-- Version: 0.1.0
-- License: GPLv3
-- Created: 05 Jul 2023
-- Updated: 05 Jul 2023
-- Homepage: https://github.com/NTBBloodbath/rocks.nvim
-- Maintainer: NTBBloodbath <bloodbathalchemist@protonmail.com>
--
-------------------------------------------------------------------------------
--
--- Commentary:
--
-- rocks.nvim configuration options
--
-------------------------------------------------------------------------------
--
--- Code:

local separator = require("rocks.constants").SYS_SEPARATOR

--- rocks.nvim configuration
---@type table
local config = {
--- Local path in your filesystem to install rocks
---@type string
rocks_path = vim.fn.stdpath("data") .. separator .. "rocks",
--- Rocks declaration file path
---@type string
config_path = vim.fn.stdpath("config") .. separator .. "rocks.toml",
}

return config

--- config.lua ends here
68 changes: 68 additions & 0 deletions lua/rocks/constants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--- constants.lua --- rocks.nvim contants module
--
-- Copyright (C) 2023 NTBBloodbath
--
-- Version: 0.1.0
-- License: GPLv3
-- Created: 05 Jul 2023
-- Updated: 05 Jul 2023
-- Homepage: https://github.com/NTBBloodbath/rocks.nvim
-- Maintainer: NTBBloodbath <bloodbathalchemist@protonmail.com>
--
-------------------------------------------------------------------------------
--
--- Commentary:
--
-- rocks.nvim constant variables that I do not want to write twice
--
-------------------------------------------------------------------------------
--
--- Code:

---@type table
local constants = {}

--- Lua version to be used in luarocks
---@type string
constants.LUA_VERSION = "5.1"

--- Rocks.nvim version
---@type string
constants.ROCKS_VERSION = "0.1.0"

--- System separator, '/' for *nix systems and '\\' for Windows
---@type string
constants.SYS_SEPARATOR = package.config:sub(1, 1)

--- Default configuration file contents
---@type string
constants.DEFAULT_CONFIG = [[
# This is your rocks.nvim plugins declaration file.
# Here is a small yet pretty detailed example on how to use it:
#
# [plugins]
# nvim-treesitter = "semver_version-rev" # e.g. "1.0.0-0"
#
# If the plugin name contains a dot then you must add quotes to the key name!
# Here is another example if you want to tweak your plugins even more:
#
# [plugins.nvim-treesitter]
# version = "semver_version-rev"
# config = "plugins/nvim-treesitter.lua" # <- ~/.config/nvim/lua
# rock_flags = "--additional-luarocks-install-flags=here"
#
# If you want more information about the possible and valid options, please
# take a look at our schema in
[rocks]
toml = "0.3.0-0" # rocks.nvim can manage its own runtime dependencies too, goated!
# magick = "1.6.0-1" # I am using magick for testing stuff sometimes, so lets keep it here
[plugins]
# "rocks.nvim" = "0.1.0-1" # rocks.nvim can also manage itself, why not?
"sweetie.nvim" = "1.2.1-1"
]]

return constants

--- constants.lua
82 changes: 82 additions & 0 deletions lua/rocks/fs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
--- fs.lua --- rocks.nvim fs module
--
-- Copyright (C) 2023 NTBBloodbath
--
-- Version: 0.1.0
-- License: GPLv3
-- Created: 05 Jul 2023
-- Updated: 05 Jul 2023
-- Homepage: https://github.com/NTBBloodbath/rocks.nvim
-- Maintainer: NTBBloodbath <bloodbathalchemist@protonmail.com>
--
-------------------------------------------------------------------------------
--
--- Commentary:
--
-- Filesystem related utilities for rocks.nvim
--
-- Diagnostics are disabled because lua_ls loves to scream at libuv functions
-- like Rust borrow checker likes to scream to newbies
--
-------------------------------------------------------------------------------
--
--- Code:

local fs = {}

--- Check if a file exists
---@param location string file path
---@return boolean
function fs.file_exists(location)
local fd = vim.loop.fs_open(location, "r", 438)
if fd then
vim.loop.fs_close(fd)
return true
end
return false
end

--- Write `contents` to a file
---@param location string file path
---@param mode string mode to open the file for
---@param contents string file contents
function fs.write_file(location, mode, contents)
-- 644 sets read and write permissions for the owner, and it sets read-only
-- mode for the group and others
vim.loop.fs_open(location, mode, tonumber("644", 8), function(err, file)
if not err then
local file_pipe = vim.loop.new_pipe(false)
---@diagnostic disable-next-line
vim.loop.pipe_open(file_pipe, file)
---@diagnostic disable-next-line
vim.loop.write(file_pipe, contents)
---@diagnostic disable-next-line
vim.loop.fs_close(file)
end
end)
end

---Reads or creates from a file
---@param location string The location of the file
---@param default string The contents to write to the file if it doesn't exist
function fs.read_or_create(location, default)
local content
if fs.file_exists(location) then
local file = vim.loop.fs_open(location, "r", 438)
---@diagnostic disable-next-line
local stat = vim.loop.fs_fstat(file)
---@diagnostic disable-next-line
content = vim.loop.fs_read(file, stat.size, 0)
---@diagnostic disable-next-line
vim.loop.fs_close(file)
else
content = vim.trim(default)
fs.write_file(location, "w+", content)
end

return content
end

return fs

--- fs.lua ends here
37 changes: 37 additions & 0 deletions lua/rocks/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--- init.lua --- rocks.nvim main module
--
-- Copyright (C) 2023 NTBBloodbath
--
-- Version: 0.1.0
-- License: GPLv3
-- Created: 05 Jul 2023
-- Updated: 05 Jul 2023
-- Homepage: https://github.com/NTBBloodbath/rocks.nvim
-- Maintainer: NTBBloodbath <bloodbathalchemist@protonmail.com>
--
-------------------------------------------------------------------------------
--
--- Commentary:
--
--
--
-------------------------------------------------------------------------------
--
--- Code:

local rocks = {}

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

function rocks.setup(opts)
-- NOTE: as much as I hate global namespaces, it seems to be the only way to
-- get everything working as expected with no hassle lol
_G.__rocks_config = vim.tbl_deep_extend("force", config, opts or {})

setup.init()
end

return rocks

--- init.lua ends here
Loading

0 comments on commit 7afdffa

Please sign in to comment.