Skip to content

Commit

Permalink
Merge pull request #1714 from zowe/refactor-settings-migration
Browse files Browse the repository at this point in the history
Refactor Zowe 2.0 settings migration script
  • Loading branch information
JillieBeanSim authored Apr 1, 2022
2 parents 7dabed0 + 65e0a3f commit f1c5f4c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 98 deletions.
4 changes: 2 additions & 2 deletions packages/zowe-explorer/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import SpoolProvider from "./SpoolProvider";
import * as nls from "vscode-nls";
import { TsoCommandHandler } from "./command/TsoCommandHandler";
import { cleanTempDir, moveTempFolder, hideTempFolder } from "./utils/TempFolder";
import { standardizeSettings } from "./utils/SettingsConfig";
import { SettingsConfig } from "./utils/SettingsConfig";
import { UIViews } from "./shared/ui-views";

// Set up localization
Expand Down Expand Up @@ -306,7 +306,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<ZoweEx

ZoweExplorerExtender.createInstance(datasetProvider, ussFileProvider, jobsProvider);

await standardizeSettings();
await SettingsConfig.standardizeSettings();
return ZoweExplorerApiRegister.getInstance();
}

Expand Down
210 changes: 114 additions & 96 deletions packages/zowe-explorer/src/utils/SettingsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,121 +20,139 @@ nls.config({
})();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();

// Dictionary describing translation from old configuration names to new standardized names
const configurationDictionary = {
"Zowe-Default-Datasets-Binary": globals.SETTINGS_DS_DEFAULT_BINARY,
"Zowe-Default-Datasets-C": globals.SETTINGS_DS_DEFAULT_C,
"Zowe-Default-Datasets-Classic": globals.SETTINGS_DS_DEFAULT_CLASSIC,
"Zowe-Default-Datasets-PDS": globals.SETTINGS_DS_DEFAULT_PDS,
"Zowe-Default-Datasets-PS": globals.SETTINGS_DS_DEFAULT_PS,
"Zowe-Temp-Folder-Location": globals.SETTINGS_TEMP_FOLDER_PATH,
"Zowe Security: Credential Key": globals.SETTINGS_SECURITY_CREDENTIAL_PLUGIN,
"Zowe Commands: History": globals.SETTINGS_COMMANDS_HISTORY,
"Zowe Commands: Always edit": globals.SETTINGS_COMMANDS_ALWAYS_EDIT,
"Zowe-Automatic-Validation": globals.SETTINGS_AUTOMATIC_PROFILE_VALIDATION,
"Zowe-DS-Persistent": globals.SETTINGS_DS_HISTORY,
"Zowe-USS-Persistent": globals.SETTINGS_USS_HISTORY,
"Zowe-Jobs-Persistent": globals.SETTINGS_JOBS_HISTORY,
};

const configurations = vscode.workspace.getConfiguration();

const zoweOldConfigurations = Object.keys(configurations).filter((key) =>
key.match(new RegExp("Zowe-*|Zowe\\s*", "g"))
);

const currentVersionNumber = semver.major(
vscode.extensions.getExtension("zowe.vscode-extension-for-zowe").packageJSON.version
);

