forked from kriscross07/atom-gpp-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
404 lines (355 loc) · 10.4 KB
/
index.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* global atom */
"use strict";
const child_process = require("child_process");
const fs = require("fs");
const path = require("path");
const os = require("os");
// const {CompositeDisposable} = require("atom");
const CompositeDisposable = require("atom").CompositeDisposable;
module.exports = {
activate() {
debug("activate()");
debug("platform", process.platform);
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.
commands.
add("atom-text-editor", {
"gpp-compiler:compile": () => {
debug("gpp-compiler:compile");
compileFile(getFileType());
},
"gpp-compiler:gdb": () => {
debug("gpp-compiler:gdb");
compileFile(getFileType(), true);
}
}));
this.subscriptions.add(atom.
commands.
add(".tree-view .file", {
"gpp-compiler:tree-compile": treeCompile,
"gpp-compiler:tree-gdb": (e) => {
debug("gpp-compiler:tree-gdb");
treeCompile(e, true);
}
}));
},
config: {
addCompilingErr: {
default: true,
description: "Add a file named `compiling_error.txt` if compiling goes wrong",
title: "Add `compiling_error.txt`",
type: "boolean"
},
debug: {
default: false,
description: "Logs function calls in console.",
title: "Debug Mode",
type: "boolean"
},
cCompilerOptions: {
default: "",
description: "C compiler command line options",
title: "C Compiler Options",
type: "string"
},
cppCompilerOptions: {
default: "",
description: "C++ compiler command line options",
title: "C++ Compiler Options",
type: "string"
},
runAfterCompile: {
default: true,
description: "Run program after compiling is done",
title: "Run After Compile",
type: "boolean"
},
showWarnings: {
default: true,
description: "Show compile warnings.",
title: "Show Warnings",
type: "boolean"
},
cCompiler: {
default: "gcc",
title: "C Compiler",
type: "string"
},
cppCompiler: {
default: "g++",
title: "C++ Compiler",
type: "string"
}
},
deactivate() {
debug("deactivate()");
this.subscriptions.dispose();
},
subscriptions: null
};
// if the user is running linux, add the option to change default terminal
if (process.platform === "linux") {
module.
exports.
config.
linuxTerminal = {
default: "XTerm",
enum: [
"XTerm",
"GNOME Terminal",
"Konsole",
"xfce4-terminal",
"pantheon-terminal"
],
title: "Linux terminal",
type: "string"
};
}
function debug(...args) {
if (atom.config.get("gpp-compiler.debug")) {
console.info(...args);
}
}
function getFileType(ext) {
debug("getFileType()", ext);
if (ext) {
for (const grammar of atom.grammars.getGrammars()) {
for (const fileType of grammar.fileTypes) {
if (ext === `.${fileType}`) {
return grammar.name;
}
}
}
} else {
return atom.
workspace.
getActiveTextEditor().
getGrammar().
name;
}
}
function getCommand(fileType) {
debug("getCommand()", fileType);
switch (fileType) {
case "C":
return atom.config.get("gpp-compiler.cCompiler");
case "C++":
return atom.config.get("gpp-compiler.cppCompiler");
}
}
function getFilePath() {
debug("getFilePath()");
return atom.
workspace.
getActiveTextEditor().
buffer.
file;
}
function getArgs(files, output, fileType, extraArgs) {
debug("getArgs()", files, output, fileType, extraArgs);
// atom throws a SyntaxError if you use ES6's default parameters
if (!extraArgs) {
extraArgs = [];
}
// array of arguments to pass to the compiler
const args = [
...extraArgs,
...files,
"-o",
output,
...atom.
config.
// string of all user-defined options
get(`gpp-compiler.c${fileType === "C++" ? "pp" : ""}CompilerOptions`).
// turn that string into an array separated by spaces
split(" ").
// remove falsy elements
filter(Boolean)
];
debug("compiler args", args);
return args;
}
function getTmp(base) {
debug("getTmp()", base);
return path.join(os.tmpdir(), base);
}
function compileFile(fileType, gdb) {
debug("compileFile()", fileType, gdb);
const file = getFilePath();
if (file) {
const filePath = file.path;
const info = path.parse(filePath);
compile(getCommand(fileType), info, getArgs([
filePath
], getTmp(info.name), fileType, gdb ? [
"-g"
] : null), gdb);
} else {
atom.
notifications.
addError("<strong>File not found.</strong><br/>Save before compiling.");
}
}
function treeCompile(e, gdb) {
debug("treeCompile()", gdb);
// array of all selected tree view files
const names = Array.from(document.querySelectorAll(".tree-view .file.selected > .name"));
// array of files to compile
const files = names.
// remove elements that are not of instance HTMLElement
filter((name) => name instanceof HTMLElement).
// replace all elements with their attribute `data-path`
map((element) => element.getAttribute("data-path"));
// file right clicked on
let element = e.target;
if (element.classList.contains("file")) {
element = element.firstChild;
}
const info = path.parse(element.getAttribute("data-path"));
const fileType = getFileType(info.ext);
// call compile, telling it to compile either C++ or C
compile(getCommand(fileType), info, getArgs(files, getTmp(info.name), fileType, gdb ? [
"-g"
] : null), gdb);
}
// spawn the compiler to compile files and optionally run the compiled files
function compile(command, info, args, gdb) {
debug("compile()", command, info, args, gdb);
debug("config", atom.config.get("gpp-compiler"));
const editor = atom.
workspace.
getActiveTextEditor();
// if the user has a text editor open, save it
if (editor) {
debug("saving...");
editor.save();
} else {
debug("no editor");
}
// spawn the compiler with the working directory of info.dir
const child = child_process.spawn(command, args, {
cwd: info.dir
});
// if the compile exits with a non-zero status, alert the user the error
let stderr = "";
child.
stderr.
on("data", (data) => {
stderr += data;
debug("stderr", data.toString());
});
// callback when the child's stdio streams close
child.on("close", (code) => {
debug("exit code", code);
// if the exit code is a non-zero status, alert the user stderr
if (code) {
atom.
notifications.
addError(stderr.replace(/\n/g, "<br/>"));
if (atom.config.get("gpp-compiler.addCompilingErr")) {
fs.writeFile(path.join(info.dir, "compiling_error.txt"), stderr);
}
} else {
// compilation was successful, but there still may be warnings
if (stderr && atom.config.get("gpp-compiler.showWarnings")) {
atom.notifications.addWarning(stderr.replace(/\n/g, "<br/>"));
}
// if the user wants the program to run after compilation, run it in their
// favorite terminal
if (atom.config.get("gpp-compiler.runAfterCompile")) {
// options to tell child_process.spawn() to run in the directory of the
// program
const options = {
cwd: info.dir
};
if (process.platform === "linux") {
// if the platform is linux, spawn the program in the user set
// terminal
const terminal = atom.
config.
get("gpp-compiler.linuxTerminal");
const file = getTmp(info.name);
let terminalCommand = null;
let args = null;
switch (terminal) {
case "GNOME Terminal":
terminalCommand = "gnome-terminal";
args = [
"--command"
];
break;
case "Konsole":
terminalCommand = "konsole";
args = [
...(gdb ? [] : [
"--hold"
]),
"-e"
];
break;
case "xfce4-terminal":
terminalCommand = "xfce4-terminal";
args = [
...(gdb ? [] : [
"--hold"
]),
"--command"
];
break;
case "pantheon-terminal":
terminalCommand = "pantheon-terminal";
args = [
"-e"
];
break;
default:
terminalCommand = "xterm";
args = [
...(gdb ? [] : [
"-hold"
]),
"-e"
];
}
debug("command", terminalCommand, args, gdb, file, options);
child_process.spawn(terminalCommand, [
...args,
// is there a better one-liner than this?
...(gdb ? [
"gdb"
] : []),
file
], options);
} else if (process.platform === "win32") {
// if the platform is Windows, run start (which is a shell builtin, so
// we can't use child_process.spawn), which spawns a new instance of
// cmd to run the program
const file = getTmp(info.name);
const command = `start "${info.name}" cmd /C "${gdb ? "gdb" : ""} ${file} ${gdb ? "" : "& echo. & pause"}`;
debug("command", command);
child_process.exec(command, options);
} else if (process.platform === "darwin") {
// if the platform is mac, spawn open, which does the same thing as
// Windows' start, but is not a builtin, so we can child_process.spawn
// it
child_process.spawn("open", [
getTmp(info.name)
], options);
}
} else {
// if the user doesn't want the program to run after compilation, give
// them an alert telling them it was successful
atom.
notifications.
addSuccess("Compilation Successful");
}
// since the compilation was successful, remove `compiling_error.txt` if
// it exists
fs.stat(path.join(info.dir, "compiling_error.txt"), (err) => {
if (!err) {
fs.unlink(path.join(info.dir, "compiling_error.txt"));
}
});
}
});
}