-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
148 lines (116 loc) · 5.15 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
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, browser: true */
/*global $, define, brackets */
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
CommandManager = brackets.getModule("command/CommandManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
FileUtils = brackets.getModule("file/FileUtils"),
Menus = brackets.getModule("command/Menus"),
NodeConnection = brackets.getModule("utils/NodeConnection"),
ProjectManager = brackets.getModule("project/ProjectManager");
var RUN_BUILD = "ant_build_cmd";
var SHOW_ANT_PANEL = "show_ant_panel_cmd";
var TARGET_REGEXP = new RegExp("(<target name=\"(([^\"])*))+", "img");
var nodeConnection;
var contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.PROJECT_MENU),
menuItems = [],
buildMenuItem = null;
// Helper function that chains a series of promise-returning
// functions together via their done callbacks.
function chain() {
var functions = Array.prototype.slice.call(arguments, 0);
if (functions.length > 0) {
var firstFunction = functions.shift();
var firstPromise = firstFunction.call();
firstPromise.done(function () {
chain.apply(null, functions);
});
}
}
AppInit.appReady(function () {
nodeConnection = new NodeConnection();
// Helper function that tries to connect to node
function connect() {
var connectionPromise = nodeConnection.connect(true);
connectionPromise.fail(function () {
console.error("[brackets-ant] failed to connect to node");
});
return connectionPromise;
}
// Helper function that loads our domain into the node server
function loadAntDomain() {
var path = ExtensionUtils.getModulePath(module, "node/AntDomain"),
loadPromise = nodeConnection.loadDomains([path], true);
loadPromise.fail(function () {
console.log("[brackets-ant] failed to load ant domain");
});
return loadPromise;
}
$(nodeConnection).on("ant.update", function (evt, data) {
console.log(data);
});
chain(connect, loadAntDomain);
});
function _isXML(fileEntry) {
return fileEntry && fileEntry.name.indexOf(".xml") >= 0;
}
function _isAntBuild(fileEntry) {
var checkBuildPromise = new $.Deferred();
FileUtils.readAsText(fileEntry).done(function (rawText) {
if (TARGET_REGEXP.test(rawText)) {
var match = null,
targets = [];
while ((match = TARGET_REGEXP.exec(rawText)) !== null) {
targets.push(match[2]);
}
checkBuildPromise.resolve(targets);
} else {
checkBuildPromise.reject();
}
}).fail(function (err) {
checkBuildPromise.reject(err);
});
return checkBuildPromise.promise();
}
//
function _runBuild(target) {
var entry = ProjectManager.getSelectedItem(),
path = entry.fullPath.substring(0, entry.fullPath.lastIndexOf("/")),
file = entry.name;
_isAntBuild(entry).done(function () {
nodeConnection.domains.ant.build(path, file, target)
.fail(function (err) {
console.error("[brackets-ant] failed to run ant.build", err);
})
.done(function (result) {
console.log("[brackets-ant] (%s)", result);
});
}).fail(function (err) {
console.log(err);
});
}
function _removeAllContextMenuItems() {
$.each(menuItems, function (index, target) {
contextMenu.removeMenuItem(target);
});
}
$(contextMenu).on("beforeContextMenuOpen", function (evt) {
var selectedEntry = ProjectManager.getSelectedItem();
_removeAllContextMenuItems();
if (_isXML(selectedEntry)) {
_isAntBuild(selectedEntry).done(function (targets) {
$.each(targets, function (index, target) {
if (!CommandManager.get(RUN_BUILD + target)) {
CommandManager.register("Build " + target + "...", RUN_BUILD + target, function () {
_runBuild(target);
});
}
contextMenu.addMenuItem(RUN_BUILD + target, "", Menus.LAST);
menuItems.push(RUN_BUILD + target);
});
});
}
});
contextMenu.addMenuItem(Menus.DIVIDER, "", Menus.LAST);
});