From 4d5492708fc0f0b558512be11347c1fdee052e45 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Tue, 19 Dec 2023 17:09:24 -0500 Subject: [PATCH] wip: updateSchema adjustments, test schemaVersion checks w/ FTP ext. Signed-off-by: Trae Yelovich --- package.json | 3 + .../src/extension.ts | 2 +- .../zowe-explorer/src/ZoweExplorerExtender.ts | 77 ++++++++----------- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 5473e1d918..e3aecb5413 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,9 @@ "@zowe/cli": "7.19.0", "vscode-nls": "4.1.2" }, + "resolutions": { + "@zowe/imperative": "file:i5.19.tgz" + }, "devDependencies": { "@types/jest": "^29.2.3", "@types/mocha": "^10.0.1", diff --git a/packages/zowe-explorer-ftp-extension/src/extension.ts b/packages/zowe-explorer-ftp-extension/src/extension.ts index abdab24b6f..9bdd6678cc 100644 --- a/packages/zowe-explorer-ftp-extension/src/extension.ts +++ b/packages/zowe-explorer-ftp-extension/src/extension.ts @@ -43,7 +43,7 @@ async function registerFtpApis(): Promise { zoweExplorerApi.registerJesApi(new FtpJesApi()); const meta = await CoreUtils.getProfileMeta(); - await zoweExplorerApi.getExplorerExtenderApi().initForZowe("zftp", meta); + await zoweExplorerApi.getExplorerExtenderApi().initForZowe("zftp", [{ ...meta[0], schemaVersion: "2.0" } as any]); await zoweExplorerApi.getExplorerExtenderApi().reloadProfiles("zftp"); await Gui.showMessage("Zowe Explorer was modified for FTP support.", { logger: ZoweLogger }); diff --git a/packages/zowe-explorer/src/ZoweExplorerExtender.ts b/packages/zowe-explorer/src/ZoweExplorerExtender.ts index 11103a1f84..c394b820b0 100644 --- a/packages/zowe-explorer/src/ZoweExplorerExtender.ts +++ b/packages/zowe-explorer/src/ZoweExplorerExtender.ts @@ -166,10 +166,11 @@ export class ZoweExplorerExtender implements ZoweExplorerApi.IApiExplorerExtende * profile management. */ let usingTeamConfig: boolean; + let profileInfo: zowe.imperative.ProfileInfo; try { - const mProfileInfo = await ProfilesUtils.getProfileInfo(globals.ISTHEIA); - await mProfileInfo.readProfilesFromDisk({ homeDir: zoweDir, projectDir }); - usingTeamConfig = mProfileInfo.usingTeamConfig; + profileInfo = await ProfilesUtils.getProfileInfo(globals.ISTHEIA); + await profileInfo.readProfilesFromDisk({ homeDir: zoweDir, projectDir }); + usingTeamConfig = profileInfo.usingTeamConfig; } catch (error) { ZoweLogger.warn(error); if (error.toString().includes("Error parsing JSON")) { @@ -197,7 +198,7 @@ export class ZoweExplorerExtender implements ZoweExplorerApi.IApiExplorerExtende // Check if schema needs updated when the end user is using a team config if (usingTeamConfig) { - this.updateSchema(projectDir); + this.updateSchema(profileInfo, profileTypeConfigurations); } // sequentially reload the internal profiles cache to satisfy all the newly added profile types @@ -207,46 +208,36 @@ export class ZoweExplorerExtender implements ZoweExplorerApi.IApiExplorerExtende } /** - * Checks to see if any profile types should be added and adds the new types to the schema, if found. - * @param projectDir (optional) The workspace directory (if the user has a workspace open) + * Adds new types to the Zowe schema. + * @param profileInfo the ProfileInfo object that has been prepared with `readProfilesFromDisk`, such as the one initialized in `initForZowe`. + * @param profileTypeConfigurations (optional) Profile type configurations to add to the schema */ - private updateSchema(projectDir?: string): void { - // check for the existence of a project-level schema; if it doesn't exist, fall back to global - const projSchemaLoc = projectDir ? path.join(projectDir, "zowe.schema.json") : null; - const projSchemaExists = projSchemaLoc != null && fs.existsSync(projSchemaLoc); - const schemaPath = projSchemaExists ? projSchemaLoc : path.join(getZoweDir(), "zowe.schema.json"); - - // try parsing the existing schema to gather the list of types to merge - try { - const schemaContents = fs.readFileSync(schemaPath).toString(); - const parsedSchema = JSON.parse(schemaContents); - - // determine new types that are not present in the on-disk schema - const schemaTypes = zowe.imperative.ConfigSchema.loadSchema(parsedSchema); - const newSchemaTypes = Profiles.getInstance() - .getConfigArray() - .filter((o) => typeof o === "object"); - - // If there are any new types to add, merge the list of profile types and rebuild the schema - const typesToAdd = newSchemaTypes.filter((s) => schemaTypes.find((newS) => s.type === newS.type) == null); - if (typesToAdd.length > 0) { - // Get profile types from config on-disk and merge with new profile types - const mergedProfTypes = [...schemaTypes, ...typesToAdd]; - - // rebuild schema to contain all profile types (including merged) and write to disk - const newSchema = JSON.stringify(zowe.imperative.ConfigSchema.buildSchema(mergedProfTypes)); - fs.writeFileSync(schemaPath, newSchema); - } - } catch (err) { - // Only show an error if we failed to update the on-disk schema. - if (err.code === "EACCES" || err.code === "EPERM") { - Gui.errorMessage( - localize( - "zowe.schema.cannotAccess", - "Failed to update Zowe schema at {0}: insufficient permissions or read-only file", - schemaPath - ) - ); + private updateSchema( + profileInfo: zowe.imperative.ProfileInfo, + profileTypeConfigurations?: zowe.imperative.ICommandProfileTypeConfiguration[], + ): void { + if (profileTypeConfigurations) { + try { + for (const typeConfig of profileTypeConfigurations) { + const addResult = profileInfo.addProfileTypeToSchema(typeConfig.type, { + schema: typeConfig.schema, + sourceApp: "Zowe Explorer (for VS Code)", + version: typeConfig.schemaVersion + }); + if (addResult.info.length > 0) { + Gui.warningMessage(addResult.info); + } + } + } catch (err) { + // Only show an error if we failed to update the on-disk schema. + if (err.code === "EACCES" || err.code === "EPERM") { + Gui.errorMessage( + localize( + "zowe.schema.cannotAccess", + "Failed to update Zowe schema: insufficient permissions or read-only file", + ) + ); + } } } }