gulpfile.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. var gulp = require('gulp');
  2. var metadata = require('./metadata');
  3. var es = require('event-stream');
  4. var path = require('path');
  5. var fs = require('fs');
  6. var rimraf = require('rimraf');
  7. var cp = require('child_process');
  8. var SAMPLES_MDOC_PATH = path.join(__dirname, 'website/playground/playground.mdoc');
  9. var WEBSITE_GENERATED_PATH = path.join(__dirname, 'website/playground/samples');
  10. gulp.task('clean-release', function(cb) { rimraf('release', { maxBusyTries: 1 }, cb); });
  11. gulp.task('release', ['clean-release'], function() {
  12. return es.merge(
  13. // dev folder
  14. releaseOne('dev'),
  15. // min folder
  16. releaseOne('min'),
  17. // package.json
  18. gulp.src('package.json')
  19. .pipe(es.through(function(data) {
  20. var json = JSON.parse(data.contents.toString());
  21. json.private = false;
  22. data.contents = new Buffer(JSON.stringify(json, null, ' '));
  23. this.emit('data', data);
  24. }))
  25. .pipe(gulp.dest('release')),
  26. // min-maps folder
  27. gulp.src('node_modules/monaco-editor-core/min-maps/**/*').pipe(gulp.dest('release/min-maps')),
  28. // other files
  29. gulp.src([
  30. 'node_modules/monaco-editor-core/LICENSE',
  31. 'node_modules/monaco-editor-core/monaco.d.ts',
  32. 'node_modules/monaco-editor-core/ThirdPartyNotices.txt',
  33. 'README.md'
  34. ]).pipe(addPluginDTS()).pipe(gulp.dest('release'))
  35. )
  36. });
  37. function releaseOne(type) {
  38. return es.merge(
  39. gulp.src('node_modules/monaco-editor-core/' + type + '/**/*')
  40. .pipe(addPluginContribs())
  41. .pipe(gulp.dest('release/' + type)),
  42. pluginStreams('release/' + type + '/')
  43. )
  44. }
  45. function pluginStreams(destinationPath) {
  46. return es.merge(
  47. metadata.METADATA.PLUGINS.map(function(plugin) {
  48. return pluginStream(plugin, destinationPath);
  49. })
  50. );
  51. }
  52. function pluginStream(plugin, destinationPath) {
  53. var contribPath = path.join(plugin.path, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
  54. return (
  55. gulp.src([
  56. plugin.path + '/**/*',
  57. '!' + contribPath
  58. ])
  59. .pipe(gulp.dest(destinationPath + plugin.modulePrefix))
  60. );
  61. }
  62. /**
  63. * Edit editor.main.js:
  64. * - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  65. * - append contribs from plugins
  66. * - append new AMD module 'vs/editor/editor.main' that stiches things together
  67. */
  68. function addPluginContribs() {
  69. return es.through(function(data) {
  70. if (!/editor\.main\.js$/.test(data.path)) {
  71. this.emit('data', data);
  72. return;
  73. }
  74. var contents = data.contents.toString();
  75. // Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  76. contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"');
  77. var extraContent = [];
  78. var allPluginsModuleIds = [];
  79. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  80. allPluginsModuleIds.push(plugin.contrib);
  81. var contribPath = path.join(__dirname, plugin.path, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
  82. var contribContents = fs.readFileSync(contribPath).toString();
  83. var contribDefineIndex = contribContents.indexOf('define("' + plugin.contrib);
  84. if (contribDefineIndex === -1) {
  85. console.error('(1) CANNOT DETERMINE AMD define location for contribution', plugin);
  86. process.exit(-1);
  87. }
  88. var depsEndIndex = contribContents.indexOf(']', contribDefineIndex);
  89. if (contribDefineIndex === -1) {
  90. console.error('(2) CANNOT DETERMINE AMD define location for contribution', plugin);
  91. process.exit(-1);
  92. }
  93. contribContents = contribContents.substring(0, depsEndIndex) + ',"vs/editor/edcore.main"' + contribContents.substring(depsEndIndex);
  94. extraContent.push(contribContents);
  95. });
  96. extraContent.push(`define("vs/editor/editor.main", ["vs/editor/edcore.main","${allPluginsModuleIds.join('","')}"], function() {});`);
  97. var insertIndex = contents.lastIndexOf('//# sourceMappingURL=');
  98. if (insertIndex === -1) {
  99. insertIndex = contents.length;
  100. }
  101. contents = contents.substring(0, insertIndex) + '\n' + extraContent.join('\n') + '\n' + contents.substring(insertIndex);
  102. data.contents = new Buffer(contents);
  103. this.emit('data', data);
  104. });
  105. }
  106. /**
  107. * Edit monaco.d.ts:
  108. * - append monaco.d.ts from plugins
  109. */
  110. function addPluginDTS() {
  111. return es.through(function(data) {
  112. if (!/monaco\.d\.ts$/.test(data.path)) {
  113. this.emit('data', data);
  114. return;
  115. }
  116. var contents = data.contents.toString();
  117. var extraContent = [];
  118. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  119. var dtsPath = path.join(plugin.path, 'monaco.d.ts');
  120. try {
  121. extraContent.push(fs.readFileSync(dtsPath).toString());
  122. } catch (err) {
  123. return;
  124. }
  125. });
  126. contents += '\n' + extraContent.join('\n');
  127. data.contents = new Buffer(contents);
  128. fs.writeFileSync('website/playground/monaco.d.ts.txt', contents);
  129. this.emit('data', data);
  130. });
  131. }
  132. // --- website
  133. gulp.task('clean-playground-samples', function(cb) { rimraf(WEBSITE_GENERATED_PATH, { maxBusyTries: 1 }, cb); });
  134. gulp.task('playground-samples', ['clean-playground-samples'], function() {
  135. function toFolderName(name) {
  136. var result = name.toLowerCase().replace(/[^a-z0-9\-_]/g, '-');
  137. while (result.indexOf('--') >= 0) {
  138. result = result.replace(/--/, '-');
  139. }
  140. while (result.charAt(result.length - 1) === '-') {
  141. result = result.substring(result, result.length - 1);
  142. }
  143. return result;
  144. }
  145. function parse(txt) {
  146. function startsWith(haystack, needle) {
  147. return haystack.substring(0, needle.length) === needle;
  148. }
  149. var CHAPTER_MARKER = "=";
  150. var SAMPLE_MARKER = "==";
  151. var SNIPPET_MARKER = "=======================";
  152. var lines = txt.split(/\r\n|\n|\r/);
  153. var result = [];
  154. var currentChapter = null;
  155. var currentSample = null;
  156. var currentSnippet = null;
  157. for (var i = 0; i < lines.length; i++) {
  158. var line = lines[i];
  159. if (startsWith(line, SNIPPET_MARKER)) {
  160. var snippetType = line.substring(SNIPPET_MARKER.length).trim();
  161. if (snippetType === 'HTML' || snippetType === 'JS' || snippetType === 'CSS') {
  162. currentSnippet = currentSample[snippetType];
  163. } else {
  164. currentSnippet = null;
  165. }
  166. continue;
  167. }
  168. if (startsWith(line, SAMPLE_MARKER)) {
  169. currentSnippet = null;
  170. currentSample = {
  171. name: line.substring(SAMPLE_MARKER.length).trim(),
  172. JS: [],
  173. HTML: [],
  174. CSS: []
  175. };
  176. currentChapter.samples.push(currentSample);
  177. continue;
  178. }
  179. if (startsWith(line, CHAPTER_MARKER)) {
  180. currentSnippet = null;
  181. currentSample = null;
  182. currentChapter = {
  183. name: line.substring(CHAPTER_MARKER.length).trim(),
  184. samples: []
  185. };
  186. result.push(currentChapter);
  187. continue;
  188. }
  189. if (currentSnippet) {
  190. currentSnippet.push(line);
  191. continue;
  192. }
  193. if (line === '') {
  194. continue;
  195. }
  196. // ignore inter-sample content
  197. console.warn('IGNORING INTER-SAMPLE CONTENT: ' + line);
  198. }
  199. return result;
  200. }
  201. var chapters = parse(fs.readFileSync(SAMPLES_MDOC_PATH).toString());
  202. var allSamples = [];
  203. fs.mkdirSync(WEBSITE_GENERATED_PATH);
  204. chapters.forEach(function(chapter) {
  205. var chapterFolderName = toFolderName(chapter.name);
  206. chapter.samples.forEach(function(sample) {
  207. var sampleId = toFolderName(chapter.name + '-' + sample.name);
  208. sample.sampleId = sampleId;
  209. var js = [
  210. '//---------------------------------------------------',
  211. '// ' + chapter.name + ' > ' + sample.name,
  212. '//---------------------------------------------------',
  213. '',
  214. ].concat(sample.JS)
  215. var sampleOut = {
  216. id: sampleId,
  217. js: js.join('\n'),
  218. html: sample.HTML.join('\n'),
  219. css: sample.CSS.join('\n')
  220. };
  221. allSamples.push({
  222. chapter: chapter.name,
  223. name: sample.name,
  224. sampleId: sampleId
  225. });
  226. var content =
  227. `// This is a generated file. Please do not edit directly.
  228. var SAMPLES = this.SAMPLES || [];
  229. SAMPLES.push(${JSON.stringify(sampleOut)});
  230. `
  231. fs.writeFileSync(path.join(WEBSITE_GENERATED_PATH, sampleId + '.js'), content);
  232. });
  233. });
  234. var content =
  235. `// This is a generated file. Please do not edit directly.
  236. this.SAMPLES = [];
  237. this.ALL_SAMPLES = ${JSON.stringify(allSamples)};`
  238. fs.writeFileSync(path.join(WEBSITE_GENERATED_PATH, 'all.js'), content);
  239. });
  240. gulp.task('clean-website', function(cb) { rimraf('../monaco-editor-website', { maxBusyTries: 1 }, cb); });
  241. gulp.task('website', ['clean-website', 'playground-samples'], function() {
  242. return (
  243. gulp.src('website/**/*')
  244. .pipe(es.through(function(data) {
  245. if (!data.contents || !/\.(js|html)$/.test(data.path)) {
  246. return this.emit('data', data);
  247. }
  248. var contents = data.contents.toString();
  249. contents = contents.replace(/\.\.\/release\/min/g, 'node_modules/monaco-editor/min');
  250. // contents = contents.replace('&copy; 2016 Microsoft', '&copy; 2016 Microsoft [' + builtTime + ']');
  251. data.contents = new Buffer(contents);
  252. this.emit('data', data);
  253. }))
  254. .pipe(gulp.dest('../monaco-editor-website'))
  255. .pipe(es.through(function(data) {
  256. this.emit('data', data);
  257. }, function() {
  258. cp.execSync('npm install monaco-editor', {
  259. cwd: path.join(__dirname, '../monaco-editor-website')
  260. });
  261. this.emit('end');
  262. }))
  263. );
  264. });