-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-device-file-system.ts
161 lines (130 loc) · 6.69 KB
/
android-device-file-system.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as path from "path";
import * as semver from "semver";
import { AndroidDeviceHashService } from "./android-device-hash-service";
import { executeActionByChunks } from "../../helpers";
import { DEFAULT_CHUNK_SIZE } from '../../constants';
export class AndroidDeviceFileSystem implements Mobile.IDeviceFileSystem {
private _deviceHashServices = Object.create(null);
constructor(private adb: Mobile.IDeviceAndroidDebugBridge,
private $fs: IFileSystem,
private $logger: ILogger,
private $mobileHelper: Mobile.IMobileHelper,
private $tempService: ITempService,
private $injector: IInjector) { }
public async listFiles(devicePath: string, appIdentifier?: string): Promise<any> {
let listCommandArgs = ["ls", "-a", devicePath];
if (appIdentifier) {
listCommandArgs = ["run-as", appIdentifier].concat(listCommandArgs);
}
return this.adb.executeShellCommand(listCommandArgs);
}
public async getFile(deviceFilePath: string, appIdentifier: string, outputPath?: string): Promise<void> {
const stdout = !outputPath;
if (stdout) {
outputPath = await this.$tempService.path({ prefix: "sync", suffix: ".tmp" });
}
await this.adb.executeCommand(["pull", deviceFilePath, outputPath]);
if (stdout) {
await new Promise<void>((resolve, reject) => {
const readStream = this.$fs.createReadStream(outputPath);
readStream.pipe(process.stdout);
readStream.on("end", () => {
resolve();
});
readStream.on("error", (err: Error) => {
reject(err);
});
});
}
}
public async getFileContent(deviceFilePath: string, appIdentifier: string): Promise<string> {
const result = await this.adb.executeShellCommand(["cat", deviceFilePath]);
return result;
}
public async putFile(localFilePath: string, deviceFilePath: string, appIdentifier: string): Promise<void> {
await this.adb.pushFile(localFilePath, deviceFilePath);
}
public async transferFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<Mobile.ILocalToDevicePathData[]> {
const directoriesToChmod: string[] = [];
const transferredFiles: Mobile.ILocalToDevicePathData[] = [];
const action = async (localToDevicePathData: Mobile.ILocalToDevicePathData) => {
const fstat = this.$fs.getFsStats(localToDevicePathData.getLocalPath());
if (fstat.isFile()) {
const devicePath = localToDevicePathData.getDevicePath();
await this.adb.pushFile(localToDevicePathData.getLocalPath(), devicePath);
transferredFiles.push(localToDevicePathData);
} else if (fstat.isDirectory()) {
const dirToChmod = localToDevicePathData.getDevicePath();
directoriesToChmod.push(dirToChmod);
}
};
await executeActionByChunks<Mobile.ILocalToDevicePathData>(localToDevicePaths, DEFAULT_CHUNK_SIZE, action);
const dirsChmodAction = (directoryToChmod: string) => this.adb.executeShellCommand(["chmod", "0777", directoryToChmod]);
await executeActionByChunks<string>(_.uniq(directoriesToChmod), DEFAULT_CHUNK_SIZE, dirsChmodAction);
return transferredFiles;
}
public async transferDirectory(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[], projectFilesPath: string): Promise<Mobile.ILocalToDevicePathData[]> {
// starting from Android 9, adb push is throwing an exception when there are subfolders
// the check could be removed when we start supporting only runtime versions with sockets
const minAndroidWithoutAdbPushDir = "9.0.0";
const isAdbPushDirSupported = semver.lt(semver.coerce(deviceAppData.device.deviceInfo.version), minAndroidWithoutAdbPushDir);
const deviceProjectDir = await deviceAppData.getDeviceProjectRootPath();
let transferredLocalToDevicePaths: Mobile.ILocalToDevicePathData[] = [];
if (isAdbPushDirSupported) {
await this.adb.pushFile(projectFilesPath, deviceProjectDir);
transferredLocalToDevicePaths = localToDevicePaths;
} else {
transferredLocalToDevicePaths = await this.pushFiles(localToDevicePaths);
}
if (transferredLocalToDevicePaths.length) {
const filesToChmodOnDevice = transferredLocalToDevicePaths.map(localToDevicePath => localToDevicePath.getDevicePath());
await this.chmodFiles(deviceProjectDir, filesToChmodOnDevice);
}
return transferredLocalToDevicePaths;
}
private async chmodFiles(deviceProjectRoot: string, filesToChmodOnDevice: string[]) {
const commandsDeviceFilePath = this.$mobileHelper.buildDevicePath(deviceProjectRoot, "nativescript.commands.sh");
await this.createFileOnDevice(commandsDeviceFilePath, `chmod 0777 ${filesToChmodOnDevice.join(" ")}`);
await this.adb.executeShellCommand([commandsDeviceFilePath]);
}
private async pushFiles(localToDevicePaths: Mobile.ILocalToDevicePathData[]) {
this.$logger.trace("Changed hashes are:", localToDevicePaths);
const transferredFiles: Mobile.ILocalToDevicePathData[] = [];
const transferFileAction = async (localToDevicePathData: Mobile.ILocalToDevicePathData) => {
transferredFiles.push(localToDevicePathData);
await this.transferFile(localToDevicePathData.getLocalPath(), localToDevicePathData.getDevicePath());
};
await executeActionByChunks<Mobile.ILocalToDevicePathData>(localToDevicePaths, DEFAULT_CHUNK_SIZE, transferFileAction);
return transferredFiles;
}
public async transferFile(localPath: string, devicePath: string): Promise<void> {
this.$logger.trace(`Transfering ${localPath} to ${devicePath}`);
const stats = this.$fs.getFsStats(localPath);
if (stats.isDirectory()) {
await this.adb.executeShellCommand(["mkdir", path.dirname(devicePath)]);
} else {
await this.adb.pushFile(localPath, devicePath);
}
}
public async createFileOnDevice(deviceFilePath: string, fileContent: string): Promise<void> {
const hostTmpDir = await this.$tempService.mkdirSync("application-");
const commandsFileHostPath = path.join(hostTmpDir, "temp.commands.file");
this.$fs.writeFile(commandsFileHostPath, fileContent);
// copy it to the device
await this.transferFile(commandsFileHostPath, deviceFilePath);
await this.adb.executeShellCommand(["chmod", "0777", deviceFilePath]);
}
public async deleteFile(deviceFilePath: string, appIdentifier: string): Promise<void> {
await this.adb.executeShellCommand(["rm", "-rf", deviceFilePath]);
}
public async updateHashesOnDevice(hashes: IStringDictionary, appIdentifier: string): Promise<void> {
const deviceHashService = this.getDeviceHashService(appIdentifier);
await deviceHashService.uploadHashFileToDevice(hashes);
}
public getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService {
if (!this._deviceHashServices[appIdentifier]) {
this._deviceHashServices[appIdentifier] = this.$injector.resolve(AndroidDeviceHashService, { adb: this.adb, appIdentifier });
}
return this._deviceHashServices[appIdentifier];
}
}