microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/packageNameResolver.ts

71lines · 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 {FileSystem} from "../../common/node/fileSystem";
5import * as path from "path";
6
7export class PackageNameResolver {
8
2010d4e2dlebu10 years ago9private static PackageNameRegexp: RegExp = /package="(.+?)"/;
8953be57dlebu10 years ago10private static ManifestName = "AndroidManifest.xml";
11private static DefaultPackagePrefix = "com.";
2010d4e2dlebu10 years ago12private static SourceRootRelPath: string[] = ["android", "app", "src", "main"];
13private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat(PackageNameResolver.ManifestName);
14private applicationName: string;
15
16constructor(applicationName: string) {
17this.applicationName = applicationName;
18}
8953be57dlebu10 years ago19
00eab8a8dlebu10 years ago20/**
21* Tries to find the package name in AndroidManifest.xml. If not found, it returns the default package name,
22* which is the application name prefixed with the default prefix.
23*/
ce5e88eeYuri Skorokhodov5 years ago24public resolvePackageName(projectRoot: string): Promise<string> {
8953be57dlebu10 years ago25let expectedAndroidManifestPath = path.join.apply(this, [projectRoot].concat(PackageNameResolver.DefaultManifestLocation));
2010d4e2dlebu10 years ago26return this.readPackageName(expectedAndroidManifestPath);
8953be57dlebu10 years ago27}
28
29/**
30* Given a manifest file path, it parses the file and returns the package name.
31* If the package name cannot be parsed, the default packge name is returned.
32*/
ce5e88eeYuri Skorokhodov5 years ago33private readPackageName(manifestPath: string): Promise<string> {
8953be57dlebu10 years ago34if (manifestPath) {
35let fs = new FileSystem();
36return fs.exists(manifestPath).then(exists => {
37if (exists) {
38return fs.readFile(manifestPath)
681bbf38dlebu10 years ago39.then(manifestContent => {
ce5e88eeYuri Skorokhodov5 years ago40let packageName = this.parsePackageName(manifestContent.toString());
8953be57dlebu10 years ago41if (!packageName) {
2010d4e2dlebu10 years ago42packageName = this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago43}
44return packageName;
45});
46} else {
2010d4e2dlebu10 years ago47return this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago48}
49});
50} else {
ce5e88eeYuri Skorokhodov5 years ago51return Promise.resolve(this.getDefaultPackageName(this.applicationName));
8953be57dlebu10 years ago52}
53}
54
00eab8a8dlebu10 years ago55/**
bc6696cbdigeff10 years ago56* Gets the default package name, based on the application name.
00eab8a8dlebu10 years ago57*/
8953be57dlebu10 years ago58private getDefaultPackageName(applicationName: string): string {
c7d7ba55digeff10 years ago59return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
8953be57dlebu10 years ago60}
61
62/**
63* Parses the application package name from the contents of an Android manifest file.
64* If a match was found, it is returned. Otherwise null is returned.
65*/
66private parsePackageName(manifestContents: string) {
67// first we remove all the comments from the file
2010d4e2dlebu10 years ago68let match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
8953be57dlebu10 years ago69return match ? match[1] : null;
70}
71}