FileSystemMapping.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.Object}
  33. */
  34. WebInspector.FileSystemMapping = function()
  35. {
  36. WebInspector.Object.call(this);
  37. this._fileSystemMappingSetting = WebInspector.settings.createSetting("fileSystemMapping", {});
  38. this._excludedFoldersSetting = WebInspector.settings.createSetting("workspaceExcludedFolders", {});
  39. var defaultCommonExcludedFolders = [
  40. "/\\.git/",
  41. "/\\.sass-cache/",
  42. "/\\.hg/",
  43. "/\\.idea/",
  44. "/\\.svn/",
  45. "/\\.cache/",
  46. "/\\.project/"
  47. ];
  48. var defaultWinExcludedFolders = [
  49. "/Thumbs.db$",
  50. "/ehthumbs.db$",
  51. "/Desktop.ini$",
  52. "/\\$RECYCLE.BIN/"
  53. ];
  54. var defaultMacExcludedFolders = [
  55. "/\\.DS_Store$",
  56. "/\\.Trashes$",
  57. "/\\.Spotlight-V100$",
  58. "/\\.AppleDouble$",
  59. "/\\.LSOverride$",
  60. "/Icon$",
  61. "/\\._.*$"
  62. ];
  63. var defaultLinuxExcludedFolders = [
  64. "/.*~$"
  65. ];
  66. var defaultExcludedFolders = defaultCommonExcludedFolders;
  67. if (WebInspector.isWin())
  68. defaultExcludedFolders = defaultExcludedFolders.concat(defaultWinExcludedFolders);
  69. else if (WebInspector.isMac())
  70. defaultExcludedFolders = defaultExcludedFolders.concat(defaultMacExcludedFolders);
  71. else
  72. defaultExcludedFolders = defaultExcludedFolders.concat(defaultLinuxExcludedFolders);
  73. var defaultExcludedFoldersPattern = defaultExcludedFolders.join("|");
  74. WebInspector.settings.workspaceFolderExcludePattern = WebInspector.settings.createSetting("workspaceFolderExcludePattern", defaultExcludedFoldersPattern);
  75. /** @type {!Object.<string, Array.<WebInspector.FileSystemMapping.Entry>>} */
  76. this._fileSystemMappings = {};
  77. /** @type {!Object.<string, Array.<WebInspector.FileSystemMapping.ExcludedFolderEntry>>} */
  78. this._excludedFolders = {};
  79. this._loadFromSettings();
  80. }
  81. WebInspector.FileSystemMapping.Events = {
  82. FileMappingAdded: "FileMappingAdded",
  83. FileMappingRemoved: "FileMappingRemoved",
  84. ExcludedFolderAdded: "ExcludedFolderAdded",
  85. ExcludedFolderRemoved: "ExcludedFolderRemoved"
  86. }
  87. WebInspector.FileSystemMapping.prototype = {
  88. _loadFromSettings: function()
  89. {
  90. var savedMapping = this._fileSystemMappingSetting.get();
  91. this._fileSystemMappings = {};
  92. for (var fileSystemPath in savedMapping) {
  93. var savedFileSystemMappings = savedMapping[fileSystemPath];
  94. this._fileSystemMappings[fileSystemPath] = [];
  95. var fileSystemMappings = this._fileSystemMappings[fileSystemPath];
  96. for (var i = 0; i < savedFileSystemMappings.length; ++i) {
  97. var savedEntry = savedFileSystemMappings[i];
  98. var entry = new WebInspector.FileSystemMapping.Entry(savedEntry.fileSystemPath, savedEntry.urlPrefix, savedEntry.pathPrefix);
  99. fileSystemMappings.push(entry);
  100. }
  101. }
  102. var savedExcludedFolders = this._excludedFoldersSetting.get();
  103. this._excludedFolders = {};
  104. for (var fileSystemPath in savedExcludedFolders) {
  105. var savedExcludedFoldersForPath = savedExcludedFolders[fileSystemPath];
  106. this._excludedFolders[fileSystemPath] = [];
  107. var excludedFolders = this._excludedFolders[fileSystemPath];
  108. for (var i = 0; i < savedExcludedFoldersForPath.length; ++i) {
  109. var savedEntry = savedExcludedFoldersForPath[i];
  110. var entry = new WebInspector.FileSystemMapping.ExcludedFolderEntry(savedEntry.fileSystemPath, savedEntry.path);
  111. excludedFolders.push(entry);
  112. }
  113. }
  114. var workspaceFolderExcludePattern = WebInspector.settings.workspaceFolderExcludePattern.get()
  115. try {
  116. var flags = WebInspector.isWin() ? "i" : "";
  117. this._workspaceFolderExcludeRegex = workspaceFolderExcludePattern ? new RegExp(workspaceFolderExcludePattern, flags) : null;
  118. } catch (e) {
  119. }
  120. this._rebuildIndexes();
  121. },
  122. _saveToSettings: function()
  123. {
  124. var savedMapping = this._fileSystemMappings;
  125. this._fileSystemMappingSetting.set(savedMapping);
  126. var savedExcludedFolders = this._excludedFolders;
  127. this._excludedFoldersSetting.set(savedExcludedFolders);
  128. this._rebuildIndexes();
  129. },
  130. _rebuildIndexes: function()
  131. {
  132. // We are building an index here to search for the longest url prefix match faster.
  133. this._mappingForURLPrefix = {};
  134. this._urlPrefixes = [];
  135. for (var fileSystemPath in this._fileSystemMappings) {
  136. var fileSystemMapping = this._fileSystemMappings[fileSystemPath];
  137. for (var i = 0; i < fileSystemMapping.length; ++i) {
  138. var entry = fileSystemMapping[i];
  139. this._mappingForURLPrefix[entry.urlPrefix] = entry;
  140. this._urlPrefixes.push(entry.urlPrefix);
  141. }
  142. }
  143. this._urlPrefixes.sort();
  144. },
  145. /**
  146. * @param {string} fileSystemPath
  147. */
  148. addFileSystem: function(fileSystemPath)
  149. {
  150. if (this._fileSystemMappings[fileSystemPath])
  151. return;
  152. this._fileSystemMappings[fileSystemPath] = [];
  153. this._saveToSettings();
  154. },
  155. /**
  156. * @param {string} fileSystemPath
  157. */
  158. removeFileSystem: function(fileSystemPath)
  159. {
  160. if (!this._fileSystemMappings[fileSystemPath])
  161. return;
  162. delete this._fileSystemMappings[fileSystemPath];
  163. delete this._excludedFolders[fileSystemPath];
  164. this._saveToSettings();
  165. },
  166. /**
  167. * @param {string} fileSystemPath
  168. * @param {string} urlPrefix
  169. * @param {string} pathPrefix
  170. */
  171. addFileMapping: function(fileSystemPath, urlPrefix, pathPrefix)
  172. {
  173. var entry = new WebInspector.FileSystemMapping.Entry(fileSystemPath, urlPrefix, pathPrefix);
  174. this._fileSystemMappings[fileSystemPath].push(entry);
  175. this._saveToSettings();
  176. this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.FileMappingAdded, entry);
  177. },
  178. /**
  179. * @param {string} fileSystemPath
  180. * @param {string} urlPrefix
  181. * @param {string} pathPrefix
  182. */
  183. removeFileMapping: function(fileSystemPath, urlPrefix, pathPrefix)
  184. {
  185. var entry = this._mappingEntryForPathPrefix(fileSystemPath, pathPrefix);
  186. if (!entry)
  187. return;
  188. this._fileSystemMappings[fileSystemPath].remove(entry);
  189. this._saveToSettings();
  190. this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.FileMappingRemoved, entry);
  191. },
  192. /**
  193. * @param {string} fileSystemPath
  194. * @param {string} excludedFolderPath
  195. */
  196. addExcludedFolder: function(fileSystemPath, excludedFolderPath)
  197. {
  198. if (!this._excludedFolders[fileSystemPath])
  199. this._excludedFolders[fileSystemPath] = [];
  200. var entry = new WebInspector.FileSystemMapping.ExcludedFolderEntry(fileSystemPath, excludedFolderPath);
  201. this._excludedFolders[fileSystemPath].push(entry);
  202. this._saveToSettings();
  203. this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.ExcludedFolderAdded, entry);
  204. },
  205. /**
  206. * @param {string} fileSystemPath
  207. * @param {string} path
  208. */
  209. removeExcludedFolder: function(fileSystemPath, path)
  210. {
  211. var entry = this._excludedFolderEntryForPath(fileSystemPath, path);
  212. if (!entry)
  213. return;
  214. this._excludedFolders[fileSystemPath].remove(entry);
  215. this._saveToSettings();
  216. this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.ExcludedFolderRemoved, entry);
  217. },
  218. /**
  219. * @return {Array.<string>}
  220. */
  221. fileSystemPaths: function()
  222. {
  223. return Object.keys(this._fileSystemMappings);
  224. },
  225. /**
  226. * @param {string} url
  227. * @return {WebInspector.FileSystemMapping.Entry}
  228. */
  229. _mappingEntryForURL: function(url)
  230. {
  231. for (var i = this._urlPrefixes.length - 1; i >= 0; --i) {
  232. var urlPrefix = this._urlPrefixes[i];
  233. if (url.startsWith(urlPrefix))
  234. return this._mappingForURLPrefix[urlPrefix];
  235. }
  236. return null;
  237. },
  238. /**
  239. * @param {string} fileSystemPath
  240. * @param {string} path
  241. * @return {?WebInspector.FileSystemMapping.ExcludedFolderEntry}
  242. */
  243. _excludedFolderEntryForPath: function(fileSystemPath, path)
  244. {
  245. var entries = this._excludedFolders[fileSystemPath];
  246. if (!entries)
  247. return null;
  248. for (var i = 0; i < entries.length; ++i) {
  249. if (entries[i].path === path)
  250. return entries[i];
  251. }
  252. return null;
  253. },
  254. /**
  255. * @param {string} fileSystemPath
  256. * @param {string} filePath
  257. * @return {?WebInspector.FileSystemMapping.Entry}
  258. */
  259. _mappingEntryForPath: function(fileSystemPath, filePath)
  260. {
  261. var entries = this._fileSystemMappings[fileSystemPath];
  262. if (!entries)
  263. return null;
  264. var entry = null;
  265. for (var i = 0; i < entries.length; ++i) {
  266. var pathPrefix = entries[i].pathPrefix;
  267. // We are looking for the longest pathPrefix match.
  268. if (entry && entry.pathPrefix.length > pathPrefix.length)
  269. continue;
  270. if (filePath.startsWith(pathPrefix.substr(1)))
  271. entry = entries[i];
  272. }
  273. return entry;
  274. },
  275. /**
  276. * @param {string} fileSystemPath
  277. * @param {string} pathPrefix
  278. * @return {WebInspector.FileSystemMapping.Entry}
  279. */
  280. _mappingEntryForPathPrefix: function(fileSystemPath, pathPrefix)
  281. {
  282. var entries = this._fileSystemMappings[fileSystemPath];
  283. for (var i = 0; i < entries.length; ++i) {
  284. if (pathPrefix === entries[i].pathPrefix)
  285. return entries[i];
  286. }
  287. return null;
  288. },
  289. /**
  290. * @param {string} fileSystemPath
  291. * @param {string} folderPath
  292. * @return {boolean}
  293. */
  294. isFileExcluded: function(fileSystemPath, folderPath)
  295. {
  296. var excludedFolders = this._excludedFolders[fileSystemPath] || [];
  297. for (var i = 0; i < excludedFolders.length; ++i) {
  298. var entry = excludedFolders[i];
  299. if (entry.path === folderPath)
  300. return true;
  301. }
  302. return this._workspaceFolderExcludeRegex && this._workspaceFolderExcludeRegex.test(folderPath);
  303. },
  304. /**
  305. * @param {string} fileSystemPath
  306. * @return {Array.<WebInspector.FileSystemMapping.ExcludedFolderEntry>}
  307. */
  308. excludedFolders: function(fileSystemPath)
  309. {
  310. var excludedFolders = this._excludedFolders[fileSystemPath];
  311. return excludedFolders ? excludedFolders.slice() : [];
  312. },
  313. /**
  314. * @param {string} fileSystemPath
  315. * @return {Array.<WebInspector.FileSystemMapping.Entry>}
  316. */
  317. mappingEntries: function(fileSystemPath)
  318. {
  319. return this._fileSystemMappings[fileSystemPath].slice();
  320. },
  321. /**
  322. * @param {string} url
  323. * @return {boolean}
  324. */
  325. hasMappingForURL: function(url)
  326. {
  327. return !!this._mappingEntryForURL(url);
  328. },
  329. /**
  330. * @param {string} url
  331. * @return {?{fileSystemPath: string, filePath: string}}
  332. */
  333. fileForURL: function(url)
  334. {
  335. var entry = this._mappingEntryForURL(url);
  336. if (!entry)
  337. return null;
  338. var file = {};
  339. file.fileSystemPath = entry.fileSystemPath;
  340. file.filePath = entry.pathPrefix.substr(1) + url.substr(entry.urlPrefix.length);
  341. return file;
  342. },
  343. /**
  344. * @param {string} fileSystemPath
  345. * @param {string} filePath
  346. * @return {string}
  347. */
  348. urlForPath: function(fileSystemPath, filePath)
  349. {
  350. var entry = this._mappingEntryForPath(fileSystemPath, filePath);
  351. if (!entry)
  352. return "";
  353. return entry.urlPrefix + filePath.substring(entry.pathPrefix.length - 1);
  354. },
  355. /**
  356. * @param {string} url
  357. */
  358. removeMappingForURL: function(url)
  359. {
  360. var entry = this._mappingEntryForURL(url);
  361. if (!entry)
  362. return;
  363. this._fileSystemMappings[entry.fileSystemPath].remove(entry);
  364. this._saveToSettings();
  365. },
  366. /**
  367. * @param {string} url
  368. * @param {string} fileSystemPath
  369. * @param {string} filePath
  370. */
  371. addMappingForResource: function(url, fileSystemPath, filePath)
  372. {
  373. var commonPathSuffixLength = 0;
  374. var normalizedFilePath = "/" + filePath;
  375. for (var i = 0; i < normalizedFilePath.length; ++i) {
  376. var filePathCharacter = normalizedFilePath[normalizedFilePath.length - 1 - i];
  377. var urlCharacter = url[url.length - 1 - i];
  378. if (filePathCharacter !== urlCharacter)
  379. break;
  380. if (filePathCharacter === "/")
  381. commonPathSuffixLength = i;
  382. }
  383. var pathPrefix = normalizedFilePath.substr(0, normalizedFilePath.length - commonPathSuffixLength);
  384. var urlPrefix = url.substr(0, url.length - commonPathSuffixLength);
  385. this.addFileMapping(fileSystemPath, urlPrefix, pathPrefix);
  386. },
  387. __proto__: WebInspector.Object.prototype
  388. }
  389. /**
  390. * @constructor
  391. * @param {string} fileSystemPath
  392. * @param {string} urlPrefix
  393. * @param {string} pathPrefix
  394. */
  395. WebInspector.FileSystemMapping.Entry = function(fileSystemPath, urlPrefix, pathPrefix)
  396. {
  397. this.fileSystemPath = fileSystemPath;
  398. this.urlPrefix = urlPrefix;
  399. this.pathPrefix = pathPrefix;
  400. }
  401. /**
  402. * @constructor
  403. * @param {string} fileSystemPath
  404. * @param {string} path
  405. */
  406. WebInspector.FileSystemMapping.ExcludedFolderEntry = function(fileSystemPath, path)
  407. {
  408. this.fileSystemPath = fileSystemPath;
  409. this.path = path;
  410. }