microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/test/resources/simulators/apkSerializer.ts

51lines · modeblame

a2e27a97digeff10 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";
5import {FileSystem} from "../../../common/node/fileSystem";
6
7interface IAPKInformation {
8packageName: string;
9}
10
11const APK_FORMAT_SIGNATURE = "APKSimulatedFormat";
12
13interface IAPKFormat {
14format: "APKSimulatedFormat";
15information: IAPKInformation;
16}
17
18/* This class stores a "fake" APK file in the file system, and then can be used to verify that a certain file is a
19valid "fake" APK file, and also read metadata from it. */
20export class APKSerializer {
21private fileSystem: FileSystem;
22
23constructor(fileSystem: FileSystem) {
24this.fileSystem = fileSystem;
25}
26
27public readPackageNameFromFile(apkPath: string): Q.Promise<string> {
28return this.fileSystem.readFile(apkPath, "utf8").then(data => {
29const information = this.readAPKData(data);
30return information.packageName;
31});
32}
33
34public writeApk(apkPath: string, information: IAPKInformation): Q.Promise<void> {
35this.fileSystem.makeDirectoryRecursiveSync(path.dirname(apkPath));
36return this.fileSystem.writeFile(apkPath, this.generateAPKData(information));
37}
38
39private generateAPKData(information: IAPKInformation): string {
40return JSON.stringify({ format: APK_FORMAT_SIGNATURE, information: information});
41}
42
43private readAPKData(data: string): IAPKInformation {
44const json = JSON.parse(data);
45if (json.format === APK_FORMAT_SIGNATURE) {
46return (<IAPKFormat>json).information;
47} else {
48throw new Error("Attempted to read an invalid simulated .apk file");
49}
50}
51}