|
1 | 1 | const { |
2 | | - exec |
| 2 | + exec |
3 | 3 | } = require("child_process"); |
4 | 4 | const fs = require("fs"); |
5 | 5 | const path = require("path"); |
6 | 6 | const yaml = require("js-yaml"); |
7 | 7 | const githubCore = require("@actions/core"); |
8 | 8 |
|
9 | 9 | async function runCommand(cmd) { |
10 | | - return new Promise((resolve, reject) => { |
11 | | - exec(cmd, (error, stdout, stderr) => { |
12 | | - if (error) reject(error); |
13 | | - else resolve(stdout.trim()); |
14 | | - }); |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + exec(cmd, (error, stdout, stderr) => { |
| 12 | + if (error) reject(error); |
| 13 | + else resolve(stdout.trim()); |
15 | 14 | }); |
| 15 | + }); |
16 | 16 | } |
17 | 17 |
|
18 | 18 | function getBaseUpperBound(baseBound) { |
19 | | - const baseBoundMatch = baseBound.match(/(<=|<)\s*((\d+)(\.(\d+))?(\.(\d+))?)/); |
20 | | - if (!baseBoundMatch) return null; |
| 19 | + const baseBoundMatch = baseBound.match(/(<=|<)\s*((\d+)(\.(\d+))?(\.(\d+))?)/); |
| 20 | + if (!baseBoundMatch) return null; |
21 | 21 |
|
22 | | - const operator = baseBoundMatch[1]; |
23 | | - const version = baseBoundMatch[2]; |
24 | | - const inclusive = operator === "<="; |
| 22 | + const operator = baseBoundMatch[1]; |
| 23 | + const version = baseBoundMatch[2]; |
| 24 | + const inclusive = operator === "<="; |
25 | 25 |
|
26 | | - return { |
27 | | - inclusive, |
28 | | - version |
29 | | - }; |
| 26 | + return { |
| 27 | + inclusive, |
| 28 | + version |
| 29 | + }; |
30 | 30 | } |
31 | 31 |
|
32 | 32 | function normalizeVersion(version, segments = 3) { |
33 | | - const parts = version.split('.').map(Number); |
34 | | - while (parts.length < segments) parts.push(0); |
35 | | - return parts.slice(0, segments).join('.'); |
| 33 | + const parts = version.split('.').map(Number); |
| 34 | + while (parts.length < segments) parts.push(0); |
| 35 | + return parts.slice(0, segments).join('.'); |
36 | 36 | } |
37 | 37 |
|
38 | 38 | function compareVersions(a, b) { |
39 | | - const pa = normalizeVersion(a).split('.').map(Number); |
40 | | - const pb = normalizeVersion(b).split('.').map(Number); |
41 | | - const len = Math.max(pa.length, pb.length); |
42 | | - |
43 | | - for (let i = 0; i < len; i++) { |
44 | | - const na = pa[i] || 0; |
45 | | - const nb = pb[i] || 0; |
46 | | - if (na > nb) return 1; |
47 | | - if (na < nb) return -1; |
48 | | - } |
49 | | - return 0; |
| 39 | + const pa = normalizeVersion(a).split('.').map(Number); |
| 40 | + const pb = normalizeVersion(b).split('.').map(Number); |
| 41 | + const len = Math.max(pa.length, pb.length); |
| 42 | + |
| 43 | + for (let i = 0; i < len; i++) { |
| 44 | + const na = pa[i] || 0; |
| 45 | + const nb = pb[i] || 0; |
| 46 | + if (na > nb) return 1; |
| 47 | + if (na < nb) return -1; |
| 48 | + } |
| 49 | + return 0; |
50 | 50 | } |
51 | 51 |
|
52 | 52 | function versionLess(baseVersion, bound) { |
53 | | - const cmp = compareVersions(baseVersion, bound.version); |
54 | | - return cmp < 0 || (cmp === 0 && bound.inclusive); |
| 53 | + const cmp = compareVersions(baseVersion, bound.version); |
| 54 | + return cmp < 0 || (cmp === 0 && bound.inclusive); |
55 | 55 | } |
56 | 56 |
|
57 | 57 | function parseBaseUpperBound(packageYamlPath) { |
58 | | - const fileContent = fs.readFileSync(packageYamlPath, "utf-8"); |
59 | | - const parsed = yaml.load(fileContent); |
| 58 | + const fileContent = fs.readFileSync(packageYamlPath, "utf-8"); |
| 59 | + const parsed = yaml.load(fileContent); |
| 60 | + |
| 61 | + const deps = parsed.dependencies; |
| 62 | + if (!deps || !Array.isArray(deps)) { |
| 63 | + throw new Error("dependencies not found or invalid in package.yaml"); |
| 64 | + } |
| 65 | + |
| 66 | + const baseDep = deps.find(dep => { |
| 67 | + if (typeof dep === "string") { |
| 68 | + return dep.startsWith("base"); |
| 69 | + } else if (typeof dep === "object" && dep.name) { |
| 70 | + return dep.name === "base"; |
| 71 | + } |
| 72 | + return false; |
| 73 | + }); |
| 74 | + |
| 75 | + if (!baseDep) { |
| 76 | + githubCore.setFailed("No base dependency found in package.yaml"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + let versionConstraint; |
| 81 | + if (typeof baseDep === "string") { |
| 82 | + versionConstraint = getBaseUpperBound(baseDep); |
| 83 | + } else if (typeof baseDep === "object") { |
| 84 | + versionConstraint = getBaseUpperBound(baseDep.version); |
| 85 | + } |
| 86 | + |
| 87 | + if (!versionConstraint) { |
| 88 | + githubCore.setFailed("No upper bound for base found in package.yaml"); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + return versionConstraint; |
| 93 | +} |
60 | 94 |
|
61 | | - const deps = parsed.dependencies; |
62 | | - if (!deps || !Array.isArray(deps)) { |
63 | | - throw new Error("dependencies not found or invalid in package.yaml"); |
| 95 | +async function main() { |
| 96 | + try { |
| 97 | + const packageYamlPath = githubCore.getInput("package-yaml-path") || path.join(process.cwd(), "package.yaml"); |
| 98 | + const baseUpperBound = parseBaseUpperBound(packageYamlPath); |
| 99 | + |
| 100 | + const ghcupListStr = await runCommand("ghcup list -t ghc -r"); |
| 101 | + const lines = ghcupListStr.split("\n").filter(Boolean); |
| 102 | + |
| 103 | + const ghcupList = lines.map(line => { |
| 104 | + const match = line.match(/^ghc\s([^\s]+)\s.*?base-([0-9.]+)/); |
| 105 | + if (!match) return null; |
| 106 | + return { |
| 107 | + version: match[1], |
| 108 | + base: match[2] |
| 109 | + }; |
| 110 | + }).filter(Boolean); |
| 111 | + |
| 112 | + if (ghcupList.length > 0) { |
| 113 | + githubCore.info(`Found ${ghcupList.length} GHC versions`); |
| 114 | + } else { |
| 115 | + githubCore.setFailed('Failed to get GHC versions from GHCup'); |
64 | 116 | } |
65 | 117 |
|
66 | | - const baseDep = deps.find(dep => { |
67 | | - if (typeof dep === "string") { |
68 | | - return dep.startsWith("base"); |
69 | | - } else if (typeof dep === "object" && dep.name) { |
70 | | - return dep.name === "base"; |
71 | | - } |
72 | | - return false; |
| 118 | + const validVersions = ghcupList.filter(ghcEntry => { |
| 119 | + return versionLess(ghcEntry.base, baseUpperBound); |
73 | 120 | }); |
74 | 121 |
|
75 | | - if (!baseDep) { |
76 | | - githubCore.setFailed("No base dependency found in package.yaml"); |
77 | | - return; |
| 122 | + if (validVersions.length === 0) { |
| 123 | + throw new Error(`No GHC version found with base <${baseUpperBound.inclusive ? "=" : ""} ${baseUpperBound.version}`); |
78 | 124 | } |
79 | 125 |
|
80 | | - let versionConstraint; |
81 | | - if (typeof baseDep === "string") { |
82 | | - versionConstraint = getBaseUpperBound(baseDep); |
83 | | - } else if (typeof baseDep === "object") { |
84 | | - versionConstraint = getBaseUpperBound(baseDep.version); |
85 | | - } |
| 126 | + validVersions.sort((a, b) => { |
| 127 | + const aVer = a.version.split('.').map(Number); |
| 128 | + const bVer = b.version.split('.').map(Number); |
| 129 | + for (let i = 0; i < Math.max(aVer.length, bVer.length); i++) { |
| 130 | + const n1 = aVer[i] || 0; |
| 131 | + const n2 = bVer[i] || 0; |
| 132 | + if (n1 > n2) return -1; |
| 133 | + if (n1 < n2) return 1; |
| 134 | + } |
| 135 | + return 0; |
| 136 | + }); |
86 | 137 |
|
87 | | - if (!versionConstraint) { |
88 | | - githubCore.setFailed("No upper bound for base found in package.yaml"); |
89 | | - return; |
90 | | - } |
| 138 | + const latestGhc = validVersions[0].version; |
91 | 139 |
|
92 | | - return versionConstraint; |
93 | | -} |
| 140 | + githubCore.info(`Latest GHC under base < ${baseUpperBound.version}: ${latestGhc}`); |
94 | 141 |
|
95 | | -async function main() { |
96 | | - try { |
97 | | - const packageYamlPath = githubCore.getInput("package-yaml-path") || path.join(process.cwd(), "package.yaml"); |
98 | | - const baseUpperBound = parseBaseUpperBound(packageYamlPath); |
99 | | - |
100 | | - const ghcupListStr = await runCommand("ghcup list -t ghc -r"); |
101 | | - const lines = ghcupListStr.split("\n").filter(Boolean); |
102 | | - |
103 | | - const ghcupList = lines.map(line => { |
104 | | - const match = line.match(/^ghc\s([^\s]+)\s.*?base-([0-9.]+)/); |
105 | | - if (!match) return null; |
106 | | - return { |
107 | | - version: match[1], |
108 | | - base: match[2] |
109 | | - }; |
110 | | - }).filter(Boolean); |
111 | | - |
112 | | - if (ghcupList.length > 0) { |
113 | | - githubCore.info(`Found ${ghcupList.length} GHC versions`); |
114 | | - } else { |
115 | | - githubCore.setFailed('Failed to get GHC versions from GHCup'); |
116 | | - } |
117 | | - |
118 | | - const validVersions = ghcupList.filter(ghcEntry => { |
119 | | - return versionLess(ghcEntry.base, baseUpperBound); |
120 | | - }); |
121 | | - |
122 | | - if (validVersions.length === 0) { |
123 | | - throw new Error(`No GHC version found with base <${baseUpperBound.inclusive ? "=" : ""} ${baseUpperBound.version}`); |
124 | | - } |
125 | | - |
126 | | - validVersions.sort((a, b) => { |
127 | | - const aVer = a.version.split('.').map(Number); |
128 | | - const bVer = b.version.split('.').map(Number); |
129 | | - for (let i = 0; i < Math.max(aVer.length, bVer.length); i++) { |
130 | | - const n1 = aVer[i] || 0; |
131 | | - const n2 = bVer[i] || 0; |
132 | | - if (n1 > n2) return -1; |
133 | | - if (n1 < n2) return 1; |
134 | | - } |
135 | | - return 0; |
136 | | - }); |
137 | | - |
138 | | - const latestGhc = validVersions[0].version; |
139 | | - |
140 | | - githubCore.info(`Latest GHC under base < ${baseUpperBound.version}: ${latestGhc}`); |
141 | | - |
142 | | - githubCore.setOutput('ghc-version', latestGhc); |
143 | | - } catch (err) { |
144 | | - githubCore.setFailed(err.message); |
145 | | - } |
| 142 | + githubCore.setOutput('ghc-version', latestGhc); |
| 143 | + } catch (err) { |
| 144 | + githubCore.setFailed(err.message); |
| 145 | + } |
146 | 146 | } |
147 | 147 |
|
148 | 148 | main(); |
0 commit comments