gulpfile.js 12 KB

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