|
| 1 | +import * as core from '@actions/core' |
| 2 | +import * as toolCache from '@actions/tool-cache' |
| 3 | +import { exec } from 'node:child_process' |
| 4 | +import { promisify } from 'node:util' |
| 5 | +import os from 'node:os' |
| 6 | + |
| 7 | +const doExec = promisify(exec) |
| 8 | + |
| 9 | +main().then(() => process.exit(0)) |
| 10 | +async function main() { |
| 11 | + try { |
| 12 | + const version = core.getInput('version', { required: true }) |
| 13 | + |
| 14 | + const url = getDownloadUrl(version) |
| 15 | + const tool = await toolCache.downloadTool(url) |
| 16 | + core.addPath(tool) |
| 17 | + |
| 18 | + const installedVersion = await determineInstalledVersion() |
| 19 | + core.setOutput('installed-version', installedVersion) |
| 20 | + } catch (err) { |
| 21 | + if (err instanceof Error) { |
| 22 | + core.setFailed(err.message) |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const archMappings = { |
| 28 | + arm64: 'aarch64', |
| 29 | + x64: 'x86_64' |
| 30 | +} |
| 31 | + |
| 32 | +function getArch() { |
| 33 | + const arch = os.arch() |
| 34 | + for (const [nodeArch, rustArch] of Object.entries(archMappings)) { |
| 35 | + if (nodeArch === arch) { |
| 36 | + return rustArch |
| 37 | + } |
| 38 | + } |
| 39 | + throw new Error( |
| 40 | + `Unsupported arch: ${arch}. We currently only support: ${Object.keys( |
| 41 | + archMappings |
| 42 | + ).join(', ')}` |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +const platformMappings = { |
| 47 | + darwin: 'darwin', |
| 48 | + linux: 'linux-gnu', |
| 49 | + win32: 'windows-msvc' |
| 50 | +} |
| 51 | + |
| 52 | +function getPlatform() { |
| 53 | + const platform = os.platform() |
| 54 | + for (const [nodePlatform, rustPlatform] of Object.entries(platformMappings)) { |
| 55 | + if (nodePlatform === platform) { |
| 56 | + return rustPlatform |
| 57 | + } |
| 58 | + } |
| 59 | + throw new Error( |
| 60 | + `Unsupported platform: ${platform}. We currently only support: ${Object.keys( |
| 61 | + platformMappings |
| 62 | + ).join(', ')}` |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +function getFileName(): string { |
| 67 | + const platform = getPlatform() |
| 68 | + const arch = getArch() |
| 69 | + |
| 70 | + return `plgt_${arch}-${platform}` |
| 71 | +} |
| 72 | + |
| 73 | +function getDownloadUrl(version: string): string { |
| 74 | + const filename = getFileName() |
| 75 | + |
| 76 | + if (version.toLowerCase() === 'latest') { |
| 77 | + return `https://github.com/supabase-community/postgres_lsp/releases/latest/download/${filename}` |
| 78 | + } else { |
| 79 | + return `https://github.com/supabase-community/postgres_lsp/releases/download/${version}/${filename}` |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +async function determineInstalledVersion(): Promise<string> { |
| 84 | + const { stdout } = await doExec('pglt --version') |
| 85 | + |
| 86 | + const version = stdout.trim() |
| 87 | + if (!version) { |
| 88 | + throw new Error('Could not determine installed PGLT version') |
| 89 | + } |
| 90 | + |
| 91 | + return version |
| 92 | +} |
0 commit comments