Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
added isActive property
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Oct 14, 2017
1 parent b95cdde commit 7cab28b
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 62 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"Cloud"
],
"activationEvents": [
"*"
"workspaceContains:/.vscode/settings.json"
],
"main": "./out/src/extension",
"contributes": {
Expand Down
8 changes: 8 additions & 0 deletions src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
95 changes: 81 additions & 14 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = <deploy_contracts.DeployConfiguration>vscode.workspace.getConfiguration("deploy");

let finished = (err: any, cfg: deploy_contracts.DeployConfiguration) => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<boolean> {
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.
*
Expand Down
105 changes: 62 additions & 43 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -77,42 +75,48 @@ 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<number>((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<any>((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
let deployFilesTo = vscode.commands.registerCommand('extension.deploy.filesTo', (files: string | string[],
targets: vs_contracts.DeployTargetList) => {
return new Promise<boolean>((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);
Expand All @@ -124,18 +128,23 @@ export function activate(context: vscode.ExtensionContext) {
let getTargets = vscode.commands.registerCommand('extension.deploy.getTargets', (cb?: GetTargetsCallback) => {
return new Promise<vs_contracts.DeployTarget[]>((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);
Expand All @@ -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
Expand All @@ -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)}`;
}

Expand All @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 7cab28b

Please sign in to comment.