|
| 1 | +local cfg = require("freeze-code.config") |
| 2 | +local u = require("freeze-code.utils") |
| 3 | +local logger = u.logger |
| 4 | + |
| 5 | +local FreezeBinaryFetcher = {} |
| 6 | + |
| 7 | +---@class FreezeBinaryFetcher |
| 8 | +---@field go_installation function: installs `freeze` using `go install github.com/charmbracelet/freeze@latest` |
| 9 | +---Freeze installation using `go install` |
| 10 | +---@param opts FreezeCodeConfig |
| 11 | +function FreezeBinaryFetcher:go_installation(opts) |
| 12 | + local cmd_args = { |
| 13 | + "go", |
| 14 | + "install", |
| 15 | + "github.com/charmbracelet/freeze@latest", |
| 16 | + } |
| 17 | + local stdio = { stdout = "", stderr = "" } |
| 18 | + local job = nil |
| 19 | + |
| 20 | + local function on_output(err, data) |
| 21 | + if err then |
| 22 | + logger.err(err) |
| 23 | + end |
| 24 | + if data then |
| 25 | + stdio.stderr = stdio.stderr .. data |
| 26 | + end |
| 27 | + end |
| 28 | + local callbacks = { |
| 29 | + on_sterr = vim.schedule_wrap(function(_, data, _) |
| 30 | + local out = table.concat(data, "\n") |
| 31 | + on_output(out) |
| 32 | + end), |
| 33 | + on_exit = vim.schedule_wrap(function() |
| 34 | + opts._installed = true |
| 35 | + opts.freeze_path = vim.env.HOME .. "/go/bin/freeze" |
| 36 | + opts.install_path = opts.freeze_path |
| 37 | + logger.warn("[freeze-code] go install github.com/charmbracelet/freeze@latest completed") |
| 38 | + cfg.setup(opts) |
| 39 | + vim.fn.jobstop(job) |
| 40 | + end), |
| 41 | + } |
| 42 | + job = vim.fn.jobstart(cmd_args, callbacks) |
| 43 | +end |
| 44 | + |
| 45 | +---@class FreezeBinaryFetcher |
| 46 | +---@field agnostic_installation function: installs `freeze` using `cURL` from GitHub release |
| 47 | +---Freeze installation using GitHub release |
| 48 | +---@param opts FreezeCodeConfig |
| 49 | +function FreezeBinaryFetcher:agnostic_installation(opts) |
| 50 | + local os_name = u.get_os_info() |
| 51 | + local release_url = u.release_file_url() |
| 52 | + if release_url == "" then |
| 53 | + logger.err("could not get release file") |
| 54 | + return |
| 55 | + end |
| 56 | + |
| 57 | + local install_path = vim.fn.expand(opts.install_path) |
| 58 | + if install_path == "" then |
| 59 | + install_path = vim.fn.expand("~/.local/bin") |
| 60 | + end |
| 61 | + local output_filename = "freeze.tar.gz" |
| 62 | + local download_command = { "curl", "-sL", "-o", output_filename, release_url } |
| 63 | + local extract_command = { "tar", "-zxf", output_filename, "-C", install_path } |
| 64 | + -- vim.loop.spawn("rm", { args = { "-rf", install_path .. "/" .. u.get_freeze_filename() } }) |
| 65 | + local rm_command_args = { "-rf", install_path .. "/" .. u.get_freeze_filename() } |
| 66 | + if os_name == "Windows" then |
| 67 | + extract_command = { "Expand-Archive", output_filename, install_path } |
| 68 | + rm_command_args = { "-r", "-Force", install_path .. "/" .. u.get_freeze_filename() } |
| 69 | + end |
| 70 | + local binary_path = vim.fn.expand(table.concat({ install_path, u.get_freeze_filename() .. "/freeze" }, "/")) |
| 71 | + |
| 72 | + -- check for existing files / folders |
| 73 | + if vim.fn.isdirectory(install_path) == 0 then |
| 74 | + vim.loop.fs_mkdir(install_path, tonumber("777", 8)) |
| 75 | + end |
| 76 | + |
| 77 | + if vim.fn.filereadable(binary_path) == 1 then |
| 78 | + local success = vim.loop.fs_unlink(binary_path) |
| 79 | + if not success then |
| 80 | + logger.err("[freeze-code.nvim] ERROR: `freeze` binary could not be removed!") |
| 81 | + return |
| 82 | + end |
| 83 | + end |
| 84 | + local stdio = { stdout = "", stderr = "" } |
| 85 | + local job = nil |
| 86 | + |
| 87 | + local function on_output(err, data) |
| 88 | + if err then |
| 89 | + logger.err(err) |
| 90 | + end |
| 91 | + if data then |
| 92 | + stdio.stderr = stdio.stderr .. data |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + -- download and install the freeze binary |
| 97 | + local callbacks = { |
| 98 | + on_sterr = vim.schedule_wrap(function(_, data, _) |
| 99 | + local out = table.concat(data, "\n") |
| 100 | + on_output(out) |
| 101 | + end), |
| 102 | + on_exit = vim.schedule_wrap(function() |
| 103 | + logger.info_fmt("[freeze-code.nvim] extracting release with `%s`", table.concat(extract_command, " ")) |
| 104 | + vim.fn.system(extract_command) |
| 105 | + if vim.v.shell_error ~= 0 then |
| 106 | + logger.err("[freeze-code.nvim] ERROR: extracting release failed") |
| 107 | + return |
| 108 | + end |
| 109 | + -- remove the archive after completion |
| 110 | + if vim.fn.filereadable(output_filename) == 1 then |
| 111 | + local success = vim.loop.fs_unlink(output_filename) |
| 112 | + if not success then |
| 113 | + logger.err("[freeze-code.nvim] ERROR: existing archive could not be removed") |
| 114 | + return |
| 115 | + end |
| 116 | + end |
| 117 | + vim.loop.spawn("mv", { args = { binary_path, install_path .. "/freeze" } }) |
| 118 | + binary_path = install_path .. "/freeze" |
| 119 | + opts.freeze_path = binary_path |
| 120 | + opts._installed = true |
| 121 | + opts.install_path = install_path |
| 122 | + logger.warn_fmt("[freeze-code.nvim] `freeze` binary installed in installed in path=%s", cfg.config.freeze_path) |
| 123 | + vim.loop.spawn("rm", { args = rm_command_args }) |
| 124 | + logger.warn_fmt("[freeze-code.nvim] `freeze` binary installed in path=%s", cfg.config.freeze_path) |
| 125 | + cfg.setup(opts) |
| 126 | + vim.fn.jobstop(job) |
| 127 | + end), |
| 128 | + } |
| 129 | + logger.info_fmt("[freeze-code.nvim] downloading release from `%s`", release_url) |
| 130 | + job = vim.fn.jobstart(download_command, callbacks) |
| 131 | +end |
| 132 | + |
| 133 | +---@class FreezeBinaryFetcher |
| 134 | +---@field install_freeze function: `freeze` installation process |
| 135 | +---Install freeze for the user |
| 136 | +---@param opts FreezeCodeConfig |
| 137 | +function FreezeBinaryFetcher:install_freeze(opts) |
| 138 | + if u.check_executable("go", vim.fn.exepath("go")) then |
| 139 | + logger.warn("[freeze-code.nvim] go install github.com/charmbracelet/freeze@latest completed") |
| 140 | + self:go_installation(opts) |
| 141 | + return |
| 142 | + end |
| 143 | + logger.info("[freeze-code.nvim] Installing info with `curl`") |
| 144 | + self:agnostic_installation(opts) |
| 145 | +end |
| 146 | + |
| 147 | +return FreezeBinaryFetcher |
0 commit comments