|
| 1 | +local uv = vim.loop |
| 2 | +local plugin_path = string.sub(debug.getinfo(1).source, 2, #"/lua/telescope-fzf-native/download_library.lua" * -1) |
| 3 | +local releases_url = "https://github.com/nvim-telescope/telescope-fzf-native.nvim/releases/download" |
| 4 | + |
| 5 | +local get_platform = function() |
| 6 | + if vim.fn.has("win32") == 1 then |
| 7 | + return 'windows' |
| 8 | + end |
| 9 | + |
| 10 | + if vim.fn.has("mac") then |
| 11 | + return 'macos' |
| 12 | + end |
| 13 | + |
| 14 | + return "ubuntu" |
| 15 | +end |
| 16 | + |
| 17 | +local get_valid_compiler = function(platform) |
| 18 | + if platform == 'windows' then |
| 19 | + return 'cc' |
| 20 | + elseif platform == 'macos' then |
| 21 | + return 'gcc' |
| 22 | + elseif platform == 'linux' then |
| 23 | + return 'gcc' |
| 24 | + end |
| 25 | +end |
| 26 | + |
| 27 | +local path_join = function(strings) |
| 28 | + local path_separator = (platform == "windows") and '\\' or '/' |
| 29 | + |
| 30 | + return table.concat(strings, path_separator) |
| 31 | +end |
| 32 | + |
| 33 | +local write_async = function(path, txt, flag) |
| 34 | + uv.fs_open(path, flag, 438, function(open_err, fd) |
| 35 | + assert(not open_err, open_err) |
| 36 | + uv.fs_write(fd, txt, -1, function(write_err) |
| 37 | + assert(not write_err, write_err) |
| 38 | + uv.fs_close(fd, function(close_err) |
| 39 | + assert(not close_err, close_err) |
| 40 | + end) |
| 41 | + end) |
| 42 | + end) |
| 43 | +end |
| 44 | + |
| 45 | +local spawn = function(cmd, on_exit) |
| 46 | + local stdout = uv.new_pipe() |
| 47 | + |
| 48 | + local buffer = "" |
| 49 | + |
| 50 | + local real_cmd = table.remove(cmd, 1) |
| 51 | + |
| 52 | + uv.spawn(real_cmd, { |
| 53 | + args = cmd, |
| 54 | + stdio = { nil, stdout }, |
| 55 | + verbatim = true |
| 56 | + }, function() |
| 57 | + stdout:read_stop() |
| 58 | + if (type(on_exit) == "function") then |
| 59 | + on_exit(buffer) |
| 60 | + end |
| 61 | + end) |
| 62 | + |
| 63 | + uv.read_start(stdout, function(err, data) |
| 64 | + assert(not err, err) |
| 65 | + if data then |
| 66 | + buffer = buffer .. data |
| 67 | + end |
| 68 | + end) |
| 69 | + |
| 70 | +end |
| 71 | + |
| 72 | +local download = function(options) |
| 73 | + options = options or {} |
| 74 | + local platform = options.platform or get_platform() |
| 75 | + local compiler = options.compiler or get_valid_compiler(platform) |
| 76 | + local version = options.version or "latest" |
| 77 | + |
| 78 | + local command = nil |
| 79 | + |
| 80 | + if platform == 'windows' then |
| 81 | + command = { |
| 82 | + 'curl', '-L', |
| 83 | + string.format("%s/%s/windows-2019-%s-libfzf.dll", releases_url, version, compiler), |
| 84 | + '-o', path_join({ plugin_path, 'build', 'libfzf.dll' }) |
| 85 | + } |
| 86 | + end |
| 87 | + |
| 88 | + if platform == 'ubuntu' then |
| 89 | + command = { |
| 90 | + "curl", "-L", |
| 91 | + string.format("%s/%s/ubuntu-%s-libfzf.so", releases_url, version, compiler), |
| 92 | + "-o", path_join({ plugin_path, 'build', 'libfzf.so' }) |
| 93 | + } |
| 94 | + end |
| 95 | + |
| 96 | + if platform == 'macos' then |
| 97 | + command = { |
| 98 | + "curl", "-L", |
| 99 | + string.format("%s/%s/macos-%s-libfzf.so", releases_url, version, compiler), |
| 100 | + "-o", path_join({ plugin_path, 'build', 'libfzf.so' }) |
| 101 | + } |
| 102 | + end |
| 103 | + |
| 104 | + -- |
| 105 | + -- Ensure the Build directory exists |
| 106 | + -- |
| 107 | + -- using format, becase we need to run the command in a subshell on windows. |
| 108 | + -- |
| 109 | + spawn({ |
| 110 | + 'sh', |
| 111 | + '-c', |
| 112 | + string.format("' mkdir %s'", path_join({ plugin_path, 'build' })) |
| 113 | + }) |
| 114 | + |
| 115 | + -- |
| 116 | + -- Curl the download |
| 117 | + -- |
| 118 | + spawn(command) |
| 119 | + |
| 120 | +end |
| 121 | + |
| 122 | +return download |
0 commit comments