microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/packageNameResolver.ts
91lines · modeblame
8953be57dlebu10 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. | |
| 3 | | |
| 4 | import * as path from "path"; | |
09f6024fHeniker4 years ago | 5 | import { FileSystem } from "../../common/node/fileSystem"; |
8953be57dlebu10 years ago | 6 | |
| 7 | export class PackageNameResolver { | |
2010d4e2dlebu10 years ago | 8 | private static PackageNameRegexp: RegExp = /package="(.+?)"/; |
5ef47d7dHarshvardhan Joshi5 months ago | 9 | private static ApplicationIdRegexp: RegExp = /applicationId\s+(=)?\s*["'](.+?)["']/; |
8953be57dlebu10 years ago | 10 | private static ManifestName = "AndroidManifest.xml"; |
5ef47d7dHarshvardhan Joshi5 months ago | 11 | private static GradleBuildName = "build.gradle"; |
8953be57dlebu10 years ago | 12 | private static DefaultPackagePrefix = "com."; |
2010d4e2dlebu10 years ago | 13 | private static SourceRootRelPath: string[] = ["android", "app", "src", "main"]; |
34472878RedMickey5 years ago | 14 | private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat( |
| 15 | PackageNameResolver.ManifestName, | |
| 16 | ); | |
5ef47d7dHarshvardhan Joshi5 months ago | 17 | private static DefaultGradleBuildLocation: string[] = [ |
| 18 | "android", | |
| 19 | "app", | |
| 20 | PackageNameResolver.GradleBuildName, | |
| 21 | ]; | |
2010d4e2dlebu10 years ago | 22 | private applicationName: string; |
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago | 23 | private fileSystem: FileSystem; |
2010d4e2dlebu10 years ago | 24 | |
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago | 25 | constructor(applicationName: string, fileSystem?: FileSystem) { |
2010d4e2dlebu10 years ago | 26 | this.applicationName = applicationName; |
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago | 27 | this.fileSystem = fileSystem || new FileSystem(); |
2010d4e2dlebu10 years ago | 28 | } |
8953be57dlebu10 years ago | 29 | |
00eab8a8dlebu10 years ago | 30 | /** |
| 31 | * Tries to find the package name in AndroidManifest.xml. If not found, it returns the default package name, | |
| 32 | * which is the application name prefixed with the default prefix. | |
| 33 | */ | |
5ef47d7dHarshvardhan Joshi5 months ago | 34 | public async resolvePackageName(projectRoot: string): Promise<string> { |
| 35 | const expectedGradleBuildPath = path.join.apply( | |
| 36 | this, | |
| 37 | [projectRoot].concat(PackageNameResolver.DefaultGradleBuildLocation), | |
| 38 | ); | |
| 39 | const gradlePackageName = await this.readApplicationId(expectedGradleBuildPath); | |
| 40 | if (gradlePackageName) { | |
| 41 | return gradlePackageName; | |
| 42 | } | |
| 43 | | |
09f6024fHeniker4 years ago | 44 | const expectedAndroidManifestPath = path.join.apply( |
34472878RedMickey5 years ago | 45 | this, |
| 46 | [projectRoot].concat(PackageNameResolver.DefaultManifestLocation), | |
| 47 | ); | |
2010d4e2dlebu10 years ago | 48 | return this.readPackageName(expectedAndroidManifestPath); |
8953be57dlebu10 years ago | 49 | } |
| 50 | | |
5ef47d7dHarshvardhan Joshi5 months ago | 51 | private async readApplicationId(gradlePath: string): Promise<string | null> { |
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago | 52 | if (!(await this.fileSystem.exists(gradlePath))) { |
| 53 | return null; | |
5ef47d7dHarshvardhan Joshi5 months ago | 54 | } |
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago | 55 | |
| 56 | const content = await this.fileSystem.readFile(gradlePath); | |
| 57 | const match = content.toString().match(PackageNameResolver.ApplicationIdRegexp); | |
| 58 | return match ? match[2] : null; | |
5ef47d7dHarshvardhan Joshi5 months ago | 59 | } |
| 60 | | |
8953be57dlebu10 years ago | 61 | /** |
| 62 | * Given a manifest file path, it parses the file and returns the package name. | |
| 63 | * If the package name cannot be parsed, the default packge name is returned. | |
| 64 | */ | |
0d77292aJiglioNero4 years ago | 65 | private async readPackageName(manifestPath: string): Promise<string> { |
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago | 66 | if (!(await this.fileSystem.exists(manifestPath))) { |
0d77292aJiglioNero4 years ago | 67 | return this.getDefaultPackageName(this.applicationName); |
8953be57dlebu10 years ago | 68 | } |
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago | 69 | |
| 70 | const manifestContent = await this.fileSystem.readFile(manifestPath); | |
| 71 | const packageName = this.parsePackageName(manifestContent.toString()); | |
| 72 | return packageName || this.getDefaultPackageName(this.applicationName); | |
8953be57dlebu10 years ago | 73 | } |
| 74 | | |
00eab8a8dlebu10 years ago | 75 | /** |
bc6696cbdigeff10 years ago | 76 | * Gets the default package name, based on the application name. |
00eab8a8dlebu10 years ago | 77 | */ |
8953be57dlebu10 years ago | 78 | private getDefaultPackageName(applicationName: string): string { |
c7d7ba55digeff10 years ago | 79 | return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase(); |
8953be57dlebu10 years ago | 80 | } |
| 81 | | |
| 82 | /** | |
| 83 | * Parses the application package name from the contents of an Android manifest file. | |
| 84 | * If a match was found, it is returned. Otherwise null is returned. | |
| 85 | */ | |
| 86 | private parsePackageName(manifestContents: string) { | |
| 87 | // first we remove all the comments from the file | |
09f6024fHeniker4 years ago | 88 | const match = manifestContents.match(PackageNameResolver.PackageNameRegexp); |
8953be57dlebu10 years ago | 89 | return match ? match[1] : null; |
| 90 | } | |
34472878RedMickey5 years ago | 91 | } |