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