-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
179 lines (141 loc) · 5.13 KB
/
extension.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
'use strict';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import * as languageClient from './reproto_language_client';
import * as install from './install';
import { Reproto } from './reproto';
function detectCandidates(config: vscode.WorkspaceConfiguration): [string[], string[]] {
const displays: string[] = [];
const candidates: string[] = [];
const platform = install.getPlatform();
const exe = install.getExe(platform);
const executable = config.get<string>("executable");
if (executable) {
displays.push("reproto.executable (user configuration)");
candidates.push(executable);
} else {
displays.push("reproto.executable (user configuration) is not defined");
}
if (process.env.REPROTO_HOME) {
displays.push("$REPROTO_HOME/reproto");
candidates.push(path.join(process.env.REPROTO_HOME), exe);
} else {
displays.push("$REPROTO_HOME is not defined");
}
if (process.env.HOME) {
displays.push("$HOME/.local/bin/reproto");
candidates.push(path.join(process.env.HOME, ".local", "bin", exe));
} else {
displays.push("$HOME is not defined");
}
if (process.env.USERPROFILE) {
displays.push("$USERPROFILE/.local/bin/reproto");
candidates.push(path.join(process.env.USERPROFILE, ".local", "bin", exe));
} else {
displays.push("$USERPROFILE is not defined");
}
if (process.env.PATH) {
displays.push("$PATH");
process.env.PATH.split(path.delimiter).forEach((p: string) => {
candidates.push(path.join(p, exe));
});
} else {
displays.push("$PATH is not defined");
}
return [displays, candidates];
}
function detectReproto(candidates: string[]): Reproto | undefined {
for (var i = 0; i < candidates.length; i++) {
var c = candidates[i];
if (c && fs.existsSync(c)) {
return new Reproto(c, vscode.workspace.rootPath);
}
}
return undefined;
}
/**
* Try to determine the version of reproto.
*/
async function detectVersion(
out: vscode.OutputChannel,
reproto: Reproto
): Promise<[number, number, number] | null> {
try {
var output = await reproto.version();
const p = output.split(/[ \t]+/);
if (p.length >= 2 && p[0] === "reproto") {
const c = p[1].split(/[\.-]/);
if (c.length >= 3) {
const major = parseInt(c[0]);
const minor = parseInt(c[1]);
const patch = parseInt(c[2]);
out.appendLine(`detected \`${reproto}\` version: ${major}.${minor}.${patch}`);
return [major, minor, patch];
}
}
} catch (e) {
out.appendLine(`failed to detect version: ${reproto}:`);
out.appendLine(e.toString());
out.show(true);
}
return null;
}
async function internalActivate(
context: vscode.ExtensionContext,
out: vscode.OutputChannel
): Promise<void> {
const config = vscode.workspace.getConfiguration("reproto");
const [displays, candidates] = detectCandidates(config);
const reproto = detectReproto(candidates);
if (reproto) {
const version = await detectVersion(out, reproto);
if (version) {
const [major, minor, patch] = version;
out.appendLine(`using reproto from \`${reproto}\``);
const item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
item.text = `reproto ${major}.${minor}.${patch}`;
item.show();
context.subscriptions.push(item);
languageClient.activate(context, config, reproto, out);
context.subscriptions.push(vscode.commands.registerCommand("reproto.init", () => {
out.appendLine("Command: Initializing new project");
return reproto.init(out).then(() => {
vscode.window.showInformationMessage("Project Initialized");
}).catch(e => {
vscode.window.showErrorMessage(`Failed to initialize project: ${e}`);
});
}));
return;
}
}
out.appendLine("usable `reproto` command could not be found!");
out.appendLine("looked in the following places:");
displays.forEach((d, i) => {
out.appendLine(`#${i}: ${d}`);
});
out.show(true);
// attempt to install
const action = await vscode.window.showErrorMessage(
"Usable `reproto` command could not be found!",
"Do nothing",
"Install reproto"
);
if (action !== "Install reproto") {
return;
}
install.install(out).then(() => {
out.appendLine("reactivating extension");
internalActivate(context, out);
}).catch(e => {
vscode.window.showErrorMessage(e.toString());
});
}
export function activate(context: vscode.ExtensionContext) {
const out = vscode.window.createOutputChannel("reproto extension");
context.subscriptions.push(out);
internalActivate(context, out);
}
// this method is called when your extension is deactivated
export function deactivate() {
}