async function promptReload() {
// Prompt user to reload VS Code window
const reloadButton = localize("standardization.reload.button", "Reload Window");
const infoMsg = localize(
"standardization.reload.infoMessage",
"Settings have been successfully migrated for Zowe Explorer version 2 and above. To apply these settings, please reload your VS Code window."
);
await vscode.window.showInformationMessage(infoMsg, ...[reloadButton])?.then(async (selection) => {
if (selection === reloadButton) {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
export class SettingsConfig {
public static async standardizeSettings(): Promise<void> {
const globalIsNotMigrated =
SettingsConfig.configurations.inspect(globals.SETTINGS_VERSION).globalValue !==
SettingsConfig.currentVersionNumber;
const workspaceIsNotMigrated =
SettingsConfig.configurations.inspect(globals.SETTINGS_VERSION).workspaceValue !==
SettingsConfig.currentVersionNumber;
const workspaceIsOpen = vscode.workspace.workspaceFolders !== undefined;
const zoweSettingsExist = SettingsConfig.zoweOldConfigurations.length > 0;

if (!zoweSettingsExist) {
return;
}
});
}

export async function standardizeGlobalSettings() {
let globalIsMigrated =
(await configurations.inspect(globals.SETTINGS_VERSION).globalValue) !== currentVersionNumber;

// Standardize global settings when old Zowe settings were found
if (zoweOldConfigurations.length > 0) {
zoweOldConfigurations.forEach(async (configuration) => {
let globalValue: any = await configurations.inspect(configuration).globalValue;

// Adjust fetching of value due to schema change
if (configuration === "Zowe-Temp-Folder-Location") {
globalValue = globalValue ? globalValue.folderPath : globalValue;
}

const newSetting = configurationDictionary[configuration];
if (workspaceIsNotMigrated && workspaceIsOpen) {
await SettingsConfig.standardizeWorkspaceSettings();
}

if (globalValue !== undefined) {
await configurations.update(newSetting, globalValue, vscode.ConfigurationTarget.Global);
globalIsMigrated = true;
if (globalIsNotMigrated) {
await SettingsConfig.standardizeGlobalSettings();
}
}
// Dictionary describing translation from old configuration names to new standardized names
private static configurationDictionary = {
"Zowe-Default-Datasets-Binary": globals.SETTINGS_DS_DEFAULT_BINARY,
"Zowe-Default-Datasets-C": globals.SETTINGS_DS_DEFAULT_C,
"Zowe-Default-Datasets-Classic": globals.SETTINGS_DS_DEFAULT_CLASSIC,
"Zowe-Default-Datasets-PDS": globals.SETTINGS_DS_DEFAULT_PDS,
"Zowe-Default-Datasets-PS": globals.SETTINGS_DS_DEFAULT_PS,
"Zowe-Temp-Folder-Location": globals.SETTINGS_TEMP_FOLDER_PATH,
"Zowe Security: Credential Key": globals.SETTINGS_SECURITY_CREDENTIAL_PLUGIN,
"Zowe Commands: History": globals.SETTINGS_COMMANDS_HISTORY,
"Zowe Commands: Always edit": globals.SETTINGS_COMMANDS_ALWAYS_EDIT,
"Zowe-Automatic-Validation": globals.SETTINGS_AUTOMATIC_PROFILE_VALIDATION,
"Zowe-DS-Persistent": globals.SETTINGS_DS_HISTORY,
"Zowe-USS-Persistent": globals.SETTINGS_USS_HISTORY,
"Zowe-Jobs-Persistent": globals.SETTINGS_JOBS_HISTORY,
};
private static configurations = vscode.workspace.getConfiguration();
private static zoweOldConfigurations = Object.keys(SettingsConfig.configurations).filter((key) =>
key.match(new RegExp("Zowe-*|Zowe\\s*", "g"))
);
private static currentVersionNumber = semver.major(
vscode.extensions.getExtension("zowe.vscode-extension-for-zowe").packageJSON.version
);
private static async promptReload(): Promise<void> {
// Prompt user to reload VS Code window
const reloadButton = localize("standardization.reload.button", "Reload Window");
const infoMsg = localize(
"standardization.reload.infoMessage",
"Settings have been successfully migrated for Zowe Explorer version 2 and above. To apply these settings, please reload your VS Code window."
);
await vscode.window.showInformationMessage(infoMsg, ...[reloadButton])?.then(async (selection) => {
if (selection === reloadButton) {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
}
});
}

if (globalIsMigrated) {
await configurations.update(globals.SETTINGS_VERSION, currentVersionNumber, vscode.ConfigurationTarget.Global);
await promptReload();
}
}

export async function standardizeWorkspaceSettings() {
let workspaceIsNotMigrated =
(await configurations.inspect(globals.SETTINGS_VERSION).workspaceValue) !== currentVersionNumber;
private static async standardizeGlobalSettings(): Promise<void> {
let globalIsMigrated =
SettingsConfig.configurations.inspect(globals.SETTINGS_VERSION).globalValue !==
SettingsConfig.currentVersionNumber;

// Standardize workspace settings when old Zowe settings were found
if (zoweOldConfigurations.length > 0) {
zoweOldConfigurations
.filter((c) => !c.match(new RegExp("Zowe-[A-Za-z]+-Persistent|Zowe Commands: History", "g")))
.forEach(async (configuration) => {
let workspaceValue: any = await configurations.inspect(configuration).workspaceValue;
// Standardize global settings when old Zowe settings were found
if (SettingsConfig.zoweOldConfigurations.length > 0) {
SettingsConfig.zoweOldConfigurations.forEach(async (configuration) => {
let globalValue: any = SettingsConfig.configurations.inspect(configuration).globalValue;

// Adjust fetching of value due to schema change
if (configuration === "Zowe-Temp-Folder-Location") {
workspaceValue = workspaceValue ? workspaceValue.folderPath : workspaceValue;
globalValue = globalValue ? globalValue.folderPath : globalValue;
}

const newSetting = configurationDictionary[configuration];
const newSetting = SettingsConfig.configurationDictionary[configuration];

if (workspaceValue !== undefined) {
await configurations.update(newSetting, workspaceValue, vscode.ConfigurationTarget.Workspace);
workspaceIsNotMigrated = true;
if (globalValue !== undefined) {
await SettingsConfig.configurations.update(
newSetting,
globalValue,
vscode.ConfigurationTarget.Global
);
globalIsMigrated = true;
}
});
}
}

if (workspaceIsNotMigrated) {
await configurations.update(
globals.SETTINGS_VERSION,
currentVersionNumber,
vscode.ConfigurationTarget.Workspace
);
if (globalIsMigrated) {
await SettingsConfig.configurations.update(
globals.SETTINGS_VERSION,
SettingsConfig.currentVersionNumber,
vscode.ConfigurationTarget.Global
);
await SettingsConfig.promptReload();
}
}
}

export async function standardizeSettings() {
const globalIsNotMigrated =
(await configurations.inspect(globals.SETTINGS_VERSION).globalValue) !== currentVersionNumber;
const workspaceIsNotMigrated =
(await configurations.inspect(globals.SETTINGS_VERSION).workspaceValue) !== currentVersionNumber;
const workspaceIsOpen = vscode.workspace.workspaceFolders !== undefined;
private static async standardizeWorkspaceSettings(): Promise<void> {
let workspaceIsMigrated = false;
// Standardize workspace settings when old Zowe settings were found
if (SettingsConfig.zoweOldConfigurations.length > 0) {
// filter to only supported workspace configurations in scope
const filteredConfigurations = SettingsConfig.zoweOldConfigurations.filter(
(c) => !c.match(new RegExp("Zowe-[A-Za-z]+-Persistent|Zowe Commands: History", "g"))
);

if (workspaceIsNotMigrated && workspaceIsOpen) {
await standardizeWorkspaceSettings();
}
for (const configuration of filteredConfigurations) {
let workspaceValue: any = SettingsConfig.configurations.inspect(configuration).workspaceValue;

if (globalIsNotMigrated) {
await standardizeGlobalSettings();
if (configuration === "Zowe-Temp-Folder-Location") {
workspaceValue = workspaceValue ? workspaceValue.folderPath : workspaceValue;
}

const newSetting = SettingsConfig.configurationDictionary[configuration];

if (workspaceValue !== undefined) {
await SettingsConfig.configurations.update(
newSetting,
workspaceValue,
vscode.ConfigurationTarget.Workspace
);
workspaceIsMigrated = true;
}
}
}

if (workspaceIsMigrated) {
await SettingsConfig.configurations.update(
globals.SETTINGS_VERSION,
SettingsConfig.currentVersionNumber,
vscode.ConfigurationTarget.Workspace
);
}
}
}

0 comments on commit f1c5f4c

Please sign in to comment.