From 77e5752cd8285c984c6dee486e8255ffe47363c9 Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Fri, 2 Jun 2023 17:58:39 +0100 Subject: [PATCH] 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. --- lib/fs.lua | 35 +++++++++++++++++++++++++++++++++++ lib/git.lua | 23 +++++++++++++++++++++++ lib/v-cache.lua | 35 +++++++++++++++++++++++++++++++++++ manifests/configz-user.lua | 5 +++++ modules/clojure.lua | 22 +++++----------------- modules/gh-cli.lua | 14 ++++++++++++++ modules/lua-lsp.lua | 21 +++++++++++++++++++++ modules/nvim.lua | 9 +++++++++ modules/nvm.lua | 12 ++++++++++++ 9 files changed, 159 insertions(+), 17 deletions(-) create mode 100644 lib/fs.lua create mode 100644 lib/git.lua create mode 100644 lib/v-cache.lua create mode 100644 manifests/configz-user.lua create mode 100644 modules/gh-cli.lua create mode 100644 modules/lua-lsp.lua create mode 100644 modules/nvim.lua create mode 100644 modules/nvm.lua diff --git a/lib/fs.lua b/lib/fs.lua new file mode 100644 index 0000000..1742efb --- /dev/null +++ b/lib/fs.lua @@ -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 diff --git a/lib/git.lua b/lib/git.lua new file mode 100644 index 0000000..d2c04e4 --- /dev/null +++ b/lib/git.lua @@ -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 diff --git a/lib/v-cache.lua b/lib/v-cache.lua new file mode 100644 index 0000000..1802e66 --- /dev/null +++ b/lib/v-cache.lua @@ -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; diff --git a/manifests/configz-user.lua b/manifests/configz-user.lua new file mode 100644 index 0000000..90d6feb --- /dev/null +++ b/manifests/configz-user.lua @@ -0,0 +1,5 @@ + +require("modules.clojure") +require("modules.lua-lsp") +require("modules.nvim") +require("modules.nvm") diff --git a/modules/clojure.lua b/modules/clojure.lua index 1bc8558..e4c6667 100644 --- a/modules/clojure.lua +++ b/modules/clojure.lua @@ -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 diff --git a/modules/gh-cli.lua b/modules/gh-cli.lua new file mode 100644 index 0000000..0ef273c --- /dev/null +++ b/modules/gh-cli.lua @@ -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") diff --git a/modules/lua-lsp.lua b/modules/lua-lsp.lua new file mode 100644 index 0000000..ae5b6cb --- /dev/null +++ b/modules/lua-lsp.lua @@ -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") diff --git a/modules/nvim.lua b/modules/nvim.lua new file mode 100644 index 0000000..61dbdd5 --- /dev/null +++ b/modules/nvim.lua @@ -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" +}) diff --git a/modules/nvm.lua b/modules/nvm.lua new file mode 100644 index 0000000..e506137 --- /dev/null +++ b/modules/nvm.lua @@ -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")