Skip to content

Commit

Permalink
chore: make cargo-contract extraction generic
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 12, 2024
1 parent 53e6d65 commit f63fff4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
17 changes: 5 additions & 12 deletions src/commands/check/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Listr } from "listr2";
import { commandStdoutOrNull } from "../../lib/index.js";
import { commandStdoutOrNull, extractCargoContractVersion } from "../../lib/index.js";
import { SwankyConfig } from "../../types/index.js";
import { pathExistsSync, readJSON, writeJson } from "fs-extra/esm";
import { readFileSync } from "fs";
Expand Down Expand Up @@ -118,19 +118,12 @@ export default class Check extends SwankyCommand<typeof Check> {
{
title: "Check cargo-contract",
task: async (ctx, task) => {
ctx.versions.tools.cargoContract = commandStdoutOrNull("cargo contract -V");
if (!ctx.versions.tools.cargoContract) {
const cargoContractVersion = extractCargoContractVersion();
ctx.versions.tools.cargoContract = cargoContractVersion;
if (!cargoContractVersion) {
throw new Error("Cargo contract is not installed");
}

const regex = /cargo-contract-contract (\d+\.\d+\.\d+(?:-[\w.]+)?)(?:-unknown-[\w-]+)/;
const match = ctx.versions.tools.cargoContract.match(regex);
if (match?.[1]) {
ctx.versions.tools.cargoContract = match[1];
} else {
throw new Error("Cargo contract version not found");
}
task.title = `Check cargo-contract: ${ctx.versions.tools.cargoContract}`;
task.title = `Check cargo-contract: ${cargoContractVersion}`;
},
exitOnError: false,
},
Expand Down
12 changes: 10 additions & 2 deletions src/commands/contract/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Args, Flags } from "@oclif/core";
import path from "node:path";
import { ensureCargoContractVersionCompatibility, generateTypes, Spinner, storeArtifacts } from "../../lib/index.js";
import { ensureCargoContractVersionCompatibility, extractCargoContractVersion, generateTypes, Spinner, storeArtifacts } from "../../lib/index.js";
import { spawn } from "node:child_process";
import { pathExists } from "fs-extra/esm";
import { SwankyCommand } from "../../lib/swankyCommand.js";
Expand Down Expand Up @@ -76,7 +76,15 @@ export class CompileContract extends SwankyCommand<typeof CompileContract> {
compileArgs.push("--release");
}
if (flags.verifiable) {
ensureCargoContractVersionCompatibility("4.0.0", ["4.0.0-alpha"]);
const cargoContractVersion = extractCargoContractVersion();
if (cargoContractVersion === null)
throw new InputError(
`Cargo contract tool is required for verifiable mode. Please ensure it is installed.`
);

ensureCargoContractVersionCompatibility(cargoContractVersion, "4.0.0", [
"4.0.0-alpha",
]);
compileArgs.push("--verifiable");
}
const compile = spawn("cargo", compileArgs);
Expand Down
12 changes: 10 additions & 2 deletions src/commands/contract/verify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Args, Flags } from "@oclif/core";
import path from "node:path";
import { ensureCargoContractVersionCompatibility, Spinner } from "../../lib/index.js";
import { ensureCargoContractVersionCompatibility, extractCargoContractVersion, Spinner } from "../../lib/index.js";
import { pathExists } from "fs-extra/esm";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, InputError, ProcessError } from "../../lib/errors.js";
Expand Down Expand Up @@ -29,7 +29,15 @@ export class VerifyContract extends SwankyCommand<typeof VerifyContract> {
async run(): Promise<void> {
const { args, flags } = await this.parse(VerifyContract);

ensureCargoContractVersionCompatibility("4.0.0", ["4.0.0-alpha"]);
const cargoContractVersion = extractCargoContractVersion();
if (cargoContractVersion === null)
throw new InputError(
`Cargo contract tool is required for verifiable mode. Please ensure it is installed.`
);

ensureCargoContractVersionCompatibility(cargoContractVersion, "4.0.0", [
"4.0.0-alpha",
]);

if (args.contractName === undefined && !flags.all) {
throw new InputError("No contracts were selected to verify", { winston: { stack: true } });
Expand Down
32 changes: 17 additions & 15 deletions src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { nodeInfo } from "./nodeInfo.js";
import decompress from "decompress";
import { Spinner } from "./spinner.js";
import { SupportedPlatforms, SupportedArch } from "../types/index.js";
import { ConfigError, InputError, NetworkError } from "./errors.js";
import { ConfigError, NetworkError, ProcessError } from "./errors.js";
import semver from "semver";
import { commandStdoutOrNull } from "./command-utils.js";

Expand Down Expand Up @@ -139,35 +139,37 @@ export async function installDeps(projectPath: string) {
}
}

export function ensureCargoContractVersionCompatibility(
minimalVersion: string,
invalidVersionsList: string[]
) {
export function extractCargoContractVersion() {
const regex = /cargo-contract-contract (\d+\.\d+\.\d+(?:-[\w.]+)?)(?:-unknown-[\w-]+)/;
const cargoContractVersionOutput = commandStdoutOrNull("cargo contract -V");
if (!cargoContractVersionOutput) {
throw new InputError(
`Cargo contract tool is required for verifiable mode. Please ensure it is installed.`
);
return null
}

const match = cargoContractVersionOutput.match(regex);
if (!match) {
throw new InputError(
throw new ProcessError(
`Unable to determine cargo-contract version. Please verify its installation.`
);
}

const cargoContractVersion = match[1].replace(/-.*$/, ""); // Remove pre-release identifiers for version comparison
if (invalidVersionsList.includes(cargoContractVersion)) {
throw new InputError(
return match[1];
}

export function ensureCargoContractVersionCompatibility(
cargoContractVersion: string,
minimalVersion: string,
invalidVersionsList?: string[]
) {
if (invalidVersionsList?.includes(cargoContractVersion)) {
throw new ProcessError(
`The cargo-contract version ${cargoContractVersion} is not supported. Please update or change the version.`
);
}

if (!semver.satisfies(cargoContractVersion, `>=${minimalVersion}`)) {
throw new InputError(
`Verifiable mode requires cargo-contract version >= ${minimalVersion}, but found version ${cargoContractVersion}. Please update to a compatible version.`
if (!semver.satisfies(cargoContractVersion.replace(/-.*$/, ""), `>=${minimalVersion}`)) {
throw new ProcessError(
`cargo-contract version >= ${minimalVersion} required, but found version ${cargoContractVersion}. Please update to a compatible version.`
);
}
}

0 comments on commit f63fff4

Please sign in to comment.