-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpath_finder.lua
More file actions
231 lines (187 loc) · 6.27 KB
/
path_finder.lua
File metadata and controls
231 lines (187 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
local uv = vim.uv or vim.loop
local Utils = require("eca.utils")
local Logger = require("eca.logger")
---@class eca.PathFinder
---@field private _cache_dir string
---@field private _version_file string
local M = {}
M.__index = M
---@return eca.PathFinder
function M:new()
local instance = setmetatable({}, M)
instance._cache_dir = Utils.get_cache_dir()
instance._version_file = instance._cache_dir .. "/eca-version"
return instance
end
---@return table<string, table<string, string>>
function M:_get_artifacts()
return {
darwin = {
x86_64 = "eca-native-macos-amd64.zip",
arm64 = "eca-native-macos-aarch64.zip",
},
linux = {
x86_64 = "eca-native-static-linux-amd64.zip",
aarch64 = "eca-native-linux-aarch64.zip",
arm64 = "eca-native-linux-aarch64.zip",
},
windows = {
x86_64 = "eca-native-windows-amd64.zip",
},
}
end
---@param platform? string
---@param arch? string
---@return string
function M:_get_artifact_name(platform, arch)
platform = platform or uv.os_uname().sysname:lower()
arch = arch or uv.os_uname().machine
-- Normalize platform names
if platform:match("darwin") then
platform = "darwin"
elseif platform:match("linux") then
platform = "linux"
elseif platform:match("windows") or platform:match("mingw") or platform:match("msys") then
platform = "windows"
end
local artifacts = self:_get_artifacts()
local platform_artifacts = artifacts[platform]
if not platform_artifacts then
error("Unsupported platform: " .. platform)
end
return platform_artifacts[arch] or "eca.jar"
end
---@param platform? string
---@param arch? string
---@return string
function M:_get_extension_server_path(platform, arch)
local artifact_name = self:_get_artifact_name(platform, arch)
local name
if artifact_name:match("%.jar$") then
name = "eca.jar"
else
platform = platform or uv.os_uname().sysname:lower()
name = platform:match("windows") and "eca.exe" or "eca"
end
return self._cache_dir .. "/" .. name
end
---@return string?
function M:_read_version_file()
local file = io.open(self._version_file, "r")
if not file then
return nil
end
local version = file:read("*a"):gsub("%s+", "")
file:close()
return version ~= "" and version or nil
end
---@param version string
function M:_write_version_file(version)
-- Ensure cache directory exists
vim.fn.mkdir(self._cache_dir, "p")
local file = io.open(self._version_file, "w")
if file then
file:write(version)
file:close()
else
Logger.warn("Could not write version file: " .. self._version_file)
end
end
---@return string?
function M:_get_latest_version()
local cmd = "curl -s https://api.github.com/repos/editor-code-assistant/eca/releases/latest"
local handle = io.popen(cmd .. " 2>/dev/null")
if not handle then
Logger.notify("Failed to check for latest ECA version", vim.log.levels.WARN)
return nil
end
local response = handle:read("*a")
handle:close()
if not response or response == "" then
return nil
end
-- Parse JSON to get tag_name
local tag_match = response:match('"tag_name"%s*:%s*"([^"]+)"')
return tag_match
end
---@param server_path string
---@param version string
---@return boolean success
function M:_download_latest_server(server_path, version)
local artifact_name = self:_get_artifact_name()
local download_url =
string.format("https://github.com/editor-code-assistant/eca/releases/download/%s/%s", version, artifact_name)
local download_path = self._cache_dir .. "/" .. artifact_name
Logger.debug("Downloading latest ECA server version from: " .. download_url)
-- Ensure cache directory exists
vim.fn.mkdir(self._cache_dir, "p")
-- Download the file
local download_cmd = string.format(
"curl -L --fail --show-error --silent -o %s %s",
vim.fn.shellescape(download_path),
vim.fn.shellescape(download_url)
)
local download_result = os.execute(download_cmd)
if download_result ~= 0 then
error("Failed to download ECA server from: " .. download_url)
end
-- Extract if it's a zip file
if artifact_name:match("%.zip$") then
local extract_cmd =
string.format("cd %s && unzip -o %s", vim.fn.shellescape(self._cache_dir), vim.fn.shellescape(artifact_name))
local extract_result = os.execute(extract_cmd)
if extract_result ~= 0 then
error("Failed to extract ECA server")
end
-- Remove the zip file after extraction
os.remove(download_path)
end
-- Make executable (if not Windows)
if not uv.os_uname().sysname:lower():match("windows") then
os.execute("chmod +x " .. vim.fn.shellescape(server_path))
end
if not Utils.file_exists(server_path) then
error("ECA server binary not found after download and extraction")
end
-- Write version file
self:_write_version_file(version)
Logger.debug("ECA server downloaded successfully")
return true
end
---@return string
function M:find(custom_path)
-- Check for custom server path first
if custom_path and custom_path:gsub("%s+", "") ~= "" then
if not Utils.file_exists(custom_path) then
error("Custom server path does not exist: " .. custom_path)
end
return custom_path
end
local server_path = self:_get_extension_server_path()
local latest_version = self:_get_latest_version()
local current_version = self:_read_version_file()
local server_exists = Utils.file_exists(server_path)
-- If we can't get the latest version and server doesn't exist, that's an error
if not latest_version and not server_exists then
error(
"Could not fetch latest version of ECA. Please check your internet connection and try again. You can also download ECA manually and set the path in the settings."
)
end
-- Download if server doesn't exist or version is outdated
if not server_exists or (latest_version and current_version ~= latest_version) then
if not latest_version then
Logger.warn("Could not check for latest version, using existing server")
return server_path
end
local success
local ok, err = pcall(function()
success = self:_download_latest_server(server_path, latest_version)
end)
if not ok or not success then
error((err and tostring(err)) or "Failed to download ECA server")
end
end
Logger.debug("Using ECA server: " .. server_path)
return server_path
end
return M