forked from solana-program/create-solana-program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversionRust.ts
More file actions
73 lines (68 loc) · 2.19 KB
/
versionRust.ts
File metadata and controls
73 lines (68 loc) · 2.19 KB
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 { hasCommand, spawnCommand } from './commands';
import { Language } from './localization';
import { logWarning } from './logs';
import {
assertIsValidVersion,
compareVersions,
getVersionAndVersionWithoutPatch,
getVersionFromStdout,
ResolvedVersion,
Version,
VersionWithoutPatch,
} from './versionCore';
export async function detectRustVersion(): Promise<Version | undefined> {
const hasRustc = await hasCommand('rustc');
if (!hasRustc) {
return undefined;
}
return getVersionFromStdout(spawnCommand('rustc', ['--version']));
}
export function resolveRustVersion(
language: Language,
solanaVersion: ResolvedVersion,
inputVersion: string | undefined,
detectedVersion: Version | undefined
): ResolvedVersion {
const solanaToRustMap: Record<VersionWithoutPatch, Version> = {
'1.17': '1.75.0',
'1.18': '1.75.0',
'2.0': '1.75.0',
'2.1': '1.79.0',
'2.2': '1.79.0',
};
const fallbackVersion =
solanaToRustMap[solanaVersion.withoutPatch] ?? '1.79.0';
const version = inputVersion ?? detectedVersion ?? fallbackVersion;
assertIsValidVersion(language, 'Rust', version);
const [full, withoutPatch] = getVersionAndVersionWithoutPatch(version);
const rustVersion = { full, withoutPatch, detected: detectedVersion };
warnAboutSolanaRustVersionMismatch(language, rustVersion, solanaVersion);
return rustVersion;
}
function warnAboutSolanaRustVersionMismatch(
language: Language,
rustVersion: ResolvedVersion,
solanaVersion: ResolvedVersion
) {
const minimumViableRustVersionPerSolanaVersion: Record<
VersionWithoutPatch,
Version
> = {
'1.17': '1.68.0',
'1.18': '1.75.0',
'2.0': '1.75.0',
'2.1': '1.79.0',
'2.2': '1.79.0',
};
const minimumViableRustVersion: Version | undefined =
minimumViableRustVersionPerSolanaVersion[solanaVersion.withoutPatch];
if (!minimumViableRustVersion) return;
if (compareVersions(rustVersion.full, minimumViableRustVersion) < 0) {
logWarning(
language.errors.rustVersionIncompatibleWithSolana
.replace('$solanaVersion', solanaVersion.withoutPatch)
.replace('$minimumRustVersion', minimumViableRustVersion)
.replace('$rustVersion', rustVersion.full)
);
}
}