-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcheck.ts
311 lines (289 loc) · 10.9 KB
/
check.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { Listr } from "listr2";
import {
extractCargoContractVersion,
extractCargoDylintVersion,
extractCargoNightlyVersion,
extractCargoVersion,
extractRustVersion,
} from "../../lib/index.js";
import { SwankyConfig } from "../../types/index.js";
import { pathExistsSync, writeJson } from "fs-extra/esm";
import { readFileSync } from "fs";
import path from "node:path";
import TOML from "@iarna/toml";
import semver from "semver";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { Flags } from "@oclif/core";
import chalk from "chalk";
import { CARGO_CONTRACT_INK_DEPS } from "../../lib/cargoContractInfo.js";
import { CLIError } from "@oclif/core/lib/errors/index.js";
import Warn = CLIError.Warn;
interface Ctx {
os: {
platform: string;
architecture: string;
};
versions: {
tools: {
rust?: string | null;
cargo?: string | null;
cargoNightly?: string | null;
cargoDylint?: string | null;
cargoContract?: string | null;
};
supportedInk?: string;
missingTools: string[];
contracts: Record<string, Record<string, string>>;
swankyNode: string | null;
};
swankyConfig: SwankyConfig;
mismatchedVersions: Record<string, string>;
looseDefinitionDetected: boolean;
}
export class Check extends SwankyCommand<typeof Check> {
static description = "Check installed package versions and compatibility";
static flags = {
print: Flags.string({
char: "o",
description: "File to write output to",
}),
};
constructor(argv: string[], baseConfig: any) {
super(argv, baseConfig);
(this.constructor as typeof SwankyCommand).ENSURE_SWANKY_CONFIG = false;
}
public async run(): Promise<void> {
const { flags } = await this.parse(Check);
const swankyNodeVersion = this.swankyConfig.node.version;
const isSwankyNodeInstalled = !!swankyNodeVersion;
const anyContracts = Object.keys(this.swankyConfig.contracts ?? {}).length > 0;
const tasks = new Listr<Ctx>([
{
title: "Check OS",
task: async (ctx, task) => {
ctx.os.platform = process.platform;
ctx.os.architecture = process.arch;
const supportedPlatforms = ["darwin", "linux"];
const supportedArch = ["arm64", "x64"];
if (!supportedPlatforms.includes(ctx.os.platform)) {
throw new Error(`Platform ${ctx.os.platform} is not supported`);
}
if (!supportedArch.includes(ctx.os.architecture)) {
throw new Error(`Architecture ${ctx.os.architecture} is not supported`);
}
task.title = `Check OS: '${ctx.os.platform}-${ctx.os.architecture}'`;
},
exitOnError: false,
},
{
title: "Check Rust",
task: async (ctx, task) => {
ctx.versions.tools.rust = extractRustVersion();
if (!ctx.versions.tools.rust) {
throw new Error("Rust is not installed");
}
task.title = `Check Rust: ${ctx.versions.tools.rust}`;
},
exitOnError: false,
},
{
title: "Check cargo",
task: async (ctx, task) => {
ctx.versions.tools.cargo = extractCargoVersion();
if (!ctx.versions.tools.cargo) {
throw new Error("Cargo is not installed");
}
task.title = `Check cargo: ${ctx.versions.tools.cargo}`;
},
exitOnError: false,
},
{
title: "Check cargo nightly",
task: async (ctx, task) => {
ctx.versions.tools.cargoNightly = extractCargoNightlyVersion();
if (!ctx.versions.tools.cargoNightly) {
throw new Error("Cargo nightly is not installed");
}
task.title = `Check cargo nightly: ${ctx.versions.tools.cargoNightly}`;
},
exitOnError: false,
},
{
title: "Check cargo dylint",
task: async (ctx, task) => {
ctx.versions.tools.cargoDylint = extractCargoDylintVersion();
if (!ctx.versions.tools.cargoDylint) {
throw new Warn("Cargo dylint is not installed");
}
task.title = `Check cargo dylint: ${ctx.versions.tools.cargoDylint}`;
},
exitOnError: false,
},
{
title: "Check cargo-contract",
task: async (ctx, task) => {
ctx.versions.tools.cargoContract = extractCargoContractVersion();
if (!ctx.versions.tools.cargoContract) {
throw new Error("Cargo contract is not installed");
}
task.title = `Check cargo-contract: ${ctx.versions.tools.cargoContract}`;
},
exitOnError: false,
},
{
title: "Check swanky node",
task: async (ctx) => {
ctx.versions.swankyNode =
this.swankyConfig.node.version !== "" ? this.swankyConfig.node.version : null;
},
},
{
title: "Read ink dependencies",
enabled: anyContracts,
skip: (ctx) => Object.keys(ctx.swankyConfig.contracts).length == 0,
task: async (ctx) => {
for (const contract in ctx.swankyConfig.contracts) {
const tomlPath = path.resolve(`contracts/${contract}/Cargo.toml`);
const doesCargoTomlExist = pathExistsSync(tomlPath);
if (!doesCargoTomlExist) {
continue;
}
const cargoTomlString = readFileSync(tomlPath, {
encoding: "utf8",
});
const cargoToml = TOML.parse(cargoTomlString);
const inkDependencies = Object.entries(cargoToml.dependencies)
.filter(([depName]) => /^ink($|_)/.test(depName))
.map(([depName, depInfo]) => {
const dependency = depInfo as Dependency;
return [depName, dependency.version ?? dependency.tag];
});
ctx.versions.contracts[contract] = Object.fromEntries(inkDependencies);
}
},
},
{
title: "Verify ink version compatibility with Swanky node",
skip: (ctx) => Object.keys(ctx.versions.contracts).length === 0,
enabled: anyContracts && isSwankyNodeInstalled,
task: async (ctx) => {
const supportedInk = ctx.swankyConfig.node.supportedInk;
const mismatched: Record<string, string> = {};
Object.entries(ctx.versions.contracts).forEach(([contract, inkDependencies]) => {
Object.entries(inkDependencies).forEach(([depName, version]) => {
if (semver.gt(version, supportedInk)) {
mismatched[`${contract}-${depName}`] =
`Version of ${depName} (${version}) in ${chalk.yellowBright(contract)} is higher than supported ink version (${supportedInk}) in current Swanky node version (${swankyNodeVersion}). A Swanky node update can fix this warning.`;
}
if (
version.startsWith(">") ||
version.startsWith("<") ||
version.startsWith("^") ||
version.startsWith("~")
) {
ctx.looseDefinitionDetected = true;
}
});
});
ctx.mismatchedVersions = mismatched;
if (Object.entries(mismatched).length > 0) {
throw new Warn(
"Ink versions in contracts don't match the Swanky node's supported version."
);
}
},
exitOnError: false,
},
{
title: "Verify cargo contract compatibility",
skip: (ctx) => !ctx.versions.tools.cargoContract,
enabled: anyContracts,
task: async (ctx) => {
const cargoContractVersion = ctx.versions.tools.cargoContract!;
const dependencyIdx = CARGO_CONTRACT_INK_DEPS.findIndex((dep) =>
semver.satisfies(
cargoContractVersion.replace(/-.*$/, ""),
`>=${dep.minCargoContractVersion}`
)
);
if (dependencyIdx === -1) {
throw new Warn(`cargo-contract version ${cargoContractVersion} is not supported`);
}
const validInkVersionRange = CARGO_CONTRACT_INK_DEPS[dependencyIdx].validInkVersionRange;
const minCargoContractVersion =
dependencyIdx === 0
? CARGO_CONTRACT_INK_DEPS[dependencyIdx].minCargoContractVersion
: CARGO_CONTRACT_INK_DEPS[dependencyIdx - 1].minCargoContractVersion;
const mismatched: Record<string, string> = {};
Object.entries(ctx.versions.contracts).forEach(([contract, inkPackages]) => {
Object.entries(inkPackages).forEach(([inkPackage, version]) => {
if (!semver.satisfies(version, validInkVersionRange)) {
mismatched[`${contract}-${inkPackage}`] =
`Version of ${inkPackage} (${version}) in ${chalk.yellowBright(contract)} requires cargo-contract version >=${minCargoContractVersion}, but version ${cargoContractVersion} is installed`;
}
});
});
ctx.mismatchedVersions = { ...ctx.mismatchedVersions, ...mismatched };
if (Object.entries(mismatched).length > 0) {
throw new Warn("cargo-contract version mismatch");
}
},
exitOnError: false,
},
{
title: "Check for missing tools",
task: async (ctx) => {
const missingTools: string[] = [];
for (const [toolName, toolVersion] of Object.entries(ctx.versions.tools)) {
if (!toolVersion) {
missingTools.push(toolName);
if (toolName === "cargoDylint") this.warn("Cargo dylint is not installed");
else this.error(`${toolName} is not installed`);
}
}
ctx.versions.missingTools = missingTools;
if (Object.entries(missingTools).length > 0) {
throw new Warn(`Missing tools: ${missingTools.join(", ")}`);
}
},
exitOnError: false,
},
]);
const context = await tasks.run({
os: { platform: "", architecture: "" },
versions: {
tools: {},
missingTools: [],
contracts: {},
swankyNode: swankyNodeVersion || null,
},
swankyConfig: this.swankyConfig,
looseDefinitionDetected: false,
mismatchedVersions: {},
});
Object.values(context.mismatchedVersions).forEach((mismatch) => this.warn(mismatch));
if (context.looseDefinitionDetected) {
this.warn(`Some of the ink dependencies do not have a fixed version.
This can lead to accidentally installing version higher than supported by the node.
Please use "=" to install a fixed version (Example: "=3.0.1")
`);
}
const output = {
...context.os,
...context.versions,
};
const filePath = flags.print;
if (filePath !== undefined) {
await this.spinner.runCommand(
async () => {
writeJson(filePath, output, { spaces: 2 });
},
`Writing output to file ${chalk.yellowBright(filePath)}`
);
}
}
}
interface Dependency {
version?: string;
tag?: string;
}