gulpfile.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var gulp = require('gulp');
  6. var tsb = require('gulp-tsb');
  7. var assign = require('object-assign');
  8. var fs = require('fs');
  9. var path = require('path');
  10. var merge = require('merge-stream');
  11. var rjs = require('gulp-requirejs');
  12. var uglify = require('gulp-uglify');
  13. var rimraf = require('rimraf');
  14. var es = require('event-stream');
  15. var TYPESCRIPT_LIB_SOURCE = path.join(__dirname, 'node_modules', 'typescript', 'lib');
  16. var TYPESCRIPT_LIB_DESTINATION = path.join(__dirname, 'src', 'lib');
  17. gulp.task('clean-release', function(cb) { rimraf('release', { maxBusyTries: 1 }, cb); });
  18. gulp.task('release', ['clean-release'], function() {
  19. var sha1 = getGitVersion(__dirname);
  20. var semver = require('./package.json').version;
  21. var headerVersion = semver + '(' + sha1 + ')';
  22. var BUNDLED_FILE_HEADER = [
  23. '/*!-----------------------------------------------------------------------------',
  24. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  25. ' * monaco-typescript version: ' + headerVersion,
  26. ' * Released under the MIT license',
  27. ' * https://github.com/Microsoft/monaco-typescript/blob/master/LICENSE.md',
  28. ' *-----------------------------------------------------------------------------*/',
  29. ''
  30. ].join('\n');
  31. function bundleOne(moduleId, exclude) {
  32. return rjs({
  33. baseUrl: '/out/amd/',
  34. name: 'vs/language/typescript/' + moduleId,
  35. out: moduleId + '.js',
  36. exclude: exclude,
  37. paths: {
  38. 'vs/language/typescript': __dirname + '/out/amd/'
  39. }
  40. })
  41. }
  42. return merge(
  43. merge(
  44. bundleOne('monaco.contribution'),
  45. bundleOne('lib/typescriptServices'),
  46. bundleOne('mode', ['vs/language/typescript/lib/typescriptServices']),
  47. bundleOne('worker', ['vs/language/typescript/lib/typescriptServices'])
  48. )
  49. .pipe(uglify({
  50. preserveComments: 'some'
  51. }))
  52. .pipe(es.through(function(data) {
  53. data.contents = new Buffer(
  54. BUNDLED_FILE_HEADER
  55. + data.contents.toString()
  56. );
  57. this.emit('data', data);
  58. }))
  59. .pipe(gulp.dest('./release/')),
  60. gulp.src('src/monaco.d.ts').pipe(gulp.dest('./release/')),
  61. );
  62. });
  63. /**
  64. * Import files from TypeScript's dist
  65. */
  66. gulp.task('import-typescript', function() {
  67. try {
  68. fs.statSync(TYPESCRIPT_LIB_DESTINATION);
  69. } catch (err) {
  70. fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
  71. }
  72. importLibDeclarationFile('lib.d.ts');
  73. importLibDeclarationFile('lib.es6.d.ts');
  74. var tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js')).toString();
  75. tsServices +=
  76. `
  77. // MONACOCHANGE
  78. // Defining the entire module name because r.js has an issue and cannot bundle this file
  79. // correctly with an anonymous define call
  80. define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
  81. // END MONACOCHANGE
  82. `;
  83. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'), tsServices);
  84. var dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts')).toString();
  85. dtsServices +=
  86. `
  87. // MONACOCHANGE
  88. export = ts;
  89. // END MONACOCHANGE
  90. `;
  91. fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'), dtsServices);
  92. });
  93. /**
  94. * Import a lib*.d.ts file from TypeScript's dist
  95. */
  96. function importLibDeclarationFile(name) {
  97. var dstName = name.replace(/\.d\.ts$/, '').replace(/\./g, '-') + '-ts';
  98. var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, name);
  99. var contents = fs.readFileSync(srcPath).toString();
  100. var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, dstName + '.ts');
  101. fs.writeFileSync(dstPath,
  102. `/*---------------------------------------------------------------------------------------------
  103. * Copyright (c) Microsoft Corporation. All rights reserved.
  104. * Licensed under the MIT License. See License.txt in the project root for license information.
  105. *--------------------------------------------------------------------------------------------*/
  106. export const contents = "${escapeText(contents)}";
  107. `);
  108. }
  109. /**
  110. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  111. */
  112. function escapeText(text) {
  113. // See http://www.javascriptkit.com/jsref/escapesequence.shtml
  114. var _backspace = '\b'.charCodeAt(0);
  115. var _formFeed = '\f'.charCodeAt(0);
  116. var _newLine = '\n'.charCodeAt(0);
  117. var _nullChar = 0;
  118. var _carriageReturn = '\r'.charCodeAt(0);
  119. var _tab = '\t'.charCodeAt(0);
  120. var _verticalTab = '\v'.charCodeAt(0);
  121. var _backslash = '\\'.charCodeAt(0);
  122. var _doubleQuote = '"'.charCodeAt(0);
  123. var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
  124. for (var i = 0, len = text.length; i < len; i++) {
  125. chrCode = text.charCodeAt(i);
  126. switch (chrCode) {
  127. case _backspace:
  128. replaceWith = '\\b';
  129. break;
  130. case _formFeed:
  131. replaceWith = '\\f';
  132. break;
  133. case _newLine:
  134. replaceWith = '\\n';
  135. break;
  136. case _nullChar:
  137. replaceWith = '\\0';
  138. break;
  139. case _carriageReturn:
  140. replaceWith = '\\r';
  141. break;
  142. case _tab:
  143. replaceWith = '\\t';
  144. break;
  145. case _verticalTab:
  146. replaceWith = '\\v';
  147. break;
  148. case _backslash:
  149. replaceWith = '\\\\';
  150. break;
  151. case _doubleQuote:
  152. replaceWith = '\\"';
  153. break;
  154. }
  155. if (replaceWith !== null) {
  156. resultPieces.push(text.substring(startPos, i));
  157. resultPieces.push(replaceWith);
  158. startPos = i + 1;
  159. replaceWith = null;
  160. }
  161. }
  162. resultPieces.push(text.substring(startPos, len));
  163. return resultPieces.join('');
  164. }
  165. function getGitVersion(repo) {
  166. var git = path.join(repo, '.git');
  167. var headPath = path.join(git, 'HEAD');
  168. var head;
  169. try {
  170. head = fs.readFileSync(headPath, 'utf8').trim();
  171. } catch (e) {
  172. return void 0;
  173. }
  174. if (/^[0-9a-f]{40}$/i.test(head)) {
  175. return head;
  176. }
  177. var refMatch = /^ref: (.*)$/.exec(head);
  178. if (!refMatch) {
  179. return void 0;
  180. }
  181. var ref = refMatch[1];
  182. var refPath = path.join(git, ref);
  183. try {
  184. return fs.readFileSync(refPath, 'utf8').trim();
  185. } catch (e) {
  186. // noop
  187. }
  188. var packedRefsPath = path.join(git, 'packed-refs');
  189. var refsRaw;
  190. try {
  191. refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
  192. } catch (e) {
  193. return void 0;
  194. }
  195. var refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
  196. var refsMatch;
  197. var refs = {};
  198. while (refsMatch = refsRegex.exec(refsRaw)) {
  199. refs[refsMatch[2]] = refsMatch[1];
  200. }
  201. return refs[ref];
  202. }