microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
gulpfile.js
307lines · 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 | |
74dba829Yuri Skorokhodov7 years ago | 4 | const gulp = require("gulp"); |
5fd423caYuri Skorokhodov7 years ago | 5 | const log = require("fancy-log"); |
| 6 | const istanbul = require("gulp-istanbul"); | |
| 7 | const isparta = require("isparta"); | |
74dba829Yuri Skorokhodov7 years ago | 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"); | |
5fd423caYuri Skorokhodov7 years ago | 18 | const es = require("event-stream"); |
| 19 | const remapIstanbul = require("remap-istanbul/lib/gulpRemapIstanbul"); | |
| 20 | const nls = require("vscode-nls-dev"); | |
f6ac01ceRuslan Bikkinin7 years ago | 21 | const libtslint = require("tslint"); |
| 22 | const tslint = require("gulp-tslint"); | |
8ba55f4cJimmy Thomson10 years ago | 23 | |
74dba829Yuri Skorokhodov7 years ago | 24 | const copyright = GulpExtras.checkCopyright; |
| 25 | const imports = GulpExtras.checkImports; | |
| 26 | const executeCommand = GulpExtras.executeCommand; | |
| 27 | | |
b6c68212Yuri Skorokhodov7 years ago | 28 | const translationProjectName = "vscode-extensions"; |
| 29 | const translationExtensionName = "vscode-react-native"; | |
74dba829Yuri Skorokhodov7 years ago | 30 | const defaultLanguages = [ |
f6ac01ceRuslan Bikkinin7 years ago | 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" }, | |
5fd423caYuri Skorokhodov7 years ago | 39 | { id: "it", folderName: "ita" }, |
74dba829Yuri Skorokhodov7 years ago | 40 | |
| 41 | // These language-pack languages are included for VS but excluded from the vscode package | |
5fd423caYuri Skorokhodov7 years ago | 42 | { id: "cs", folderName: "csy" }, |
| 43 | { id: "tr", folderName: "trk" }, | |
d5de836bYuri Skorokhodov7 years ago | 44 | { id: "pt-br", folderName: "ptb", transifexId: "pt-BR" }, |
5fd423caYuri Skorokhodov7 years ago | 45 | { id: "pl", folderName: "plk" } |
74dba829Yuri Skorokhodov7 years ago | 46 | ]; |
936ae21cmax-mironov8 years ago | 47 | |
f6ac01ceRuslan Bikkinin7 years ago | 48 | const srcPath = "src"; |
| 49 | const testPath = "test"; | |
89973b41dlebu10 years ago | 50 | |
f6ac01ceRuslan Bikkinin7 years ago | 51 | const sources = [srcPath, testPath].map((tsFolder) => tsFolder + "/**/*.ts"); |
8ba55f4cJimmy Thomson10 years ago | 52 | |
f6ac01ceRuslan Bikkinin7 years ago | 53 | const knownOptions = { |
62b110e3Dmitry Zinovyev9 years ago | 54 | string: "env", |
| 55 | default: { env: "production" } | |
572cd3badigeff10 years ago | 56 | }; |
| 57 | | |
f6ac01ceRuslan Bikkinin7 years ago | 58 | const options = minimist(process.argv.slice(2), knownOptions); |
3aa3017bDmitry Zinovyev9 years ago | 59 | |
f6ac01ceRuslan Bikkinin7 years ago | 60 | let lintSources = [srcPath, testPath].map((tsFolder) => tsFolder + "/**/*.ts"); |
| 61 | lintSources = lintSources.concat([ | |
| 62 | "!src/typings/**", | |
f2568005Ruslan Bikkinin7 years ago | 63 | "!test/resources/sampleReactNative022Project/**", |
| 64 | "!test/smoke/**", | |
| 65 | "!/SmokeTestLogs/**" | |
f6ac01ceRuslan Bikkinin7 years ago | 66 | ]); |
5fd423caYuri Skorokhodov7 years ago | 67 | |
| 68 | function build(failOnError, buildNls) { | |
f6ac01ceRuslan Bikkinin7 years ago | 69 | const tsProject = ts.createProject("tsconfig.json"); |
| 70 | const isProd = options.env === "production"; | |
| 71 | const preprocessorContext = isProd ? { PROD: true } : { DEBUG: true }; | |
74dba829Yuri Skorokhodov7 years ago | 72 | let gotError = false; |
572cd3badigeff10 years ago | 73 | log(`Building with preprocessor context: ${JSON.stringify(preprocessorContext)}`); |
f6ac01ceRuslan Bikkinin7 years ago | 74 | const tsResult = tsProject.src() |
9c1d0f41digeff10 years ago | 75 | .pipe(preprocess({ context: preprocessorContext })) //To set environment variables in-line |
8ba55f4cJimmy Thomson10 years ago | 76 | .pipe(sourcemaps.init()) |
5fd423caYuri Skorokhodov7 years ago | 77 | .pipe(tsProject()); |
74dba829Yuri Skorokhodov7 years ago | 78 | |
f6ac01ceRuslan Bikkinin7 years ago | 79 | return tsResult.js |
74dba829Yuri Skorokhodov7 years ago | 80 | .pipe(buildNls ? nls.rewriteLocalizeCalls() : es.through()) |
5fd423caYuri Skorokhodov7 years ago | 81 | .pipe(buildNls ? nls.createAdditionalLanguageFiles(defaultLanguages, "i18n", ".") : es.through()) |
1287ea58Yuri Skorokhodov7 years ago | 82 | .pipe(buildNls ? nls.bundleMetaDataFiles("msjsdiag.vscode-react-native", ".") : es.through()) |
f6ac01ceRuslan Bikkinin7 years ago | 83 | .pipe(buildNls ? nls.bundleLanguageFiles() : es.through()) |
5fd423caYuri Skorokhodov7 years ago | 84 | .pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "." })) |
f6ac01ceRuslan Bikkinin7 years ago | 85 | .pipe(gulp.dest((file) => file.cwd)) |
5fd423caYuri Skorokhodov7 years ago | 86 | .once("error", () => { |
74dba829Yuri Skorokhodov7 years ago | 87 | gotError = true; |
| 88 | }) | |
5fd423caYuri Skorokhodov7 years ago | 89 | .once("finish", () => { |
74dba829Yuri Skorokhodov7 years ago | 90 | if (failOnError && gotError) { |
| 91 | process.exit(1); | |
| 92 | } | |
| 93 | }); | |
3aa3017bDmitry Zinovyev9 years ago | 94 | } |
8ba55f4cJimmy Thomson10 years ago | 95 | |
f6ac01ceRuslan Bikkinin7 years ago | 96 | function test() { |
| 97 | // Check if arguments were passed | |
| 98 | if (options.pattern) { | |
| 99 | log(`\nTesting cases that match pattern: ${options.pattern}`); | |
| 100 | } else { | |
e586af40Yuri Skorokhodov7 years ago | 101 | log("\nTesting cases that don't match pattern: extensionContext|localizationContext"); |
f6ac01ceRuslan Bikkinin7 years ago | 102 | } |
| 103 | | |
e586af40Yuri Skorokhodov7 years ago | 104 | const testResultsPath = path.join(__dirname, "test", "DebuggerTests.xml"); |
| 105 | process.env.MOCHA_FILE = testResultsPath; | |
f6ac01ceRuslan Bikkinin7 years ago | 106 | return gulp.src(["test/**/*.test.js", "!test/extension/**"]) |
| 107 | .pipe(mocha({ | |
| 108 | ui: "tdd", | |
| 109 | useColors: true, | |
| 110 | invert: !options.pattern, | |
e586af40Yuri Skorokhodov7 years ago | 111 | grep: options.pattern || "(extensionContext|localizationContext)", |
| 112 | reporter: "mocha-multi-reporters", | |
| 113 | reporterOptions: { | |
| 114 | configFile: path.resolve("test/mochaReporterConfig.json"), | |
| 115 | }, | |
f6ac01ceRuslan Bikkinin7 years ago | 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/**", | |
f2568005Ruslan Bikkinin7 years ago | 134 | "!test/resources/sampleReactNative022Project/**/*.js", |
| 135 | "!test/smoke/node_modules/**", | |
| 136 | "!test/smoke/resources/**" | |
f6ac01ceRuslan Bikkinin7 years ago | 137 | ]) |
| 138 | .pipe(copyright()); | |
| 139 | }); | |
| 140 | | |
| 141 | gulp.task("tslint", () => { | |
| 142 | const program = libtslint.Linter.createProgram("./tsconfig.json"); | |
fc602bb6Yuri Skorokhodov7 years ago | 143 | return gulp.src(lintSources, { base: "." }) |
| 144 | .pipe(tslint({ | |
| 145 | formatter: "verbose", | |
| 146 | program: program | |
| 147 | })) | |
| 148 | .pipe(tslint.report()); | |
| 149 | }); | |
| 150 | | |
5fd423caYuri Skorokhodov7 years ago | 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) | |
f6ac01ceRuslan Bikkinin7 years ago | 154 | gulp.task("build", gulp.series("check-imports", "check-copyright", "tslint", function runBuild(done) { |
e416b901Yuri Skorokhodov7 years ago | 155 | build(true, true) |
f6ac01ceRuslan Bikkinin7 years ago | 156 | .once("finish", () => { |
| 157 | done(); | |
| 158 | }); | |
5fd423caYuri Skorokhodov7 years ago | 159 | })); |
| 160 | | |
f6ac01ceRuslan Bikkinin7 years ago | 161 | gulp.task("build-dev", gulp.series("check-imports", "check-copyright", function runBuild(done) { |
e416b901Yuri Skorokhodov7 years ago | 162 | build(false, false) |
f6ac01ceRuslan Bikkinin7 years ago | 163 | .once("finish", () => { |
| 164 | done(); | |
| 165 | }); | |
5fd423caYuri Skorokhodov7 years ago | 166 | })); |
| 167 | | |
fc602bb6Yuri Skorokhodov7 years ago | 168 | gulp.task("quick-build", gulp.series("build-dev")); |
5fd423caYuri Skorokhodov7 years ago | 169 | |
f6ac01ceRuslan Bikkinin7 years ago | 170 | gulp.task("watch", gulp.series("build", function runWatch() { |
62b110e3Dmitry Zinovyev9 years ago | 171 | log("Watching build sources..."); |
5fd423caYuri Skorokhodov7 years ago | 172 | return gulp.watch(sources, gulp.series("build")); |
| 173 | })); | |
8ba55f4cJimmy Thomson10 years ago | 174 | |
f6ac01ceRuslan Bikkinin7 years ago | 175 | gulp.task("clean", () => { |
| 176 | const del = require("del"); | |
| 177 | const pathsToDelete = [ | |
5fd423caYuri Skorokhodov7 years ago | 178 | "src/**/*.js", |
| 179 | "src/**/*.js.map", | |
| 180 | "test/**/*.js", | |
| 181 | "test/**/*.js.map", | |
| 182 | "out/", | |
| 183 | "!test/resources/sampleReactNative022Project/**/*.js", | |
| 184 | ".vscode-test/", | |
fc602bb6Yuri Skorokhodov7 years ago | 185 | "nls.*.json", |
49798d4dRedMickey6 years ago | 186 | "!test/smoke/**/*.js", |
| 187 | "!test/smoke/**/*.js.map", | |
5fd423caYuri Skorokhodov7 years ago | 188 | ] |
| 189 | return del(pathsToDelete, { force: true }); | |
3fb37ad5unknown10 years ago | 190 | }); |
8ba55f4cJimmy Thomson10 years ago | 191 | |
fc602bb6Yuri Skorokhodov7 years ago | 192 | gulp.task("default", gulp.series("clean", "build")); |
5fd423caYuri Skorokhodov7 years ago | 193 | |
| 194 | gulp.task("test", gulp.series("build", "tslint", test)); | |
28d84585Vladimir Kotikov8 years ago | 195 | |
f6ac01ceRuslan Bikkinin7 years ago | 196 | gulp.task("coverage:instrument", () => { |
0a283547Anna Kocheshkova8 years ago | 197 | return gulp.src(["src/**/*.js", "!test/**"]) |
28d84585Vladimir Kotikov8 years ago | 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 | | |
f6ac01ceRuslan Bikkinin7 years ago | 207 | gulp.task("coverage:report", () => { |
28d84585Vladimir Kotikov8 years ago | 208 | return gulp.src( |
0a283547Anna Kocheshkova8 years ago | 209 | ["src/**/*.js", "!test/**"], |
28d84585Vladimir Kotikov8 years ago | 210 | { read: false } |
| 211 | ) | |
f6ac01ceRuslan Bikkinin7 years ago | 212 | .pipe(istanbul.writeReports({ |
| 213 | reporters: ["json", "text-summary"] | |
| 214 | })); | |
28d84585Vladimir Kotikov8 years ago | 215 | }); |
| 216 | | |
f6ac01ceRuslan Bikkinin7 years ago | 217 | gulp.task("coverage:remap", () => { |
5fd423caYuri Skorokhodov7 years ago | 218 | return gulp.src("coverage/coverage-final.json") |
28d84585Vladimir Kotikov8 years ago | 219 | .pipe(remapIstanbul({ |
| 220 | reports: { | |
5fd423caYuri Skorokhodov7 years ago | 221 | "json": "coverage/coverage.json", |
| 222 | "html": "coverage/html-report" | |
28d84585Vladimir Kotikov8 years ago | 223 | } |
| 224 | })); | |
| 225 | }); | |
| 226 | | |
62b110e3Dmitry Zinovyev9 years ago | 227 | gulp.task("test-no-build", test); |
8ba55f4cJimmy Thomson10 years ago | 228 | |
5fd423caYuri Skorokhodov7 years ago | 229 | gulp.task("test:coverage", gulp.series("quick-build", "coverage:instrument", "test-no-build", "coverage:report", "coverage:remap")); |
89973b41dlebu10 years ago | 230 | |
f6ac01ceRuslan Bikkinin7 years ago | 231 | gulp.task("watch-build-test", gulp.series("build", "test", function runWatch() { |
5fd423caYuri Skorokhodov7 years ago | 232 | return gulp.watch(sources, gulp.series("build", "test")); |
| 233 | })); | |
7212311dDaniel Lebu10 years ago | 234 | |
f6ac01ceRuslan Bikkinin7 years ago | 235 | gulp.task("package", (callback) => { |
| 236 | const command = path.join(__dirname, "node_modules", ".bin", "vsce"); | |
| 237 | const args = ["package"]; | |
7212311dDaniel Lebu10 years ago | 238 | executeCommand(command, args, callback); |
92f13422Jimmy Thomson9 years ago | 239 | }); |
| 240 | | |
f6ac01ceRuslan Bikkinin7 years ago | 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")); | |
92f13422Jimmy Thomson9 years ago | 244 | if (!fs.existsSync(backupFolder)) { |
| 245 | fs.mkdirSync(backupFolder); | |
| 246 | } | |
| 247 | | |
| 248 | return Q({}) | |
f6ac01ceRuslan Bikkinin7 years ago | 249 | .then(() => { |
92f13422Jimmy Thomson9 years ago | 250 | /* back up LICENSE.txt, ThirdPartyNotices.txt, README.md */ |
f6ac01ceRuslan Bikkinin7 years ago | 251 | log("Backing up license files to " + backupFolder + "..."); |
| 252 | licenseFiles.forEach((fileName) => { | |
92f13422Jimmy Thomson9 years ago | 253 | fs.writeFileSync(path.join(backupFolder, fileName), fs.readFileSync(fileName)); |
| 254 | }); | |
| 255 | | |
| 256 | /* copy over the release package license files */ | |
f6ac01ceRuslan Bikkinin7 years ago | 257 | log("Preparing license files for release..."); |
62b110e3Dmitry Zinovyev9 years ago | 258 | fs.writeFileSync("LICENSE.txt", fs.readFileSync("release/LICENSE.txt")); |
| 259 | fs.writeFileSync("ThirdPartyNotices.txt", fs.readFileSync("release/ThirdPartyNotices.txt")); | |
3aa3017bDmitry Zinovyev9 years ago | 260 | }).then(() => { |
f6ac01ceRuslan Bikkinin7 years ago | 261 | log("Creating release package..."); |
92f13422Jimmy Thomson9 years ago | 262 | var deferred = Q.defer(); |
| 263 | // NOTE: vsce must see npm 3.X otherwise it will not correctly strip out dev dependencies. | |
f6ac01ceRuslan Bikkinin7 years ago | 264 | executeCommand("vsce", ["package"], (arg) => { if (arg) { deferred.reject(arg); } deferred.resolve() }, { cwd: path.resolve(__dirname) }); |
92f13422Jimmy Thomson9 years ago | 265 | return deferred.promise; |
f6ac01ceRuslan Bikkinin7 years ago | 266 | }).finally(() => { |
92f13422Jimmy Thomson9 years ago | 267 | /* restore backed up files */ |
f6ac01ceRuslan Bikkinin7 years ago | 268 | log("Restoring modified files..."); |
| 269 | licenseFiles.forEach((fileName) => { | |
92f13422Jimmy Thomson9 years ago | 270 | fs.writeFileSync(path.join(__dirname, fileName), fs.readFileSync(path.join(backupFolder, fileName))); |
| 271 | }); | |
| 272 | }); | |
5fd423caYuri Skorokhodov7 years ago | 273 | })); |
74dba829Yuri Skorokhodov7 years ago | 274 | |
fc602bb6Yuri Skorokhodov7 years ago | 275 | // Creates package.i18n.json files for all languages from {workspaceRoot}/i18n folder into project root |
f6ac01ceRuslan Bikkinin7 years ago | 276 | gulp.task("add-i18n", () => { |
5fd423caYuri Skorokhodov7 years ago | 277 | return gulp.src(["package.nls.json"]) |
| 278 | .pipe(nls.createAdditionalLanguageFiles(defaultLanguages, "i18n")) | |
f6ac01ceRuslan Bikkinin7 years ago | 279 | .pipe(gulp.dest(".")) |
74dba829Yuri Skorokhodov7 years ago | 280 | }); |
| 281 | | |
b6c68212Yuri Skorokhodov7 years ago | 282 | // Creates MLCP readable .xliff file and saves it locally |
| 283 | gulp.task("translations-export", gulp.series("build", function runTranslationExport() { | |
f6ac01ceRuslan Bikkinin7 years ago | 284 | return gulp.src(["package.nls.json", "nls.metadata.header.json", "nls.metadata.json"]) |
b6c68212Yuri Skorokhodov7 years ago | 285 | .pipe(nls.createXlfFiles(translationProjectName, translationExtensionName)) |
| 286 | .pipe(gulp.dest(path.join("..", `${translationProjectName}-localization-export`))); | |
5fd423caYuri Skorokhodov7 years ago | 287 | })); |
74dba829Yuri Skorokhodov7 years ago | 288 | |
b6c68212Yuri Skorokhodov7 years ago | 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 | }); | |
f6ac01ceRuslan Bikkinin7 years ago | 297 | es.merge(defaultLanguages.map((language) => { |
b6c68212Yuri Skorokhodov7 years ago | 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`)) | |
74dba829Yuri Skorokhodov7 years ago | 301 | .pipe(nls.prepareJsonFiles()) |
b6c68212Yuri Skorokhodov7 years ago | 302 | .pipe(gulp.dest(path.join("./i18n", language.folderName))); |
4dbbdaadYuri Skorokhodov7 years ago | 303 | })) |
f6ac01ceRuslan Bikkinin7 years ago | 304 | .pipe(es.wait(() => { |
| 305 | done(); | |
| 306 | })); | |
74dba829Yuri Skorokhodov7 years ago | 307 | }); |