microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/v-peq/add-network-inspector-server-tests

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/packageNameResolver.ts

91lines · 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 ApplicationIdRegexp: RegExp = /applicationId\s+(=)?\s*["'](.+?)["']/;
10 private static ManifestName = "AndroidManifest.xml";
11 private static GradleBuildName = "build.gradle";
12 private static DefaultPackagePrefix = "com.";
13 private static SourceRootRelPath: string[] = ["android", "app", "src", "main"];
14 private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat(
15 PackageNameResolver.ManifestName,
16 );
17 private static DefaultGradleBuildLocation: string[] = [
18 "android",
19 "app",
20 PackageNameResolver.GradleBuildName,
21 ];
22 private applicationName: string;
23 private fileSystem: FileSystem;
24
25 constructor(applicationName: string, fileSystem?: FileSystem) {
26 this.applicationName = applicationName;
27 this.fileSystem = fileSystem || new FileSystem();
28 }
29
30 /**
31 * Tries to find the package name in AndroidManifest.xml. If not found, it returns the default package name,
32 * which is the application name prefixed with the default prefix.
33 */
34 public async resolvePackageName(projectRoot: string): Promise<string> {
35 const expectedGradleBuildPath = path.join.apply(
36 this,
37 [projectRoot].concat(PackageNameResolver.DefaultGradleBuildLocation),
38 );
39 const gradlePackageName = await this.readApplicationId(expectedGradleBuildPath);
40 if (gradlePackageName) {
41 return gradlePackageName;
42 }
43
44 const expectedAndroidManifestPath = path.join.apply(
45 this,
46 [projectRoot].concat(PackageNameResolver.DefaultManifestLocation),
47 );
48 return this.readPackageName(expectedAndroidManifestPath);
49 }
50
51 private async readApplicationId(gradlePath: string): Promise<string | null> {
52 if (!(await this.fileSystem.exists(gradlePath))) {
53 return null;
54 }
55
56 const content = await this.fileSystem.readFile(gradlePath);
57 const match = content.toString().match(PackageNameResolver.ApplicationIdRegexp);
58 return match ? match[2] : null;
59 }
60
61 /**
62 * Given a manifest file path, it parses the file and returns the package name.
63 * If the package name cannot be parsed, the default packge name is returned.
64 */
65 private async readPackageName(manifestPath: string): Promise<string> {
66 if (!(await this.fileSystem.exists(manifestPath))) {
67 return this.getDefaultPackageName(this.applicationName);
68 }
69
70 const manifestContent = await this.fileSystem.readFile(manifestPath);
71 const packageName = this.parsePackageName(manifestContent.toString());
72 return packageName || this.getDefaultPackageName(this.applicationName);
73 }
74
75 /**
76 * Gets the default package name, based on the application name.
77 */
78 private getDefaultPackageName(applicationName: string): string {
79 return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
80 }
81
82 /**
83 * Parses the application package name from the contents of an Android manifest file.
84 * If a match was found, it is returned. Otherwise null is returned.
85 */
86 private parsePackageName(manifestContents: string) {
87 // first we remove all the comments from the file
88 const match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
89 return match ? match[1] : null;
90 }
91}
92