-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve.ts
73 lines (64 loc) · 1.81 KB
/
resolve.ts
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
import { getExecOutput } from '@actions/exec';
import * as apt from './apt';
import * as ghcup from './ghcup';
export type Ordering = -1 | 0 | 1
export function compareVersions(a: string, b: string): Ordering {
const xs = a.split('.');
const ys = b.split('.');
const range = xs.length < ys.length ? ys : xs;
for (const i in range) {
const x = Number(xs[i] || 0);
const y = Number(ys[i] || 0);
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
}
return 0;
}
export function resolveVersion(requested: string, versions: Set<string>): string | undefined {
if (versions.has(requested)) {
return requested;
} else {
const prefix = requested == 'latest' ? '' : requested + '.';
return [...versions]
.filter(x => x.startsWith(prefix))
.sort(compareVersions)
.reverse()[0];
}
}
export type ResolvedVersion = {
version: string;
source: 'system' | 'apt' | 'ghcup';
};
export async function resolve(requested: string): Promise<ResolvedVersion> {
const systemVersion = await installed();
const aptVersions = apt.versions();
let version;
if (requested === 'system') {
version = systemVersion;
} else if (apt.versions().has(requested)) {
version = requested;
} else {
const versions = new Set([...apt.versions(), ...await ghcup.list()]);
version = resolveVersion(requested, versions);
}
if (version) {
const source = version === systemVersion ? 'system' :
aptVersions.has(version) ? 'apt' : 'ghcup';
return {
version,
source,
};
} else {
throw new Error(`GHC version ${JSON.stringify(requested)} is not available.`);
}
}
export async function installed(): Promise<string> {
const result = await getExecOutput('ghc', ['--numeric-version'], {
silent: true,
});
return result.stdout.trim();
}