function PLUGIN:Available(ctx)
local http = require("http")
local base_url = os.getenv("ANDROID_SDK_MIRROR_URL") or "https://dl.google.com/android/repository"
local metadata_url = base_url .. "/repository2-3.xml"
local resp = http.get({ url = metadata_url })
if resp.status_code ~= 200 then
error("Failed to fetch Android SDK metadata: HTTP " .. resp.status_code)
end
local versions = {}
local seen = {}
for path_attr in resp.body:gmatch('remotePackage%s+path="([^"]+)"') do
local version = path_attr:match("^cmdline%-tools;(.+)$")
if version and version ~= "latest" and not seen[version] then
seen[version] = true
table.insert(versions, {
version = version,
note = "",
})
end
end
table.sort(versions, function(a, b)
local a_number = tonumber(a.version)
local b_number = tonumber(b.version)
if a_number and b_number then
return a_number > b_number
elseif a_number then
return true
elseif b_number then
return false
end
return a.version > b.version
end)
return versions
end