From f63fff47bcbb2ec6955cdc584a4cf65fbb92e29a Mon Sep 17 00:00:00 2001 From: Igor Papandinas Date: Mon, 12 Feb 2024 19:14:52 +0100 Subject: [PATCH] chore: make cargo-contract extraction generic --- src/commands/check/index.ts | 17 +++++------------ src/commands/contract/compile.ts | 12 ++++++++++-- src/commands/contract/verify.ts | 12 ++++++++++-- src/lib/tasks.ts | 32 +++++++++++++++++--------------- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/commands/check/index.ts b/src/commands/check/index.ts index a0334c79..1875a8eb 100644 --- a/src/commands/check/index.ts +++ b/src/commands/check/index.ts @@ -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"; @@ -118,19 +118,12 @@ export default class Check extends SwankyCommand { { 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, }, diff --git a/src/commands/contract/compile.ts b/src/commands/contract/compile.ts index 937ff4a9..56129c01 100644 --- a/src/commands/contract/compile.ts +++ b/src/commands/contract/compile.ts @@ -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"; @@ -76,7 +76,15 @@ export class CompileContract extends SwankyCommand { 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); diff --git a/src/commands/contract/verify.ts b/src/commands/contract/verify.ts index 459b1d4e..e6ea3991 100644 --- a/src/commands/contract/verify.ts +++ b/src/commands/contract/verify.ts @@ -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"; @@ -29,7 +29,15 @@ export class VerifyContract extends SwankyCommand { async run(): Promise { 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 } }); diff --git a/src/lib/tasks.ts b/src/lib/tasks.ts index 750a048b..2705ac42 100644 --- a/src/lib/tasks.ts +++ b/src/lib/tasks.ts @@ -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"; @@ -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.` ); } }