microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
gulpfile.js
322lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | const gulp = require("gulp"); |
| 5 | const log = require("fancy-log"); |
| 6 | const istanbul = require("gulp-istanbul"); |
| 7 | const isparta = require("isparta"); |
| 8 | const sourcemaps = require("gulp-sourcemaps"); |
| 9 | const path = require("path"); |
| 10 | const preprocess = require("gulp-preprocess"); |
| 11 | const runSequence = require("run-sequence"); |
| 12 | const ts = require("gulp-typescript"); |
| 13 | const mocha = require("gulp-mocha"); |
| 14 | const GulpExtras = require("./tools/gulp-extras"); |
| 15 | const minimist = require("minimist"); |
| 16 | const os = require("os"); |
| 17 | const fs = require("fs"); |
| 18 | const Q = require("q"); |
| 19 | const es = require("event-stream"); |
| 20 | const remapIstanbul = require("remap-istanbul/lib/gulpRemapIstanbul"); |
| 21 | const execSync = require("child_process").execSync; |
| 22 | const nls = require("vscode-nls-dev"); |
| 23 | |
| 24 | const copyright = GulpExtras.checkCopyright; |
| 25 | const imports = GulpExtras.checkImports; |
| 26 | const executeCommand = GulpExtras.executeCommand; |
| 27 | |
| 28 | const transifexApiHostname = "www.transifex.com" |
| 29 | const transifexApiName = "api"; |
| 30 | const transifexApiToken = process.env.TRANSIFEX_API_TOKEN; |
| 31 | const transifexProjectName = "vscode-extensions"; |
| 32 | const transifexExtensionName = "vscode-react-native"; |
| 33 | |
| 34 | const defaultLanguages = [ |
| 35 | { id: "zh-tw", folderName: "cht", transifexId: "zh-hant" }, |
| 36 | { id: "zh-cn", folderName: "chs", transifexId: "zh-hans" }, |
| 37 | { id: "ja", folderName: "jpn" }, |
| 38 | { id: "ko", folderName: "kor" }, |
| 39 | { id: "de", folderName: "deu" }, |
| 40 | { id: "fr", folderName: "fra" }, |
| 41 | { id: "es", folderName: "esn" }, |
| 42 | { id: "ru", folderName: "rus" }, |
| 43 | { id: "it", folderName: "ita" }, |
| 44 | |
| 45 | // These language-pack languages are included for VS but excluded from the vscode package |
| 46 | { id: "cs", folderName: "csy" }, |
| 47 | { id: "tr", folderName: "trk" }, |
| 48 | { id: "pt-br", folderName: "ptb", transifexId: "pt_BR" }, |
| 49 | { id: "pl", folderName: "plk" } |
| 50 | ]; |
| 51 | |
| 52 | var srcPath = "src"; |
| 53 | var testPath = "test"; |
| 54 | |
| 55 | var sources = [ |
| 56 | srcPath, |
| 57 | testPath, |
| 58 | ].map(function (tsFolder) { return tsFolder + "/**/*.ts"; }); |
| 59 | |
| 60 | var knownOptions = { |
| 61 | string: "env", |
| 62 | default: { env: "production" } |
| 63 | }; |
| 64 | |
| 65 | var options = minimist(process.argv.slice(2), knownOptions); |
| 66 | |
| 67 | var tsProject = ts.createProject("tsconfig.json"); |
| 68 | |
| 69 | gulp.task("check-imports", function () { |
| 70 | return tsProject.src() |
| 71 | .pipe(imports()); |
| 72 | }); |
| 73 | |
| 74 | gulp.task("check-copyright", function () { |
| 75 | return gulp.src([ |
| 76 | "**/*.ts", |
| 77 | "**/*.js", |
| 78 | "!**/*.d.ts", |
| 79 | "!coverage/**", |
| 80 | "!node_modules/**", |
| 81 | "!test/**/*.js", |
| 82 | "!SampleApplication/**", |
| 83 | "!test/resources/sampleReactNative022Project/**/*.js" |
| 84 | ]) |
| 85 | .pipe(copyright()); |
| 86 | }); |
| 87 | |
| 88 | |
| 89 | function build(failOnError, buildNls) { |
| 90 | var tsProject = ts.createProject("tsconfig.json"); |
| 91 | var isProd = options.env === "production"; |
| 92 | var preprocessorContext = isProd ? { PROD: true } : { DEBUG: true }; |
| 93 | let gotError = false; |
| 94 | log(`Building with preprocessor context: ${JSON.stringify(preprocessorContext)}`); |
| 95 | var tsResult = tsProject.src() |
| 96 | .pipe(preprocess({ context: preprocessorContext })) //To set environment variables in-line |
| 97 | .pipe(sourcemaps.init()) |
| 98 | .pipe(tsProject()); |
| 99 | |
| 100 | return tsResult.js |
| 101 | .pipe(buildNls ? nls.rewriteLocalizeCalls() : es.through()) |
| 102 | .pipe(buildNls ? nls.createAdditionalLanguageFiles(defaultLanguages, "i18n", ".") : es.through()) |
| 103 | .pipe(buildNls ? nls.bundleMetaDataFiles("vsmobile.vscode-react-native", ".") : es.through()) |
| 104 | .pipe(buildNls ? nls.bundleLanguageFiles() : es.through()) |
| 105 | .pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "." })) |
| 106 | .pipe(gulp.dest(function (file) { |
| 107 | return file.cwd; |
| 108 | })) |
| 109 | .once("error", () => { |
| 110 | gotError = true; |
| 111 | }) |
| 112 | .once("finish", () => { |
| 113 | if (failOnError && gotError) { |
| 114 | process.exit(1); |
| 115 | } |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | var libtslint = require("tslint"); |
| 120 | var tslint = require("gulp-tslint"); |
| 121 | gulp.task("tslint", function () { |
| 122 | var program = libtslint.Linter.createProgram("./tsconfig.json"); |
| 123 | return gulp.src(lintSources, { base: "." }) |
| 124 | .pipe(tslint({ |
| 125 | formatter: "verbose", |
| 126 | program: program |
| 127 | })) |
| 128 | .pipe(tslint.report()); |
| 129 | }); |
| 130 | |
| 131 | // TODO: The file property should point to the generated source (this implementation adds an extra folder to the path) |
| 132 | // We should also make sure that we always generate urls in all the path properties (We shouldn"t have \\s. This seems to |
| 133 | // be an issue on Windows platforms) |
| 134 | gulp.task("build", gulp.series("check-imports", "check-copyright", "tslint", function (done) { |
| 135 | build(true, true) |
| 136 | .once("finish", () => { |
| 137 | done(); |
| 138 | }); |
| 139 | })); |
| 140 | |
| 141 | gulp.task("build-dev", gulp.series("check-imports", "check-copyright", function (done) { |
| 142 | build(false, false) |
| 143 | .once("finish", () => { |
| 144 | done(); |
| 145 | }); |
| 146 | })); |
| 147 | |
| 148 | gulp.task("quick-build", gulp.series("build-dev")); |
| 149 | |
| 150 | gulp.task("watch", gulp.series("build", function () { |
| 151 | log("Watching build sources..."); |
| 152 | return gulp.watch(sources, gulp.series("build")); |
| 153 | })); |
| 154 | |
| 155 | gulp.task("clean", function () { |
| 156 | var del = require("del"); |
| 157 | var pathsToDelete = [ |
| 158 | "src/**/*.js", |
| 159 | "src/**/*.js.map", |
| 160 | "test/**/*.js", |
| 161 | "test/**/*.js.map", |
| 162 | "out/", |
| 163 | "!test/resources/sampleReactNative022Project/**/*.js", |
| 164 | ".vscode-test/", |
| 165 | "nls.*.json", |
| 166 | "package.nls.*.json" |
| 167 | ] |
| 168 | return del(pathsToDelete, { force: true }); |
| 169 | }); |
| 170 | |
| 171 | gulp.task("default", gulp.series("clean", "build")); |
| 172 | |
| 173 | var lintSources = [ |
| 174 | srcPath, |
| 175 | testPath |
| 176 | ].map(function (tsFolder) { return tsFolder + "/**/*.ts"; }); |
| 177 | lintSources = lintSources.concat([ |
| 178 | "!src/typings/**", |
| 179 | "!test/resources/sampleReactNative022Project/**" |
| 180 | ]); |
| 181 | |
| 182 | function test() { |
| 183 | // Check if arguments were passed |
| 184 | if (options.pattern) { |
| 185 | console.log("\nTesting cases that match pattern: " + options.pattern); |
| 186 | } else { |
| 187 | console.log("\nTesting cases that don't match pattern: extensionContext"); |
| 188 | } |
| 189 | |
| 190 | return gulp.src(["test/**/*.test.js", "!test/extension/**"]) |
| 191 | .pipe(mocha({ |
| 192 | ui: "tdd", |
| 193 | useColors: true, |
| 194 | invert: !options.pattern, |
| 195 | grep: options.pattern || "extensionContext" |
| 196 | })); |
| 197 | } |
| 198 | |
| 199 | gulp.task("test", gulp.series("build", "tslint", test)); |
| 200 | |
| 201 | gulp.task("coverage:instrument", function () { |
| 202 | return gulp.src(["src/**/*.js", "!test/**"]) |
| 203 | .pipe(istanbul({ |
| 204 | // Use the isparta instrumenter (code coverage for ES6) |
| 205 | instrumenter: isparta.Instrumenter, |
| 206 | includeUntested: true |
| 207 | })) |
| 208 | // Force `require` to return covered files |
| 209 | .pipe(istanbul.hookRequire()); |
| 210 | }); |
| 211 | |
| 212 | gulp.task("coverage:report", function (done) { |
| 213 | return gulp.src( |
| 214 | ["src/**/*.js", "!test/**"], |
| 215 | { read: false } |
| 216 | ) |
| 217 | .pipe(istanbul.writeReports({ |
| 218 | reporters: ["json", "text-summary"] |
| 219 | })); |
| 220 | }); |
| 221 | |
| 222 | gulp.task("coverage:remap", function () { |
| 223 | return gulp.src("coverage/coverage-final.json") |
| 224 | .pipe(remapIstanbul({ |
| 225 | reports: { |
| 226 | "json": "coverage/coverage.json", |
| 227 | "html": "coverage/html-report" |
| 228 | } |
| 229 | })); |
| 230 | }); |
| 231 | |
| 232 | gulp.task("test-no-build", test); |
| 233 | |
| 234 | gulp.task("test:coverage", gulp.series("quick-build", "coverage:instrument", "test-no-build", "coverage:report", "coverage:remap")); |
| 235 | |
| 236 | gulp.task("watch-build-test", gulp.series("build", "test", function () { |
| 237 | return gulp.watch(sources, gulp.series("build", "test")); |
| 238 | })); |
| 239 | |
| 240 | gulp.task("package", function (callback) { |
| 241 | var command = path.join(__dirname, "node_modules", ".bin", "vsce"); |
| 242 | var args = ["package"]; |
| 243 | executeCommand(command, args, callback); |
| 244 | }); |
| 245 | |
| 246 | gulp.task("release", gulp.series("build", function () { |
| 247 | var licenseFiles = ["LICENSE.txt", "ThirdPartyNotices.txt"]; |
| 248 | var backupFolder = path.resolve(path.join(os.tmpdir(), "vscode-react-native")); |
| 249 | if (!fs.existsSync(backupFolder)) { |
| 250 | fs.mkdirSync(backupFolder); |
| 251 | } |
| 252 | |
| 253 | return Q({}) |
| 254 | .then(function () { |
| 255 | /* back up LICENSE.txt, ThirdPartyNotices.txt, README.md */ |
| 256 | console.log("Backing up license files to " + backupFolder + "..."); |
| 257 | licenseFiles.forEach(function (fileName) { |
| 258 | fs.writeFileSync(path.join(backupFolder, fileName), fs.readFileSync(fileName)); |
| 259 | }); |
| 260 | |
| 261 | /* copy over the release package license files */ |
| 262 | console.log("Preparing license files for release..."); |
| 263 | fs.writeFileSync("LICENSE.txt", fs.readFileSync("release/LICENSE.txt")); |
| 264 | fs.writeFileSync("ThirdPartyNotices.txt", fs.readFileSync("release/ThirdPartyNotices.txt")); |
| 265 | }).then(() => { |
| 266 | console.log("Creating release package..."); |
| 267 | var deferred = Q.defer(); |
| 268 | // NOTE: vsce must see npm 3.X otherwise it will not correctly strip out dev dependencies. |
| 269 | executeCommand("vsce", ["package"], function (arg) { if (arg) { deferred.reject(arg); } deferred.resolve() }, { cwd: path.resolve(__dirname) }); |
| 270 | return deferred.promise; |
| 271 | }).finally(function () { |
| 272 | /* restore backed up files */ |
| 273 | console.log("Restoring modified files..."); |
| 274 | licenseFiles.forEach(function (fileName) { |
| 275 | fs.writeFileSync(path.join(__dirname, fileName), fs.readFileSync(path.join(backupFolder, fileName))); |
| 276 | }); |
| 277 | }); |
| 278 | })); |
| 279 | |
| 280 | // Creates package.i18n.json files for all languages from {workspaceRoot}/i18n folder into project root |
| 281 | gulp.task("add-i18n", function () { |
| 282 | return gulp.src(["package.nls.json"]) |
| 283 | .pipe(nls.createAdditionalLanguageFiles(defaultLanguages, "i18n")) |
| 284 | .pipe(gulp.dest(".")); |
| 285 | }); |
| 286 | |
| 287 | // Gathers all strings to Transifex readable .xliff file for translating and pushes them to Transifex |
| 288 | gulp.task("transifex-push", gulp.series("build", function () { |
| 289 | return gulp.src(["package.nls.json", "nls.metadata.header.json","nls.metadata.json"]) |
| 290 | .pipe(nls.createXlfFiles(transifexProjectName, transifexExtensionName)) |
| 291 | .pipe(nls.pushXlfFiles(transifexApiHostname, transifexApiName, transifexApiToken)); |
| 292 | })); |
| 293 | |
| 294 | // Creates Transifex readable .xliff file and saves it locally |
| 295 | gulp.task("transifex-push-test", gulp.series("build", function() { |
| 296 | return gulp.src(["package.nls.json", "nls.metadata.header.json","nls.metadata.json"]) |
| 297 | .pipe(nls.createXlfFiles(transifexProjectName, transifexExtensionName)) |
| 298 | .pipe(gulp.dest(path.join("..", `${transifexExtensionName}-push-test`))); |
| 299 | })); |
| 300 | |
| 301 | // Gets the files with localized strings from Transifex |
| 302 | gulp.task("transifex-pull", function (done) { |
| 303 | es.merge(defaultLanguages.map(function(language) { |
| 304 | return nls.pullXlfFiles(transifexApiHostname, transifexApiName, transifexApiToken, language, [{ name: transifexExtensionName, project: transifexProjectName }]) |
| 305 | .pipe(gulp.dest(`../${transifexExtensionName}-localization/${language.folderName}`)) |
| 306 | })) |
| 307 | .pipe(es.wait(function() { |
| 308 | done(); |
| 309 | })); |
| 310 | }); |
| 311 | |
| 312 | // Imports localization from raw localized Transifex strings to VS Code .i18n.json files |
| 313 | gulp.task("i18n-import", function(done) { |
| 314 | es.merge(defaultLanguages.map(function(language) { |
| 315 | return gulp.src(`../${transifexExtensionName}-localization/${language.folderName}/**/*.xlf`) |
| 316 | .pipe(nls.prepareJsonFiles()) |
| 317 | .pipe(gulp.dest(path.join("./i18n", language.folderName))) |
| 318 | })) |
| 319 | .pipe(es.wait(function() { |
| 320 | done(); |
| 321 | })); |
| 322 | }); |
| 323 | |