Skip to content

Commit 807b4b6

Browse files
committed
refactor: update after review
1 parent 9e285ca commit 807b4b6

File tree

8 files changed

+34
-39
lines changed

8 files changed

+34
-39
lines changed

lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const SRC_DIR = "src";
2424
export const MAIN_DIR = "main";
2525
export const ASSETS_DIR = "assets";
2626
export const MANIFEST_FILE_NAME = "AndroidManifest.xml";
27+
export const INCLUDE_GRADLE_NAME = "include.gradle";
2728
export const BUILD_DIR = "build";
2829
export const OUTPUTS_DIR = "outputs";
2930
export const APK_DIR = "apk";

lib/definitions/android-plugin-migrator.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

2-
interface IBuildOptions {
2+
interface IBuildOptions extends IAndroidBuildOptions{
3+
}
4+
5+
interface IAndroidBuildOptions {
36
platformsAndroidDirPath: string,
47
pluginName: string,
58
aarOutputDir: string,

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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as path from "path";
2-
import * as shell from "shelljs";
2+
import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR } from "../constants";
33
import { Builder, parseString } from "xml2js";
4+
import { ILogger } from "log4js";
45

56
export class AndroidPluginBuildService implements IAndroidPluginBuildService {
67

78
constructor(private $fs: IFileSystem,
89
private $childProcess: IChildProcess,
910
private $hostInfo: IHostInfo,
10-
private $androidToolsInfo: IAndroidToolsInfo) { }
11+
private $androidToolsInfo: IAndroidToolsInfo,
12+
private $logger: ILogger) { }
1113

12-
private static ANDROID_MANIFEST_XML = "AndroidManifest.xml";
13-
private static INCLUDE_GRADLE = "include.gradle";
1414
private static MANIFEST_ROOT = {
1515
$: {
1616
"xmlns:android": "http://schemas.android.com/apk/res/android"
@@ -19,7 +19,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
1919
private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin";
2020

2121
private getAndroidSourceDirectories(source: string): Array<string> {
22-
const directories = ["res", "java", "assets", "jniLibs"];
22+
const directories = [RESOURCES_DIR, "java", ASSETS_DIR, "jniLibs"];
2323
const resultArr: Array<string> = [];
2424

2525
this.$fs.enumerateFilesInDirectorySync(source, (file, stat) => {
@@ -33,7 +33,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
3333
}
3434

3535
private getManifest(platformsDir: string): string {
36-
const manifest = path.join(platformsDir, AndroidPluginBuildService.ANDROID_MANIFEST_XML);
36+
const manifest = path.join(platformsDir, MANIFEST_FILE_NAME);
3737
return this.$fs.exists(manifest) ? manifest : null;
3838
}
3939

@@ -42,10 +42,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
4242
}
4343

4444
private async updateManifestContent(oldManifestContent: string, defaultPackageName: string): Promise<string> {
45-
const content = oldManifestContent;
46-
let newManifestContent;
47-
48-
let xml: any = await this.getXml(content);
45+
let xml: any = await this.getXml(oldManifestContent);
4946

5047
let packageName = defaultPackageName;
5148
// if the manifest file is full-featured and declares settings inside the manifest scope
@@ -67,17 +64,16 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
6764
newManifest.manifest["$"]["package"] = packageName;
6865

6966
const xmlBuilder = new Builder();
70-
newManifestContent = xmlBuilder.buildObject(newManifest);
67+
const newManifestContent = xmlBuilder.buildObject(newManifest);
7168

7269
return newManifestContent;
7370
}
7471

7572
private createManifestContent(packageName: string): string {
76-
let newManifestContent;
7773
const newManifest: any = { manifest: AndroidPluginBuildService.MANIFEST_ROOT };
7874
newManifest.manifest["$"]["package"] = packageName;
7975
const xmlBuilder: any = new Builder();
80-
newManifestContent = xmlBuilder.buildObject(newManifest);
76+
const newManifestContent = xmlBuilder.buildObject(newManifest);
8177

8278
return newManifestContent;
8379
}
@@ -96,10 +92,6 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
9692
return promise;
9793
}
9894

99-
private copyRecursive(source: string, destination: string): void {
100-
shell.cp("-R", source, destination);
101-
}
102-
10395
private getIncludeGradleCompileDependenciesScope(includeGradleFileContent: string): Array<string> {
10496
const indexOfDependenciesScope = includeGradleFileContent.indexOf("dependencies");
10597
const result: Array<string> = [];
@@ -125,23 +117,23 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
125117
private getScope(scopeName: string, content: string): string {
126118
const indexOfScopeName = content.indexOf(scopeName);
127119
let result = "";
128-
const OPENING_BRACKET = "{";
129-
const CLOSING_BRACKET = "}";
120+
const openingBracket = "{";
121+
const closingBracket = "}";
130122
let openBrackets = 0;
131123
let foundFirstBracket = false;
132124

133125
let i = indexOfScopeName;
134126
while (i < content.length) {
135127
const currCharacter = content[i];
136-
if (currCharacter === OPENING_BRACKET) {
128+
if (currCharacter === openingBracket) {
137129
if (openBrackets === 0) {
138130
foundFirstBracket = true;
139131
}
140132

141133
openBrackets++;
142134
}
143135

144-
if (currCharacter === CLOSING_BRACKET) {
136+
if (currCharacter === closingBracket) {
145137
openBrackets--;
146138
}
147139

@@ -180,7 +172,6 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
180172
// find manifest file
181173
//// prepare manifest file content
182174
const manifestFilePath = this.getManifest(options.platformsAndroidDirPath);
183-
let updatedManifestContent;
184175
let shouldBuildAar = false;
185176

186177
// look for AndroidManifest.xml
@@ -197,6 +188,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
197188

198189
// if a manifest OR/AND resource files are present - write files, build plugin
199190
if (shouldBuildAar) {
191+
let updatedManifestContent;
200192
this.$fs.ensureDirectoryExists(newPluginMainSrcDir);
201193

202194
if (manifestFilePath) {
@@ -215,7 +207,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
215207
}
216208

217209
// write the AndroidManifest in the temp-dir/plugin-name/src/main
218-
const pathToNewAndroidManifest = path.join(newPluginMainSrcDir, AndroidPluginBuildService.ANDROID_MANIFEST_XML);
210+
const pathToNewAndroidManifest = path.join(newPluginMainSrcDir, MANIFEST_FILE_NAME);
219211
try {
220212
this.$fs.writeFile(pathToNewAndroidManifest, updatedManifestContent);
221213
} catch (e) {
@@ -231,14 +223,14 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
231223
const destination = path.join(newPluginMainSrcDir, dirName);
232224
this.$fs.ensureDirectoryExists(destination);
233225

234-
this.copyRecursive(path.join(dir, "*"), destination);
226+
this.$fs.copyFile(path.join(dir, "*"), destination);
235227
}
236228

237229
// copy the preconfigured gradle android library project template to the temporary android library
238-
this.copyRecursive(path.join(path.resolve(path.join(__dirname, AndroidPluginBuildService.ANDROID_PLUGIN_GRADLE_TEMPLATE), "*")), newPluginDir);
230+
this.$fs.copyFile(path.join(path.resolve(path.join(__dirname, AndroidPluginBuildService.ANDROID_PLUGIN_GRADLE_TEMPLATE), "*")), newPluginDir);
239231

240232
// sometimes the AndroidManifest.xml or certain resources in /res may have a compile dependency to a library referenced in include.gradle. Make sure to compile the plugin with a compile dependency to those libraries
241-
const includeGradlePath = path.join(options.platformsAndroidDirPath, "include.gradle");
233+
const includeGradlePath = path.join(options.platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
242234
if (this.$fs.exists(includeGradlePath)) {
243235
const includeGradleContent = this.$fs.readText(includeGradlePath);
244236
const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent);
@@ -280,7 +272,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
280272
if (this.$fs.exists(pathToBuiltAar)) {
281273
try {
282274
if (options.aarOutputDir) {
283-
this.copyRecursive(pathToBuiltAar, path.join(options.aarOutputDir, `${shortPluginName}.aar`));
275+
this.$fs.copyFile(pathToBuiltAar, path.join(options.aarOutputDir, `${shortPluginName}.aar`));
284276
}
285277
} catch (e) {
286278
throw new Error(`Failed to copy built aar to destination. ${e.message}`);
@@ -302,7 +294,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
302294
public migrateIncludeGradle(options: IBuildOptions): void {
303295
this.validatePlatformsAndroidDirPathOption(options);
304296

305-
const includeGradleFilePath = path.join(options.platformsAndroidDirPath, AndroidPluginBuildService.INCLUDE_GRADLE);
297+
const includeGradleFilePath = path.join(options.platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
306298

307299
if (this.$fs.exists(includeGradleFilePath)) {
308300
let includeGradleFileContent: string;
@@ -330,11 +322,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
330322
}
331323

332324
if (!options.pluginName) {
333-
console.log("No plugin name provided, defaulting to 'myPlugin'.");
325+
this.$logger.info("No plugin name provided, defaulting to 'myPlugin'.");
334326
}
335327

336328
if (!options.aarOutputDir) {
337-
console.log("No aarOutputDir provided, defaulting to the build outputs directory of the plugin");
329+
this.$logger.info("No aarOutputDir provided, defaulting to the build outputs directory of the plugin");
338330
}
339331

340332
if (!options.tempPluginDirPath) {

lib/services/android-project-service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
453453
return path.resolve(pathToPlatformsAndroid, item.directory);
454454
});
455455

456-
for (const dependencyKey in productionDependencies) {
457-
const dependency = productionDependencies[dependencyKey];
456+
for (const dependency of productionDependencies) {
458457
const jsonContent = this.$fs.readJson(path.join(dependency, constants.PACKAGE_JSON_FILE_NAME));
459458
const isPlugin = !!jsonContent.nativescript;
460459
const pluginName = jsonContent.name;

lib/services/platform-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export class PlatformService extends EventEmitter implements IPlatformService {
203203

204204
public async preparePlatform(platformInfo: IPreparePlatformInfo): Promise<boolean> {
205205
const changesInfo = await this.getChangesInfo(platformInfo);
206-
const shouldPrepare = changesInfo.nativeChanged || await this.shouldPrepare({ platformInfo, changesInfo });
206+
const shouldPrepare = await this.shouldPrepare({ platformInfo, changesInfo });
207207

208208
if (shouldPrepare) {
209209
// Always clear up the app directory in platforms if `--bundle` value has changed in between builds or is passed in general

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"winreg": "0.0.17",
8181
"ws": "2.2.0",
8282
"xcode": "https://github.com/NativeScript/node-xcode/archive/1.4.0.tar.gz",
83-
"xml2js": "^0.4.19",
83+
"xml2js": "0.4.19",
8484
"xmldom": "0.1.21",
8585
"xmlhttprequest": "https://github.com/telerik/node-XMLHttpRequest/tarball/master",
8686
"yargs": "6.0.0",

test/ios-entitlements-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("IOSEntitlements Service Tests", () => {
4747
injector = createTestInjector();
4848

4949
platformsData = injector.resolve("platformsData");
50-
projectData = $injector.resolve<IProjectData>("projectData");
50+
projectData = injector.resolve<IProjectData>("projectData");
5151
projectData.projectName = 'testApp';
5252

5353
projectData.platformsDir = temp.mkdirSync("platformsDir");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ dependencies {
110110
await androidBuildPluginService.buildAar(config);
111111
const hasGeneratedAar = fs.exists(path.join(tempFolder, expectedAarName));
112112

113-
assert.equal(hasGeneratedAar, true);
113+
assert.isTrue(hasGeneratedAar);
114114
});
115115

116116
it('if android manifest is missing', async () => {
@@ -126,7 +126,7 @@ dependencies {
126126
await androidBuildPluginService.buildAar(config);
127127
const hasGeneratedAar = fs.exists(path.join(tempFolder, expectedAarName));
128128

129-
assert.equal(hasGeneratedAar, true);
129+
assert.isTrue(hasGeneratedAar);
130130
});
131131

132132
it('if there is only an android manifest file', async () => {
@@ -142,7 +142,7 @@ dependencies {
142142
await androidBuildPluginService.buildAar(config);
143143
const hasGeneratedAar = fs.exists(path.join(tempFolder, expectedAarName));
144144

145-
assert.equal(hasGeneratedAar, true);
145+
assert.isTrue(hasGeneratedAar);
146146
});
147147
});
148148

0 commit comments

Comments
 (0)