diff --git a/src/commands/env/check.ts b/src/commands/env/check.ts index 603fb844..417e6741 100644 --- a/src/commands/env/check.ts +++ b/src/commands/env/check.ts @@ -1,5 +1,5 @@ import { Listr } from "listr2"; -import { commandStdoutOrNull, extractCargoContractVersion } from "../../lib/index.js"; +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"; @@ -79,7 +79,7 @@ export class Check extends SwankyCommand { { title: "Check Rust", task: async (ctx, task) => { - ctx.versions.tools.rust = commandStdoutOrNull("rustc --version")?.match(/rustc (.*) \((.*)/)?.[1]; + ctx.versions.tools.rust = extractRustVersion(); if (!ctx.versions.tools.rust) { throw new Error("Rust is not installed"); } @@ -90,7 +90,7 @@ export class Check extends SwankyCommand { { title: "Check cargo", task: async (ctx, task) => { - ctx.versions.tools.cargo = commandStdoutOrNull("cargo -V")?.match(/cargo (.*) \((.*)/)?.[1]; + ctx.versions.tools.cargo = extractCargoVersion(); if (!ctx.versions.tools.cargo) { throw new Error("Cargo is not installed"); } @@ -101,7 +101,7 @@ export class Check extends SwankyCommand { { title: "Check cargo nightly", task: async (ctx, task) => { - ctx.versions.tools.cargoNightly = commandStdoutOrNull("cargo +nightly -V")?.match(/cargo (.*)-nightly \((.*)/)?.[1]; + ctx.versions.tools.cargoNightly = extractCargoNightlyVersion(); if (!ctx.versions.tools.cargoNightly) { throw new Error("Cargo nightly is not installed"); } @@ -112,7 +112,7 @@ export class Check extends SwankyCommand { { title: "Check cargo dylint", task: async (ctx, task) => { - ctx.versions.tools.cargoDylint = commandStdoutOrNull("cargo dylint -V")?.match(/cargo-dylint (.*)/)?.[1]; + ctx.versions.tools.cargoDylint = extractCargoDylintVersion(); if (!ctx.versions.tools.cargoDylint) { throw new Warn("Cargo dylint is not installed"); } @@ -123,12 +123,11 @@ export class Check extends SwankyCommand { { title: "Check cargo-contract", task: async (ctx, task) => { - const cargoContractVersion = extractCargoContractVersion(); - ctx.versions.tools.cargoContract = cargoContractVersion; - if (!cargoContractVersion) { + ctx.versions.tools.cargoContract = extractCargoContractVersion(); + if (!ctx.versions.tools.cargoContract) { throw new Error("Cargo contract is not installed"); } - task.title = `Check cargo-contract: ${cargoContractVersion}`; + task.title = `Check cargo-contract: ${ctx.versions.tools.cargoContract}`; }, exitOnError: false, }, diff --git a/src/commands/env/install.ts b/src/commands/env/install.ts index 9b30c73e..a0ed55ff 100644 --- a/src/commands/env/install.ts +++ b/src/commands/env/install.ts @@ -17,13 +17,18 @@ export class Install extends SwankyCommand { required: false, description: `Install the specified dev dependency name and version in the format . The following options are supported: ${Object.keys( SUPPORTED_DEPS - ).join(", ")}`, + ).join(", ")}. For installing rust nightly version run: env install --deps rust@nightly`, multiple: true, default: [], char: "d", }), }; + constructor(argv: string[], baseConfig: any) { + super(argv, baseConfig); + (this.constructor as typeof SwankyCommand).ENSURE_SWANKY_CONFIG = false; + } + async run(): Promise { const { flags } = await this.parse(Install); @@ -45,7 +50,8 @@ export class Install extends SwankyCommand { newDeps[key] = value || "latest"; } - const newEnv = { ...this.swankyConfig.env, ...newDeps }; + const globalConfig = getSwankyConfig("global"); + const newEnv = { ...globalConfig.env, ...newDeps }; const deps = Object.entries(newEnv); for (const [dep, version] of deps) { const typedDep = dep as DependencyName; @@ -64,6 +70,6 @@ export class Install extends SwankyCommand { }, "Updating swanky config"); } - this.log("Dev Dependencies Installed successfully"); + this.log("Swanky Dev Dependencies Installed successfully"); } } diff --git a/src/lib/command-utils.ts b/src/lib/command-utils.ts index e3219350..5284c4ad 100644 --- a/src/lib/command-utils.ts +++ b/src/lib/command-utils.ts @@ -15,10 +15,9 @@ import { DEFAULT_RUST_DEP_VERSION, DEFAULT_CARGO_CONTRACT_DEP_VERSION, DEFAULT_CARGO_DYLINT_DEP_VERSION, - DEFAULT_RUST_NIGHTLY_DEP_VERSION, } from "./consts.js"; import { SwankyConfig, SwankySystemConfig } from "../types/index.js"; -import { ConfigError, FileError } from "./errors.js"; +import { ConfigError, FileError, ProcessError } from "./errors.js"; import { userInfo } from "os"; import { existsSync } from "fs"; @@ -154,7 +153,7 @@ export function ensureAccountIsSet(account: string | undefined, config: SwankyCo } } -export function buildSwankyConfig() { +export function buildSwankyConfig() { return { node: { localPath: "", @@ -185,10 +184,9 @@ export function buildSwankyConfig() { }, contracts: {}, env: { - rust: DEFAULT_RUST_DEP_VERSION, - "rust-nightly": DEFAULT_RUST_NIGHTLY_DEP_VERSION, - "cargo-dylint": DEFAULT_CARGO_DYLINT_DEP_VERSION, - "cargo-contract": DEFAULT_CARGO_CONTRACT_DEP_VERSION, + rust: extractRustVersion() ?? DEFAULT_RUST_DEP_VERSION, + "cargo-dylint": extractCargoDylintVersion() ?? DEFAULT_CARGO_DYLINT_DEP_VERSION, + "cargo-contract": extractCargoContractVersion() ?? DEFAULT_CARGO_CONTRACT_DEP_VERSION, }, }; } @@ -216,4 +214,40 @@ export function configName(): string { } return process.env.SWANKY_CONFIG?.split("/").pop() ?? DEFAULT_CONFIG_NAME; +} + +export function extractVersion(command: string, regex: RegExp) { + const output = commandStdoutOrNull(command); + if (!output) { + return null; + } + + const match = output.match(regex); + if (!match) { + throw new ProcessError( + `Unable to determine version from command '${command}'. Please verify its installation.` + ); + } + + return match[1]; +} + +export function extractRustVersion() { + return extractVersion("rustc --version", /rustc (.*) \((.*)/); +} + +export function extractCargoVersion() { + return extractVersion("cargo -V", /cargo (.*) \((.*)/); +} + +export function extractCargoNightlyVersion() { + return extractVersion("cargo +nightly -V", /cargo (.*)-nightly \((.*)/); +} + +export function extractCargoDylintVersion() { + return extractVersion("cargo dylint -V", /cargo-dylint (.*)/); +} + +export function extractCargoContractVersion() { + return extractVersion("cargo contract -V", /cargo-contract-contract (\d+\.\d+\.\d+(?:-[\w.]+)?)(?:-unknown-[\w-]+)/); } \ No newline at end of file diff --git a/src/lib/config-builder.ts b/src/lib/config-builder.ts index 9624cbcd..0938a099 100644 --- a/src/lib/config-builder.ts +++ b/src/lib/config-builder.ts @@ -33,8 +33,7 @@ export class ConfigBuilder { } updateEnv(env: Record): ConfigBuilder { - const prevEnv = this.config.env; - this.config.env = {...prevEnv, ...env}; + this.config.env = {...this.config.env, ...env}; return this; } diff --git a/src/lib/consts.ts b/src/lib/consts.ts index c6e55cab..ad7d4209 100644 --- a/src/lib/consts.ts +++ b/src/lib/consts.ts @@ -15,13 +15,11 @@ export const ARTIFACTS_PATH = "artifacts"; export const TYPED_CONTRACTS_PATH = "typedContracts"; export const DEFAULT_RUST_DEP_VERSION = "1.76.0"; -export const DEFAULT_RUST_NIGHTLY_DEP_VERSION = "nightly-2024-02-26"; export const DEFAULT_CARGO_DYLINT_DEP_VERSION = "2.6.1"; export const DEFAULT_CARGO_CONTRACT_DEP_VERSION = "4.0.0-rc.2"; export const SUPPORTED_DEPS = { rust: DEFAULT_RUST_DEP_VERSION, - "rust-nightly": DEFAULT_RUST_NIGHTLY_DEP_VERSION, "cargo-dylint": DEFAULT_CARGO_DYLINT_DEP_VERSION, "cargo-contract": DEFAULT_CARGO_CONTRACT_DEP_VERSION, } as const; diff --git a/src/lib/swankyCommand.ts b/src/lib/swankyCommand.ts index 62a7b52a..bb53c7a1 100644 --- a/src/lib/swankyCommand.ts +++ b/src/lib/swankyCommand.ts @@ -59,7 +59,7 @@ export abstract class SwankyCommand extends Command { this.swankyConfig = { ...this.swankyConfig, ...systemConfig }; } catch (error) { this.warn( - `No Swanky system config found; creating one in "/${DEFAULT_CONFIG_FOLDER_NAME}/${DEFAULT_CONFIG_NAME}}" at home directory` + `No Swanky system config found; creating one in "/${DEFAULT_CONFIG_FOLDER_NAME}/${DEFAULT_CONFIG_NAME}" at home directory` ); await this.storeConfig(this.swankyConfig, "global"); } diff --git a/src/lib/tasks.ts b/src/lib/tasks.ts index d4c0ca24..f023c567 100644 --- a/src/lib/tasks.ts +++ b/src/lib/tasks.ts @@ -17,7 +17,6 @@ import { zombienetConfig } from "../commands/zombienet/init.js"; import { readFileSync } from "fs"; import TOML from "@iarna/toml"; import { writeFileSync } from "node:fs"; -import { commandStdoutOrNull } from "./command-utils.js"; export async function checkCliDependencies(spinner: Spinner) { const dependencyList = [ @@ -41,12 +40,6 @@ export async function installCliDevDeps(spinner: Spinner, name: DependencyName, spinner.text(` Installing rust`); await execaCommand(`rustup toolchain install ${version}`); await execaCommand(`rustup default ${version}`); - break; - } - case "rust-nightly": { - spinner.text(` Installing nightly`); - await execaCommand(`rustup toolchain install ${version}`); - await execaCommand(`rustup default ${version}`); await execaCommand(`rustup component add rust-src --toolchain ${version}`); await execaCommand(`rustup target add wasm32-unknown-unknown --toolchain ${version}`); break; @@ -358,23 +351,6 @@ export async function installDeps(projectPath: string) { } } -export function extractCargoContractVersion() { - const regex = /cargo-contract-contract (\d+\.\d+\.\d+(?:-[\w.]+)?)(?:-unknown-[\w-]+)/; - const cargoContractVersionOutput = commandStdoutOrNull("cargo contract -V"); - if (!cargoContractVersionOutput) { - return null - } - - const match = cargoContractVersionOutput.match(regex); - if (!match) { - throw new ProcessError( - `Unable to determine cargo-contract version. Please verify its installation.` - ); - } - - return match[1]; -} - export function ensureCargoContractVersionCompatibility( cargoContractVersion: string, minimalVersion: string,