-
Notifications
You must be signed in to change notification settings - Fork 323
/
commandsAndMenu.ts
337 lines (304 loc) · 9.51 KB
/
commandsAndMenu.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { JupyterFrontEnd } from '@jupyterlab/application';
import {
Dialog,
InputDialog,
MainAreaWidget,
showDialog,
showErrorMessage
} from '@jupyterlab/apputils';
import { FileBrowser } from '@jupyterlab/filebrowser';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ITerminal } from '@jupyterlab/terminal';
import { CommandRegistry } from '@lumino/commands';
import { Menu } from '@lumino/widgets';
import { IGitExtension } from './tokens';
import { GitExtension } from './model';
import { GitCredentialsForm } from './widgets/CredentialsBox';
import { doGitClone } from './widgets/gitClone';
import { GitPullPushDialog, Operation } from './widgets/gitPushPull';
const RESOURCES = [
{
text: 'Set Up Remotes',
url: 'https://www.atlassian.com/git/tutorials/setting-up-a-repository'
},
{
text: 'Git Documentation',
url: 'https://git-scm.com/doc'
}
];
/**
* The command IDs used by the git plugin.
*/
export namespace CommandIDs {
export const gitUI = 'git:ui';
export const gitTerminalCommand = 'git:terminal-command';
export const gitInit = 'git:init';
export const gitOpenUrl = 'git:open-url';
export const gitToggleSimpleStaging = 'git:toggle-simple-staging';
export const gitToggleDoubleClickDiff = 'git:toggle-double-click-diff';
export const gitAddRemote = 'git:add-remote';
export const gitClone = 'git:clone';
export const gitOpenGitignore = 'git:open-gitignore';
export const gitPush = 'git:push';
export const gitPull = 'git:pull';
}
/**
* Add the commands for the git extension.
*/
export function addCommands(
app: JupyterFrontEnd,
model: IGitExtension,
fileBrowser: FileBrowser,
settings: ISettingRegistry.ISettings
) {
const { commands, shell } = app;
/**
* Add open terminal in the Git repository
*/
commands.addCommand(CommandIDs.gitTerminalCommand, {
label: 'Open Git Repository in Terminal',
caption: 'Open a New Terminal to the Git Repository',
execute: async args => {
const main = (await commands.execute(
'terminal:create-new',
args
)) as MainAreaWidget<ITerminal.ITerminal>;
try {
if (model.pathRepository !== null) {
const terminal = main.content;
terminal.session.send({
type: 'stdin',
content: [`cd "${model.pathRepository.split('"').join('\\"')}"\n`]
});
}
return main;
} catch (e) {
console.error(e);
main.dispose();
}
},
isEnabled: () => model.pathRepository !== null
});
/** Add open/go to git interface command */
commands.addCommand(CommandIDs.gitUI, {
label: 'Git Interface',
caption: 'Go to Git user interface',
execute: () => {
try {
shell.activateById('jp-git-sessions');
} catch (err) {
console.error('Fail to open Git tab.');
}
}
});
/** Add git init command */
commands.addCommand(CommandIDs.gitInit, {
label: 'Initialize a Repository',
caption: 'Create an empty Git repository or reinitialize an existing one',
execute: async () => {
const currentPath = fileBrowser.model.path;
const result = await showDialog({
title: 'Initialize a Repository',
body: 'Do you really want to make this directory a Git Repo?',
buttons: [Dialog.cancelButton(), Dialog.warnButton({ label: 'Yes' })]
});
if (result.button.accept) {
await model.init(currentPath);
model.pathRepository = currentPath;
}
},
isEnabled: () => model.pathRepository === null
});
/** Open URL externally */
commands.addCommand(CommandIDs.gitOpenUrl, {
label: args => args['text'] as string,
execute: args => {
const url = args['url'] as string;
window.open(url);
}
});
/** add toggle for simple staging */
commands.addCommand(CommandIDs.gitToggleSimpleStaging, {
label: 'Simple staging',
isToggled: () => !!settings.composite['simpleStaging'],
execute: args => {
settings.set('simpleStaging', !settings.composite['simpleStaging']);
}
});
/** add toggle for double click opens diffs */
commands.addCommand(CommandIDs.gitToggleDoubleClickDiff, {
label: 'Double click opens diff',
isToggled: () => !!settings.composite['doubleClickDiff'],
execute: args => {
settings.set('doubleClickDiff', !settings.composite['doubleClickDiff']);
}
});
/** Command to add a remote Git repository */
commands.addCommand(CommandIDs.gitAddRemote, {
label: 'Add Remote Repository',
caption: 'Add a Git remote repository',
isEnabled: () => model.pathRepository !== null,
execute: async args => {
if (model.pathRepository === null) {
console.warn('Not in a Git repository. Unable to add a remote.');
return;
}
let url = args['url'] as string;
const name = args['name'] as string;
if (!url) {
const result = await InputDialog.getText({
title: 'Add a remote repository',
placeholder: 'Remote Git repository URL'
});
if (result.button.accept) {
url = result.value;
}
}
if (url) {
try {
await model.addRemote(url, name);
} catch (error) {
console.error(error);
showErrorMessage('Error when adding remote repository', error);
}
}
}
});
/** Add git clone command */
commands.addCommand(CommandIDs.gitClone, {
label: 'Clone a Repository',
caption: 'Clone a repository from a URL',
isEnabled: () => model.pathRepository === null,
execute: async () => {
await doGitClone(model, fileBrowser.model.path);
fileBrowser.model.refresh();
}
});
/** Add git open gitignore command */
commands.addCommand(CommandIDs.gitOpenGitignore, {
label: 'Open .gitignore',
caption: 'Open .gitignore',
isEnabled: () => model.pathRepository !== null,
execute: async () => {
await model.ensureGitignore();
const gitModel = model as GitExtension;
await gitModel.commands.execute('docmanager:reload');
await gitModel.commands.execute('docmanager:open', {
path: model.getRelativeFilePath('.gitignore')
});
}
});
/** Add git push command */
commands.addCommand(CommandIDs.gitPush, {
label: 'Push to Remote',
caption: 'Push code to remote repository',
isEnabled: () => model.pathRepository !== null,
execute: async () => {
await Private.showGitOperationDialog(model, Operation.Push).catch(
reason => {
console.error(
`Encountered an error when pushing changes. Error: ${reason}`
);
}
);
}
});
/** Add git pull command */
commands.addCommand(CommandIDs.gitPull, {
label: 'Pull from Remote',
caption: 'Pull latest code from remote repository',
isEnabled: () => model.pathRepository !== null,
execute: async () => {
await Private.showGitOperationDialog(model, Operation.Pull).catch(
reason => {
console.error(
`Encountered an error when pulling changes. Error: ${reason}`
);
}
);
}
});
}
/**
* Adds commands and menu items.
*
* @private
* @param app - Jupyter front end
* @param gitExtension - Git extension instance
* @param fileBrowser - file browser instance
* @param settings - extension settings
* @returns menu
*/
export function createGitMenu(commands: CommandRegistry): Menu {
const menu = new Menu({ commands });
menu.title.label = 'Git';
[
CommandIDs.gitInit,
CommandIDs.gitClone,
CommandIDs.gitPush,
CommandIDs.gitPull,
CommandIDs.gitAddRemote,
CommandIDs.gitTerminalCommand
].forEach(command => {
menu.addItem({ command });
});
menu.addItem({ type: 'separator' });
menu.addItem({ command: CommandIDs.gitToggleSimpleStaging });
menu.addItem({ command: CommandIDs.gitToggleDoubleClickDiff });
menu.addItem({ type: 'separator' });
menu.addItem({ command: CommandIDs.gitOpenGitignore });
menu.addItem({ type: 'separator' });
const tutorial = new Menu({ commands });
tutorial.title.label = ' Help ';
RESOURCES.map(args => {
tutorial.addItem({
args,
command: CommandIDs.gitOpenUrl
});
});
menu.addItem({ type: 'submenu', submenu: tutorial });
return menu;
}
/* eslint-disable no-inner-declarations */
namespace Private {
/**
* Displays an error dialog when a Git operation fails.
*
* @private
* @param model - Git extension model
* @param operation - Git operation name
* @returns Promise for displaying a dialog
*/
export async function showGitOperationDialog(
model: IGitExtension,
operation: Operation
): Promise<void> {
const title = `Git ${operation}`;
let result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
let retry = false;
while (!result.button.accept) {
const credentials = await showDialog({
title: 'Git credentials required',
body: new GitCredentialsForm(
'Enter credentials for remote repository',
retry ? 'Incorrect username or password.' : ''
),
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })]
});
if (!credentials.button.accept) {
break;
}
result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation, credentials.value),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
retry = true;
}
}
}
/* eslint-enable no-inner-declarations */