microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/packageNameResolver.ts

70lines · 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 * as path from "path";
5import { FileSystem } from "../../common/node/fileSystem";
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 const 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 const 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 }
49 return this.getDefaultPackageName(this.applicationName);
50 }
51 return this.getDefaultPackageName(this.applicationName);
52 }
53
54 /**
55 * Gets the default package name, based on the application name.
56 */
57 private getDefaultPackageName(applicationName: string): string {
58 return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
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
67 const match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
68 return match ? match[1] : null;
69 }
70}
71