This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.ts
172 lines (144 loc) · 4.19 KB
/
main.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
import * as cp from "child_process";
import * as fs from "fs";
import * as path from "path";
import { AutoLanguageClient, DownloadFile } from "atom-languageclient";
import { StatusBar, Tile } from "atom/status-bar";
import decompress = require("decompress");
const serverDownload = {
darwin: {
url:
"https://github.com/crystal-lang-tools/scry/releases/download/v0.7.1/scry_macOS.tar.gz"
},
linux: {
url:
"https://github.com/crystal-lang-tools/scry/releases/download/v0.7.1/scry_linux.tar.gz"
}
};
const serverBinary = "scry";
class CrystalLanguageServer extends AutoLanguageClient {
private statusElement: HTMLSpanElement;
constructor() {
super();
this.statusElement = document.createElement("span");
this.statusElement.className = "inline-block";
}
protected getGrammarScopes() {
return ["source.crystal"];
}
protected getLanguageName() {
return "Crystal";
}
protected getServerName() {
return "scry";
}
protected consumeStatusBar(statusBar: StatusBar) {
statusBar.addRightTile({
item: this.statusElement,
priority: 1000
});
}
protected async startServerProcess(projectPath: string) {
if (!this.isPlatformSupported()) {
throw new Error(
`${this.getServerName()} not supported on ${process.platform}`
);
}
const serverHome = path.join(__dirname, "..", "server");
const binary = await this.getOrInstallLauncher(serverHome);
this.logger.debug(`starting "${binary} at ${projectPath}"`);
return cp.spawn(binary, [], { cwd: projectPath });
}
private async getOrInstallLauncher(serverHome: string) {
const fullBinaryPath = path.join(
serverHome,
"bin",
process.platform,
serverBinary
);
if (await this.fileExists(fullBinaryPath)) {
return fullBinaryPath;
}
await this.installServer(serverHome);
return fullBinaryPath;
}
private async installServer(serverHome: string) {
const localFileName = path.join(serverHome, "download.tar.gz");
const url = this.getScryDownload();
this.logger.log(`Downloading ${url} to ${localFileName}`);
const serverHomeExists = await this.fileExists(serverHome);
if (!serverHomeExists) {
new Promise((resolve, reject) => {
fs.mkdir(serverHome, err => {
if (err) {
return reject(err);
}
return resolve();
});
});
}
await DownloadFile(url, localFileName, (bytesDone, percent) =>
this.updateStatusBar(`downloading ${percent}%`)
);
this.updateStatusBar("unpacking");
await decompress(localFileName, serverHome);
const serverBinExists = await this.fileExists(
path.join(serverHome, "bin", process.platform, serverBinary)
);
if (!serverBinExists) {
throw Error(
`Failed to install the ${this.getServerName()} language server`
);
}
this.updateStatusBar("installed");
await new Promise((resolve, reject) => {
fs.unlink(localFileName, err => {
if (err) {
return reject(err);
}
return resolve();
});
});
}
private updateStatusBar(text: string) {
this.statusElement.textContent = `${this.name} ${text}`;
}
private fileExists(path: string): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.access(path, fs.constants.R_OK, error => {
resolve(!error || error.code !== "ENOENT");
});
});
}
private deleteFileIfExists(path: string) {
return new Promise((resolve, reject) => {
fs.unlink(path, error => {
if (error && error.code !== "ENOENT") {
reject(error);
} else {
resolve();
}
});
});
}
private isPlatformSupported(): boolean {
switch (process.platform) {
case "darwin":
case "linux":
return true;
default:
return false;
}
}
private getScryDownload(): string {
switch (process.platform) {
case "darwin":
case "linux":
return serverDownload[process.platform].url;
default:
throw new Error(
`${this.getServerName()} not supported on ${process.platform}`
);
}
}
}
module.exports = new CrystalLanguageServer();