-
Notifications
You must be signed in to change notification settings - Fork 30k
/
cli.contribution.ts
152 lines (127 loc) · 5.47 KB
/
cli.contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import * as path from 'path';
import * as cp from 'child_process';
import * as pfs from 'vs/base/node/pfs';
import { nfcall } from 'vs/base/common/async';
import { TPromise } from 'vs/base/common/winjs.base';
import URI from 'vs/base/common/uri';
import { Action } from 'vs/base/common/actions';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
import { Registry } from 'vs/platform/registry/common/platform';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
import { IMessageService, Severity } from 'vs/platform/message/common/message';
import product from 'vs/platform/node/product';
function ignore<T>(code: string, value: T = null): (err: any) => TPromise<T> {
return err => err.code === code ? TPromise.as<T>(value) : TPromise.wrapError<T>(err);
}
const root = URI.parse(require.toUrl('')).fsPath;
const source = path.resolve(root, '..', 'bin', 'code');
function isAvailable(): TPromise<boolean> {
return pfs.exists(source);
}
class InstallAction extends Action {
static readonly ID = 'workbench.action.installCommandLine';
static LABEL = nls.localize('install', "Install '{0}' command in PATH", product.applicationName);
constructor(
id: string,
label: string,
@IMessageService private messageService: IMessageService
) {
super(id, label);
}
private get target(): string {
return `/usr/local/bin/${product.applicationName}`;
}
run(): TPromise<void> {
return isAvailable().then(isAvailable => {
if (!isAvailable) {
const message = nls.localize('not available', "This command is not available");
this.messageService.show(Severity.Info, message);
return undefined;
}
return this.isInstalled()
.then(isInstalled => {
if (!isAvailable || isInstalled) {
return TPromise.as(null);
} else {
const createSymlink = () => {
return pfs.unlink(this.target)
.then(null, ignore('ENOENT'))
.then(() => pfs.symlink(source, this.target));
};
return createSymlink().then(null, err => {
if (err.code === 'EACCES' || err.code === 'ENOENT') {
return this.createBinFolder()
.then(() => createSymlink());
}
return TPromise.wrapError(err);
});
}
})
.then(() => {
this.messageService.show(Severity.Info, nls.localize('successIn', "Shell command '{0}' successfully installed in PATH.", product.applicationName));
});
});
}
private isInstalled(): TPromise<boolean> {
return pfs.lstat(this.target)
.then(stat => stat.isSymbolicLink())
.then(() => pfs.readlink(this.target))
.then(link => link === source)
.then(null, ignore('ENOENT', false));
}
private createBinFolder(): TPromise<void> {
return new TPromise<void>((c, e) => {
const message = nls.localize('warnEscalation', "Code will now prompt with 'osascript' for Administrator privileges to install the shell command.");
const actions = [
new Action('ok', nls.localize('ok', "OK"), '', true, () => {
const command = 'osascript -e "do shell script \\"mkdir -p /usr/local/bin && chown \\" & (do shell script (\\"whoami\\")) & \\" /usr/local/bin\\" with administrator privileges"';
nfcall(cp.exec, command, {})
.then(null, _ => TPromise.wrapError(new Error(nls.localize('cantCreateBinFolder', "Unable to create '/usr/local/bin'."))))
.done(c, e);
return null;
}),
new Action('cancel2', nls.localize('cancel2', "Cancel"), '', true, () => { e(new Error(nls.localize('aborted', "Aborted"))); return null; })
];
this.messageService.show(Severity.Info, { message, actions });
});
}
}
class UninstallAction extends Action {
static readonly ID = 'workbench.action.uninstallCommandLine';
static LABEL = nls.localize('uninstall', "Uninstall '{0}' command from PATH", product.applicationName);
constructor(
id: string,
label: string,
@IMessageService private messageService: IMessageService
) {
super(id, label);
}
private get target(): string {
return `/usr/local/bin/${product.applicationName}`;
}
run(): TPromise<void> {
return isAvailable().then(isAvailable => {
if (!isAvailable) {
const message = nls.localize('not available', "This command is not available");
this.messageService.show(Severity.Info, message);
return undefined;
}
return pfs.unlink(this.target)
.then(null, ignore('ENOENT'))
.then(() => {
this.messageService.show(Severity.Info, nls.localize('successFrom', "Shell command '{0}' successfully uninstalled from PATH.", product.applicationName));
});
});
}
}
if (process.platform === 'darwin') {
const category = nls.localize('shellCommand', "Shell Command");
const workbenchActionsRegistry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(InstallAction, InstallAction.ID, InstallAction.LABEL), 'Shell Command: Install \'code\' command in PATH', category);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(UninstallAction, UninstallAction.ID, UninstallAction.LABEL), 'Shell Command: Uninstall \'code\' command from PATH', category);
}