Repository navigation
Expand file tree
/
Copy pathExtensionUtils.js
More file actions
294 lines (258 loc) · 10.7 KB
/
Copy pathExtensionUtils.js
File metadata and controls
294 lines (258 loc) · 10.7 KB
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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/*global less, path */
// jshint ignore: start
/**
* ExtensionUtils defines utility methods for implementing extensions.
*/
define(function (require, exports, module) {
const FileSystem = require("filesystem/FileSystem"),
PathUtils = require("thirdparty/path-utils/path-utils"),
Package = require("extensibility/Package");
/**
* Appends a "style" tag to the document's head.
*
* @param {!string} css CSS code to use as the tag's content
* @return {!HTMLStyleElement} The generated HTML node
**/
function addEmbeddedStyleSheet(css) {
return $("<style>").text(css).appendTo("head")[0];
}
/**
* Appends a "link" tag to the document's head.
*
* @param {!string} url URL to a style sheet
* @param {$.Deferred=} deferred Optionally check for load and error events
* @return {!HTMLLinkElement} The generated HTML node
**/
function addLinkedStyleSheet(url, deferred) {
var attributes = {
type: "text/css",
rel: "stylesheet",
href: url
};
var $link = $("<link/>").attr(attributes);
if (deferred) {
$link.on('load', deferred.resolve).on('error', deferred.reject);
}
$link.appendTo("head");
return $link[0];
}
/**
* getModuleUrl returns different urls for win platform
* so that's why we need a different check here
*
* @private
* @see #getModuleUrl
* @param {!string} pathOrUrl that should be checked if it's absolute
* @return {!boolean} returns true if pathOrUrl is absolute url on win platform
* or when it's absolute path on other platforms
*/
function isAbsolutePathOrUrl(pathOrUrl) {
return brackets.platform === "win" ? PathUtils.isAbsoluteUrl(pathOrUrl) : FileSystem.isAbsolutePath(pathOrUrl);
}
/**
* Parses LESS code and returns a promise that resolves with plain CSS code.
*
* Pass the link url argument to resolve relative URLs contained in the code.
* Make sure URLs in the code are wrapped in quotes, like so:
* background-image: url("image.png");
*
* @param {!string} code LESS code to parse
* @param {?string} url URL to the file containing the code
* @return {!$.Promise} A promise object that is resolved with CSS code if the LESS code can be parsed
*/
function parseLessCode(code, url) {
var result = new $.Deferred(),
options;
if (url) {
var dir = url.slice(0, url.lastIndexOf("/") + 1);
options = {
filename: url,
rootpath: dir
};
if (isAbsolutePathOrUrl(url)) {
options.currentFileInfo = {
currentDirectory: dir,
entryPath: dir,
filename: url,
rootFilename: url,
rootpath: dir
};
}
}
less.render(code, options, function onParse(err, tree) {
if (err) {
result.reject(err);
} else {
result.resolve(tree.css);
}
});
return result.promise();
}
/**
* Returns a path to an extension module.
*
* @param {!module} module Module provided by RequireJS
* @param {?string} path Relative path from the extension folder to a file
* @return {!string} The path to the module's folder
*/
function getModulePath(module, path) {
var modulePath = module.uri.substr(0, module.uri.lastIndexOf("/") + 1);
if (path) {
modulePath += path;
}
return modulePath;
}
/**
* Returns a URL to an extension module.
*
* @param {!module} module Module provided by RequireJS
* @param {?string} path Relative path from the extension folder to a file
* @return {!string} The URL to the module's folder
*/
function getModuleUrl(module, path) {
return encodeURI(getModulePath(module, path));
}
/**
* Performs a GET request using a path relative to an extension module.
*
* The resulting URL can be retrieved in the resolve callback by accessing
*
* @param {!module} module Module provided by RequireJS
* @param {!string} path Relative path from the extension folder to a file
* @return {!$.Promise} A promise object that is resolved with the contents of the requested file
*/
function loadFile(module, path) {
var url = PathUtils.isAbsoluteUrl(path) ? path : getModuleUrl(module, path);
let result = new $.Deferred();
$.get(url)
.done(function (content) {
result.resolve(content, url);
})
.fail(result.reject);
return result;
}
/**
* Loads a style sheet (CSS or LESS) relative to the extension module.
*
* @param {!module} module Module provided by RequireJS
* @param {!string} path Relative path from the extension folder to a CSS or LESS file
* @return {!$.Promise} A promise object that is resolved with an HTML node if the file can be loaded.
*/
function loadStyleSheet(module, path) {
var result = new $.Deferred();
loadFile(module, path)
.done(function (content, URL) {
var url = URL || this.url;
if (url.slice(-5) === ".less") {
parseLessCode(content, url)
.done(function (css) {
result.resolve(addEmbeddedStyleSheet(css));
})
.fail(result.reject);
} else {
// fix css load
var deferred = new $.Deferred(),
link = addLinkedStyleSheet(url, deferred);
deferred
.done(function () {
result.resolve(link);
})
.fail(result.reject);
}
})
.fail(result.reject);
// Summarize error info to console for easier debugging
result.fail(function (error, textStatus, httpError) {
if (error.readyState !== undefined) {
// If first arg is a jQXHR object, the real error info is in the next two args
console.error("[Extension] Unable to read stylesheet " + path + ":", textStatus, httpError);
} else {
console.error("[Extension] Unable to process stylesheet " + path, error);
}
});
return result.promise();
}
/**
* Loads the package.json file in the given extension folder as well as any additional
* metadata.
*
* @private
* @param {string} baseExtensionUrl The extension folder.
* @param {?string} extensionName optional extension name
* @return {$.Promise} A promise object that is resolved with the parsed contents of the package.json file,
* or rejected if there is no package.json with the boolean indicating whether .disabled file exists.
*/
function _loadExtensionMetadata(baseExtensionUrl, extensionName) {
const packageJSONFile = baseExtensionUrl + "/package.json";
const result = new $.Deferred();
let json = {
name: extensionName || path.basename(baseExtensionUrl)
};
$.getJSON(packageJSONFile)
.then(function (packageResult) {
json = packageResult;
}).always(function () {
// if we don't have any metadata for the extension
// we should still create an empty one, so we can attach
// disabled property on it in case it's disabled
let disabled,
defaultDisabled = JSON.parse(PhStore.getItem(Package.DEFAULT_DISABLED_EXTENSIONS_KEY) || "[]");
if (Array.isArray(defaultDisabled) && defaultDisabled.indexOf(baseExtensionUrl) !== -1) {
console.warn("Extension has been disabled on startup: " + baseExtensionUrl);
disabled = true;
}
json.disabled = disabled;
result.resolve(json);
});
return result.promise();
}
/**
* Loads the package.json file in the given extension folder as well as any additional
* metadata for default extensions in the source directory.
*
* If there's a .disabled file in the extension directory, then the content of package.json
* will be augmented with disabled property set to true. It will override whatever value of
* disabled might be set.
*
* @param {string} metadataURL The extension folder/base url for default extensions.
* @param {string} extensionName name of the extension
* @return {$.Promise} A promise object that is resolved with the parsed contents of the package.json file,
* or rejected if there is no package.json with the boolean indicating whether .disabled file exists.
*/
function loadMetadata(metadataURL, extensionName) {
if(metadataURL.startsWith("http://") || metadataURL.startsWith("https://")
|| metadataURL.startsWith("phtauri://") || metadataURL.startsWith("asset://")) {
return _loadExtensionMetadata(metadataURL, extensionName);
}
throw new Error(`Cannot load extension metadata for ${extensionName} at path ${metadataURL}`);
}
exports.addEmbeddedStyleSheet = addEmbeddedStyleSheet;
exports.addLinkedStyleSheet = addLinkedStyleSheet;
exports.parseLessCode = parseLessCode;
exports.getModulePath = getModulePath;
exports.getModuleUrl = getModuleUrl;
exports.loadFile = loadFile;
exports.loadStyleSheet = loadStyleSheet;
exports.loadMetadata = loadMetadata;
});