microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
gulpfile.js
249lines · modeblame
b8ecee4ddigeff10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
8ba55f4cJimmy Thomson10 years ago | 3 | |
62b110e3Dmitry Zinovyev9 years ago | 4 | var gulp = require("gulp"); |
| 5 | var log = require("gulp-util").log; | |
28d84585Vladimir Kotikov8 years ago | 6 | var istanbul = require('gulp-istanbul'); |
| 7 | var isparta = require('isparta'); | |
62b110e3Dmitry Zinovyev9 years ago | 8 | var sourcemaps = require("gulp-sourcemaps"); |
| 9 | var path = require("path"); | |
| 10 | var preprocess = require("gulp-preprocess"); | |
07edfe91Sergey Akhalkov8 years ago | 11 | var install = require("gulp-install"); |
3fb37ad5unknown10 years ago | 12 | var runSequence = require("run-sequence"); |
62b110e3Dmitry Zinovyev9 years ago | 13 | var ts = require("gulp-typescript"); |
| 14 | var mocha = require("gulp-mocha"); | |
9adec70dJoshua Skelton10 years ago | 15 | var GulpExtras = require("./tools/gulp-extras"); |
62b110e3Dmitry Zinovyev9 years ago | 16 | var minimist = require("minimist"); |
92f13422Jimmy Thomson9 years ago | 17 | var os = require("os"); |
| 18 | var fs = require("fs"); | |
| 19 | var Q = require("q"); | |
28d84585Vladimir Kotikov8 years ago | 20 | var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul'); |
3b6f5f91Sergey Akhalkov8 years ago | 21 | var execSync = require('child_process').execSync; |
572cd3badigeff10 years ago | 22 | |
9adec70dJoshua Skelton10 years ago | 23 | var copyright = GulpExtras.checkCopyright; |
| 24 | var imports = GulpExtras.checkImports; | |
7212311dDaniel Lebu10 years ago | 25 | var executeCommand = GulpExtras.executeCommand; |
8ba55f4cJimmy Thomson10 years ago | 26 | |
936ae21cmax-mironov8 years ago | 27 | |
62b110e3Dmitry Zinovyev9 years ago | 28 | var srcPath = "src"; |
3c172a05Artem Egorov8 years ago | 29 | var testPath = "test"; |
89973b41dlebu10 years ago | 30 | |
8ba55f4cJimmy Thomson10 years ago | 31 | var sources = [ |
89973b41dlebu10 years ago | 32 | srcPath, |
3c172a05Artem Egorov8 years ago | 33 | testPath, |
| 34 | ].map(function (tsFolder) { return tsFolder + "/**/*.ts"; }); | |
8ba55f4cJimmy Thomson10 years ago | 35 | |
572cd3badigeff10 years ago | 36 | var knownOptions = { |
62b110e3Dmitry Zinovyev9 years ago | 37 | string: "env", |
| 38 | default: { env: "production" } | |
572cd3badigeff10 years ago | 39 | }; |
| 40 | | |
| 41 | var options = minimist(process.argv.slice(2), knownOptions); | |
| 42 | | |
3aa3017bDmitry Zinovyev9 years ago | 43 | var tsProject = ts.createProject("tsconfig.json"); |
| 44 | | |
c55d31ecdigeff10 years ago | 45 | // TODO: The file property should point to the generated source (this implementation adds an extra folder to the path) |
| 46 | // We should also make sure that we always generate urls in all the path properties (We shouldn't have \\s. This seems to | |
| 47 | // be an issue on Windows platforms) | |
3aa3017bDmitry Zinovyev9 years ago | 48 | gulp.task("build", ["check-imports", "check-copyright"], build); |
| 49 | | |
| 50 | gulp.task("quick-build", build); | |
| 51 | | |
| 52 | function build(callback) { | |
62b110e3Dmitry Zinovyev9 years ago | 53 | var tsProject = ts.createProject("tsconfig.json"); |
| 54 | var isProd = options.env === "production"; | |
cc7a6803digeff10 years ago | 55 | var preprocessorContext = isProd ? { PROD: true } : { DEBUG: true }; |
572cd3badigeff10 years ago | 56 | log(`Building with preprocessor context: ${JSON.stringify(preprocessorContext)}`); |
8ba55f4cJimmy Thomson10 years ago | 57 | return tsProject.src() |
9c1d0f41digeff10 years ago | 58 | .pipe(preprocess({ context: preprocessorContext })) //To set environment variables in-line |
8ba55f4cJimmy Thomson10 years ago | 59 | .pipe(sourcemaps.init()) |
6d5c8798Nikita Matrosov9 years ago | 60 | .pipe(tsProject()) |
62b110e3Dmitry Zinovyev9 years ago | 61 | .on("error", function (e) { |
d63a3130digeff10 years ago | 62 | callback(e); |
| 63 | }) | |
62b110e3Dmitry Zinovyev9 years ago | 64 | .pipe(sourcemaps.write(".", { |
c55d31ecdigeff10 years ago | 65 | includeContent: false, |
8bb73579Dmitry Zinovyev9 years ago | 66 | sourceRoot: "." |
c55d31ecdigeff10 years ago | 67 | })) |
678db279Artem Egorov8 years ago | 68 | .pipe(gulp.dest(function (file) { |
| 69 | return file.cwd; | |
| 70 | })); | |
3aa3017bDmitry Zinovyev9 years ago | 71 | } |
8ba55f4cJimmy Thomson10 years ago | 72 | |
62b110e3Dmitry Zinovyev9 years ago | 73 | gulp.task("watch", ["build"], function (cb) { |
| 74 | log("Watching build sources..."); | |
| 75 | return gulp.watch(sources, ["build"]); | |
8ba55f4cJimmy Thomson10 years ago | 76 | }); |
| 77 | | |
62b110e3Dmitry Zinovyev9 years ago | 78 | gulp.task("default", function (callback) { |
89973b41dlebu10 years ago | 79 | runSequence("clean", "build", "tslint", callback); |
3fb37ad5unknown10 years ago | 80 | }); |
8ba55f4cJimmy Thomson10 years ago | 81 | |
| 82 | var lintSources = [ | |
89973b41dlebu10 years ago | 83 | srcPath, |
3c172a05Artem Egorov8 years ago | 84 | testPath |
62b110e3Dmitry Zinovyev9 years ago | 85 | ].map(function (tsFolder) { return tsFolder + "/**/*.ts"; }); |
3fde2079Jimmy Thomson10 years ago | 86 | lintSources = lintSources.concat([ |
62b110e3Dmitry Zinovyev9 years ago | 87 | "!src/typings/**", |
3c172a05Artem Egorov8 years ago | 88 | "!test/resources/sampleReactNative022Project/**", |
ba841d7fSergey Akhalkov8 years ago | 89 | "!src/extension/appcenter/lib/**" |
3fde2079Jimmy Thomson10 years ago | 90 | ]); |
8ba55f4cJimmy Thomson10 years ago | 91 | |
27710197Vladimir Kotikov8 years ago | 92 | var libtslint = require("tslint"); |
62b110e3Dmitry Zinovyev9 years ago | 93 | var tslint = require("gulp-tslint"); |
| 94 | gulp.task("tslint", function () { | |
27710197Vladimir Kotikov8 years ago | 95 | var program = libtslint.Linter.createProgram("./tsconfig.json"); |
62b110e3Dmitry Zinovyev9 years ago | 96 | return gulp.src(lintSources, { base: "." }) |
27710197Vladimir Kotikov8 years ago | 97 | .pipe(tslint({ |
| 98 | formatter: "verbose", | |
| 99 | program: program | |
| 100 | })) | |
| 101 | .pipe(tslint.report()); | |
8ba55f4cJimmy Thomson10 years ago | 102 | }); |
| 103 | | |
| 104 | function test() { | |
211ffe84digeff10 years ago | 105 | // Check if arguments were passed |
572081b0Mark Oswald10 years ago | 106 | if (options.pattern) { |
| 107 | console.log("\nTesting cases that match pattern: " + options.pattern); | |
7d22ec66digeff10 years ago | 108 | } else { |
572081b0Mark Oswald10 years ago | 109 | console.log("\nTesting cases that don't match pattern: extensionContext"); |
211ffe84digeff10 years ago | 110 | } |
| 111 | | |
678db279Artem Egorov8 years ago | 112 | return gulp.src(["test/**/*.test.js", "!test/extension/**"]) |
d500558fJimmy Thomson10 years ago | 113 | .pipe(mocha({ |
62b110e3Dmitry Zinovyev9 years ago | 114 | ui: "tdd", |
d500558fJimmy Thomson10 years ago | 115 | useColors: true, |
572081b0Mark Oswald10 years ago | 116 | invert: !options.pattern, |
| 117 | grep: options.pattern || "extensionContext" | |
d500558fJimmy Thomson10 years ago | 118 | })); |
8ba55f4cJimmy Thomson10 years ago | 119 | } |
| 120 | | |
62b110e3Dmitry Zinovyev9 years ago | 121 | gulp.task("test", ["build", "tslint"], test); |
28d84585Vladimir Kotikov8 years ago | 122 | |
| 123 | gulp.task('coverage:instrument', function () { | |
936ae21cmax-mironov8 years ago | 124 | return gulp.src(["src/**/*.js", "!test/**", "!src/extension/appcenter/lib/**"]) |
28d84585Vladimir Kotikov8 years ago | 125 | .pipe(istanbul({ |
| 126 | // Use the isparta instrumenter (code coverage for ES6) | |
| 127 | instrumenter: isparta.Instrumenter, | |
| 128 | includeUntested: true | |
| 129 | })) | |
| 130 | // Force `require` to return covered files | |
| 131 | .pipe(istanbul.hookRequire()); | |
| 132 | }); | |
| 133 | | |
| 134 | gulp.task('coverage:report', function (done) { | |
| 135 | return gulp.src( | |
936ae21cmax-mironov8 years ago | 136 | ["src/**/*.js", "!test/**", "!src/extension/appcenter/lib/**"], |
28d84585Vladimir Kotikov8 years ago | 137 | { read: false } |
| 138 | ) | |
| 139 | .pipe(istanbul.writeReports({ | |
297822a1Vladimir Kotikov8 years ago | 140 | reporters: ['json', 'text-summary'] |
28d84585Vladimir Kotikov8 years ago | 141 | })); |
| 142 | }); | |
| 143 | | |
| 144 | gulp.task('coverage:remap', function () { | |
| 145 | return gulp.src('coverage/coverage-final.json') | |
| 146 | .pipe(remapIstanbul({ | |
| 147 | reports: { | |
| 148 | 'json': 'coverage/coverage.json', | |
| 149 | 'html': 'coverage/html-report' | |
| 150 | } | |
| 151 | })); | |
| 152 | }); | |
| 153 | | |
| 154 | gulp.task("test:coverage", function (done) { | |
| 155 | runSequence("quick-build", 'coverage:instrument', | |
| 156 | "test-no-build", 'coverage:report', 'coverage:remap', done); | |
| 157 | }); | |
| 158 | | |
62b110e3Dmitry Zinovyev9 years ago | 159 | gulp.task("test-no-build", test); |
8ba55f4cJimmy Thomson10 years ago | 160 | |
62b110e3Dmitry Zinovyev9 years ago | 161 | gulp.task("check-imports", function (cb) { |
9adec70dJoshua Skelton10 years ago | 162 | return tsProject.src() |
| 163 | .pipe(imports()); | |
7d0d8776digeff10 years ago | 164 | }); |
| 165 | | |
62b110e3Dmitry Zinovyev9 years ago | 166 | gulp.task("check-copyright", function (cb) { |
a3fd5ee9Joshua Skelton10 years ago | 167 | return gulp.src([ |
9c1d0f41digeff10 years ago | 168 | "**/*.ts", |
| 169 | "**/*.js", | |
| 170 | "!**/*.d.ts", | |
2331a749Vladimir Kotikov8 years ago | 171 | "!coverage/**", |
0fc1f1deJimmy Thomson9 years ago | 172 | "!node_modules/**", |
4b37483dmax-mironov8 years ago | 173 | "!lib/**", |
678db279Artem Egorov8 years ago | 174 | "!test/**/*.js", |
0fc1f1deJimmy Thomson9 years ago | 175 | "!SampleApplication/**", |
3c172a05Artem Egorov8 years ago | 176 | "!test/resources/sampleReactNative022Project/**/*.js", |
ba841d7fSergey Akhalkov8 years ago | 177 | "!src/extension/appcenter/lib/**", |
9c1d0f41digeff10 years ago | 178 | ]) |
9adec70dJoshua Skelton10 years ago | 179 | .pipe(copyright()); |
7d0d8776digeff10 years ago | 180 | }); |
379834c9Jimmy Thomson10 years ago | 181 | |
62b110e3Dmitry Zinovyev9 years ago | 182 | gulp.task("watch-build-test", ["build", "build-test"], function () { |
| 183 | return gulp.watch(sources, ["build", "build-test"]); | |
8ba55f4cJimmy Thomson10 years ago | 184 | }); |
89973b41dlebu10 years ago | 185 | |
ee16550eGuillaume Jenkins10 years ago | 186 | gulp.task("clean", function () { |
89973b41dlebu10 years ago | 187 | var del = require("del"); |
ee16550eGuillaume Jenkins10 years ago | 188 | var pathsToDelete = [ |
678db279Artem Egorov8 years ago | 189 | "src/**/*.js", |
| 190 | "src/**/*.js.map", | |
| 191 | "test/**/*.js", | |
| 192 | "test/**/*.js.map", | |
| 193 | "out/", | |
| 194 | "!test/resources/sampleReactNative022Project/**/*.js", | |
| 195 | ".vscode-test/", | |
ba841d7fSergey Akhalkov8 years ago | 196 | "!src/extension/appcenter/lib/**/*.js", |
678db279Artem Egorov8 years ago | 197 | ] |
ee16550eGuillaume Jenkins10 years ago | 198 | return del(pathsToDelete, { force: true }); |
8edb50b4Jimmy Thomson10 years ago | 199 | }); |
7212311dDaniel Lebu10 years ago | 200 | |
| 201 | gulp.task("package", function (callback) { | |
92f13422Jimmy Thomson9 years ago | 202 | var command = path.join(__dirname, "node_modules", ".bin", "vsce"); |
7212311dDaniel Lebu10 years ago | 203 | var args = ["package"]; |
| 204 | executeCommand(command, args, callback); | |
92f13422Jimmy Thomson9 years ago | 205 | }); |
| 206 | | |
| 207 | gulp.task("release", ["build"], function () { | |
| 208 | var licenseFiles = ["LICENSE.txt", "ThirdPartyNotices.txt"]; | |
62b110e3Dmitry Zinovyev9 years ago | 209 | var backupFolder = path.resolve(path.join(os.tmpdir(), "vscode-react-native")); |
92f13422Jimmy Thomson9 years ago | 210 | if (!fs.existsSync(backupFolder)) { |
| 211 | fs.mkdirSync(backupFolder); | |
| 212 | } | |
| 213 | | |
| 214 | return Q({}) | |
| 215 | .then(function () { | |
| 216 | /* back up LICENSE.txt, ThirdPartyNotices.txt, README.md */ | |
| 217 | console.log("Backing up license files to " + backupFolder + "..."); | |
| 218 | licenseFiles.forEach(function (fileName) { | |
| 219 | fs.writeFileSync(path.join(backupFolder, fileName), fs.readFileSync(fileName)); | |
| 220 | }); | |
| 221 | | |
| 222 | /* copy over the release package license files */ | |
| 223 | console.log("Preparing license files for release..."); | |
62b110e3Dmitry Zinovyev9 years ago | 224 | fs.writeFileSync("LICENSE.txt", fs.readFileSync("release/LICENSE.txt")); |
| 225 | fs.writeFileSync("ThirdPartyNotices.txt", fs.readFileSync("release/ThirdPartyNotices.txt")); | |
3aa3017bDmitry Zinovyev9 years ago | 226 | }).then(() => { |
92f13422Jimmy Thomson9 years ago | 227 | console.log("Creating release package..."); |
| 228 | var deferred = Q.defer(); | |
| 229 | // NOTE: vsce must see npm 3.X otherwise it will not correctly strip out dev dependencies. | |
3aa3017bDmitry Zinovyev9 years ago | 230 | executeCommand("vsce", ["package"], function (arg) { if (arg) { deferred.reject(arg); } deferred.resolve() }, { cwd: path.resolve(__dirname) }); |
92f13422Jimmy Thomson9 years ago | 231 | return deferred.promise; |
| 232 | }).finally(function () { | |
| 233 | /* restore backed up files */ | |
| 234 | console.log("Restoring modified files..."); | |
| 235 | licenseFiles.forEach(function (fileName) { | |
| 236 | fs.writeFileSync(path.join(__dirname, fileName), fs.readFileSync(path.join(backupFolder, fileName))); | |
| 237 | }); | |
| 238 | }); | |
07edfe91Sergey Akhalkov8 years ago | 239 | }); |
| 240 | | |
3b6f5f91Sergey Akhalkov8 years ago | 241 | gulp.task("postinstall", function (done) { |
| 242 | execSync('node ./node_modules/vscode/bin/install'); | |
| 243 | | |
| 244 | const packages = [ | |
07edfe91Sergey Akhalkov8 years ago | 245 | path.join(__dirname, "src", "extension", "appcenter", "lib", "codepush-node-sdk", "dist", "package.json"), |
| 246 | ]; | |
| 247 | return gulp.src(packages) | |
| 248 | .pipe(install()); | |
| 249 | }); |