microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/android/packageNameResolver.ts

71lines · 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
4import {FileSystem} from "../../common/node/fileSystem";
5import * as path from "path";
6
7export class PackageNameResolver {
8
9 private static PackageNameRegexp: RegExp = /package="(.+?)"/;
10 private static ManifestName = "AndroidManifest.xml";
11 private static DefaultPackagePrefix = "com.";
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 }
19
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 */
24 public resolvePackageName(projectRoot: string): Promise<string> {
25 let expectedAndroidManifestPath = path.join.apply(this, [projectRoot].concat(PackageNameResolver.DefaultManifestLocation));
26 return this.readPackageName(expectedAndroidManifestPath);
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 */
33 private readPackageName(manifestPath: string): Promise<string> {
34 if (manifestPath) {
35 let fs = new FileSystem();
36 return fs.exists(manifestPath).then(exists => {
37 if (exists) {
38 return fs.readFile(manifestPath)
39 .then(manifestContent => {
40 let packageName = this.parsePackageName(manifestContent.toString());
41 if (!packageName) {
42 packageName = this.getDefaultPackageName(this.applicationName);
43 }
44 return packageName;
45 });
46 } else {
47 return this.getDefaultPackageName(this.applicationName);
48 }
49 });
50 } else {
51 return Promise.resolve(this.getDefaultPackageName(this.applicationName));
52 }
53 }
54
55 /**
56 * Gets the default package name, based on the application name.
57 */
58 private getDefaultPackageName(applicationName: string): string {
59 return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
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
68 let match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
69 return match ? match[1] : null;
70 }
71}