gulpfile.js 12 KB

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