gulpfile.js 11 KB

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