chore(core): add some more modules in configz

Start building out the config and modules / libs that will make up the
dotfiles. This will start to take over the puppet and eventually be the
only things getting use going forward.
This commit is contained in:
Ade Attwood 2023-06-02 17:58:39 +01:00
parent d23a017232
commit 77e5752cd8
9 changed files with 159 additions and 17 deletions

35
lib/fs.lua Normal file
View file

@ -0,0 +1,35 @@
local fs = {};
--- Read the content of a file and return the content
---@param file_path string
---@return boolean
---@return string
fs.read_file = function(file_path)
local file = io.open(file_path, "rb")
if file == nil then
return false, ""
end
local content = file:read "*all"
file:close()
return true, content
end
--- Write content to a file replacing the existing content if the file already
--- exists. If not then the file will be created.
---@param file_path string
---@param content string
---@return boolean
fs.write_file = function(file_path, content)
local file = io.open(file_path, "w+")
if file == nil then
return false
end
file:write(content)
file:close()
return true
end
return fs

23
lib/git.lua Normal file
View file

@ -0,0 +1,23 @@
local git = {}
---@class GitRepoConfig
---@field src string The source url to the repo
---@field target string The target directory where you want the repo to be cloned
---@field version string The git revision. Can be a sha, branch or tag
--- Tracks a git repo keeping it up to date with a revision. Will clone the
--- repo if its already cloned. Will then checkout the required revision.
---@param config GitRepoConfig
git.repo = function (config)
assert(config.src ~= nil, "Git repo must have a source")
assert(config.target ~= nil, "Git repo must have a target")
assert(config.version ~= nil, "Git repo must have a version")
if not configz.is_directory(config.target) then
configz.run(string.format("git clone %s %s", config.src, config.target))
end
configz.run(string.format("cd %s && git pull && git checkout %s", config.target, config.version))
end
return git

35
lib/v-cache.lua Normal file
View file

@ -0,0 +1,35 @@
local fs = require("lib.fs")
local v_cache = {};
-- Where all the infomation about what configz has installed is stored.
local configz_dir = os.getenv("HOME") .. "/.config/configz/installed/";
-- Check to see if we have the version of a package installed locally
---@param package string
---@param version string
v_cache.is_installed = function (package, version)
configz.directory(configz_dir)
if not configz.is_file(configz_dir .. package) then
return false;
end
local ok, installed_version = fs.read_file(configz_dir .. package)
if not ok then
return false
end
return installed_version == version
end
-- Sets a package to be installed.
---@param package string
---@param version string
v_cache.install = function (package, version)
configz.directory(configz_dir)
return fs.write_file(configz_dir .. package, version)
end
return v_cache;

View file

@ -0,0 +1,5 @@
require("modules.clojure")
require("modules.lua-lsp")
require("modules.nvim")
require("modules.nvm")

View file

@ -1,6 +1,7 @@
-- Configz module for the clojure dev tools
local v_cache = require("lib.v-cache")
local function install_clojure_lsp()
if not v_cache.is_installed("closure-lsp", "2023.02.27-13.12.12") then
configz.download("/tmp/closure-lsp.zip", {
sha256 = "c23a5c9029b3a548a6b8e66a0662103c13e44f220ad8e3f97abf0b7c53a994b1",
url = "https://github.com/clojure-lsp/clojure-lsp/releases/download/2023.02.27-13.12.12/clojure-lsp-native-static-linux-amd64.zip",
@ -8,19 +9,10 @@ local function install_clojure_lsp()
configz.run "cd /tmp; unzip /tmp/closure-lsp.zip"
configz.file(os.getenv "HOME" .. "/.local/bin/clojure-lsp", { source = "/tmp/clojure-lsp" })
v_cache.install("closure-lsp", "2023.02.27-13.12.12")
end
local function has_correct_version(command, version)
local ok, _ = configz.run(command .. " --version | grep " .. version)
return ok
end
local clojure_lsp_installed, clojure_lsp = configz.get_executable "clojure-lsp"
if not clojure_lsp_installed or not has_correct_version(clojure_lsp, "2023.02.27-13.12.12") then
install_clojure_lsp()
end
local function install_babashka()
if not v_cache.is_installed("babashka", "v1.3.176") then
configz.download("/tmp/babashka.tar.gz", {
sha256 = "46c866c28ea9d99a5544c75c554b0c1e22edfa20843d927d3c175b7021ca7252",
url = "https://github.com/babashka/babashka/releases/download/v1.3.176/babashka-1.3.176-linux-amd64.tar.gz",
@ -28,9 +20,5 @@ local function install_babashka()
configz.run "cd /tmp; tar -xzf babashka.tar.gz"
configz.file(os.getenv "HOME" .. "/.local/bin/bb", { source = "/tmp/bb" })
end
local bb_installed, bb = configz.get_executable "bb"
if not bb_installed or not has_correct_version(bb, "1.3.176") then
install_babashka()
v_cache.install("babashka", "v1.3.176")
end

14
modules/gh-cli.lua Normal file
View file

@ -0,0 +1,14 @@
local v_cache = require('lib.v-cache')
if v_cache.is_installed("gh-cli", "v2.29.0") then
return
end
configz.download( "/tmp/gh-cli.tar.gz", {
url = "https://github.com/cli/cli/releases/download/v2.29.0/gh_2.29.0_linux_amd64.tar.gz",
sha256 = "9fe05f43a11a7bf8eacf731422452d1997e6708d4160ef0efcb13c103320390e"
})
configz.run("cd /tmp; tar -xzf /tmp/gh-cli.tar.gz")
configz.file(os.getenv("HOME") .. "/.local/bin/gh", { source = "/tmp/gh_2.29.0_linux_amd64/bin/gh" })
v_cache.install("gh-cli", "v2.29.0")

21
modules/lua-lsp.lua Normal file
View file

@ -0,0 +1,21 @@
local v_cache = require('lib.v-cache')
if v_cache.is_installed("lua-language-server", "3.6.21") then
configz.info "Skipping lua lsp install, its already installed!"
return;
end
configz.directory(os.getenv("HOME") .. "/.local/share/lua-lsp")
configz.download(
os.getenv("HOME") .. "/.local/share/lua-lsp/language-server.tar.gz",
-- TODO: Add sha to the download
{ url = "https://github.com/LuaLS/lua-language-server/releases/download/3.6.21/lua-language-server-3.6.21-linux-x64.tar.gz" }
)
local ok = configz.run("cd /.local/share/lua-lsp && tar -xzf language-server.tar.gz")
if not ok then
return false;
end
v_cache.install("lua-language-server", "3.6.21")

9
modules/nvim.lua Normal file
View file

@ -0,0 +1,9 @@
local git = require('lib.git')
local nvim_pluing_dir_start = os.getenv("HOME") .. "/.config/nvim/pack/bundle/start"
git.repo({
src = "https://github.com/sbdchd/neoformat",
target = nvim_pluing_dir_start .. "/neoformat",
version = "master"
})

12
modules/nvm.lua Normal file
View file

@ -0,0 +1,12 @@
local is_installed = configz.is_directory(os.getenv("HOME") .. "/.nvm")
if is_installed then
configz.info("Skipping nvm install")
return
end
configz.download('/tmp/nvm-install.sh', {
url = "https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh",
sha256 = "2ed5e94ba12434370f0358800deb69f514e8bce90f13beb0e1b241d42c6abafd"
})
configz.run("chmod +x /tmp/nvm-install.sh && /tmp/nvm-install.sh")