From 1beee480fae24256ea499772bcaad3471a0b282d Mon Sep 17 00:00:00 2001 From: Alison Hart Date: Mon, 16 Dec 2024 16:09:09 -0500 Subject: [PATCH] Resolving bugs related to minimum version of ansible-creator (#1726) --- src/definitions/constants.ts | 2 + .../createAnsibleCollectionPage.ts | 57 +++++++++++++------ .../createAnsibleProjectPage.ts | 35 ++++++------ src/features/contentCreator/utils.ts | 8 +++ 4 files changed, 67 insertions(+), 35 deletions(-) diff --git a/src/definitions/constants.ts b/src/definitions/constants.ts index fce616d79..67759a1e4 100644 --- a/src/definitions/constants.ts +++ b/src/definitions/constants.ts @@ -44,6 +44,8 @@ export const ANSIBLE_LIGHTSPEED_API_TIMEOUT = 28000; export const ANSIBLE_CREATOR_VERSION_MIN = "24.10.1"; +export const ANSIBLE_CREATOR_COLLECTION_VERSION_MIN = "24.7.1"; + export const DevfileImages = { Upstream: "ghcr.io/ansible/ansible-workspace-env-reference:latest", }; diff --git a/src/features/contentCreator/createAnsibleCollectionPage.ts b/src/features/contentCreator/createAnsibleCollectionPage.ts index ed1adfce1..b512e7a17 100644 --- a/src/features/contentCreator/createAnsibleCollectionPage.ts +++ b/src/features/contentCreator/createAnsibleCollectionPage.ts @@ -8,8 +8,16 @@ import { getNonce } from "../utils/getNonce"; import { AnsibleCollectionFormInterface, PostMessageEvent } from "./types"; import { withInterpreter } from "../utils/commandRunner"; import { SettingsManager } from "../../settings"; -import { expandPath, getBinDetail, runCommand } from "./utils"; -import { ANSIBLE_CREATOR_VERSION_MIN } from "../../definitions/constants"; +import { + expandPath, + getBinDetail, + getCreatorVersion, + runCommand, +} from "./utils"; +import { + ANSIBLE_CREATOR_COLLECTION_VERSION_MIN, + ANSIBLE_CREATOR_VERSION_MIN, +} from "../../definitions/constants"; export class CreateAnsibleCollection { public static currentPanel: CreateAnsibleCollection | undefined; @@ -334,12 +342,20 @@ export class CreateAnsibleCollection { return; } - private async getCreatorVersion(): Promise { - const creatorVersion = ( - await getBinDetail("ansible-creator", "--version") - ).toString(); - console.log("ansible-creator version: ", creatorVersion); - return creatorVersion; + public async getCollectionCreatorCommand( + namespaceName: string, + collectionName: string, + initPathUrl: string, + ): Promise { + let command = ""; + const creatorVersion = await getCreatorVersion(); + + if (semver.gte(creatorVersion, ANSIBLE_CREATOR_COLLECTION_VERSION_MIN)) { + command = `ansible-creator init collection ${namespaceName}.${collectionName} ${initPathUrl} --no-ansi`; + } else { + command = `ansible-creator init ${namespaceName}.${collectionName} --init-path=${initPathUrl} --no-ansi`; + } + return command; } public async runInitCommand( @@ -363,7 +379,11 @@ export class CreateAnsibleCollection { ? initPath : `${os.homedir()}/.ansible/collections/ansible_collections`; - let ansibleCreatorInitCommand = `ansible-creator init collection ${namespaceName}.${collectionName} ${initPathUrl} --no-ansi`; + let ansibleCreatorInitCommand = await this.getCollectionCreatorCommand( + namespaceName, + collectionName, + initPathUrl, + ); // adjust collection url for using it in ade and opening it in workspace // NOTE: this is done in order to synchronize the behavior of ade and extension @@ -386,13 +406,18 @@ export class CreateAnsibleCollection { let adeCommand = `ade install --venv ${venvPathUrl} --editable ${collectionUrl} --no-ansi`; - const creatorVersion = await this.getCreatorVersion(); - if (isOverwritten) { - if (semver.gte(creatorVersion, ANSIBLE_CREATOR_VERSION_MIN)) { - ansibleCreatorInitCommand += " --overwrite"; - } else { - ansibleCreatorInitCommand += " --force"; - } + const creatorVersion = await getCreatorVersion(); + const exceedMinVersion = semver.gte( + creatorVersion, + ANSIBLE_CREATOR_VERSION_MIN, + ); + + if (exceedMinVersion && isOverwritten) { + ansibleCreatorInitCommand += " --overwrite"; + } else if (!exceedMinVersion && isOverwritten) { + ansibleCreatorInitCommand += " --force"; + } else if (exceedMinVersion && !isOverwritten) { + ansibleCreatorInitCommand += " --no-overwrite"; } switch (verbosity) { diff --git a/src/features/contentCreator/createAnsibleProjectPage.ts b/src/features/contentCreator/createAnsibleProjectPage.ts index fd10ba676..83b59033d 100644 --- a/src/features/contentCreator/createAnsibleProjectPage.ts +++ b/src/features/contentCreator/createAnsibleProjectPage.ts @@ -8,7 +8,7 @@ import { getNonce } from "../utils/getNonce"; import { AnsibleProjectFormInterface, PostMessageEvent } from "./types"; import { withInterpreter } from "../utils/commandRunner"; import { SettingsManager } from "../../settings"; -import { expandPath, getBinDetail, runCommand } from "./utils"; +import { expandPath, getCreatorVersion, runCommand } from "./utils"; import { ANSIBLE_CREATOR_VERSION_MIN } from "../../definitions/constants"; export class CreateAnsibleProject { @@ -280,21 +280,13 @@ export class CreateAnsibleProject { ); } - private async getCreatorVersion(): Promise { - const creatorVersion = ( - await getBinDetail("ansible-creator", "--version") - ).toString(); - console.log("ansible-creator version: ", creatorVersion); - return creatorVersion; - } - - public async getCreatorCommand( + public async getPlaybookCreatorCommand( namespace: string, collection: string, url: string, ): Promise { let command = ""; - const creatorVersion = await this.getCreatorVersion(); + const creatorVersion = await getCreatorVersion(); if (semver.gte(creatorVersion, ANSIBLE_CREATOR_VERSION_MIN)) { command = `ansible-creator init playbook ${namespace}.${collection} ${url} --no-ansi`; @@ -345,19 +337,24 @@ export class CreateAnsibleProject { ? destinationPath : `${os.homedir()}/${namespaceName}-${collectionName}`; - let ansibleCreatorInitCommand = await this.getCreatorCommand( + let ansibleCreatorInitCommand = await this.getPlaybookCreatorCommand( namespaceName, collectionName, destinationPathUrl, ); - const creatorVersion = await this.getCreatorVersion(); - if (isOverwritten) { - if (semver.gte(creatorVersion, ANSIBLE_CREATOR_VERSION_MIN)) { - ansibleCreatorInitCommand += " --overwrite"; - } else { - ansibleCreatorInitCommand += " --force"; - } + const creatorVersion = await getCreatorVersion(); + const exceedMinVersion = semver.gte( + creatorVersion, + ANSIBLE_CREATOR_VERSION_MIN, + ); + + if (exceedMinVersion && isOverwritten) { + ansibleCreatorInitCommand += " --overwrite"; + } else if (!exceedMinVersion && isOverwritten) { + ansibleCreatorInitCommand += " --force"; + } else if (exceedMinVersion && !isOverwritten) { + ansibleCreatorInitCommand += " --no-overwrite"; } switch (verbosity) { diff --git a/src/features/contentCreator/utils.ts b/src/features/contentCreator/utils.ts index c419829a9..c38b990f3 100644 --- a/src/features/contentCreator/utils.ts +++ b/src/features/contentCreator/utils.ts @@ -19,6 +19,14 @@ export async function getBinDetail(cmd: string, arg: string) { } } +export async function getCreatorVersion(): Promise { + const creatorVersion = ( + await getBinDetail("ansible-creator", "--version") + ).toString(); + console.log("ansible-creator version: ", creatorVersion); + return creatorVersion; +} + export async function runCommand( command: string, runEnv: NodeJS.ProcessEnv | undefined,