Skip to content

Commit

Permalink
Resolving bugs related to minimum version of ansible-creator (#1726)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonlhart authored Dec 16, 2024
1 parent 13df898 commit 1beee48
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/definitions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
57 changes: 41 additions & 16 deletions src/features/contentCreator/createAnsibleCollectionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -334,12 +342,20 @@ export class CreateAnsibleCollection {
return;
}

private async getCreatorVersion(): Promise<string> {
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<string> {
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(
Expand All @@ -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
Expand All @@ -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) {
Expand Down
35 changes: 16 additions & 19 deletions src/features/contentCreator/createAnsibleProjectPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -280,21 +280,13 @@ export class CreateAnsibleProject {
);
}

private async getCreatorVersion(): Promise<string> {
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<string> {
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`;
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/features/contentCreator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export async function getBinDetail(cmd: string, arg: string) {
}
}

export async function getCreatorVersion(): Promise<string> {
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,
Expand Down

0 comments on commit 1beee48

Please sign in to comment.