microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
test-microbuild1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/packageNameResolver.ts

70lines · 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="(.+?)"/;
8953be57dlebu10 years ago9private static ManifestName = "AndroidManifest.xml";
10private static DefaultPackagePrefix = "com.";
2010d4e2dlebu10 years ago11private static SourceRootRelPath: string[] = ["android", "app", "src", "main"];
34472878RedMickey5 years ago12private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat(
13PackageNameResolver.ManifestName,
14);
2010d4e2dlebu10 years ago15private applicationName: string;
16
17constructor(applicationName: string) {
18this.applicationName = applicationName;
19}
8953be57dlebu10 years ago20
00eab8a8dlebu10 years ago21/**
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*/
ce5e88eeYuri Skorokhodov5 years ago25public resolvePackageName(projectRoot: string): Promise<string> {
09f6024fHeniker4 years ago26const expectedAndroidManifestPath = path.join.apply(
34472878RedMickey5 years ago27this,
28[projectRoot].concat(PackageNameResolver.DefaultManifestLocation),
29);
2010d4e2dlebu10 years ago30return this.readPackageName(expectedAndroidManifestPath);
8953be57dlebu10 years ago31}
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*/
0d77292aJiglioNero4 years ago37private async readPackageName(manifestPath: string): Promise<string> {
8953be57dlebu10 years ago38if (manifestPath) {
09f6024fHeniker4 years ago39const fs = new FileSystem();
0d77292aJiglioNero4 years ago40const exists = await fs.exists(manifestPath);
41if (exists) {
42const manifestContent = await fs.readFile(manifestPath);
43let packageName = this.parsePackageName(manifestContent.toString());
44if (!packageName) {
45packageName = this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago46}
0d77292aJiglioNero4 years ago47return packageName;
48}
49return this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago50}
09f6024fHeniker4 years ago51return this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago52}
53
00eab8a8dlebu10 years ago54/**
bc6696cbdigeff10 years ago55* Gets the default package name, based on the application name.
00eab8a8dlebu10 years ago56*/
8953be57dlebu10 years ago57private getDefaultPackageName(applicationName: string): string {
c7d7ba55digeff10 years ago58return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
8953be57dlebu10 years ago59}
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*/
65private parsePackageName(manifestContents: string) {
66// first we remove all the comments from the file
09f6024fHeniker4 years ago67const match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
8953be57dlebu10 years ago68return match ? match[1] : null;
69}
34472878RedMickey5 years ago70}