microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/packageNameResolver.ts
70lines · 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="(.+?)"/; |
8953be57dlebu10 years ago | 9 | private static ManifestName = "AndroidManifest.xml"; |
| 10 | private static DefaultPackagePrefix = "com."; | |
2010d4e2dlebu10 years ago | 11 | private static SourceRootRelPath: string[] = ["android", "app", "src", "main"]; |
34472878RedMickey5 years ago | 12 | private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat( |
| 13 | PackageNameResolver.ManifestName, | |
| 14 | ); | |
2010d4e2dlebu10 years ago | 15 | private applicationName: string; |
| 16 | | |
| 17 | constructor(applicationName: string) { | |
| 18 | this.applicationName = applicationName; | |
| 19 | } | |
8953be57dlebu10 years ago | 20 | |
00eab8a8dlebu10 years ago | 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 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 25 | public resolvePackageName(projectRoot: string): Promise<string> { |
09f6024fHeniker4 years ago | 26 | const expectedAndroidManifestPath = path.join.apply( |
34472878RedMickey5 years ago | 27 | this, |
| 28 | [projectRoot].concat(PackageNameResolver.DefaultManifestLocation), | |
| 29 | ); | |
2010d4e2dlebu10 years ago | 30 | return this.readPackageName(expectedAndroidManifestPath); |
8953be57dlebu10 years ago | 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 | */ | |
0d77292aJiglioNero4 years ago | 37 | private async readPackageName(manifestPath: string): Promise<string> { |
8953be57dlebu10 years ago | 38 | if (manifestPath) { |
09f6024fHeniker4 years ago | 39 | const fs = new FileSystem(); |
0d77292aJiglioNero4 years ago | 40 | const exists = await fs.exists(manifestPath); |
| 41 | if (exists) { | |
| 42 | const manifestContent = await fs.readFile(manifestPath); | |
| 43 | let packageName = this.parsePackageName(manifestContent.toString()); | |
| 44 | if (!packageName) { | |
| 45 | packageName = this.getDefaultPackageName(this.applicationName); | |
8953be57dlebu10 years ago | 46 | } |
0d77292aJiglioNero4 years ago | 47 | return packageName; |
| 48 | } | |
| 49 | return this.getDefaultPackageName(this.applicationName); | |
8953be57dlebu10 years ago | 50 | } |
09f6024fHeniker4 years ago | 51 | return this.getDefaultPackageName(this.applicationName); |
8953be57dlebu10 years ago | 52 | } |
| 53 | | |
00eab8a8dlebu10 years ago | 54 | /** |
bc6696cbdigeff10 years ago | 55 | * Gets the default package name, based on the application name. |
00eab8a8dlebu10 years ago | 56 | */ |
8953be57dlebu10 years ago | 57 | private getDefaultPackageName(applicationName: string): string { |
c7d7ba55digeff10 years ago | 58 | return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase(); |
8953be57dlebu10 years ago | 59 | } |
| 60 | | |
| 61 | /** | |
| 62 | * Parses the application package name from the contents of an Android manifest file. | |
| 63 | * If a match was found, it is returned. Otherwise null is returned. | |
| 64 | */ | |
| 65 | private parsePackageName(manifestContents: string) { | |
| 66 | // first we remove all the comments from the file | |
09f6024fHeniker4 years ago | 67 | const match = manifestContents.match(PackageNameResolver.PackageNameRegexp); |
8953be57dlebu10 years ago | 68 | return match ? match[1] : null; |
| 69 | } | |
34472878RedMickey5 years ago | 70 | } |