microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/android/packageNameResolver.ts

72lines · 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 Q = require("q");
6import * as path from "path";
7
8export class PackageNameResolver {
9
2010d4e2dlebu10 years ago10private static PackageNameRegexp: RegExp = /package="(.+?)"/;
8953be57dlebu10 years ago11private static ManifestName = "AndroidManifest.xml";
12private static DefaultPackagePrefix = "com.";
2010d4e2dlebu10 years ago13private static SourceRootRelPath: string[] = ["android", "app", "src", "main"];
14private static DefaultManifestLocation: string[] = PackageNameResolver.SourceRootRelPath.concat(PackageNameResolver.ManifestName);
15private 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*/
2010d4e2dlebu10 years ago25public resolvePackageName(projectRoot: string): Q.Promise<string> {
8953be57dlebu10 years ago26let expectedAndroidManifestPath = path.join.apply(this, [projectRoot].concat(PackageNameResolver.DefaultManifestLocation));
2010d4e2dlebu10 years ago27return this.readPackageName(expectedAndroidManifestPath);
8953be57dlebu10 years ago28}
29
30/**
31* Given a manifest file path, it parses the file and returns the package name.
32* If the package name cannot be parsed, the default packge name is returned.
33*/
2010d4e2dlebu10 years ago34private readPackageName(manifestPath: string): Q.Promise<string> {
8953be57dlebu10 years ago35if (manifestPath) {
36let fs = new FileSystem();
37return fs.exists(manifestPath).then(exists => {
38if (exists) {
39return fs.readFile(manifestPath)
681bbf38dlebu10 years ago40.then(manifestContent => {
41let packageName = this.parsePackageName(manifestContent);
8953be57dlebu10 years ago42if (!packageName) {
2010d4e2dlebu10 years ago43packageName = this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago44}
45return packageName;
46});
47} else {
2010d4e2dlebu10 years ago48return this.getDefaultPackageName(this.applicationName);
8953be57dlebu10 years ago49}
50});
51} else {
2010d4e2dlebu10 years ago52return Q.resolve(this.getDefaultPackageName(this.applicationName));
8953be57dlebu10 years ago53}
54}
55
00eab8a8dlebu10 years ago56/**
bc6696cbdigeff10 years ago57* Gets the default package name, based on the application name.
00eab8a8dlebu10 years ago58*/
8953be57dlebu10 years ago59private getDefaultPackageName(applicationName: string): string {
c7d7ba55digeff10 years ago60return (PackageNameResolver.DefaultPackagePrefix + applicationName).toLowerCase();
8953be57dlebu10 years ago61}
62
63/**
64* Parses the application package name from the contents of an Android manifest file.
65* If a match was found, it is returned. Otherwise null is returned.
66*/
67private parsePackageName(manifestContents: string) {
68// first we remove all the comments from the file
2010d4e2dlebu10 years ago69let match = manifestContents.match(PackageNameResolver.PackageNameRegexp);
8953be57dlebu10 years ago70return match ? match[1] : null;
71}
72}