-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensionServer.ts
153 lines (135 loc) · 5.36 KB
/
extensionServer.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
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as crypto from 'crypto';
import * as vscode from 'vscode';
import * as extProtocol from './extensionProtocol';
import {Services} from '../services/extensionHostServices';
import {QuickPickItem} from "vscode";
let ipc = require('node-ipc');
export class ExtensionServer {
private _isRunning: boolean;
public static getTempFilePathForDirectory(directoryPath: string) {
let fileName: string = 'vsc-ns-ext-' + crypto.createHash('md5').update(directoryPath).digest("hex") + '.sock';
return path.join(os.tmpdir(), fileName);
}
constructor() {
this._isRunning = false;
}
public getPipeHandlePath(): string {
return vscode.workspace.rootPath ?
ExtensionServer.getTempFilePathForDirectory(vscode.workspace.rootPath) :
null;
}
public start() {
if (!this._isRunning) {
let pipeHandlePath = this.getPipeHandlePath();
if (pipeHandlePath) {
ipc.serve(
pipeHandlePath,
() => {
ipc.server.on('extension-protocol-message', (data: extProtocol.Request, socket) => {
return (<Promise<Object>>this[data.method].call(this, data.args)).then(result => {
let response: extProtocol.Response = { requestId: data.id, result: result };
return ipc.server.emit(socket, 'extension-protocol-message', response);
});
});
});
ipc.server.start();
this._isRunning = true;
}
}
return this._isRunning;
}
public stop() {
if (this._isRunning) {
ipc.server.stop();
this._isRunning = false;
}
}
public isRunning() {
return this._isRunning;
}
public getInitSettings(): Promise<extProtocol.InitSettingsResult> {
let tnsPath = Services.workspaceConfigService().tnsPath;
return Promise.resolve({ tnsPath: tnsPath });
}
public analyticsLaunchDebugger(args: extProtocol.AnalyticsLaunchDebuggerArgs): Promise<any> {
return Services.analyticsService().launchDebugger(args.request, args.platform);
}
public runRunCommand(args: extProtocol.AnalyticsRunRunCommandArgs): Promise<any> {
return Services.analyticsService().runRunCommand(args.platform);
}
public selectTeam(): Promise<{ id: string, name: string }> {
return new Promise((resolve, reject) => {
const workspaceTeamId = vscode.workspace.getConfiguration().get<string>("nativescript.iosTeamId");
if (workspaceTeamId) {
resolve({
id: workspaceTeamId,
name: undefined // irrelevant
});
return;
}
let developmentTeams = this.getDevelopmentTeams();
if (developmentTeams.length > 1) {
let quickPickItems: Array<QuickPickItem> = developmentTeams.map((team) => {
return {
label: team.name,
description: team.id
};
});
vscode.window.showQuickPick(
quickPickItems, {
placeHolder: "Select your development team"
})
.then((val: QuickPickItem) => {
vscode.workspace.getConfiguration().update("nativescript.iosTeamId", val.description);
resolve({
id: val.description,
name: val.label
})
});
} else {
resolve();
}
});
}
private getDevelopmentTeams(): Array<{ id: string, name: string }> {
try {
let dir = path.join(process.env.HOME, "Library/MobileDevice/Provisioning Profiles/");
let files = fs.readdirSync(dir);
let teamIds: any = {};
for (let file of files) {
let filePath = path.join(dir, file);
let data = fs.readFileSync(filePath, { encoding: "utf8" });
let teamId = this.getProvisioningProfileValue("TeamIdentifier", data);
let teamName = this.getProvisioningProfileValue("TeamName", data);
if (teamId) {
teamIds[teamId] = teamName;
}
}
let teamIdsArray = new Array<{ id: string, name: string }>();
for (let teamId in teamIds) {
teamIdsArray.push({ id: teamId, name: teamIds[teamId] });
}
return teamIdsArray;
} catch (e) {
// no matter what happens, don't break
return new Array<{ id: string, name: string }>();
}
}
private getProvisioningProfileValue(name: string, text: string): string {
let findStr = "<key>" + name + "</key>";
let index = text.indexOf(findStr);
if (index > 0) {
index = text.indexOf("<string>", index + findStr.length);
if (index > 0) {
index += "<string>".length;
let endIndex = text.indexOf("</string>", index);
let result = text.substring(index, endIndex);
return result;
}
}
return null;
}
}