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