|
| 1 | +--- Parse the legacy file found by vfox to determine the version of the tool. |
| 2 | +--- Useful to extract version numbers from files like JavaScript's package.json or Golangs go.mod. |
| 3 | +function PLUGIN:ParseLegacyFile(ctx) |
| 4 | + local filepath = ctx.filepath |
| 5 | + local file = io.open(filepath, "r") |
| 6 | + local content = file:read("*a") |
| 7 | + file:close() |
| 8 | + content = content:gsub("%s+", "") |
| 9 | + if content == "" then |
| 10 | + return {} |
| 11 | + end |
| 12 | + function resolve_legacy_version(strategy, query) |
| 13 | + local list = {} |
| 14 | + |
| 15 | + if strategy == "latest_installed" then |
| 16 | + list = ctx:getInstalledVersions() |
| 17 | + elseif strategy == "latest_available" then |
| 18 | + for _, av in pairs(self:Available({})) do |
| 19 | + table.insert(list, av.version) |
| 20 | + end |
| 21 | + else |
| 22 | + -- Just return the original query |
| 23 | + return query |
| 24 | + end |
| 25 | + |
| 26 | + local resolved = "" |
| 27 | + for _, item in pairs(list) do |
| 28 | + if item:match("^" .. query) then |
| 29 | + resolved = item |
| 30 | + break |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + if resolved ~= "" then |
| 35 | + return resolved |
| 36 | + elseif strategy ~= "latest_available" then |
| 37 | + -- If no version is installed, fallback to latest_available |
| 38 | + return resolve_legacy_version("latest_available", query) |
| 39 | + else |
| 40 | + -- Give up and pretty the query itself |
| 41 | + return query |
| 42 | + end |
| 43 | + end |
| 44 | + local query = resolve_version(content) |
| 45 | + |
| 46 | + query = resolve_legacy_version("latest_installed", query) |
| 47 | + |
| 48 | + return { |
| 49 | + version = query |
| 50 | + } |
| 51 | +end |
| 52 | + |
| 53 | +function resolve_version(query) |
| 54 | + query = string.lower(query:gsub("v", "")) |
| 55 | + |
| 56 | + if query:match("^lts-") then |
| 57 | + query = query:gsub("-", "/") |
| 58 | + end |
| 59 | + |
| 60 | + local nodejs_codenames = { |
| 61 | + argon = 4, |
| 62 | + boron = 6, |
| 63 | + carbon = 8, |
| 64 | + dubnium = 10, |
| 65 | + erbium = 12, |
| 66 | + fermium = 14, |
| 67 | + gallium = 16, |
| 68 | + hydrogen = 18, |
| 69 | + iron = 20 |
| 70 | + } |
| 71 | + |
| 72 | + for codename, version_number in pairs(nodejs_codenames) do |
| 73 | + if query == "lts/" .. codename then |
| 74 | + query = tostring(version_number) |
| 75 | + break |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + if query == "lts" or query == "lts/*" then |
| 80 | + query = tostring(nodejs_codenames[#nodejs_codenames]) |
| 81 | + end |
| 82 | + |
| 83 | + return query |
| 84 | +end |
| 85 | + |
0 commit comments