commands.ts 3.7 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

J
Joao Moreno 已提交
8 9
import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource, window } from 'vscode';
import { Model, Resource } from './model';
J
Joao Moreno 已提交
10
import { log } from './util';
J
Joao Moreno 已提交
11
import * as path from 'path';
J
Joao Moreno 已提交
12

J
Joao Moreno 已提交
13 14
type Command = (...args: any[]) => any;

J
Joao Moreno 已提交
15 16
async function refresh(model: Model): Promise<void> {
	return await model.update();
J
Joao Moreno 已提交
17
}
J
Joao Moreno 已提交
18

J
Joao Moreno 已提交
19
function openChange(model: Model, resource: Resource): void {
J
Joao Moreno 已提交
20
	log('open change', resource);
J
Joao Moreno 已提交
21 22
}

J
Joao Moreno 已提交
23
function openFile(model: Model, resource: Resource): void {
J
Joao Moreno 已提交
24
	log('open file', resource);
J
Joao Moreno 已提交
25 26
}

J
Joao Moreno 已提交
27 28
async function stage(model: Model, resource: Resource): Promise<void> {
	return await model.stage(resource);
J
Joao Moreno 已提交
29 30
}

J
Joao Moreno 已提交
31 32
async function stageAll(model: Model): Promise<void> {
	return await model.stage();
J
Joao Moreno 已提交
33 34
}

J
Joao Moreno 已提交
35 36
async function unstage(model: Model, resource: Resource): Promise<void> {
	return await model.unstage(resource);
J
Joao Moreno 已提交
37 38
}

J
Joao Moreno 已提交
39 40
async function unstageAll(model: Model): Promise<void> {
	return await model.unstage();
J
Joao Moreno 已提交
41 42
}

J
Joao Moreno 已提交
43 44 45 46 47 48 49 50 51 52 53 54
async function clean(model: Model, resource: Resource): Promise<void> {
	const basename = path.basename(resource.uri.fsPath);
	const message = `Are you sure you want to clean changes in ${basename}?`;
	const yes = 'Yes';
	const no = 'No, keep them';
	const pick = await window.showQuickPick([no, yes], { placeHolder: message });

	if (pick !== yes) {
		return;
	}

	return await model.clean(resource);
J
Joao Moreno 已提交
55 56
}

J
Joao Moreno 已提交
57 58 59 60 61 62 63 64 65 66 67
async function cleanAll(model: Model): Promise<void> {
	const message = `Are you sure you want to clean all changes?`;
	const yes = 'Yes';
	const no = 'No, keep them';
	const pick = await window.showQuickPick([no, yes], { placeHolder: message });

	if (pick !== yes) {
		return;
	}

	return await model.clean(...model.workingTreeGroup.resources);
J
Joao Moreno 已提交
68
}
J
Joao Moreno 已提交
69

J
Joao Moreno 已提交
70 71 72 73
function checkout(model: Model): void {
	console.log('checkout');
}

J
Joao Moreno 已提交
74
function resolveURI<R>(command: (t: SCMResource | SCMResourceGroup | undefined) => R): (uri: Uri) => R | undefined {
J
Joao Moreno 已提交
75 76 77 78 79 80 81 82 83 84
	return uri => {
		if (uri.authority !== 'git') {
			return;
		}

		const result = scm.getResourceFromURI(uri);

		if (!result) {
			return;
		}
J
Joao Moreno 已提交
85

J
Joao Moreno 已提交
86 87
		return command(result);
	};
J
Joao Moreno 已提交
88 89
}

J
Joao Moreno 已提交
90
// TODO: do more with these errors
J
Joao Moreno 已提交
91 92
function catchErrors<T, R>(command: (...args: any[]) => Promise<R>): (...args: any[]) => void {
	return (...args) => command(...args).catch(err => console.error(err));
J
Joao Moreno 已提交
93 94
}

J
Joao Moreno 已提交
95 96
function compose(command: Command, ...args: Function[]): Command {
	return args.reduce((r, fn) => fn(r), command) as Command;
J
Joao Moreno 已提交
97 98
}

99
export function registerCommands(model: Model): Disposable {
J
Joao Moreno 已提交
100 101
	const bindModel = command => (...args: any[]) => command(model, ...args);

J
Joao Moreno 已提交
102
	const disposables = [
J
Joao Moreno 已提交
103
		commands.registerCommand('git.refresh', compose(refresh, catchErrors, bindModel)),
J
Joao Moreno 已提交
104 105 106 107 108 109
		commands.registerCommand('git.openChange', compose(openChange, bindModel, resolveURI)),
		commands.registerCommand('git.openFile', compose(openFile, bindModel, resolveURI)),
		commands.registerCommand('git.stage', compose(stage, catchErrors, bindModel, resolveURI)),
		commands.registerCommand('git.stageAll', compose(stageAll, catchErrors, bindModel)),
		commands.registerCommand('git.unstage', compose(unstage, catchErrors, bindModel, resolveURI)),
		commands.registerCommand('git.unstageAll', compose(unstageAll, catchErrors, bindModel)),
J
Joao Moreno 已提交
110 111
		commands.registerCommand('git.clean', compose(clean, catchErrors, bindModel, resolveURI)),
		commands.registerCommand('git.cleanAll', compose(cleanAll, catchErrors, bindModel)),
J
Joao Moreno 已提交
112
		commands.registerCommand('git.checkout', compose(checkout, bindModel)),
J
Joao Moreno 已提交
113 114 115
	];

	return Disposable.from(...disposables);
J
Joao Moreno 已提交
116
}