microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/packageNameResolver.ts
72lines · 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 Q = require("q"); | |
| 6 | import * as path from "path"; | |
| 7 | | |
| 8 | export class PackageNameResolver { | |
| 9 | | |
2010d4e2dlebu10 years ago | 10 | private static PackageNameRegexp: RegExp = /package="(.+?)"/; |
8953be57dlebu10 years ago | 11 | private static ManifestName = "AndroidManifest.xml"; |
| 12 | private static DefaultPackagePrefix = "com."; | |
2010d4e2dlebu10 years ago | 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 | } | |
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 | */ | |
2010d4e2dlebu10 years ago | 25 | public resolvePackageName(projectRoot: string): Q.Promise<string> { |
8953be57dlebu10 years ago | 26 | let expectedAndroidManifestPath = path.join.apply(this, [projectRoot].concat(PackageNameResolver.DefaultManifestLocation)); |
2010d4e2dlebu10 years ago | 27 | return this.readPackageName(expectedAndroidManifestPath); |
8953be57dlebu10 years ago | 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 | */ | |
2010d4e2dlebu10 years ago | 34 | private readPackageName(manifestPath: string): Q.Promise<string> { |
8953be57dlebu10 years ago | 35 | if (manifestPath) { |
| 36 | let fs = new FileSystem(); | |
| 37 | return fs.exists(manifestPath).then(exists => { | |
| 38 | if (exists) { | |
| 39 | return fs.readFile(manifestPath) | |
681bbf38dlebu10 years ago | 40 | .then(manifestContent => { |
| 41 | let packageName = this.parsePackageName(manifestContent); | |
8953be57dlebu10 years ago | 42 | if (!packageName) { |
2010d4e2dlebu10 years ago | 43 | packageName = this.getDefaultPackageName(this.applicationName); |
8953be57dlebu10 years ago | 44 | } |
| 45 | return packageName; | |
| 46 | }); | |
| 47 | } else { | |
2010d4e2dlebu10 years ago | 48 | return this.getDefaultPackageName(this.applicationName); |
8953be57dlebu10 years ago | 49 | } |
| 50 | }); | |
| 51 | } else { | |
2010d4e2dlebu10 years ago | 52 | return Q.resolve(this.getDefaultPackageName(this.applicationName)); |
8953be57dlebu10 years ago | 53 | } |
| 54 | } | |
| 55 | | |
00eab8a8dlebu10 years ago | 56 | /** |
bc6696cbdigeff10 years ago | 57 | * Gets the default package name, based on the application name. |
00eab8a8dlebu10 years ago | 58 | */ |
8953be57dlebu10 years ago | 59 | private getDefaultPackageName(applicationName: string): string { |
c7d7ba55digeff10 years ago | 60 | return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase(); |
8953be57dlebu10 years ago | 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 | |
2010d4e2dlebu10 years ago | 69 | let match = manifestContents.match(PackageNameResolver.PackageNameRegexp); |
8953be57dlebu10 years ago | 70 | return match ? match[1] : null; |
| 71 | } | |
| 72 | } |