Skip to content

Commit ff90b0f

Browse files
Merge pull request #3745 from NativeScript/tachev/fix-aar-livesync
fix: start tracking the AAR and include.gradle changes during livesync
2 parents 27305f9 + c4deddf commit ff90b0f

File tree

4 files changed

+49
-35
lines changed

4 files changed

+49
-35
lines changed

lib/common

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

+13-12
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
127127

128128
private getScope(scopeName: string, content: string): string {
129129
const indexOfScopeName = content.indexOf(scopeName);
130-
let result = "";
131130
const openingBracket = "{";
132131
const closingBracket = "}";
133-
let openBrackets = 0;
134132
let foundFirstBracket = false;
133+
let openBrackets = 0;
134+
let result = "";
135135

136136
let i = indexOfScopeName;
137-
while (i < content.length) {
137+
while (i !== -1 && i < content.length) {
138138
const currCharacter = content[i];
139139
if (currCharacter === openingBracket) {
140140
if (openBrackets === 0) {
@@ -337,15 +337,16 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
337337
}
338338

339339
const productFlavorsScope = this.getScope("productFlavors", includeGradleFileContent);
340-
341-
try {
342-
const newIncludeGradleFileContent = includeGradleFileContent.replace(productFlavorsScope, "");
343-
this.$fs.writeFile(includeGradleFilePath, newIncludeGradleFileContent);
344-
345-
return true;
346-
} catch (e) {
347-
this.$errors.failWithoutHelp(`Failed to write the updated include.gradle ` +
348-
`in - ${includeGradleFilePath}. Error is: ${e.toString()}`);
340+
if (productFlavorsScope) {
341+
try {
342+
const newIncludeGradleFileContent = includeGradleFileContent.replace(productFlavorsScope, "");
343+
this.$fs.writeFile(includeGradleFilePath, newIncludeGradleFileContent);
344+
345+
return true;
346+
} catch (e) {
347+
this.$errors.failWithoutHelp(`Failed to write the updated include.gradle ` +
348+
`in - ${includeGradleFilePath}. Error is: ${e.toString()}`);
349+
}
349350
}
350351
}
351352

lib/services/livesync/livesync-service.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from "path";
22
import * as choki from "chokidar";
33
import { EOL } from "os";
44
import { EventEmitter } from "events";
5-
import { hook, isAllowedFinalFile } from "../../common/helpers";
5+
import { hook } from "../../common/helpers";
66
import { PACKAGE_JSON_FILE_NAME, LiveSyncTrackActionNames, USER_INTERACTION_NEEDED_EVENT_NAME, DEBUGGER_ATTACHED_EVENT_NAME, DEBUGGER_DETACHED_EVENT_NAME, TrackActionNames } from "../../constants";
77
import { DeviceTypes, DeviceDiscoveryEventNames } from "../../common/constants";
88
import { cache } from "../../common/decorators";
@@ -671,12 +671,10 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
671671

672672
this.$logger.trace(`Chokidar raised event ${event} for ${filePath}.`);
673673

674-
if (!isAllowedFinalFile(filePath)) {
675-
if (event === "add" || event === "addDir" || event === "change" /* <--- what to do when change event is raised ? */) {
676-
filesToSync.push(filePath);
677-
} else if (event === "unlink" || event === "unlinkDir") {
678-
filesToRemove.push(filePath);
679-
}
674+
if (event === "add" || event === "addDir" || event === "change" /* <--- what to do when change event is raised ? */) {
675+
filesToSync.push(filePath);
676+
} else if (event === "unlink" || event === "unlinkDir") {
677+
filesToRemove.push(filePath);
680678
}
681679

682680
startSyncFilesTimeout();

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

+30-15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('androidPluginBuildService', () => {
2222
addManifest?: boolean,
2323
addResFolder?: boolean,
2424
addAssetsFolder?: boolean,
25+
addIncludeGradle?: boolean,
2526
addLegacyIncludeGradle?: boolean,
2627
addProjectDir?: boolean,
2728
addProjectRuntime?: boolean,
@@ -111,6 +112,7 @@ describe('androidPluginBuildService', () => {
111112
addManifest?: boolean,
112113
addResFolder?: boolean,
113114
addAssetsFolder?: boolean,
115+
addIncludeGradle?: boolean,
114116
addLegacyIncludeGradle?: boolean
115117
}) {
116118
const validAndroidManifestContent = `<?xml version="1.0" encoding="UTF-8"?>
@@ -122,21 +124,23 @@ describe('androidPluginBuildService', () => {
122124
name="string_name"
123125
>text_string</string>
124126
</resources>`;
125-
const validIncludeGradleContent = `android {
126-
productFlavors {
127-
"nativescript-pro-ui" {
128-
dimension "nativescript-pro-ui"
129-
}
130-
}
127+
const validIncludeGradleContent =
128+
`android {` +
129+
(options.addLegacyIncludeGradle ? `
130+
productFlavors {
131+
"nativescript-pro-ui" {
132+
dimension "nativescript-pro-ui"
131133
}
134+
}` : ``) + `
135+
}
132136
133-
def supportVersion = project.hasProperty("supportVersion") ? project.supportVersion : "23.3.0"
137+
def supportVersion = project.hasProperty("supportVersion") ? project.supportVersion : "23.3.0"
134138
135-
dependencies {
136-
compile "com.android.support:appcompat-v7:$supportVersion"
137-
compile "com.android.support:recyclerview-v7:$supportVersion"
138-
compile "com.android.support:design:$supportVersion"
139-
}`;
139+
dependencies {
140+
compile "com.android.support:appcompat-v7:$supportVersion"
141+
compile "com.android.support:recyclerview-v7:$supportVersion"
142+
compile "com.android.support:design:$supportVersion"
143+
}`;
140144

141145
if (options.addManifest) {
142146
fs.writeFile(path.join(pluginFolder, "AndroidManifest.xml"), validAndroidManifestContent);
@@ -154,7 +158,7 @@ describe('androidPluginBuildService', () => {
154158
fs.writeFile(path.join(imagesFolder, "myicon.png"), "123");
155159
}
156160

157-
if (options.addLegacyIncludeGradle) {
161+
if (options.addLegacyIncludeGradle || options.addIncludeGradle) {
158162
fs.writeFile(path.join(pluginFolder, INCLUDE_GRADLE_NAME), validIncludeGradleContent);
159163
}
160164
}
@@ -291,12 +295,23 @@ describe('androidPluginBuildService', () => {
291295
addLegacyIncludeGradle: true
292296
});
293297

294-
await androidBuildPluginService.migrateIncludeGradle(config);
295-
298+
const isMigrated = await androidBuildPluginService.migrateIncludeGradle(config);
296299
const includeGradleContent = fs.readText(path.join(pluginFolder, INCLUDE_GRADLE_NAME).toString());
297300
const areProductFlavorsRemoved = includeGradleContent.indexOf("productFlavors") === -1;
301+
302+
assert.isTrue(isMigrated);
298303
assert.isTrue(areProductFlavorsRemoved);
299304
});
305+
306+
it('if there is an already migrated include.gradle file', async () => {
307+
const config: IBuildOptions = setup({
308+
addIncludeGradle: true
309+
});
310+
311+
const isMigrated = await androidBuildPluginService.migrateIncludeGradle(config);
312+
313+
assert.isFalse(isMigrated);
314+
});
300315
});
301316

302317
function getGradleAndroidPluginVersion() {

0 commit comments

Comments
 (0)