gulpfile.js 10 KB

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