diff --git a/src/deploy.ts b/src/deploy.ts index e23fa33..929099b 100644 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -4183,6 +4183,8 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { deploy_templates.checkOfficialRepositoryVersions .apply(ME, []); } + + deploy_switch.printSwitchStates.apply(ME, []); } finally { DOES_NOT_RELOAD_CONFIG_ANYMORE(); diff --git a/src/i18.ts b/src/i18.ts index 809b662..de046ff 100644 --- a/src/i18.ts +++ b/src/i18.ts @@ -300,10 +300,13 @@ export interface Translation { defaultName?: string; defaultOptionName?: string; description?: string; + item?: string; noDefined?: string; noOptionsDefined?: string; + noOptionSelected?: string; selectOption?: string; selectSwitch?: string; + states?: string; }, test?: { description?: string; diff --git a/src/lang/de.ts b/src/lang/de.ts index 8819974..4da3f8b 100644 --- a/src/lang/de.ts +++ b/src/lang/de.ts @@ -291,10 +291,13 @@ export const translation: Translation = { defaultName: 'Schalter #{0:trim}', defaultOptionName: 'Schalter-Option #{0:trim}', description: 'Schaltet zwischen anderen existierenden Zielen um', + item: "{0:trim,surround}: ", noDefined: 'Es wurden keine Schalter gefunden!', noOptionsDefined: 'Es wurden keine Optionen für den Schalter {0:trim,surround} definiert!', + noOptionSelected: 'KEINE OPTION AUSGEWÄHLT', selectOption: 'Wählen Sie eine Option für den Schalter {0:trim,surround}...', selectSwitch: 'Wählen Sie einen Schalter aus...', + states: 'Schalter-Stati:', }, test: { description: 'Ein Test-PlugIn, welches lediglich anzeigt, welche Dateien bereitgestellt würden', diff --git a/src/lang/en.ts b/src/lang/en.ts index 2353753..a55a32c 100644 --- a/src/lang/en.ts +++ b/src/lang/en.ts @@ -293,10 +293,13 @@ export const translation: Translation = { defaultName: 'Switch #{0:trim}', defaultOptionName: 'Switch option #{0:trim}', description: 'Switches between existing targets', + item: "{0:trim,surround}: ", noDefined: 'No swicthes available!', noOptionsDefined: 'No options were defined for the switch {0:trim,surround}!', + noOptionSelected: 'NO OPTION SELECTED', selectOption: 'Select an option for the switch {0:trim,surround}...', selectSwitch: 'Select a switch...', + states: 'Switch states:', }, test: { description: 'A mock deployer that only displays what files would be deployed', diff --git a/src/plugins/switch.ts b/src/plugins/switch.ts index 110d2af..ac16813 100644 --- a/src/plugins/switch.ts +++ b/src/plugins/switch.ts @@ -30,6 +30,7 @@ import * as deploy_switch from '../switch'; import * as deploy_targets from '../targets'; import * as Enumerable from 'node-enumerable'; import * as i18 from '../i18'; +import * as vscode from 'vscode'; import * as Workflows from 'node-workflows'; @@ -53,6 +54,12 @@ export interface DeployTargetSwitchOption extends deploy_contracts.Sortable { * Gets the ID of that option. */ __id?: any; + /** + * [INTERNAL] DO NOT DEFINE OR OVERWRITE THIS PROPERTY BY YOUR OWN! + * + * The zero-based index. + */ + __index?: number; /** * The description. @@ -78,125 +85,6 @@ export interface DeployTargetSwitchOption extends deploy_contracts.Sortable { export type DeployTargetSwitchOptionValue = DeployTargetSwitchOption | string; -/** - * Returns the current option of a target. - * - * @param {DeployTargetSwitch} target The target. - * @param {TDefault} [defaultValue] The custom default value. - * - * @return {DeployTargetSwitchOption|TDefault} The option (if found). - */ -export function getCurrentOptionOf(target: DeployTargetSwitch, - defaultValue = false): DeployTargetSwitchOption | TDefault { - if (!target) { - return target; - } - - const TARGET_NAME = deploy_helpers.normalizeString( target.name ); - - const STATES = deploy_switch.getSelectedSwitchOptions(); - if (STATES) { - const OPTION = STATES[TARGET_NAME]; - if ('object' === typeof OPTION) { - return OPTION; // found - } - else { - // get first (default) one - // instead - - return Enumerable.from( - getTargetOptionsOf(target) - ).orderBy(o => { - return deploy_helpers.toBooleanSafe(o.isDefault) ? 0 : 1; - }).firstOrDefault(x => true, - defaultValue); - } - } - - return defaultValue; -} - - - -/** - * Returns the options of a switch target. - * - * @param {DeployTargetSwitch} target The target. - * - * @return {DeployTargetSwitchOption[]} The options. - */ -export function getTargetOptionsOf(target: DeployTargetSwitch): DeployTargetSwitchOption[] { - if (deploy_helpers.isNullOrUndefined(target)) { - return target; - } - - const TARGET_NAME = deploy_helpers.normalizeString(target.name); - - const OPTIONS: DeployTargetSwitchOption[] = []; - - let objIndex = -1; - Enumerable.from( deploy_helpers.asArray(target.options) ).where(v => { - return !deploy_helpers.isNullOrUndefined(v); - }).select(v => { - ++objIndex; - - v = deploy_helpers.cloneObject(v); - - if ('object' !== typeof v) { - v = { - targets: [ deploy_helpers.normalizeString(v) ] - }; - } - - v.__id = `${target.__id}\n` + - `${deploy_helpers.normalizeString(deploy_helpers.getSortValue(v))}\n` + - `${objIndex}\n` + - `${deploy_helpers.normalizeString(v.name)}`; - - v.targets = Enumerable.from( deploy_helpers.asArray(v.targets) ).select(t => { - return deploy_helpers.normalizeString(t); - }).where(t => '' !== t && - TARGET_NAME !== t) - .distinct() - .toArray(); - - return v; - }) - .pushTo(OPTIONS); - - return OPTIONS.sort((x, y) => { - return deploy_helpers.compareValuesBy(x, y, - o => deploy_helpers.getSortValue(o)); - }); -} - -/** - * Sets the current option for a switch target. - * - * @param {DeployTargetSwitch} target The target. - * @param {DeployTargetSwitchOption} option The option to set. - * - * @return {Object} The new data. - */ -export function setCurrentOptionFor(target: DeployTargetSwitch, option: DeployTargetSwitchOption): { option: DeployTargetSwitchOption, target: DeployTargetSwitch } { - if (!target) { - return target; - } - - const NAME = deploy_helpers.normalizeString( target.name ); - - const STATES = deploy_switch.getSelectedSwitchOptions(); - if (STATES) { - STATES[NAME] = option; - - return { - option: STATES[NAME], - target: target, - }; - } -} - - class SwitchPlugin extends deploy_objects.MultiFileDeployPluginBase { public get canGetFileInfo(): boolean { return true; @@ -537,9 +425,11 @@ class SwitchPlugin extends deploy_objects.MultiFileDeployPluginBase { } private getSwitchOption(target: DeployTargetSwitch): DeployTargetSwitchOption | false { - const OPTION = getCurrentOptionOf(target); + const OPTION = deploy_switch.getCurrentOptionOf(target); if (false === OPTION) { - //TODO: show message + vscode.window.showWarningMessage( + '[vs-deploy] ' + i18.t('plugins.switch.noOptionSelected') + ); return false; } diff --git a/src/switch.ts b/src/switch.ts index 1af0505..b31d056 100644 --- a/src/switch.ts +++ b/src/switch.ts @@ -34,6 +34,20 @@ import * as vscode from 'vscode'; type SavedStates = { [switchName: string]: string }; +/** + * Stores the new option data for a target. + */ +export interface NewSwitchOptionState { + /** + * The new option. + */ + option: deploy_plugins_switch.DeployTargetSwitchOption; + /** + * The underlying target. + */ + target: deploy_plugins_switch.DeployTargetSwitch; +} + /** * Repository of selected switch options. */ @@ -62,8 +76,10 @@ export async function changeSwitch() { return; } - deploy_plugins_switch.setCurrentOptionFor(selectedTarget, selectedOption); + setCurrentOptionFor(selectedTarget, selectedOption); await saveStates.apply(ME, []); + + printSwitchStates.apply(ME, []); }; const SELECT_TARGET_OPTION = async (index: number) => { @@ -73,7 +89,7 @@ export async function changeSwitch() { const SWITCH_NAME = getSwitchName(selectedTarget, index); - const OPTIONS = Enumerable.from( deploy_plugins_switch.getTargetOptionsOf(selectedTarget) ) + const OPTIONS = Enumerable.from( getTargetOptionsOf(selectedTarget) ) .toArray() .sort((x, y) => { return deploy_helpers.compareValuesBy(x, y, @@ -88,7 +104,7 @@ export async function changeSwitch() { let details = ''; let isSelected = false; - const SELECTED_OPTION_OF_TARGET = deploy_plugins_switch.getCurrentOptionOf(selectedTarget); + const SELECTED_OPTION_OF_TARGET = getCurrentOptionOf(selectedTarget); if (SELECTED_OPTION_OF_TARGET) { if (o.__id === SELECTED_OPTION_OF_TARGET.__id) { isSelected = true; @@ -180,6 +196,44 @@ export async function changeSwitch() { } } +/** + * Returns the current option of a target. + * + * @param {deploy_plugins_switch.DeployTargetSwitch} target The target. + * @param {TDefault} [defaultValue] The custom default value. + * + * @return {deploy_plugins_switch.DeployTargetSwitchOption|TDefault} The option (if found). + */ +export function getCurrentOptionOf(target: deploy_plugins_switch.DeployTargetSwitch, + defaultValue = false): deploy_plugins_switch.DeployTargetSwitchOption | TDefault { + if (!target) { + return target; + } + + const TARGET_NAME = deploy_helpers.normalizeString( target.name ); + + const STATES = getSelectedSwitchOptions(); + if (STATES) { + const OPTION = STATES[TARGET_NAME]; + if ('object' === typeof OPTION) { + return OPTION; // found + } + else { + // get first (default) one + // instead + + return Enumerable.from( + getTargetOptionsOf(target) + ).orderBy(o => { + return deploy_helpers.toBooleanSafe(o.isDefault) ? 0 : 1; + }).firstOrDefault(x => true, + defaultValue); + } + } + + return defaultValue; +} + /** * Returns the object that stores the states of all switches. * @@ -225,6 +279,59 @@ function getSwitchOptionName(target: deploy_plugins_switch.DeployTargetSwitchOpt return name; } +/** + * Returns the options of a switch target. + * + * @param {deploy_plugins_switch.DeployTargetSwitch} target The target. + * + * @return {deploy_plugins_switch.DeployTargetSwitchOption[]} The options. + */ +export function getTargetOptionsOf(target: deploy_plugins_switch.DeployTargetSwitch): deploy_plugins_switch.DeployTargetSwitchOption[] { + if (deploy_helpers.isNullOrUndefined(target)) { + return target; + } + + const TARGET_NAME = deploy_helpers.normalizeString(target.name); + + const OPTIONS: deploy_plugins_switch.DeployTargetSwitchOption[] = []; + + let objIndex = -1; + Enumerable.from( deploy_helpers.asArray(target.options) ).where(v => { + return !deploy_helpers.isNullOrUndefined(v); + }).select(v => { + ++objIndex; + + v = deploy_helpers.cloneObject(v); + + if ('object' !== typeof v) { + v = { + targets: [ deploy_helpers.normalizeString(v) ] + }; + } + + v.__id = `${target.__id}\n` + + `${deploy_helpers.normalizeString(deploy_helpers.getSortValue(v))}\n` + + `${objIndex}\n` + + `${deploy_helpers.normalizeString(v.name)}`; + v.__index = objIndex; + + v.targets = Enumerable.from( deploy_helpers.asArray(v.targets) ).select(t => { + return deploy_helpers.normalizeString(t); + }).where(t => '' !== t && + TARGET_NAME !== t) + .distinct() + .toArray(); + + return v; + }) + .pushTo(OPTIONS); + + return OPTIONS.sort((x, y) => { + return deploy_helpers.compareValuesBy(x, y, + o => deploy_helpers.getSortValue(o)); + }); +} + function isSwitch(target: deploy_contracts.DeployTarget): target is deploy_plugins_switch.DeployTargetSwitch { if (target) { return [ @@ -235,6 +342,46 @@ function isSwitch(target: deploy_contracts.DeployTarget): target is deploy_plugi return false; } +export function printSwitchStates() { + const ME: vs_deploy.Deployer = this; + + try { + const SWITCHES: deploy_plugins_switch.DeployTargetSwitch[] = getSwitches.apply(ME, []); + if (SWITCHES.length > 0) { + ME.outputChannel.appendLine(''); + ME.outputChannel.appendLine( + i18.t('plugins.switch.states') + ); + + SWITCHES.forEach((s, i) => { + const TARGET_NAME = getSwitchName(s, i); + + ME.outputChannel.append( + i18.t('plugins.switch.item', + TARGET_NAME), + ); + + const OPTION = getCurrentOptionOf(s); + if (false === OPTION) { + ME.outputChannel.appendLine( + `<${i18.t('plugins.switch.noOptionSelected')}>`, + ); + } + else { + ME.outputChannel.appendLine( + "'" + getSwitchOptionName(OPTION, OPTION.__index) + "'", + ); + } + }); + + ME.outputChannel.appendLine(''); + } + } + catch (e) { + ME.log(`[ERROR :: vs-deploy] switch.printSwitchStates(): ${deploy_helpers.toStringSafe(e)}`); + } +} + /** * Reloads the target states for switches. */ @@ -258,17 +405,23 @@ export function reloadTargetStates() { SWITCHES.filter(s => { return TARGET_NAME === deploy_helpers.normalizeString(s.name); }).forEach(s => { - Enumerable.from( deploy_plugins_switch.getTargetOptionsOf(s) ).where(o => { + Enumerable.from( getTargetOptionsOf(s) ).where(o => { return o.__id === OPTION_ID; }).forEach(o => { - deploy_plugins_switch.setCurrentOptionFor(s, o); + setCurrentOptionFor(s, o); }); }); } } + + // clean update + saveStates.apply(ME, []).then(() => { + }).catch((err) => { + ME.log(`[ERROR :: vs-deploy] switch.reloadTargetStates(2): ${deploy_helpers.toStringSafe(err)}`); + }); } catch (e) { - ME.log(`[ERROR :: vs-deploy] switch.reloadTargetStates(): ${deploy_helpers.toStringSafe(e)}`); + ME.log(`[ERROR :: vs-deploy] switch.reloadTargetStates(1): ${deploy_helpers.toStringSafe(e)}`); } } @@ -304,3 +457,29 @@ export async function saveStates() { ME.log(`[ERROR :: vs-deploy] switch.saveStates(): ${deploy_helpers.toStringSafe(e)}`); } } + +/** + * Sets the current option for a switch target. + * + * @param {deploy_plugins_switch.DeployTargetSwitch} target The target. + * @param {deploy_plugins_switch.DeployTargetSwitchOption} option The option to set. + * + * @return {Object} The new data. + */ +export function setCurrentOptionFor(target: deploy_plugins_switch.DeployTargetSwitch, option: deploy_plugins_switch.DeployTargetSwitchOption): NewSwitchOptionState { + if (!target) { + return target; + } + + const NAME = deploy_helpers.normalizeString( target.name ); + + const STATES = getSelectedSwitchOptions(); + if (STATES) { + STATES[NAME] = option; + + return { + option: STATES[NAME], + target: target, + }; + } +}