forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication-manager-base.ts
238 lines (207 loc) · 7.02 KB
/
application-manager-base.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { EventEmitter } from "events";
import { TARGET_FRAMEWORK_IDENTIFIERS } from "../constants";
import { IBuildData } from "../../definitions/build";
import { IDictionary, IHooksService } from "../declarations";
import * as _ from "lodash";
export abstract class ApplicationManagerBase
extends EventEmitter
implements Mobile.IDeviceApplicationManager {
private lastInstalledAppIdentifiers: string[];
private lastAvailableDebuggableApps: Mobile.IDeviceApplicationInformation[];
private lastAvailableDebuggableAppViews: IDictionary<
Mobile.IDebugWebViewInfo[]
> = {};
constructor(
protected $logger: ILogger,
protected $hooksService: IHooksService,
protected $deviceLogProvider: Mobile.IDeviceLogProvider
) {
super();
}
public async setTransferredAppFiles(files: string[]): Promise<void> {
for (const file of files) {
await this.$deviceLogProvider.setSourceFileLocation(file);
}
}
public async reinstallApplication(
appIdentifier: string,
packageFilePath: string,
buildData?: IBuildData
): Promise<void> {
const isApplicationInstalled = await this.isApplicationInstalled(
appIdentifier
);
if (isApplicationInstalled) {
await this.uninstallApplication(appIdentifier);
}
await this.installApplication(packageFilePath, appIdentifier, buildData);
}
public async restartApplication(
appData: Mobile.IApplicationData
): Promise<void> {
await this.stopApplication(appData);
await this.startApplication(appData);
}
public async isApplicationInstalled(appIdentifier: string): Promise<boolean> {
await this.checkForApplicationUpdates();
return _.includes(this.lastInstalledAppIdentifiers, appIdentifier);
}
private checkForApplicationUpdatesPromise: Promise<void>;
public async checkForApplicationUpdates(): Promise<void> {
if (!this.checkForApplicationUpdatesPromise) {
this.checkForApplicationUpdatesPromise = new Promise<void>(
async (resolve, reject) => {
let isFulfilled = false;
// As this method is called on 500ms, but it's execution may last much longer
// use locking, so the next executions will not get into the body, while the first one is still working.
// In case we do not break the next executions, we'll report each app as newly installed several times.
try {
const currentlyInstalledAppIdentifiers = await this.getInstalledApplications();
const previouslyInstalledAppIdentifiers =
this.lastInstalledAppIdentifiers || [];
const newAppIdentifiers = _.difference(
currentlyInstalledAppIdentifiers,
previouslyInstalledAppIdentifiers
);
const removedAppIdentifiers = _.difference(
previouslyInstalledAppIdentifiers,
currentlyInstalledAppIdentifiers
);
this.lastInstalledAppIdentifiers = currentlyInstalledAppIdentifiers;
_.each(newAppIdentifiers, (appIdentifier) =>
this.emit("applicationInstalled", appIdentifier)
);
_.each(removedAppIdentifiers, (appIdentifier) =>
this.emit("applicationUninstalled", appIdentifier)
);
await this.checkForAvailableDebuggableAppsChanges();
} catch (err) {
isFulfilled = true;
reject(err);
} finally {
this.checkForApplicationUpdatesPromise = null;
if (!isFulfilled) {
resolve();
}
}
}
);
}
return this.checkForApplicationUpdatesPromise;
}
public async tryStartApplication(
appData: Mobile.IApplicationData
): Promise<void> {
try {
await this.startApplication(appData);
} catch (err) {
this.$logger.trace(
`Unable to start application ${appData.appId} with name ${appData.projectName}. Error is: ${err.message}`
);
}
}
public abstract installApplication(
packageFilePath: string,
appIdentifier?: string,
buildData?: IBuildData
): Promise<void>;
public abstract uninstallApplication(
appIdentifier: string
): Promise<void>;
public abstract startApplication(
appData: Mobile.IApplicationData
): Promise<void>;
public abstract stopApplication(
appData: Mobile.IApplicationData
): Promise<void>;
public abstract getInstalledApplications(): Promise<string[]>;
public abstract getDebuggableApps(): Promise<
Mobile.IDeviceApplicationInformation[]
>;
public abstract getDebuggableAppViews(
appIdentifiers: string[]
): Promise<IDictionary<Mobile.IDebugWebViewInfo[]>>;
private async checkForAvailableDebuggableAppsChanges(): Promise<void> {
const currentlyAvailableDebuggableApps = await this.getDebuggableApps();
const previouslyAvailableDebuggableApps =
this.lastAvailableDebuggableApps || [];
const newAvailableDebuggableApps = _.differenceBy(
currentlyAvailableDebuggableApps,
previouslyAvailableDebuggableApps,
"appIdentifier"
);
const notAvailableAppsForDebugging = _.differenceBy(
previouslyAvailableDebuggableApps,
currentlyAvailableDebuggableApps,
"appIdentifier"
);
this.lastAvailableDebuggableApps = currentlyAvailableDebuggableApps;
_.each(
newAvailableDebuggableApps,
(appInfo: Mobile.IDeviceApplicationInformation) => {
this.emit("debuggableAppFound", appInfo);
}
);
_.each(
notAvailableAppsForDebugging,
(appInfo: Mobile.IDeviceApplicationInformation) => {
this.emit("debuggableAppLost", appInfo);
if (
_.has(this.lastAvailableDebuggableAppViews, appInfo.appIdentifier)
) {
// Prevent emitting debuggableViewLost when application cannot be debugged anymore.
delete this.lastAvailableDebuggableAppViews[appInfo.appIdentifier];
}
}
);
const cordovaDebuggableAppIdentifiers = _(currentlyAvailableDebuggableApps)
.filter((c) => c.framework === TARGET_FRAMEWORK_IDENTIFIERS.Cordova)
.map((c) => c.appIdentifier)
.value();
const currentlyAvailableAppViews = await this.getDebuggableAppViews(
cordovaDebuggableAppIdentifiers
);
_.each(
currentlyAvailableAppViews,
(currentlyAvailableViews, appIdentifier) => {
const previouslyAvailableViews = this.lastAvailableDebuggableAppViews[
appIdentifier
];
const newAvailableViews = _.differenceBy(
currentlyAvailableViews,
previouslyAvailableViews,
"id"
);
const notAvailableViews = _.differenceBy(
previouslyAvailableViews,
currentlyAvailableViews,
"id"
);
_.each(notAvailableViews, (debugWebViewInfo) => {
this.emit("debuggableViewLost", appIdentifier, debugWebViewInfo);
});
_.each(newAvailableViews, (debugWebViewInfo) => {
this.emit("debuggableViewFound", appIdentifier, debugWebViewInfo);
});
// Determine which of the views had changed since last check and raise debuggableViewChanged event for them:
const keptViews = _.differenceBy(
currentlyAvailableViews,
newAvailableViews,
"id"
);
_.each(keptViews, (view) => {
const previousTimeViewInfo = _.find(
previouslyAvailableViews,
(previousView) => previousView.id === view.id
);
if (!_.isEqual(view, previousTimeViewInfo)) {
this.emit("debuggableViewChanged", appIdentifier, view);
}
});
this.lastAvailableDebuggableAppViews[
appIdentifier
] = currentlyAvailableViews;
}
);
}
}