-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.js
114 lines (95 loc) · 4.49 KB
/
main.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
/**
(c) by Victor Hornets
Allow to run build programs (such as running Python/Ruby/Node/etc scripts) from Brackets and display results in panel. It is possible to create own build systems via 'Edit>Edit Builder' menu item and editing opened JSON-file (you need to restart Brackets).
**/
/*jslint plusplus: true, vars: true, nomen: true */
/*global define, brackets, console, setTimeout */
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
NodeConnection = brackets.getModule("utils/NodeConnection"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
DocumentManager = brackets.getModule("document/DocumentManager"),
KeyBindingManager = brackets.getModule('command/KeyBindingManager'),
FileUtils = brackets.getModule("file/FileUtils"),
PanelManager = brackets.getModule("view/PanelManager"),
Dialogs = brackets.getModule("widgets/Dialogs"),
nodeConnection = new NodeConnection(),
domainPath = ExtensionUtils.getModulePath(module) + "domain";
var curOpenDir,
curOpenFile,
curOpenLang,
cmd = '';
var builders = JSON.parse(require('text!builder.json')),
panel,
panelHTML = require('text!brackets-builder-panel.html'),
panelIsVisible = false;
function _processCmdOutput(data) {
data = JSON.stringify(data);
data = data
.replace(/\\r/g, '\r')
.replace(/\\n/g, '\n')
.replace(/\"/g, '')
.replace(/\\t/g, '\t');
return data;
}
function handle() {
CommandManager.execute("file.saveAll")
curOpenDir = DocumentManager.getCurrentDocument().file._parentPath;
curOpenFile = DocumentManager.getCurrentDocument().file._path;
curOpenLang = DocumentManager.getCurrentDocument().language._name;
nodeConnection.connect(true).fail(function (err) {
console.error("[[Brackets Builder]] Cannot connect to node: ", err);
}).then(function () {
console.log('Building ' + curOpenLang + ' in ' + curOpenFile + '...\n');
return nodeConnection.loadDomains([domainPath], true).fail(function (err) {
console.error("[[Brackets Builder]] Cannot register domain: ", err);
});
}).then(function () {
builders.forEach(function (el) {
if (el.name.toLowerCase() === curOpenLang.toLowerCase()) {
cmd = el.cmd;
}
});
cmd = cmd.replace(/\$FILE/g, "\"" + curOpenFile + "\"");
}).then(function () {
nodeConnection.domains["builder.execute"].exec(curOpenDir, cmd)
.fail(function (err) {
$('#builder-panel .builder-content').html(_processCmdOutput(err));
panel.show();
})
.then(function (data) {
if(data != "") {
$('#builder-panel .builder-content').html(_processCmdOutput(data));
panel.show();
}
});
}).done();
}
AppInit.appReady(function () {
panel = PanelManager.createBottomPanel("brackets-builder-panel", $(panelHTML), 100);
$('#builder-panel .close').on('click', function () {
panel.hide();
});
CommandManager.register('Handling Build', 'builder.build', handle);
KeyBindingManager.addBinding('builder.build', 'Ctrl-Alt-B');
// Add menu item to edit .json file
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
// Create menu item that opens the config .json-file
CommandManager.register("Edit Builder", 'builder.open-conf', function () {
Dialogs.showModalDialog('', 'Brackets Builder Extention', 'You must restart Brackets after changing this file.');
var src = FileUtils.getNativeModuleDirectoryPath(module) + "/builder.json";
DocumentManager.getDocumentForPath(src).done(
function (doc) {
DocumentManager.setCurrentDocument(doc);
}
);
});
menu.addMenuItem('builder.open-conf');
// Load panel css
ExtensionUtils.loadStyleSheet(module, "brackets-builder.css");
});
});