-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
82 lines (75 loc) · 2.57 KB
/
extension.js
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const cgcf = require('cgcf');
const path = require('path');
const fs = require('fs');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
const workspaceRoot = vscode.workspace.rootPath;
// const workspaceRoot = vscode.workspace.workspaceFolders[0].uri.path;
const tmp = '.git/cc.changes';
const target_path = path.join(workspaceRoot,tmp);
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let copyChanges = vscode.commands.registerCommand('cc.copyChanges', function(){
cgcf.clear(target_path);
let changes = cgcf.getGitRepoChanges(workspaceRoot);
if(changes.length === 0){
vscode.window.showWarningMessage('无文件改动!');
return ;
}
let source_file = "", target_file = "";
let count = 0;
for (let item of changes) {
source_file = path.resolve(workspaceRoot, item);
target_file= path.join(target_path, item);
count++;
cgcf.copy(source_file, target_file);
}
if(cgcf.openInExplorer(target_path))
vscode.window.showInformationMessage(`拷贝成功!共${count}个项目`);
else
vscode.window.showInformationMessage(`拷贝出错!`);
})
let copySelectChanges= vscode.commands.registerCommand('cc.copySelectedChanges', function(){
cgcf.clear(target_path);
let changes = arguments;
if(changes.length === 0){
vscode.window.showWarningMessage('无文件改动!');
return ;
}
let source_file = "", target_file = "";
let count = 0;
for (let item of changes) {
if(!item) continue;
source_file = item['resourceUri']['_fsPath'];
// 文件不存在时,忽略
if(!fs.existsSync(source_file)) continue;
target_file = source_file.replace(workspaceRoot, target_path);
count++;
cgcf.copy(source_file, target_file);
}
if(count == 0){
vscode.window.showWarningMessage('无新增或修改的文件!');
return ;
}
if(cgcf.openInExplorer(target_path))
vscode.window.showInformationMessage(`拷贝成功!共${count}个项目`);
else
vscode.window.showInformationMessage(`拷贝出错!`);
})
context.subscriptions.push(copyChanges);
context.subscriptions.push(copySelectChanges);
}
// this method is called when your extension is deactivated
function deactivate() {
if(fs.existsSync(target_path))
cgcf.clear(target_path);
}
module.exports = {
activate,
deactivate
}