-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
235 lines (194 loc) · 8.99 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
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
/*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"),
Commands = brackets.getModule("command/Commands"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
FileIndexManager = brackets.getModule("project/FileIndexManager"),
FileUtils = brackets.getModule("file/FileUtils"),
Menus = brackets.getModule("command/Menus"),
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem,
NodeConnection = brackets.getModule("utils/NodeConnection"),
PanelManager = brackets.getModule("view/PanelManager"),
ProjectManager = brackets.getModule("project/ProjectManager");
var RUN_BUILD = "grunt_build_cmd";
var SHOW_ANT_PANEL = "show_grunt_panel_cmd";
var nodeConnection;
var PROJECT_MENU = "project-menu",
PROJECT_MENU_NAME = "Project",
GRUNT_DEFAULT = "project-grunt-default";
var contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.PROJECT_MENU),
projectMenu,
gruntFile,
defaultMenuItem,
gruntDefaultCommand,
gruntMenuItems = [],
buildMenuItem = null,
$grunt;
var gruntParser = require("GruntParser");
// 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);
});
}
}
function _loadGruntTasks() {
function loadTargets(targets) {
$.each(targets, function (index, target) {
var id = RUN_BUILD + target.replace(":","-");
if (!CommandManager.get(id)) {
CommandManager.register("Grunt " + target, id, function () {
_runBuild(target);
});
}
projectMenu.addMenuItem(id, "", Menus.LAST);
gruntMenuItems.push(id);
});
}
function getGruntFile(directory) {
directory.getFile("Gruntfile.js", {create: false, exclusive: false},
function (file) {
gruntFile = file;
gruntDefaultCommand.setEnabled(true);
_loadGruntfile(file).done(loadTargets);
},
function (error) {
gruntFile = undefined;
gruntDefaultCommand.setEnabled(false);
});
}
_removeAllProjectMenuItems();
gruntMenuItems = [];
var root = ProjectManager.getProjectRoot();
NativeFileSystem.resolveNativeFileSystemPath(root.fullPath, getGruntFile);
}
function _initializeTopMenu() {
projectMenu = Menus.getMenu(PROJECT_MENU);
if (projectMenu) {
projectMenu.addMenuDivider();
} else {
projectMenu = Menus.addMenu(PROJECT_MENU_NAME, PROJECT_MENU, Menus.AFTER, Menus.AppMenuBar.NAVIGATE_MENU);
}
gruntDefaultCommand = CommandManager.register("Grunt default", GRUNT_DEFAULT, function () {
_runBuild();
});
defaultMenuItem = projectMenu.addMenuItem(GRUNT_DEFAULT, "F6");
_loadGruntTasks();
$(ProjectManager).on("projectOpen", function() {
_loadGruntTasks();
});
$(ProjectManager).on("projectFilesChanged", function() {
_loadGruntTasks();
});
}
AppInit.appReady(function () {
_initializeTopMenu();
nodeConnection = new NodeConnection();
// Helper function that tries to connect to node
function connect() {
var connectionPromise = nodeConnection.connect(true);
connectionPromise.fail(function () {
console.error("[brackets-grunt] failed to connect to node");
});
return connectionPromise;
}
// Helper function that loads our domain into the node server
function loadGruntDomain() {
var path = ExtensionUtils.getModulePath(module, "node/GruntDomain"),
loadPromise = nodeConnection.loadDomains([path], true);
loadPromise.fail(function () {
console.log("[brackets-grunt] failed to load Grunt domain");
});
return loadPromise;
}
$(nodeConnection).on("grunt.update", function (evt, data) {
console.log(data);
$("#grunt .table-container").append($("<p>" + data + "</p>"));
});
chain(connect, loadGruntDomain);
});
AppInit.htmlReady(function () {
//add the HTML UI
var content = ' <div id="grunt" class="bottom-panel">'
+ ' <div class="toolbar simple-toolbar-layout">'
+ ' <div class="title">Grunt</div><a href="#" class="close">×</a>'
+ ' </div>'
+ ' <div id="grunt-panel" class="table-container" style="padding: 4px; overflow: scroll"/>'
+ '</div>';
$grunt = PanelManager.createBottomPanel("grunt.display.grunt",$(content),200);
$('#grunt .close').click(function () {
$grunt.hide();
});
});
function _isGruntfile(fileEntry) {
return fileEntry && fileEntry.name.toLowerCase() === "gruntfile.js";
}
function _loadGruntfile(fileEntry) {
var checkBuildPromise = new $.Deferred();
FileUtils.readAsText(fileEntry).done(function (rawText) {
var targets = gruntParser.parse(rawText);
if (targets && targets.length) {
checkBuildPromise.resolve(targets);
} else {
checkBuildPromise.reject();
}
}).fail(function (err) {
checkBuildPromise.reject(err);
});
return checkBuildPromise.promise();
}
//
function _runBuild(target) {
var entry = gruntFile,
path = entry.fullPath.substring(0, entry.fullPath.lastIndexOf("/")),
file = entry.name;
CommandManager.execute(Commands.FILE_SAVE_ALL);
$grunt.show();
var $output = $("#grunt .table-container");
$output.empty().append($("<p>Running...</p>"));
_loadGruntfile(entry).done(function () {
nodeConnection.domains.grunt.build(path, target)
.fail(function (err) {
console.error("[brackets-grunt] failed to run grunt", err);
$output.append($("<p>Failed to run grunt; error code " + err.code + "</p>"));
})
.done(function (result) {
result = result.replace(/\r?\n/mg, "<br />", "mg");
$output.empty().append($("<p>" + result + "</p>"));
$output.scrollTop($output[0].scrollHeight);
});
}).fail(function (err) {
console.log(err);
});
}
function _removeAllContextMenuItems() {
$.each(gruntMenuItems, function (index, target) {
contextMenu.removeMenuItem(target);
});
}
function _removeAllProjectMenuItems() {
$.each(gruntMenuItems, function (index, target) {
projectMenu.removeMenuItem(target);
});
}
$(contextMenu).on("beforeContextMenuOpen", function (evt) {
var selectedEntry = ProjectManager.getSelectedItem();
console.log(selectedEntry);
_removeAllContextMenuItems();
if (_isGruntfile(selectedEntry)) {
contextMenu.addMenuItem(gruntDefaultCommand, "", Menus.LAST);
$.each(gruntMenuItems, function (index, target) {
contextMenu.addMenuItem(target, "", Menus.LAST);
});
contextMenu.addMenuItem(Menus.DIVIDER, "", Menus.LAST);
}
});
});