From 7cab28b31d279acdd72435ac3757db603ac289ba Mon Sep 17 00:00:00 2001 From: Marcel Kloubert Date: Sat, 14 Oct 2017 11:38:36 +0200 Subject: [PATCH] added isActive property --- package.json | 2 +- src/contracts.ts | 8 ++++ src/deploy.ts | 95 +++++++++++++++++++++++++++++++++++------- src/extension.ts | 105 ++++++++++++++++++++++++++++------------------- src/plugins.ts | 2 + src/workspace.ts | 6 +-- 6 files changed, 156 insertions(+), 62 deletions(-) diff --git a/package.json b/package.json index b826a0b..807730c 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "Cloud" ], "activationEvents": [ - "*" + "workspaceContains:/.vscode/settings.json" ], "main": "./out/src/extension", "contributes": { diff --git a/src/contracts.ts b/src/contracts.ts index 1b51cc1..3f4b218 100644 --- a/src/contracts.ts +++ b/src/contracts.ts @@ -125,6 +125,10 @@ export const EVENT_SYNCWHENOPEN_ENABLE = 'deploy.syncWhenOpen.enable'; * feature should be toggled. */ export const EVENT_SYNCWHENOPEN_TOGGLE = 'deploy.syncWhenOpen.toggle'; +/** + * Name of the event that is raised when workspace (folder) changed. + */ +export const EVENT_WORKSPACE_CHANGED = 'deploy.workspace.changed'; /** * An object that can handle access keys. @@ -828,6 +832,10 @@ export interface DeployContext extends ConditionalItemFilter, vscode.Disposable, * @chainable */ info: (msg: any) => DeployContext; + /** + * Returns if the underlying extension is currently active or not. + */ + isActive: () => boolean; /** * Returns if cancellation has been requested or not. * diff --git a/src/deploy.ts b/src/deploy.ts index 9b5c941..79f2d25 100644 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -2142,6 +2142,26 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { return this._htmlDocs; } + /** + * Gets if the extension is currently active or not. + */ + public get isActive(): boolean { + return !deploy_helpers.isEmptyString(deploy_workspace.getRootPath()); + } + + /** + * Checks if a file (or directory) path is ignored. + * + * @param {string} fileOrDir The file / directory to check. + * + * @return {boolean} Is ignored or not. + */ + public isFileIgnored(fileOrDir: string): boolean { + return deploy_helpers.isFileIgnored(fileOrDir, this.config.ignore, + this.config.useGitIgnoreStylePatterns, + this.config.fastCheckForIgnores); + } + /** * Gets the timestamp of the last config update. */ @@ -2239,19 +2259,6 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { } } - /** - * Checks if a file (or directory) path is ignored. - * - * @param {string} fileOrDir The file / directory to check. - * - * @return {boolean} Is ignored or not. - */ - public isFileIgnored(fileOrDir: string): boolean { - return deploy_helpers.isFileIgnored(fileOrDir, this.config.ignore, - this.config.useGitIgnoreStylePatterns, - this.config.fastCheckForIgnores); - } - /** * Logs a message. * @@ -2295,7 +2302,9 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { this.registerGlobalEvents(); - this.reloadConfiguration(); + deploy_globals.EVENTS.emit( + deploy_contracts.EVENT_WORKSPACE_CHANGED + ); this.setupFileSystemWatcher(); } @@ -2340,6 +2349,18 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { this.reloadConfiguration(); } + /** + * Event after list of workspace folders changed. + * + * @param {vscode.WorkspaceFoldersChangeEvent} e The event arguments. + */ + public onDidChangeWorkspaceFolders(e: vscode.WorkspaceFoldersChangeEvent) { + deploy_globals.EVENTS.emit( + deploy_contracts.EVENT_WORKSPACE_CHANGED, + e + ); + } + /** * Event after a document has been saved. * @@ -2555,6 +2576,10 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { return; } + if (!this.isActive) { + return; // not active + } + if (deploy_helpers.toBooleanSafe(this.config.deployOnSave, true) && this._isDeployOnSaveEnabled) { @@ -2571,6 +2596,9 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { */ protected onFileChange(e: vscode.Uri, type: string) { let me = this; + if (!me.isActive) { + return; // not active + } if (deploy_helpers.toBooleanSafe(me._isDeployOnChangeFreezed)) { // freezed @@ -3788,6 +3816,10 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { protected registerGlobalEvents() { let me = this; + deploy_globals.EVENTS.on(deploy_contracts.EVENT_WORKSPACE_CHANGED, () => { + me.reloadConfiguration(); + }); + // deploy.deployOnChange.* deploy_globals.EVENTS.on(deploy_contracts.EVENT_DEPLOYONCHANGE_DISABLE, function() { me._isDeployOnChangeEnabled = false; @@ -3936,6 +3968,11 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { public reloadConfiguration() { let me = this; + if (!me.isActive) { + me._config = null; + return; + } + let loadedCfg = vscode.workspace.getConfiguration("deploy"); let finished = (err: any, cfg: deploy_contracts.DeployConfiguration) => { @@ -4459,6 +4496,7 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { }; ctx.filterConditionalItems = (items) => me.filterConditionalItems(items); ctx.globals = () => me.getGlobals(); + ctx.isActive = () => me.isActive; ctx.log = function(msg) { me.log(msg); return this; @@ -4770,6 +4808,35 @@ export class Deployer extends Events.EventEmitter implements vscode.Disposable { } } + /** + * Shows a warning message if extension is currently active or not. + * + * @param ifActive + */ + public async showWarningIfNotActive(ifActive?: () => any): Promise { + const ME = this; + + if (ME.isActive) { + if (ifActive) { + await Promise.resolve( + ifActive() + ); + } + + return true; + } + else { + vscode.window.showWarningMessage( + "[vs-deploy] The extension is currently not active! Please open a workspace, before you continue." + ).then(() => {}, (err) => { + ME.log(i18.t('errors.withCategory', + 'Deployer.showWarningIfNotActive()', err)); + }); + + return false; + } + } + /** * Starts external extensions. * diff --git a/src/extension.ts b/src/extension.ts index 9483bc7..0ced2c6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -46,8 +46,6 @@ let deployer: vs_deploy.Deployer; export function activate(context: vscode.ExtensionContext) { let now = Moment(); - deploy_workspace.resetSelectedWorkspaceFolder(); - // version let pkgFile: vs_contracts.PackageFile; try { @@ -77,31 +75,32 @@ export function activate(context: vscode.ExtensionContext) { deployer = new vs_deploy.Deployer(context, outputChannel, pkgFile); + context.subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders(deployer.onDidChangeWorkspaceFolders, deployer)); + deploy_workspace.resetSelectedWorkspaceFolder(); + // deploy workspace - let deploy = vscode.commands.registerCommand('extension.deploy', () => { - return new Promise((resolve, reject) => { - deployer.deployWorkspace().then((code) => { - resolve(code); - }).catch((e) => { - reject(e); - }); + let deploy = vscode.commands.registerCommand('extension.deploy', async () => { + let code: number; + + await deployer.showWarningIfNotActive(async () => { + code = await deployer.deployWorkspace(); }); + + return code; }); // compare local file with remote - let compareFiles = vscode.commands.registerCommand('extension.deploy.compareFiles', (u?) => { - return new Promise((resolve, reject) => { - deployer.compareFiles(u).then((r) => { - resolve(r); - }).catch((err) => { - reject(err); - }); + let compareFiles = vscode.commands.registerCommand('extension.deploy.compareFiles', async (u?) => { + await deployer.showWarningIfNotActive(async () => { + await deployer.compareFiles(u); }); }); // deploy open file or selected folder - let deployFileOrFolder = vscode.commands.registerCommand('extension.deploy.file', (u?) => { - deployer.deployFileOrFolder(u); + let deployFileOrFolder = vscode.commands.registerCommand('extension.deploy.file', async (u?) => { + await deployer.showWarningIfNotActive(async () => { + await deployer.deployFileOrFolder(u); + }); }); // deploys files using global events @@ -109,10 +108,15 @@ export function activate(context: vscode.ExtensionContext) { targets: vs_contracts.DeployTargetList) => { return new Promise((resolve, reject) => { try { - let sym = Symbol('extension.deploy.filesTo'); - - resolve(deploy_globals.EVENTS.emit(vs_contracts.EVENT_DEPLOYFILES, - files, targets, sym)); + if (deployer.isActive) { + let sym = Symbol('extension.deploy.filesTo'); + + resolve(deploy_globals.EVENTS.emit(vs_contracts.EVENT_DEPLOYFILES, + files, targets, sym)); + } + else { + reject(new Error(`vs-deploy NOT ACTIVE!`)); + } } catch (e) { reject(e); @@ -124,18 +128,23 @@ export function activate(context: vscode.ExtensionContext) { let getTargets = vscode.commands.registerCommand('extension.deploy.getTargets', (cb?: GetTargetsCallback) => { return new Promise((resolve, reject) => { try { - let targets = deployer.getTargets(); - - if (cb) { - try { - cb(null, targets); - } - catch (e) { - cb(e); + if (deployer.isActive) { + let targets = deployer.getTargets(); + + if (cb) { + try { + cb(null, targets); + } + catch (e) { + cb(e); + } } + + resolve(targets); + } + else { + resolve(null); } - - resolve(targets); } catch (e) { reject(e); @@ -144,8 +153,10 @@ export function activate(context: vscode.ExtensionContext) { }); // listen for files - let listen = vscode.commands.registerCommand('extension.deploy.listen', () => { - deployer.listen(); + let listen = vscode.commands.registerCommand('extension.deploy.listen', async () => { + await deployer.showWarningIfNotActive(() => { + deployer.listen(); + }); }); // open HTML document @@ -167,7 +178,7 @@ export function activate(context: vscode.ExtensionContext) { `&x=${encodeURIComponent(deploy_helpers.toStringSafe(new Date().getTime()))}`); let title = deploy_helpers.toStringSafe(doc.title).trim(); - if (!title) { + if ('' === title) { title = `[vs-deploy] HTML document #${deploy_helpers.toStringSafe(doc.id)}`; } @@ -189,23 +200,31 @@ export function activate(context: vscode.ExtensionContext) { }); // open template - let openTemplate = vscode.commands.registerCommand('extension.deploy.openTemplate', () => { - deployer.openTemplate(); + let openTemplate = vscode.commands.registerCommand('extension.deploy.openTemplate', async () => { + await deployer.showWarningIfNotActive(() => { + deployer.openTemplate(); + }); }); // quick deploy packages - let quickDeploy = vscode.commands.registerCommand('extension.deploy.quickDeploy', () => { - deployer.quickDeploy(); + let quickDeploy = vscode.commands.registerCommand('extension.deploy.quickDeploy', async () => { + await deployer.showWarningIfNotActive(() => { + deployer.quickDeploy(); + }); }); // pull workspace - let pull = vscode.commands.registerCommand('extension.deploy.pullWorkspace', () => { - deployer.pullWorkspace(); + let pull = vscode.commands.registerCommand('extension.deploy.pullWorkspace', async () => { + await deployer.showWarningIfNotActive(() => { + deployer.pullWorkspace(); + }); }); // pull open file or selected folder - let pullFileOrFolder = vscode.commands.registerCommand('extension.deploy.pullFile', (u?: any) => { - deployer.pullFileOrFolder(u); + let pullFileOrFolder = vscode.commands.registerCommand('extension.deploy.pullFile', async (u?: any) => { + await deployer.showWarningIfNotActive(() => { + deployer.pullFileOrFolder(u); + }); }); let htmlViewer = vscode.workspace.registerTextDocumentContentProvider('vs-deploy-html', diff --git a/src/plugins.ts b/src/plugins.ts index 3cc060e..d65cdf7 100644 --- a/src/plugins.ts +++ b/src/plugins.ts @@ -85,6 +85,7 @@ export function createPluginContext(baseCtx?: deploy_contracts.DeployContext): d return this; }, + isActive: () => !deploy_helpers.isEmptyString(deploy_workspace.getRootPath()), isCancelling: () => hasCancelled, log: null, once: function(event, cb) { @@ -153,6 +154,7 @@ export function createPluginContext(baseCtx?: deploy_contracts.DeployContext): d }; ctx.filterConditionalItems = (items) => baseCtx.filterConditionalItems(items), ctx.globals = () => baseCtx.globals(); + ctx.isActive = () => baseCtx.isActive(); ctx.log = function(msg) { baseCtx.log(msg); return this; diff --git a/src/workspace.ts b/src/workspace.ts index 75130e9..c902f00 100644 --- a/src/workspace.ts +++ b/src/workspace.ts @@ -66,11 +66,9 @@ export function getRootPath() { } } - if ('undefined' === typeof workspace_root) { - workspace_root = './'; + if ('undefined' !== typeof workspace_root) { + return Path.resolve(workspace_root); } - - return Path.resolve(workspace_root); } /**