|
| 1 | +local M = {} |
| 2 | +local system = require('fff.utils.system') |
| 3 | +local uv = vim and vim.uv or require('luv') |
| 4 | + |
| 5 | +local GITHUB_REPO = 'dmtrKovalenko/fff.nvim' |
| 6 | + |
| 7 | +local function get_current_version(plugin_dir, callback) |
| 8 | + vim.system({ 'git', 'rev-parse', '--short', 'HEAD' }, { cwd = plugin_dir }, function(result) |
| 9 | + if result.code ~= 0 or not result.stdout or result.stdout == '' then |
| 10 | + callback(nil) |
| 11 | + return |
| 12 | + end |
| 13 | + callback(result.stdout:gsub('%s+', '')) |
| 14 | + end) |
| 15 | +end |
| 16 | + |
| 17 | +local function get_binary_dir(plugin_dir) return plugin_dir .. '/../target' end |
| 18 | + |
| 19 | +local function get_binary_path(plugin_dir) |
| 20 | + local binary_dir = get_binary_dir(plugin_dir) |
| 21 | + local extension = system.get_lib_extension() |
| 22 | + return binary_dir .. '/libfff_nvim.' .. extension |
| 23 | +end |
| 24 | + |
| 25 | +local function binary_exists(plugin_dir) |
| 26 | + local binary_path = get_binary_path(plugin_dir) |
| 27 | + local stat = uv.fs_stat(binary_path) |
| 28 | + return stat and stat.type == 'file' |
| 29 | +end |
| 30 | + |
| 31 | +local function download_file(url, output_path, opts, callback) |
| 32 | + opts = opts or {} |
| 33 | + |
| 34 | + local dir = vim.fn.fnamemodify(output_path, ':h') |
| 35 | + uv.fs_mkdir(dir, 493, function(err) -- 493 = 0755 octal |
| 36 | + if err and not err:match('EEXIST') then |
| 37 | + callback(false, 'Failed to create directory: ' .. err) |
| 38 | + return |
| 39 | + end |
| 40 | + |
| 41 | + local curl_args = { |
| 42 | + 'curl', |
| 43 | + '--fail', |
| 44 | + '--location', |
| 45 | + '--silent', |
| 46 | + '--show-error', |
| 47 | + '--output', |
| 48 | + output_path, |
| 49 | + } |
| 50 | + |
| 51 | + if opts.proxy then |
| 52 | + table.insert(curl_args, '--proxy') |
| 53 | + table.insert(curl_args, opts.proxy) |
| 54 | + end |
| 55 | + |
| 56 | + table.insert(curl_args, url) |
| 57 | + vim.system(curl_args, {}, function(result) |
| 58 | + if result.code ~= 0 then |
| 59 | + callback(false, 'Failed to download: ' .. (result.stderr or 'unknown error')) |
| 60 | + return |
| 61 | + end |
| 62 | + callback(true, nil) |
| 63 | + end) |
| 64 | + end) |
| 65 | +end |
| 66 | + |
| 67 | +local function download_from_github(version, binary_path, opts, callback) |
| 68 | + opts = opts or {} |
| 69 | + |
| 70 | + local triple = system.get_triple() |
| 71 | + local extension = system.get_lib_extension() |
| 72 | + local binary_name = triple .. '.' .. extension |
| 73 | + local url = string.format('https://github.com/%s/releases/download/%s/%s', GITHUB_REPO, version, binary_name) |
| 74 | + vim.schedule(function() |
| 75 | + vim.notify(string.format('Downloading fff.nvim binary for ' .. version), vim.log.levels.INFO) |
| 76 | + vim.notify(string.format('Do not open fff until you see a success notification.'), vim.log.levels.WARN) |
| 77 | + end) |
| 78 | + |
| 79 | + download_file(url, binary_path, { |
| 80 | + proxy = opts.proxy, |
| 81 | + extra_curl_args = opts.extra_curl_args, |
| 82 | + }, function(success, err) |
| 83 | + if not success then |
| 84 | + callback(false, err) |
| 85 | + return |
| 86 | + end |
| 87 | + |
| 88 | + -- Verify the binary can be loaded |
| 89 | + local ok, err_msg = pcall(function() package.loadlib(binary_path, 'luaopen_fff_nvim') end) |
| 90 | + |
| 91 | + if not ok then |
| 92 | + uv.fs_unlink(binary_path) |
| 93 | + callback(false, 'Downloaded binary is not valid: ' .. (err_msg or 'unknown error')) |
| 94 | + return |
| 95 | + end |
| 96 | + |
| 97 | + vim.schedule(function() vim.notify('fff.nvim binary downloaded successfully!', vim.log.levels.INFO) end) |
| 98 | + callback(true, nil) |
| 99 | + end) |
| 100 | +end |
| 101 | + |
| 102 | +function M.ensure_downloaded(opts, callback) |
| 103 | + opts = opts or {} |
| 104 | + local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':h:h') |
| 105 | + |
| 106 | + if binary_exists(plugin_dir) and not opts.force then |
| 107 | + callback(true, nil) |
| 108 | + return |
| 109 | + end |
| 110 | + |
| 111 | + local function on_version(target_version) |
| 112 | + if not target_version then |
| 113 | + callback(false, 'Could not determine target version') |
| 114 | + return |
| 115 | + end |
| 116 | + |
| 117 | + local binary_path = get_binary_path(plugin_dir) |
| 118 | + download_from_github(target_version, binary_path, opts, callback) |
| 119 | + end |
| 120 | + |
| 121 | + if opts.version then |
| 122 | + on_version(opts.version) |
| 123 | + else |
| 124 | + get_current_version(plugin_dir, on_version) |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +function M.download_binary(callback) |
| 129 | + M.ensure_downloaded({ force = true }, function(success, err) |
| 130 | + if not success then |
| 131 | + if callback then |
| 132 | + callback(false, err) |
| 133 | + else |
| 134 | + error('Failed to download fff.nvim binary: ' .. (err or 'unknown error')) |
| 135 | + end |
| 136 | + return |
| 137 | + end |
| 138 | + if callback then callback(true, nil) end |
| 139 | + end) |
| 140 | +end |
| 141 | + |
| 142 | +function M.build_binary(callback) |
| 143 | + local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':h:h') |
| 144 | + local has_rustup = vim.fn.executable('rustup') == 1 |
| 145 | + if not has_rustup then |
| 146 | + callback( |
| 147 | + false, |
| 148 | + 'rustup is not found. It is required to build the fff.nvim binary. Install it from https://rustup.rs/' |
| 149 | + ) |
| 150 | + return |
| 151 | + end |
| 152 | + |
| 153 | + vim.system({ 'cargo', 'build', '--release' }, { cwd = plugin_dir }, function(result) |
| 154 | + if result.code ~= 0 then |
| 155 | + callback(false, 'Failed to build rust binary: ' .. (result.stderr or 'unknown error')) |
| 156 | + return |
| 157 | + end |
| 158 | + callback(true, nil) |
| 159 | + end) |
| 160 | +end |
| 161 | + |
| 162 | +function M.download_or_build_binary() |
| 163 | + M.ensure_downloaded({ force = true }, function(download_success, download_error) |
| 164 | + if download_success then return end |
| 165 | + |
| 166 | + vim.schedule( |
| 167 | + function() |
| 168 | + vim.notify( |
| 169 | + 'Error downloading binary: ' .. (download_error or 'unknown error') .. '\nTrying cargo build --release\n', |
| 170 | + vim.log.levels.WARN |
| 171 | + ) |
| 172 | + end |
| 173 | + ) |
| 174 | + |
| 175 | + M.build_binary(function(build_success, build_error) |
| 176 | + if not build_success then |
| 177 | + error('Failed to build fff.nvim binary. Build error: ' .. (build_error or 'unknown error')) |
| 178 | + else |
| 179 | + vim.schedule(function() vim.notify('fff.nvim binary built successfully!', vim.log.levels.INFO) end) |
| 180 | + end |
| 181 | + end) |
| 182 | + end) |
| 183 | +end |
| 184 | + |
| 185 | +function M.get_binary_path() |
| 186 | + local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':h:h') |
| 187 | + return get_binary_path(plugin_dir) |
| 188 | +end |
| 189 | + |
| 190 | +return M |
0 commit comments