microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
prepare-for-1.13.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/packageNameResolver.ts

91lines · modeblame

8953be57dlebu10 years ago1// 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";
09f6024fHeniker4 years ago5import { FileSystem } from "../../common/node/fileSystem";
8953be57dlebu10 years ago6
7export class PackageNameResolver {
2010d4e2dlebu10 years ago8private static PackageNameRegexp: RegExp = /package="(.+?)"/;
5ef47d7dHarshvardhan Joshi5 months ago9private static ApplicationIdRegexp: RegExp = /applicationId\s+(=)?\s*["'](.+?)["']/;
8953be57dlebu10 years ago10private static ManifestName = "AndroidManifest.xml";
5ef47d7dHarshvardhan Joshi5 months ago11private static GradleBuildName = "build.gradle";
8953be57dlebu10 years ago12private static DefaultPackagePrefix = "com.";
2010d4e2dlebu10 years ago13private static SourceRootRelPath: string[] = ["android", "app", "src", "main"];
34472878RedMickey5 years ago14private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat(
15PackageNameResolver.ManifestName,
16);
5ef47d7dHarshvardhan Joshi5 months ago17private static DefaultGradleBuildLocation: string[] = [
18"android",
19"app",
20PackageNameResolver.GradleBuildName,
21];
2010d4e2dlebu10 years ago22private applicationName: string;
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago23private fileSystem: FileSystem;
2010d4e2dlebu10 years ago24
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago25constructor(applicationName: string, fileSystem?: FileSystem) {
2010d4e2dlebu10 years ago26this.applicationName = applicationName;
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago27this.fileSystem = fileSystem || new FileSystem();
2010d4e2dlebu10 years ago28}
8953be57dlebu10 years ago29
00eab8a8dlebu10 years ago30/**
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*/
5ef47d7dHarshvardhan Joshi5 months ago34public async resolvePackageName(projectRoot: string): Promise<string> {
35const expectedGradleBuildPath = path.join.apply(
36this,
37[projectRoot].concat(PackageNameResolver.DefaultGradleBuildLocation),
38);
39const gradlePackageName = await this.readApplicationId(expectedGradleBuildPath);
40if (gradlePackageName) {
41return gradlePackageName;
42}
43
09f6024fHeniker4 years ago44const expectedAndroidManifestPath = path.join.apply(
34472878RedMickey5 years ago45this,
46[projectRoot].concat(PackageNameResolver.DefaultManifestLocation),
47);
2010d4e2dlebu10 years ago48return this.readPackageName(expectedAndroidManifestPath);
8953be57dlebu10 years ago49}
50
5ef47d7dHarshvardhan Joshi5 months ago51private async readApplicationId(gradlePath: string): Promise<string | null> {
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago52if (!(await this.fileSystem.exists(gradlePath))) {
53return null;
5ef47d7dHarshvardhan Joshi5 months ago54}
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago55
56const content = await this.fileSystem.readFile(gradlePath);
57const match = content.toString().match(PackageNameResolver.ApplicationIdRegexp);
58return match ? match[2] : null;
5ef47d7dHarshvardhan Joshi5 months ago59}
60
8953be57dlebu10 years ago61/**
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*/
0d77292aJiglioNero4 years ago65private async readPackageName(manifestPath: string): Promise<string> {
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago66if (!(await this.fileSystem.exists(manifestPath))) {
0d77292aJiglioNero4 years ago67return this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago68}
7a864bd0Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)5 months ago69
70const manifestContent = await this.fileSystem.readFile(manifestPath);
71const packageName = this.parsePackageName(manifestContent.toString());
72return packageName || this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago73}
74
00eab8a8dlebu10 years ago75/**
bc6696cbdigeff10 years ago76* Gets the default package name, based on the application name.
00eab8a8dlebu10 years ago77*/
8953be57dlebu10 years ago78private getDefaultPackageName(applicationName: string): string {
c7d7ba55digeff10 years ago79return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
8953be57dlebu10 years ago80}
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*/
86private parsePackageName(manifestContents: string) {
87// first we remove all the comments from the file
09f6024fHeniker4 years ago88const match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
8953be57dlebu10 years ago89return match ? match[1] : null;
90}
34472878RedMickey5 years ago91}