From dad48848ef4d54d7291f8206b6f386bd8f9a55a4 Mon Sep 17 00:00:00 2001 From: Marcel Kloubert Date: Fri, 29 Dec 2017 22:58:40 +0100 Subject: [PATCH] code cleanups and improvements --- CHANGELOG.md | 5 ++++ package-lock.json | 2 +- package.json | 2 +- src/commands.ts | 5 +++- src/contracts.ts | 9 ++++++ src/plugins.ts | 11 +++++--- src/plugins/script.ts | 5 +++- src/pull.ts | 11 +++++--- src/session.ts | 24 ++++++++++++++++ src/targets/operations/http.ts | 11 +++++--- src/targets/operations/script.ts | 11 +++++--- src/workspaces.ts | 48 +++++++++++++++----------------- 12 files changed, 98 insertions(+), 46 deletions(-) create mode 100644 src/session.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index c3f56b2..bec275e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log (vscode-deploy-reloaded) +## 0.9.0 (December 30th, 2017; improvements) + +* bugfixes +* code improvements + ## 0.8.0 (December 29th, 2017; target operations) * bugfixes diff --git a/package-lock.json b/package-lock.json index c573c34..231fc6e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "vscode-deploy-reloaded", - "version": "0.8.0", + "version": "0.9.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ec09308..4afbe43 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-deploy-reloaded", "displayName": "Deploy (Reloaded)", "description": "Deploys files of a workspace to a destination.", - "version": "0.8.0", + "version": "0.9.0", "publisher": "mkloubert", "engines": { "vscode": "^1.19.0" diff --git a/src/commands.ts b/src/commands.ts index f6798f2..36a4436 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -19,6 +19,7 @@ import * as deploy_contracts from './contracts'; import * as deploy_events from './events'; import * as deploy_helpers from './helpers'; import * as deploy_log from './log'; +import * as deploy_session from './session'; import * as deploy_values from './values'; import * as deploy_workspaces from './workspaces'; import * as Events from 'events'; @@ -188,7 +189,8 @@ export async function reloadCommands(newCfg: deploy_contracts.Configuration) { const CTX: ScriptCommandExecutionContext = { button: btn, command: id, - events: ME.sessionState['commands']['events'], + events: ME.workspaceSessionState['commands']['events'], + extension: ME.context.extension, globalEvents: deploy_events.EVENTS, globals: ME.globals, globalState: GLOBAL_STATE, @@ -200,6 +202,7 @@ export async function reloadCommands(newCfg: deploy_contracts.Configuration) { require: (moduleId) => { return deploy_helpers.requireFromExtension(moduleId); }, + sessionState: deploy_session.SESSION_STATE, state: undefined, }; diff --git a/src/contracts.ts b/src/contracts.ts index bf663ef..5cc1b90 100644 --- a/src/contracts.ts +++ b/src/contracts.ts @@ -373,6 +373,10 @@ export interface ScriptArguments { * Event emitter for scripts of that kind. */ readonly events: NodeJS.EventEmitter; + /** + * The context of the underlying extension. + */ + readonly extension: vscode.ExtensionContext; /** * Gets the emitter for global extension events. */ @@ -406,6 +410,11 @@ export interface ScriptArguments { * Imports a module from the extension context. */ readonly require: (id: any) => any; + /** + * The extension wide object that shares data with anything + * across the extension. + */ + readonly sessionState: KeyValuePairs; /** * Gets or sets a state value for the underlying script. */ diff --git a/src/plugins.ts b/src/plugins.ts index 9ed6563..5602461 100644 --- a/src/plugins.ts +++ b/src/plugins.ts @@ -22,6 +22,7 @@ import * as deploy_files from './files'; import * as deploy_helpers from './helpers'; import * as deploy_log from './log'; import * as deploy_objects from './objects'; +import * as deploy_session from './session'; import * as deploy_targets from './targets'; import * as deploy_transformers from './transformers'; import * as deploy_values from './values'; @@ -534,10 +535,11 @@ export abstract class FileToUploadBase implements FileToUpload { ); const CONTEXT: deploy_transformers.DataTransformerContext = { - events: ME.workspace.sessionState['upload']['events'], + events: ME.workspace.workspaceSessionState['upload']['events'], + extension: ME.workspace.context.extension, globalEvents: deploy_events.EVENTS, globals: ME.workspace.globals, - globalState: ME.workspace.sessionState['upload']['states']['global'], + globalState: ME.workspace.workspaceSessionState['upload']['states']['global'], logger: deploy_log.CONSOLE, mode: deploy_transformers.DataTransformerMode.Transform, options: ME.transformerOptions, @@ -548,6 +550,7 @@ export abstract class FileToUploadBase implements FileToUpload { require: (id) => { return deploy_helpers.requireFromExtension(id); }, + sessionState: deploy_session.SESSION_STATE, state: undefined, }; @@ -556,11 +559,11 @@ export abstract class FileToUploadBase implements FileToUpload { enumerable: true, get: () => { - return ME.workspace.sessionState['upload']['states']['data_transformers'][STATE_KEY]; + return ME.workspace.workspaceSessionState['upload']['states']['data_transformers'][STATE_KEY]; }, set: (newValue) => { - ME.workspace.sessionState['upload']['states']['data_transformers'][STATE_KEY] = newValue; + ME.workspace.workspaceSessionState['upload']['states']['data_transformers'][STATE_KEY] = newValue; } }); diff --git a/src/plugins/script.ts b/src/plugins/script.ts index d1394c5..4d16327 100644 --- a/src/plugins/script.ts +++ b/src/plugins/script.ts @@ -21,6 +21,7 @@ import * as deploy_files from '../files'; import * as deploy_helpers from '../helpers'; import * as deploy_log from '../log'; import * as deploy_plugins from '../plugins'; +import * as deploy_session from '../session'; import * as deploy_targets from '../targets'; import * as deploy_workspaces from '../workspaces'; import * as Events from 'events'; @@ -122,10 +123,11 @@ class ScriptPlugin extends deploy_plugins.PluginBase { cancellationToken: undefined, dir: context['dir'], events: ME._EVENTS, + extension: context.target.__workspace.context.extension, files: context['files'], globalEvents: deploy_events.EVENTS, globals: context.target.__workspace.globals, - globalState: this._GLOBAL_STATE, + globalState: ME._GLOBAL_STATE, isCancelling: undefined, logger: deploy_log.CONSOLE, operation: operation, @@ -137,6 +139,7 @@ class ScriptPlugin extends deploy_plugins.PluginBase { require: (id) => { return deploy_helpers.requireFromExtension(id); }, + sessionState: deploy_session.SESSION_STATE, state: undefined, target: context.target, workspace: undefined, diff --git a/src/pull.ts b/src/pull.ts index 65fd67d..421f3b5 100644 --- a/src/pull.ts +++ b/src/pull.ts @@ -21,6 +21,7 @@ import * as deploy_helpers from './helpers'; import * as deploy_log from './log'; import * as deploy_packages from './packages'; import * as deploy_plugins from './plugins'; +import * as deploy_session from './session'; import * as deploy_targets from './targets'; import * as deploy_transformers from './transformers'; import * as deploy_workspaces from './workspaces'; @@ -268,10 +269,11 @@ export async function pullFilesFrom(files: string[], ); const CONTEXT: deploy_transformers.DataTransformerContext = { - events: ME.sessionState['pull']['events'], + events: ME.workspaceSessionState['pull']['events'], + extension: ME.context.extension, globalEvents: deploy_events.EVENTS, globals: ME.globals, - globalState: ME.sessionState['pull']['states']['global'], + globalState: ME.workspaceSessionState['pull']['states']['global'], logger: deploy_log.CONSOLE, mode: deploy_transformers.DataTransformerMode.Restore, options: TRANSFORMER_OPTIONS, @@ -281,6 +283,7 @@ export async function pullFilesFrom(files: string[], require: (id) => { return deploy_helpers.requireFromExtension(id); }, + sessionState: deploy_session.SESSION_STATE, state: undefined, }; @@ -289,11 +292,11 @@ export async function pullFilesFrom(files: string[], enumerable: true, get: () => { - return ME.sessionState['pull']['states']['data_transformers'][STATE_KEY]; + return ME.workspaceSessionState['pull']['states']['data_transformers'][STATE_KEY]; }, set: (newValue) => { - ME.sessionState['pull']['states']['data_transformers'][STATE_KEY] = newValue; + ME.workspaceSessionState['pull']['states']['data_transformers'][STATE_KEY] = newValue; } }); diff --git a/src/session.ts b/src/session.ts new file mode 100644 index 0000000..a5586c4 --- /dev/null +++ b/src/session.ts @@ -0,0 +1,24 @@ +/** + * This file is part of the vscode-deploy-reloaded distribution. + * Copyright (c) Marcel Joachim Kloubert. + * + * vscode-deploy-reloaded is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, version 3. + * + * vscode-deploy-reloaded is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +import * as deploy_contracts from './contracts'; + + +/** + * The global extension wide state object. + */ +export const SESSION_STATE: deploy_contracts.KeyValuePairs = {}; diff --git a/src/targets/operations/http.ts b/src/targets/operations/http.ts index 7dd3854..c1c88ff 100644 --- a/src/targets/operations/http.ts +++ b/src/targets/operations/http.ts @@ -20,6 +20,7 @@ import * as deploy_events from '../../events'; import * as deploy_helpers from '../../helpers'; import * as deploy_http from '../../http'; import * as deploy_log from '../../log'; +import * as deploy_session from '../../session'; import * as deploy_targets from '../../targets'; import * as i18 from '../../i18'; import * as OS from 'os'; @@ -203,10 +204,11 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo getBodyToSend = async () => { const ARGS: HttpBodyModuleExecutionArguments = { context: context, - events: WORKSPACE.sessionState['target_operations']['http']['events'], + events: WORKSPACE.workspaceSessionState['target_operations']['http']['events'], + extension: WORKSPACE.context.extension, globalEvents: deploy_events.EVENTS, globals: WORKSPACE.globals, - globalState: WORKSPACE.sessionState['target_operations']['http']['global'], + globalState: WORKSPACE.workspaceSessionState['target_operations']['http']['global'], logger: deploy_log.CONSOLE, options: deploy_helpers.cloneObject(OPERATION.options), replaceWithValues: (val) => { @@ -215,6 +217,7 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo require: (id) => { return deploy_helpers.requireFromExtension(id); }, + sessionState: deploy_session.SESSION_STATE, state: undefined, url: URL, }; @@ -224,11 +227,11 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo enumerable: true, get: () => { - return WORKSPACE.sessionState['target_operations']['http']['body_scripts'][bodyScriptFullPath]; + return WORKSPACE.workspaceSessionState['target_operations']['http']['body_scripts'][bodyScriptFullPath]; }, set: (newValue) => { - WORKSPACE.sessionState['target_operations']['http']['body_scripts'][bodyScriptFullPath] = newValue; + WORKSPACE.workspaceSessionState['target_operations']['http']['body_scripts'][bodyScriptFullPath] = newValue; } }); diff --git a/src/targets/operations/script.ts b/src/targets/operations/script.ts index 71b935e..d9be456 100644 --- a/src/targets/operations/script.ts +++ b/src/targets/operations/script.ts @@ -19,6 +19,7 @@ import * as deploy_contracts from '../../contracts'; import * as deploy_events from '../../events'; import * as deploy_helpers from '../../helpers'; import * as deploy_log from '../../log'; +import * as deploy_session from '../../session'; import * as deploy_targets from '../../targets'; import * as i18 from '../../i18'; @@ -101,10 +102,11 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo if (EXECUTE) { const ARGS: ScriptTargetOperationExecutionArguments = { context: context, - events: WORKSPACE.sessionState['target_operations']['script']['events'], + events: WORKSPACE.workspaceSessionState['target_operations']['script']['events'], + extension: WORKSPACE.context.extension, globalEvents: deploy_events.EVENTS, globals: WORKSPACE.globals, - globalState: WORKSPACE.sessionState['target_operations']['script']['global'], + globalState: WORKSPACE.workspaceSessionState['target_operations']['script']['global'], logger: deploy_log.CONSOLE, options: deploy_helpers.cloneObject(OPERATION.options), replaceWithValues: (val) => { @@ -113,6 +115,7 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo require: (id) => { return deploy_helpers.requireFromExtension(id); }, + sessionState: deploy_session.SESSION_STATE, state: undefined, }; @@ -121,11 +124,11 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo enumerable: true, get: () => { - return WORKSPACE.sessionState['target_operations']['script']['scripts'][scriptFullPath]; + return WORKSPACE.workspaceSessionState['target_operations']['script']['scripts'][scriptFullPath]; }, set: (newValue) => { - WORKSPACE.sessionState['target_operations']['script']['scripts'][scriptFullPath] = newValue; + WORKSPACE.workspaceSessionState['target_operations']['script']['scripts'][scriptFullPath] = newValue; } }); diff --git a/src/workspaces.ts b/src/workspaces.ts index e027828..2d3f9a9 100644 --- a/src/workspaces.ts +++ b/src/workspaces.ts @@ -263,7 +263,6 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c private _PACKAGE_BUTTONS: PackageWithButton[] = []; private _packages: deploy_packages.Package[]; private _rootPath: string | false; - private _sessionState: deploy_contracts.KeyValuePairs; private _selectedSwitches: deploy_contracts.KeyValuePairs; /** * Stores the start time. @@ -275,6 +274,7 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c * The current translation function. */ protected _translator: i18next.TranslationFunction; + private _workspaceSessionState: deploy_contracts.KeyValuePairs; /** * Initializes a new instance of that class. @@ -288,7 +288,8 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c public readonly context: WorkspaceContext) { super(); - this.state = new WorkspaceMemento(this); + this.state = new WorkspaceMemento(this, + context.extension.workspaceState); } /** @@ -461,7 +462,7 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c return false; } - private createSessionState(newCfg: WorkspaceSettings) { + private createWorkspaceSessionState(newCfg: WorkspaceSettings) { const NEW_SESSION_STATE: deploy_contracts.KeyValuePairs = {}; NEW_SESSION_STATE['commands'] = {}; @@ -2142,7 +2143,7 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c const OLD_CFG = ME._config; ME._config = loadedCfg; - ME._sessionState = ME.createSessionState(loadedCfg); + ME._workspaceSessionState = ME.createWorkspaceSessionState(loadedCfg); ME._lastConfigUpdate = Moment(); try { @@ -2392,6 +2393,7 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c const PACKAGE_TO_DEPLOY = P; const PACKAGE_NAME = deploy_packages.getPackageName(PACKAGE_TO_DEPLOY); + // additional values const VALUES: deploy_values.Value[] = [ new deploy_values.StaticValue({ value: pb @@ -2403,14 +2405,6 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c }, 'package' ), - new deploy_values.FunctionValue( - () => ME.name, - 'workspace' - ), - new deploy_values.FunctionValue( - () => ME.rootPath, - 'workspace_folder' - ) ]; let newCmdId = deploy_helpers.toStringSafe(pb.command); @@ -2431,11 +2425,13 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c } }); + //TODO: translate deploy_log.CONSOLE .info(`Registrated command '${newCmdId}' for button of package '${PACKAGE_NAME}'.`, 'workspaces.Workspace.reloadPackageButtons()'); } else { + //TODO: translate deploy_log.CONSOLE .warn(`Button of package '${PACKAGE_NAME}' will use the existing command '${newCmdId}'.`, 'workspaces.Workspace.reloadPackageButtons()'); @@ -2719,13 +2715,6 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c return this._selectedSwitches; } - /** - * Gets the current session data storage. - */ - public get sessionState(): deploy_contracts.KeyValuePairs { - return this._sessionState; - } - /** * Gets the full path of a settings folder. */ @@ -2816,7 +2805,7 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c * Gets the states for 'sync when open'. */ public get syncWhenOpenStates(): SyncWhenOpenStates { - return this.sessionState['sync']['whenOpen']['states']; + return this.workspaceSessionState['sync']['whenOpen']['states']; } /** @inheritdoc */ @@ -2964,7 +2953,7 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c ME.replaceWithValues(SB.settings.color, ADDITIONAL_VALUES) ); if ('' === color) { - color = '#ffffff'; + color = false === OPTION ? '#ffff00' : '#ffff00'; } let text = deploy_helpers.toStringSafe( @@ -2984,8 +2973,6 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c if (false === OPTION) { tooltip = i18.t('plugins.switch.button.tooltip', i18.t('plugins.switch.noOptionSelected')); - - color = '#ffff00'; } else { tooltip = i18.t('plugins.switch.button.tooltip', @@ -3002,6 +2989,13 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c .trace(e, 'workspaces.Workspace.updateSwitchButtons()'); } } + + /** + * Gets the current session data storage. + */ + public get workspaceSessionState(): deploy_contracts.KeyValuePairs { + return this._workspaceSessionState; + } } /** @@ -3012,13 +3006,15 @@ export class WorkspaceMemento implements vscode.Memento { * Initializes a new instance of that class. * * @param {Workspace} workspace The underlying workspace. + * @param {vscode.Memento} _MEMENTO The memento to use. */ - constructor(public readonly workspace: Workspace) { + constructor(public readonly workspace: Workspace, + private readonly _MEMENTO: vscode.Memento) { } /** @inheritdoc */ public get(key: any, defaultValue?: TDefault): T | TDefault { - return this.workspace.context.extension.workspaceState.get( + return this._MEMENTO.get( this.normalizeKey(key), defaultValue ); @@ -3030,7 +3026,7 @@ export class WorkspaceMemento implements vscode.Memento { /** @inheritdoc */ public async update(key: any, value: any) { - await this.workspace.context.extension.workspaceState.update( + await this._MEMENTO.update( this.normalizeKey(key), value, );