gulpfile.js 12 KB

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