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