forked from snailuncle/Auto.js-VSCode-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
134 lines (114 loc) · 3.5 KB
/
extension.ts
File metadata and controls
134 lines (114 loc) · 3.5 KB
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
'use strict';
import * as vscode from 'vscode';
import {AutoJs, Device} from './autojs';
var server = new AutoJs(1209);
var recentDevice = null;
server
.on('connect', ()=>{
vscode.window.showInformationMessage('Auto.js server running');
})
.on('new_device', (device: Device)=>{
var messageShown = false;
var showMessage = () =>{
if(messageShown)
return;
vscode.window.showInformationMessage('New device attached: ' + device);
messageShown = true;
};
setTimeout(showMessage, 1000);
device.on('data:device_name', showMessage);
});
class Extension {
startServer(){
server.listen();
}
stopServer(){
server.disconnect();
vscode.window.showInformationMessage('Auto.js server stopped');
}
run(){
this.runOn(server);
}
stop(){
server.send({
'type': 'command',
'view_id': vscode.window.activeTextEditor.document.fileName,
'command': 'stop',
})
}
stopAll(){
server.send({
'type': 'command',
'command': 'stopAll'
})
}
rerun(){
let editor = vscode.window.activeTextEditor;
server.send({
'type': 'command',
'command': 'rerun',
'view_id': editor.document.fileName,
'name': editor.document.fileName,
'script': editor.document.getText()
});
}
runOnDevice(){
this.selectDevice(device => this.runOn(device));
}
selectDevice(callback){
let devices = server.devices;
if(recentDevice){
let i = devices.indexOf(recentDevice);
if(i > 0){
devices = devices.slice(0);
devices[i] = devices[0];
devices[0] = recentDevice;
}
}
let names = devices.map(device => device.toString());
vscode.window.showQuickPick(names)
.then(select => {
let device = devices[names.indexOf(select)];
recentDevice = device;
callback(device);
});
}
runOn(target: AutoJs | Device){
let editor = vscode.window.activeTextEditor;
target.send({
'type': 'command',
'command': 'run',
'view_id': editor.document.fileName,
'name': editor.document.fileName,
'script': editor.document.getText()
})
}
save(){
this.saveTo(server);
}
saveToDevice(){
this.selectDevice(device => this.saveTo(device));
}
saveTo(target: AutoJs | Device){
let editor = vscode.window.activeTextEditor;
target.send({
'command': 'save',
'type': 'command',
'view_id': editor.document.fileName,
'name': editor.document.fileName,
'script': editor.document.getText()
})
}
};
const commands = ['startServer', 'stopServer', 'run', 'runOnDevice', 'stop', 'stopAll', 'rerun', 'save', 'saveToDevice'];
let extension = new Extension();
export function activate(context: vscode.ExtensionContext) {
console.log('extension "auto-js-vscodeext" is now active.');
commands.forEach((command)=>{
let action: Function = extension[command];
context.subscriptions.push(vscode.commands.registerCommand('extension.' + command, action.bind(extension)));
})
}
export function deactivate() {
server.disconnect();
}