microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4cd259621ddfbd348fade892a2f3ee87fd1924c5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/packageNameResolver.ts

72lines · 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 private static PackageNameRegexp: RegExp = /package="(.+?)"/;
9 private static ManifestName = "AndroidManifest.xml";
10 private static DefaultPackagePrefix = "com.";
11 private static SourceRootRelPath: string[] = ["android", "app", "src", "main"];
12 private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat(
13 PackageNameResolver.ManifestName,
14 );
15 private applicationName: string;
16
17 constructor(applicationName: string) {
18 this.applicationName = applicationName;
19 }
20
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 */
25 public resolvePackageName(projectRoot: string): Promise<string> {
26 let expectedAndroidManifestPath = path.join.apply(
27 this,
28 [projectRoot].concat(PackageNameResolver.DefaultManifestLocation),
29 );
30 return this.readPackageName(expectedAndroidManifestPath);
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 */
37 private async readPackageName(manifestPath: string): Promise<string> {
38 if (manifestPath) {
39 let fs = new FileSystem();
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);
46 }
47 return packageName;
48 } else {
49 return this.getDefaultPackageName(this.applicationName);
50 }
51 } else {
52 return this.getDefaultPackageName(this.applicationName);
53 }
54 }
55
56 /**
57 * Gets the default package name, based on the application name.
58 */
59 private getDefaultPackageName(applicationName: string): string {
60 return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
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
69 let match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
70 return match ? match[1] : null;
71 }
72}
73