Skip to content

Commit 671f33d

Browse files
authored
Merge pull request #4773 from NativeScript/fatme/fix-install-aar
fix: fix multiple installation on android device
2 parents ed42427 + be5af83 commit 671f33d

8 files changed

+52
-14
lines changed

lib/bootstrap.ts

+2
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,6 @@ $injector.require("applePortalSessionService", "./services/apple-portal/apple-po
223223
$injector.require("applePortalCookieService", "./services/apple-portal/apple-portal-cookie-service");
224224
$injector.require("applePortalApplicationService", "./services/apple-portal/apple-portal-application-service");
225225

226+
$injector.require("watchIgnoreListService", "./services/watch-ignore-list-service");
227+
226228
$injector.requirePublicClass("initializeService", "./services/initialize-service");

lib/controllers/prepare-controller.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export class PrepareController extends EventEmitter {
2525
private $prepareNativePlatformService: IPrepareNativePlatformService,
2626
private $projectChangesService: IProjectChangesService,
2727
private $projectDataService: IProjectDataService,
28-
private $webpackCompilerService: IWebpackCompilerService
28+
private $webpackCompilerService: IWebpackCompilerService,
29+
private $watchIgnoreListService: IWatchIgnoreListService
2930
) { super(); }
3031

3132
@performanceLog()
@@ -131,8 +132,12 @@ export class PrepareController extends EventEmitter {
131132
const watcher = choki.watch(patterns, watcherOptions)
132133
.on("all", async (event: string, filePath: string) => {
133134
filePath = path.join(projectData.projectDir, filePath);
134-
this.$logger.trace(`Chokidar raised event ${event} for ${filePath}.`);
135-
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hmrData: null, hasNativeChanges: true, platform: platformData.platformNameLowerCase });
135+
if (this.$watchIgnoreListService.isFileInIgnoreList(filePath)) {
136+
this.$watchIgnoreListService.removeFileFromIgnoreList(filePath);
137+
} else {
138+
this.$logger.info(`Chokidar raised event ${event} for ${filePath}.`);
139+
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hmrData: null, hasNativeChanges: true, platform: platformData.platformNameLowerCase });
140+
}
136141
});
137142

138143
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].nativeFilesWatcher = watcher;

lib/controllers/run-controller.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { cache, performanceLog } from "../common/decorators";
44
import { EventEmitter } from "events";
55

