Skip to content

Commit

Permalink
fix: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 28, 2024
1 parent cc464fe commit 37d8db7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 48 deletions.
17 changes: 8 additions & 9 deletions src/commands/env/check.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -79,7 +79,7 @@ export class Check extends SwankyCommand<typeof Check> {
{
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");
}
Expand All @@ -90,7 +90,7 @@ export class Check extends SwankyCommand<typeof Check> {
{
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");
}
Expand All @@ -101,7 +101,7 @@ export class Check extends SwankyCommand<typeof Check> {
{
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");
}
Expand All @@ -112,7 +112,7 @@ export class Check extends SwankyCommand<typeof Check> {
{
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");
}
Expand All @@ -123,12 +123,11 @@ export class Check extends SwankyCommand<typeof Check> {
{
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,
},
Expand Down
12 changes: 9 additions & 3 deletions src/commands/env/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ export class Install extends SwankyCommand<typeof Install> {
required: false,
description: `Install the specified dev dependency name and version in the format <dependency@version>. 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<void> {
const { flags } = await this.parse(Install);

Expand All @@ -45,7 +50,8 @@ export class Install extends SwankyCommand<typeof Install> {
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;
Expand All @@ -64,6 +70,6 @@ export class Install extends SwankyCommand<typeof Install> {
}, "Updating swanky config");
}

this.log("Dev Dependencies Installed successfully");
this.log("Swanky Dev Dependencies Installed successfully");
}
}
48 changes: 41 additions & 7 deletions src/lib/command-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -154,7 +153,7 @@ export function ensureAccountIsSet(account: string | undefined, config: SwankyCo
}
}

export function buildSwankyConfig() {
export function buildSwankyConfig() {
return {
node: {
localPath: "",
Expand Down Expand Up @@ -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,
},
};
}
Expand Down Expand Up @@ -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-]+)/);
}
3 changes: 1 addition & 2 deletions src/lib/config-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export class ConfigBuilder<T extends SwankySystemConfig | SwankyConfig> {
}

updateEnv(env: Record<string, string>): ConfigBuilder<T> {
const prevEnv = this.config.env;
this.config.env = {...prevEnv, ...env};
this.config.env = {...this.config.env, ...env};
return this;
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/swankyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export abstract class SwankyCommand<T extends typeof Command> 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");
}
Expand Down
24 changes: 0 additions & 24 deletions src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 37d8db7

Please sign in to comment.