gulpfile.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. gulp.task('clean-release', function(cb) { rimraf('release', { maxBusyTries: 1 }, cb); });
  16. gulp.task('release', ['clean-release','compile'], function() {
  17. var sha1 = getGitVersion(__dirname);
  18. var semver = require('./package.json').version;
  19. var headerVersion = semver + '(' + sha1 + ')';
  20. var BUNDLED_FILE_HEADER = [
  21. '/*!-----------------------------------------------------------------------------',
  22. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  23. ' * monaco-json version: ' + headerVersion,
  24. ' * Released under the MIT license',
  25. ' * https://github.com/Microsoft/monaco-json/blob/master/LICENSE.md',
  26. ' *-----------------------------------------------------------------------------*/',
  27. ''
  28. ].join('\n');
  29. function bundleOne(moduleId, exclude) {
  30. return rjs({
  31. baseUrl: '/out/',
  32. name: 'vs/language/json/' + moduleId,
  33. out: moduleId + '.js',
  34. exclude: exclude,
  35. paths: {
  36. 'vs/language/json': __dirname + '/out'
  37. },
  38. packages: [{
  39. name: 'vscode-json-languageservice',
  40. location: __dirname + '/node_modules/vscode-json-languageservice/lib',
  41. main: 'jsonLanguageService'
  42. }, {
  43. name: 'vscode-languageserver-types',
  44. location: __dirname + '/node_modules/vscode-languageserver-types/lib',
  45. main: 'main'
  46. }, {
  47. name: 'jsonc-parser',
  48. location: __dirname + '/node_modules/jsonc-parser/lib',
  49. main: 'main'
  50. }, {
  51. name: 'vscode-nls',
  52. location: __dirname + '/out/fillers',
  53. main: 'vscode-nls'
  54. }]
  55. })
  56. }
  57. return merge(
  58. merge(
  59. bundleOne('monaco.contribution', ['vs/language/json/jsonMode']),
  60. bundleOne('jsonMode'),
  61. bundleOne('jsonWorker')
  62. )
  63. .pipe(es.through(function(data) {
  64. data.contents = new Buffer(
  65. BUNDLED_FILE_HEADER
  66. + data.contents.toString()
  67. );
  68. this.emit('data', data);
  69. }))
  70. .pipe(gulp.dest('./release/dev'))
  71. .pipe(uglify({
  72. preserveComments: 'some'
  73. }))
  74. .pipe(gulp.dest('./release/min')),
  75. gulp.src('src/monaco.d.ts').pipe(gulp.dest('./release/min'))
  76. );
  77. });
  78. var compilation = tsb.create(assign({ verbose: true }, require('./src/tsconfig.json').compilerOptions));
  79. var tsSources = 'src/**/*.ts';
  80. function compileTask() {
  81. return merge(
  82. gulp.src(tsSources).pipe(compilation())
  83. )
  84. .pipe(gulp.dest('out'));
  85. }
  86. gulp.task('clean-out', function(cb) { rimraf('out', { maxBusyTries: 1 }, cb); });
  87. gulp.task('compile', ['clean-out'], compileTask);
  88. gulp.task('compile-without-clean', compileTask);
  89. gulp.task('watch', ['compile'], function() {
  90. gulp.watch(tsSources, ['compile-without-clean']);
  91. });
  92. /**
  93. * Escape text such that it can be used in a javascript string enclosed by double quotes (")
  94. */
  95. function escapeText(text) {
  96. // http://www.javascriptkit.com/jsref/escapesequence.shtml
  97. // \b Backspace.
  98. // \f Form feed.
  99. // \n Newline.
  100. // \O Nul character.
  101. // \r Carriage return.
  102. // \t Horizontal tab.
  103. // \v Vertical tab.
  104. // \' Single quote or apostrophe.
  105. // \" Double quote.
  106. // \\ Backslash.
  107. // \ddd The Latin-1 character specified by the three octal digits between 0 and 377. ie, copyright symbol is \251.
  108. // \xdd The Latin-1 character specified by the two hexadecimal digits dd between 00 and FF. ie, copyright symbol is \xA9.
  109. // \udddd The Unicode character specified by the four hexadecimal digits dddd. ie, copyright symbol is \u00A9.
  110. var _backspace = '\b'.charCodeAt(0);
  111. var _formFeed = '\f'.charCodeAt(0);
  112. var _newLine = '\n'.charCodeAt(0);
  113. var _nullChar = 0;
  114. var _carriageReturn = '\r'.charCodeAt(0);
  115. var _tab = '\t'.charCodeAt(0);
  116. var _verticalTab = '\v'.charCodeAt(0);
  117. var _backslash = '\\'.charCodeAt(0);
  118. var _doubleQuote = '"'.charCodeAt(0);
  119. var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
  120. for (var i = 0, len = text.length; i < len; i++) {
  121. chrCode = text.charCodeAt(i);
  122. switch (chrCode) {
  123. case _backspace:
  124. replaceWith = '\\b';
  125. break;
  126. case _formFeed:
  127. replaceWith = '\\f';
  128. break;
  129. case _newLine:
  130. replaceWith = '\\n';
  131. break;
  132. case _nullChar:
  133. replaceWith = '\\0';
  134. break;
  135. case _carriageReturn:
  136. replaceWith = '\\r';
  137. break;
  138. case _tab:
  139. replaceWith = '\\t';
  140. break;
  141. case _verticalTab:
  142. replaceWith = '\\v';
  143. break;
  144. case _backslash:
  145. replaceWith = '\\\\';
  146. break;
  147. case _doubleQuote:
  148. replaceWith = '\\"';
  149. break;
  150. }
  151. if (replaceWith !== null) {
  152. resultPieces.push(text.substring(startPos, i));
  153. resultPieces.push(replaceWith);
  154. startPos = i + 1;
  155. replaceWith = null;
  156. }
  157. }
  158. resultPieces.push(text.substring(startPos, len));
  159. return resultPieces.join('');
  160. }
  161. function getGitVersion(repo) {
  162. var git = path.join(repo, '.git');
  163. var headPath = path.join(git, 'HEAD');
  164. var head;
  165. try {
  166. head = fs.readFileSync(headPath, 'utf8').trim();
  167. } catch (e) {
  168. return void 0;
  169. }
  170. if (/^[0-9a-f]{40}$/i.test(head)) {
  171. return head;
  172. }
  173. var refMatch = /^ref: (.*)$/.exec(head);
  174. if (!refMatch) {
  175. return void 0;
  176. }
  177. var ref = refMatch[1];
  178. var refPath = path.join(git, ref);
  179. try {
  180. return fs.readFileSync(refPath, 'utf8').trim();
  181. } catch (e) {
  182. // noop
  183. }
  184. var packedRefsPath = path.join(git, 'packed-refs');
  185. var refsRaw;
  186. try {
  187. refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
  188. } catch (e) {
  189. return void 0;
  190. }
  191. var refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
  192. var refsMatch;
  193. var refs = {};
  194. while (refsMatch = refsRegex.exec(refsRaw)) {
  195. refs[refsMatch[2]] = refsMatch[1];
  196. }
  197. return refs[ref];
  198. }