66
export class RunController extends EventEmitter implements IRunController {
7-
private rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = { };
8-
97
constructor(
108
protected $analyticsService: IAnalyticsService,
119
private $buildController: IBuildController,
@@ -246,7 +244,7 @@ export class RunController extends EventEmitter implements IRunController {
246244
}
247245

248246
private async syncInitialDataOnDevices(projectData: IProjectData, liveSyncInfo: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceDescriptor[]): Promise<void> {
249-
this.rebuiltInformation = {};
247+
const rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = { };
250248

251249
const deviceAction = async (device: Mobile.IDevice) => {
252250
const deviceDescriptor = _.find(deviceDescriptors, dd => dd.identifier === device.deviceInfo.identifier);
@@ -267,14 +265,14 @@ export class RunController extends EventEmitter implements IRunController {
267265

268266
// Case where we have three devices attached, a change that requires build is found,
269267
// we'll rebuild the app only for the first device, but we should install new package on all three devices.
270-
if (this.rebuiltInformation[platformData.platformNameLowerCase] && (this.$mobileHelper.isAndroidPlatform(platformData.platformNameLowerCase) || this.rebuiltInformation[platformData.platformNameLowerCase].isEmulator === device.isEmulator)) {
271-
packageFilePath = this.rebuiltInformation[platformData.platformNameLowerCase].packageFilePath;
268+
if (rebuiltInformation[platformData.platformNameLowerCase] && (this.$mobileHelper.isAndroidPlatform(platformData.platformNameLowerCase) || rebuiltInformation[platformData.platformNameLowerCase].isEmulator === device.isEmulator)) {
269+
packageFilePath = rebuiltInformation[platformData.platformNameLowerCase].packageFilePath;
272270
await this.$deviceInstallAppService.installOnDevice(device, buildData, packageFilePath);
273271
} else {
274272
const shouldBuild = prepareResultData.hasNativeChanges || await this.$buildController.shouldBuild(buildData);
275273
if (shouldBuild) {
276274
packageFilePath = await deviceDescriptor.buildAction();
277-
this.rebuiltInformation[platformData.platformNameLowerCase] = { isEmulator: device.isEmulator, platform: platformData.platformNameLowerCase, packageFilePath };
275+
rebuiltInformation[platformData.platformNameLowerCase] = { isEmulator: device.isEmulator, platform: platformData.platformNameLowerCase, packageFilePath };
278276
} else {
279277
await this.$analyticsService.trackEventActionInGoogleAnalytics({
280278
action: TrackActionNames.LiveSync,
@@ -315,7 +313,7 @@ export class RunController extends EventEmitter implements IRunController {
315313
}
316314

317315
private async syncChangedDataOnDevices(data: IFilesChangeEventData, projectData: IProjectData, liveSyncInfo: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceDescriptor[]): Promise<void> {
318-
this.rebuiltInformation = {};
316+
const rebuiltInformation: IDictionary<{ packageFilePath: string, platform: string, isEmulator: boolean }> = { };
319317

320318
const deviceAction = async (device: Mobile.IDevice) => {
321319
const deviceDescriptor = _.find(deviceDescriptors, dd => dd.identifier === device.deviceInfo.identifier);
@@ -343,14 +341,14 @@ export class RunController extends EventEmitter implements IRunController {
343341
const deviceAppData = await platformLiveSyncService.getAppData(_.merge({ device, watch: true }, watchInfo));
344342

345343
if (data.hasNativeChanges) {
346-
const rebuiltInfo = this.rebuiltInformation[platformData.platformNameLowerCase] && (this.$mobileHelper.isAndroidPlatform(platformData.platformNameLowerCase) || this.rebuiltInformation[platformData.platformNameLowerCase].isEmulator === device.isEmulator);
344+
const rebuiltInfo = rebuiltInformation[platformData.platformNameLowerCase] && (this.$mobileHelper.isAndroidPlatform(platformData.platformNameLowerCase) || rebuiltInformation[platformData.platformNameLowerCase].isEmulator === device.isEmulator);
347345
if (!rebuiltInfo) {
348346
await this.$prepareNativePlatformService.prepareNativePlatform(platformData, projectData, prepareData);
349347
await deviceDescriptor.buildAction();
350-
this.rebuiltInformation[platformData.platformNameLowerCase] = { isEmulator: device.isEmulator, platform: platformData.platformNameLowerCase, packageFilePath: null };
348+
rebuiltInformation[platformData.platformNameLowerCase] = { isEmulator: device.isEmulator, platform: platformData.platformNameLowerCase, packageFilePath: null };
351349
}
352350

353-
await this.$deviceInstallAppService.installOnDevice(device, deviceDescriptor.buildData, this.rebuiltInformation[platformData.platformNameLowerCase].packageFilePath);
351+
await this.$deviceInstallAppService.installOnDevice(device, deviceDescriptor.buildData, rebuiltInformation[platformData.platformNameLowerCase].packageFilePath);
354352
await platformLiveSyncService.syncAfterInstall(device, watchInfo);
355353
await platformLiveSyncService.restartApplication(projectData, { deviceAppData, modifiedFilesData: [], isFullSync: false, useHotModuleReload: liveSyncInfo.useHotModuleReload });
356354
} else {

lib/declarations.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1049,4 +1049,10 @@ interface IPlatformCommandHelper {
10491049
getAvailablePlatforms(projectData: IProjectData): string[];
10501050
getPreparedPlatforms(projectData: IProjectData): string[];
10511051
getCurrentPlatformVersion(platform: string, projectData: IProjectData): string;
1052+
}
1053+
1054+
interface IWatchIgnoreListService {
1055+
addFileToIgnoreList(filePath: string): void;
1056+
removeFileFromIgnoreList(filePath: string): void;
1057+
isFileInIgnoreList(filePath: string): boolean;
10521058
}

lib/services/android-plugin-build-service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
2020
private $errors: IErrors,
2121
private $filesHashService: IFilesHashService,
2222
public $hooksService: IHooksService,
23-
private $injector: IInjector
23+
private $injector: IInjector,
24+
private $watchIgnoreListService: IWatchIgnoreListService
2425
) { }
2526

2627
private static MANIFEST_ROOT = {
@@ -189,6 +190,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
189190
this.copySourceSetDirectories(androidSourceDirectories, pluginTempMainSrcDir);
190191
await this.setupGradle(pluginTempDir, options.platformsAndroidDirPath, options.projectDir);
191192
await this.buildPlugin({ pluginDir: pluginTempDir, pluginName: options.pluginName });
193+
this.$watchIgnoreListService.addFileToIgnoreList(path.join(options.aarOutputDir, `${shortPluginName}.aar`));
192194
this.copyAar(shortPluginName, pluginTempDir, options.aarOutputDir);
193195
this.writePluginHashInfo(pluginSourceFileHashesInfo, pluginTempDir);
194196
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export class WatchIgnoreListService implements IWatchIgnoreListService {
2+
private ignoreMap: IDictionary<boolean> = {};
3+
4+
public addFileToIgnoreList(filePath: string): void {
5+
this.ignoreMap[filePath] = true;
6+
}
7+
8+
public removeFileFromIgnoreList(filePath: string): void {
9+
this.ignoreMap[filePath] = false;
10+
}
11+
12+
public isFileInIgnoreList(filePath: string): boolean {
13+
return !!this.ignoreMap[filePath];
14+
}
15+
}
16+
$injector.register("watchIgnoreListService", WatchIgnoreListService);

test/controllers/prepare-controller.ts

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ function createTestInjector(data: { hasNativeChanges: boolean }): IInjector {
4949
getProductionDependencies: () => (<any>[])
5050
});
5151

52+
injector.register("watchIgnoreListService", {
53+
addFileToIgnoreList: () => ({}),
54+
isFileInIgnoreList: () => false
55+
});
56+
5257
const prepareController: PrepareController = injector.resolve("prepareController");
5358
prepareController.emit = (eventName: string, eventData: any) => {
5459
emittedEventNames.push(eventName);

test/services/android-plugin-build-service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ describe('androidPluginBuildService', () => {
7575
hasChangesInShasums: (oldHashes: IStringDictionary, newHashes: IStringDictionary): boolean => !!options.hasChangesInShasums
7676
});
7777

78+
testInjector.register("watchIgnoreListService", {
79+
addFileToIgnoreList: () => ({})
80+
});
81+
7882
fs = testInjector.resolve("fs");
7983
androidBuildPluginService = testInjector.resolve<AndroidPluginBuildService>(AndroidPluginBuildService);
8084
}

0 commit comments

Comments
 (0)