microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/android/packageNameResolver.ts
72lines · 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 | import {FileSystem} from "../../common/node/fileSystem"; |
| 5 | import Q = require("q"); |
| 6 | import * as path from "path"; |
| 7 | |
| 8 | export class PackageNameResolver { |
| 9 | |
| 10 | private static PackageNameRegexp: RegExp = /package="(.+?)"/; |
| 11 | private static ManifestName = "AndroidManifest.xml"; |
| 12 | private static DefaultPackagePrefix = "com."; |
| 13 | private static SourceRootRelPath: string[] = ["android", "app", "src", "main"]; |
| 14 | private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat(PackageNameResolver.ManifestName); |
| 15 | private applicationName: string; |
| 16 | |
| 17 | constructor(applicationName: string) { |
| 18 | this.applicationName = applicationName; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Tries to find the package name in AndroidManifest.xml. If not found, it returns the default package name, |
| 23 | * which is the application name prefixed with the default prefix. |
| 24 | */ |
| 25 | public resolvePackageName(projectRoot: string): Q.Promise<string> { |
| 26 | let expectedAndroidManifestPath = path.join.apply(this, [projectRoot].concat(PackageNameResolver.DefaultManifestLocation)); |
| 27 | return this.readPackageName(expectedAndroidManifestPath); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Given a manifest file path, it parses the file and returns the package name. |
| 32 | * If the package name cannot be parsed, the default packge name is returned. |
| 33 | */ |
| 34 | private readPackageName(manifestPath: string): Q.Promise<string> { |
| 35 | if (manifestPath) { |
| 36 | let fs = new FileSystem(); |
| 37 | return fs.exists(manifestPath).then(exists => { |
| 38 | if (exists) { |
| 39 | return fs.readFile(manifestPath) |
| 40 | .then(manifestContent => { |
| 41 | let packageName = this.parsePackageName(manifestContent); |
| 42 | if (!packageName) { |
| 43 | packageName = this.getDefaultPackageName(this.applicationName); |
| 44 | } |
| 45 | return packageName; |
| 46 | }); |
| 47 | } else { |
| 48 | return this.getDefaultPackageName(this.applicationName); |
| 49 | } |
| 50 | }); |
| 51 | } else { |
| 52 | return Q.resolve(this.getDefaultPackageName(this.applicationName)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Gets the default package name, based on the application name. |
| 58 | */ |
| 59 | private getDefaultPackageName(applicationName: string): string { |
| 60 | return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Parses the application package name from the contents of an Android manifest file. |
| 65 | * If a match was found, it is returned. Otherwise null is returned. |
| 66 | */ |
| 67 | private parsePackageName(manifestContents: string) { |
| 68 | // first we remove all the comments from the file |
| 69 | let match = manifestContents.match(PackageNameResolver.PackageNameRegexp); |
| 70 | return match ? match[1] : null; |
| 71 | } |
| 72 | } |