From 983c3b7d68eeb00ee9d071d87269e856c4fd299a Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Tue, 3 Jul 2018 16:16:32 +0300 Subject: [PATCH 1/7] refactor: make the buildAar method shorter and more readable --- lib/definitions/android-plugin-migrator.d.ts | 2 +- lib/services/android-plugin-build-service.ts | 170 +++++++++---------- 2 files changed, 82 insertions(+), 90 deletions(-) diff --git a/lib/definitions/android-plugin-migrator.d.ts b/lib/definitions/android-plugin-migrator.d.ts index 4ede88b918..0949327521 100644 --- a/lib/definitions/android-plugin-migrator.d.ts +++ b/lib/definitions/android-plugin-migrator.d.ts @@ -33,5 +33,5 @@ interface IBuildAndroidPluginData { /** * Information about tools that will be used to build the plugin, for example compile SDK version, build tools version, etc. */ - androidToolsInfo: IAndroidToolsInfoData; + androidToolsInfo?: IAndroidToolsInfoData; } diff --git a/lib/services/android-plugin-build-service.ts b/lib/services/android-plugin-build-service.ts index 6532e4bede..7774c7d582 100644 --- a/lib/services/android-plugin-build-service.ts +++ b/lib/services/android-plugin-build-service.ts @@ -5,7 +5,6 @@ import { Builder, parseString } from "xml2js"; import { ILogger } from "log4js"; export class AndroidPluginBuildService implements IAndroidPluginBuildService { - /** * Required for hooks execution to work. */ @@ -164,113 +163,101 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { */ public async buildAar(options: IBuildOptions): Promise { this.validateOptions(options); - - // IDEA: Accept as an additional parameter the Android Support Library version from CLI, pass it on as -PsupportVersion later to the build - // IDEA: apply app.gradle here in order to get any and all user-defined variables - // IDEA: apply the entire include.gradle here instead of copying over the repositories {} and dependencies {} scopes - - const shortPluginName = getShortPluginName(options.pluginName); - const newPluginDir = path.join(options.tempPluginDirPath, shortPluginName); - const newPluginMainSrcDir = path.join(newPluginDir, "src", "main"); - const defaultPackageName = "org.nativescript." + shortPluginName; - - // find manifest file - //// prepare manifest file content const manifestFilePath = this.getManifest(options.platformsAndroidDirPath); - let shouldBuildAar = false; - - // look for AndroidManifest.xml - if (manifestFilePath) { - shouldBuildAar = true; - } - - // look for android resources const androidSourceSetDirectories = this.getAndroidSourceDirectories(options.platformsAndroidDirPath); + const shouldBuildAar = !!manifestFilePath || androidSourceSetDirectories.length > 0; - if (androidSourceSetDirectories.length > 0) { - shouldBuildAar = true; - } - - // if a manifest OR/AND resource files are present - write files, build plugin if (shouldBuildAar) { - let updatedManifestContent; - this.$fs.ensureDirectoryExists(newPluginMainSrcDir); - - if (manifestFilePath) { - let androidManifestContent; - try { - androidManifestContent = this.$fs.readText(manifestFilePath); - } catch (err) { - throw new Error( - `Failed to fs.readFileSync the manifest file located at ${manifestFilePath}` - ); - } + const shortPluginName = getShortPluginName(options.pluginName); + const pluginTempDir = path.join(options.tempPluginDirPath, shortPluginName); + const pluginTempMainSrcDir = path.join(pluginTempDir, "src", "main"); + + await this.updateManifest(manifestFilePath, pluginTempMainSrcDir, shortPluginName); + this.copySourceSetDirectories(androidSourceSetDirectories, pluginTempMainSrcDir); + this.setupGradle(pluginTempDir, options.platformsAndroidDirPath); + await this.buildPlugin({ pluginDir: pluginTempDir, pluginName: options.pluginName }); + this.copyAar(shortPluginName, pluginTempDir, options.aarOutputDir); + } - updatedManifestContent = await this.updateManifestContent(androidManifestContent, defaultPackageName); - } else { - updatedManifestContent = this.createManifestContent(defaultPackageName); - } + return shouldBuildAar; + } - // write the AndroidManifest in the temp-dir/plugin-name/src/main - const pathToNewAndroidManifest = path.join(newPluginMainSrcDir, MANIFEST_FILE_NAME); + private async updateManifest(manifestFilePath: string, pluginTempMainSrcDir: string, shortPluginName: string): Promise { + let updatedManifestContent; + this.$fs.ensureDirectoryExists(pluginTempMainSrcDir); + const defaultPackageName = "org.nativescript." + shortPluginName; + if (manifestFilePath) { + let androidManifestContent; try { - this.$fs.writeFile(pathToNewAndroidManifest, updatedManifestContent); - } catch (e) { - throw new Error(`Failed to write the updated AndroidManifest in the new location - ${pathToNewAndroidManifest}`); + androidManifestContent = this.$fs.readText(manifestFilePath); + } catch (err) { + throw new Error( + `Failed to fs.readFileSync the manifest file located at ${manifestFilePath}` + ); } - // copy all android sourceset directories to the new temporary plugin dir - for (const dir of androidSourceSetDirectories) { - // get only the last subdirectory of the entire path string. e.g. 'res', 'java', etc. - const dirNameParts = dir.split(path.sep); - const dirName = dirNameParts[dirNameParts.length - 1]; + updatedManifestContent = await this.updateManifestContent(androidManifestContent, defaultPackageName); + } else { + updatedManifestContent = this.createManifestContent(defaultPackageName); + } - const destination = path.join(newPluginMainSrcDir, dirName); - this.$fs.ensureDirectoryExists(destination); + const pathToTempAndroidManifest = path.join(pluginTempMainSrcDir, MANIFEST_FILE_NAME); + try { + this.$fs.writeFile(pathToTempAndroidManifest, updatedManifestContent); + } catch (e) { + throw new Error(`Failed to write the updated AndroidManifest in the new location - ${pathToTempAndroidManifest}`); + } + } - this.$fs.copyFile(path.join(dir, "*"), destination); - } + private copySourceSetDirectories(androidSourceSetDirectories: string[], pluginTempMainSrcDir: string): void { + for (const dir of androidSourceSetDirectories) { + const dirNameParts = dir.split(path.sep); + // get only the last subdirectory of the entire path string. e.g. 'res', 'java', etc. + const dirName = dirNameParts[dirNameParts.length - 1]; + const destination = path.join(pluginTempMainSrcDir, dirName); - // copy the preconfigured gradle android library project template to the temporary android library - this.$fs.copyFile(path.join(path.resolve(path.join(__dirname, AndroidPluginBuildService.ANDROID_PLUGIN_GRADLE_TEMPLATE), "*")), newPluginDir); + this.$fs.ensureDirectoryExists(destination); + this.$fs.copyFile(path.join(dir, "*"), destination); + } + } - // 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 - const includeGradlePath = path.join(options.platformsAndroidDirPath, INCLUDE_GRADLE_NAME); - if (this.$fs.exists(includeGradlePath)) { - const includeGradleContent = this.$fs.readText(includeGradlePath); - const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent); + private setupGradle(pluginTempDir: string, platformsAndroidDirPath: string): void { + const gradleTemplatePath = path.resolve(path.join(__dirname, AndroidPluginBuildService.ANDROID_PLUGIN_GRADLE_TEMPLATE)); + const allGradleTemplateFiles = path.join(gradleTemplatePath, "*"); - // dependencies { } object was found - append dependencies scope - if (repositoriesAndDependenciesScopes.length > 0) { - const buildGradlePath = path.join(newPluginDir, "build.gradle"); - this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n")); - } - } + this.$fs.copyFile(allGradleTemplateFiles, pluginTempDir); + this.addCompileDependencies(platformsAndroidDirPath, pluginTempDir); + // TODO: replace placeholders in target destination + } - // finally build the plugin - this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true }); - const androidToolsInfo = this.$androidToolsInfo.getToolsInfo(); - await this.buildPlugin( { pluginDir: newPluginDir, pluginName: options.pluginName, androidToolsInfo }); - - const finalAarName = `${shortPluginName}-release.aar`; - const pathToBuiltAar = path.join(newPluginDir, "build", "outputs", "aar", finalAarName); - - if (this.$fs.exists(pathToBuiltAar)) { - try { - if (options.aarOutputDir) { - this.$fs.copyFile(pathToBuiltAar, path.join(options.aarOutputDir, `${shortPluginName}.aar`)); - } - } catch (e) { - throw new Error(`Failed to copy built aar to destination. ${e.message}`); - } + private addCompileDependencies(platformsAndroidDirPath: string, pluginTempDir: string): void { + const includeGradlePath = path.join(platformsAndroidDirPath, INCLUDE_GRADLE_NAME); + if (this.$fs.exists(includeGradlePath)) { + const includeGradleContent = this.$fs.readText(includeGradlePath); + const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent); - return true; - } else { - throw new Error(`No built aar found at ${pathToBuiltAar}`); + if (repositoriesAndDependenciesScopes.length > 0) { + const buildGradlePath = path.join(pluginTempDir, "build.gradle"); + this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n")); } } + } - return false; + private copyAar(shortPluginName: string, pluginTempDir: string, aarOutputDir: string): void { + const finalAarName = `${shortPluginName}-release.aar`; + const pathToBuiltAar = path.join(pluginTempDir, "build", "outputs", "aar", finalAarName); + + if (this.$fs.exists(pathToBuiltAar)) { + try { + if (aarOutputDir) { + this.$fs.copyFile(pathToBuiltAar, path.join(aarOutputDir, `${shortPluginName}.aar`)); + } + } catch (e) { + throw new Error(`Failed to copy built aar to destination. ${e.message}`); + } + } else { + throw new Error(`No built aar found at ${pathToBuiltAar}`); + } } /** @@ -307,6 +294,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { @hook("buildAndroidPlugin") private async buildPlugin(pluginBuildSettings: IBuildAndroidPluginData): Promise { + if (!pluginBuildSettings.androidToolsInfo) { + this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true }); + pluginBuildSettings.androidToolsInfo = this.$androidToolsInfo.getToolsInfo(); + } + const gradlew = this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew"; const localArgs = [ From 543ded62332f7780bdfea5a7cc9f2cbcc106498c Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Tue, 3 Jul 2018 16:57:01 +0300 Subject: [PATCH 2/7] feat: use the Gradle and Android plugin versions specified in the package.json of the runtime instead of hardcoded ones --- lib/declarations.d.ts | 8 +++ lib/definitions/android-plugin-migrator.d.ts | 1 + lib/node-package-manager.ts | 2 +- lib/services/android-plugin-build-service.ts | 70 ++++++++++++++++--- lib/services/android-project-service.ts | 11 +-- vendor/gradle-plugin/build.gradle | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- 7 files changed, 74 insertions(+), 22 deletions(-) diff --git a/lib/declarations.d.ts b/lib/declarations.d.ts index 4ab4a6497b..ca9107238a 100644 --- a/lib/declarations.d.ts +++ b/lib/declarations.d.ts @@ -850,3 +850,11 @@ interface IAssetsGenerationService { */ generateSplashScreens(splashesGenerationData: ISplashesGenerationData): Promise; } + +/** + * Describes the Gradle versions specified in the package.json of the Android runtime + */ +interface IRuntimeGradleVersions { + gradleVersion?: string; + gradleAndroidPluginVersion?: string; +} \ No newline at end of file diff --git a/lib/definitions/android-plugin-migrator.d.ts b/lib/definitions/android-plugin-migrator.d.ts index 0949327521..363108ee6a 100644 --- a/lib/definitions/android-plugin-migrator.d.ts +++ b/lib/definitions/android-plugin-migrator.d.ts @@ -1,5 +1,6 @@ interface IBuildOptions extends IAndroidBuildOptions{ + projectDir?: string } interface IAndroidBuildOptions { diff --git a/lib/node-package-manager.ts b/lib/node-package-manager.ts index b8da3ee23a..0bdadc059b 100644 --- a/lib/node-package-manager.ts +++ b/lib/node-package-manager.ts @@ -123,7 +123,7 @@ export class NodePackageManager implements INodePackageManager { const url = `https://registry.npmjs.org/${packageName}`; this.$logger.trace(`Trying to get data from npm registry for package ${packageName}, url is: ${url}`); const responseData = (await this.$httpClient.httpRequest(url)).body; - this.$logger.trace(`Successfully received data from npm registry for package ${packageName}. Response data is: ${responseData}`); + this.$logger.trace(`Successfully received data from npm registry for package ${packageName}.`); const jsonData = JSON.parse(responseData); this.$logger.trace(`Successfully parsed data from npm registry for package ${packageName}.`); return jsonData; diff --git a/lib/services/android-plugin-build-service.ts b/lib/services/android-plugin-build-service.ts index 7774c7d582..8f5dce3c01 100644 --- a/lib/services/android-plugin-build-service.ts +++ b/lib/services/android-plugin-build-service.ts @@ -1,5 +1,5 @@ import * as path from "path"; -import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR } from "../constants"; +import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR, TNS_ANDROID_RUNTIME_NAME } from "../constants"; import { getShortPluginName, hook } from "../common/helpers"; import { Builder, parseString } from "xml2js"; import { ILogger } from "log4js"; @@ -12,19 +12,25 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { return this.$injector.resolve("hooksService"); } + private get $platformService(): IPlatformService { + return this.$injector.resolve("platformService"); + } + constructor(private $injector: IInjector, private $fs: IFileSystem, private $childProcess: IChildProcess, private $hostInfo: IHostInfo, private $androidToolsInfo: IAndroidToolsInfo, - private $logger: ILogger) { } + private $logger: ILogger, + private $npm: INodePackageManager, + private $projectDataService: IProjectDataService, + private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { } private static MANIFEST_ROOT = { $: { "xmlns:android": "http://schemas.android.com/apk/res/android" } }; - private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin"; private getAndroidSourceDirectories(source: string): Array { const directories = [RESOURCES_DIR, "java", ASSETS_DIR, "jniLibs"]; @@ -174,7 +180,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { await this.updateManifest(manifestFilePath, pluginTempMainSrcDir, shortPluginName); this.copySourceSetDirectories(androidSourceSetDirectories, pluginTempMainSrcDir); - this.setupGradle(pluginTempDir, options.platformsAndroidDirPath); + await this.setupGradle(pluginTempDir, options.platformsAndroidDirPath, options.projectDir); await this.buildPlugin({ pluginDir: pluginTempDir, pluginName: options.pluginName }); this.copyAar(shortPluginName, pluginTempDir, options.aarOutputDir); } @@ -221,23 +227,67 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { } } - private setupGradle(pluginTempDir: string, platformsAndroidDirPath: string): void { - const gradleTemplatePath = path.resolve(path.join(__dirname, AndroidPluginBuildService.ANDROID_PLUGIN_GRADLE_TEMPLATE)); + private async setupGradle(pluginTempDir: string, platformsAndroidDirPath: string, projectDir: string): Promise { + const gradleTemplatePath = path.resolve(path.join(__dirname, "../../vendor/gradle-plugin")); const allGradleTemplateFiles = path.join(gradleTemplatePath, "*"); + const buildGradlePath = path.join(pluginTempDir, "build.gradle"); this.$fs.copyFile(allGradleTemplateFiles, pluginTempDir); - this.addCompileDependencies(platformsAndroidDirPath, pluginTempDir); - // TODO: replace placeholders in target destination + this.addCompileDependencies(platformsAndroidDirPath, buildGradlePath); + const runtimeGradleVersions = await this.getRuntimeGradleVersions(projectDir); + this.replaceGradleVersion(pluginTempDir, runtimeGradleVersions.gradleVersion); + this.replaceGradleAndroidPluginVersion(buildGradlePath, runtimeGradleVersions.gradleAndroidPluginVersion); + } + + private async getRuntimeGradleVersions(projectDir: string): Promise { + const runtimeVersions: IRuntimeGradleVersions = {}; + const registryData = await this.$npm.getRegistryPackageData(TNS_ANDROID_RUNTIME_NAME); + let runtimeVersion: string = registryData["dist-tags"].latest; + if (projectDir) { + runtimeVersion = this.$platformService.getCurrentPlatformVersion( + this.$devicePlatformsConstants.Android, + this.$projectDataService.getProjectData(projectDir)); + } + + const latestPackageData = registryData.versions[runtimeVersion]; + const packageJsonGradle = latestPackageData && latestPackageData.gradle; + if (packageJsonGradle) { + runtimeVersions.gradleVersion = packageJsonGradle.version; + runtimeVersions.gradleAndroidPluginVersion = packageJsonGradle.android; + } + + return runtimeVersions; + } + + private replaceGradleVersion(pluginTempDir: string, version: string): void { + const gradleVersion = version || "4.4"; + const gradleVersionPlaceholder = "{{runtimeGradleVersion}}"; + const gradleWrapperPropertiesPath = path.join(pluginTempDir, "gradle", "wrapper", "gradle-wrapper.properties"); + + this.replaceFileContent(gradleWrapperPropertiesPath, gradleVersionPlaceholder, gradleVersion); + } + + private replaceGradleAndroidPluginVersion(buildGradlePath: string, version: string): void { + const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}"; + const gradleAndroidPluginVersion = version || "3.1.2"; + + this.replaceFileContent(buildGradlePath, gradleAndroidPluginVersionPlaceholder,gradleAndroidPluginVersion); + } + + private replaceFileContent(filePath: string, content: string, replacement: string) { + const fileContent = this.$fs.readText(filePath); + const contentRegex = new RegExp(content, "g"); + const replacedFileContent = fileContent.replace(contentRegex, replacement); + this.$fs.writeFile(filePath, replacedFileContent); } - private addCompileDependencies(platformsAndroidDirPath: string, pluginTempDir: string): void { + private addCompileDependencies(platformsAndroidDirPath: string, buildGradlePath: string): void { const includeGradlePath = path.join(platformsAndroidDirPath, INCLUDE_GRADLE_NAME); if (this.$fs.exists(includeGradlePath)) { const includeGradleContent = this.$fs.readText(includeGradlePath); const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent); if (repositoriesAndDependenciesScopes.length > 0) { - const buildGradlePath = path.join(pluginTempDir, "build.gradle"); this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n")); } } diff --git a/lib/services/android-project-service.ts b/lib/services/android-project-service.ts index 87df48fc29..00970707e6 100644 --- a/lib/services/android-project-service.ts +++ b/lib/services/android-project-service.ts @@ -100,9 +100,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject return this._platformData; } - // TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov - // Similar to the private method of the same name in platform-service. - private getCurrentPlatformVersion(platformData: IPlatformData, projectData: IProjectData): string { + public getCurrentPlatformVersion(platformData: IPlatformData, projectData: IProjectData): string { const currentPlatformData: IDictionary = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); return currentPlatformData && currentPlatformData[constants.VERSION_STRING]; @@ -422,6 +420,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject const pluginPlatformsFolderPath = this.getPluginPlatformsFolderPath(pluginData, AndroidProjectService.ANDROID_PLATFORM_NAME); if (this.$fs.exists(pluginPlatformsFolderPath)) { const options: IBuildOptions = { + projectDir: projectData.projectDir, pluginName: pluginData.name, platformsAndroidDirPath: pluginPlatformsFolderPath, aarOutputDir: pluginPlatformsFolderPath, @@ -533,9 +532,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject public async removePluginNativeCode(pluginData: IPluginData, projectData: IProjectData): Promise { try { - // check whether the dependency that's being removed has native code - // TODO: Remove prior to the 4.1 CLI release @Pip3r4o @PanayotCankov - // the updated gradle script will take care of cleaning the prepared android plugins if (!this.runtimeVersionIsGreaterThanOrEquals(projectData, "3.3.0")) { const pluginConfigDir = path.join(this.getPlatformData(projectData).projectRoot, "configurations", pluginData.name); if (this.$fs.exists(pluginConfigDir)) { @@ -563,8 +559,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject if (shouldUseNewRoutine) { this.provideDependenciesJson(projectData, dependencies); } else { - // TODO: Remove prior to the 4.1 CLI release @Pip3r4o @PanayotCankov - const platformDir = path.join(projectData.platformsDir, AndroidProjectService.ANDROID_PLATFORM_NAME); const buildDir = path.join(platformDir, "build-tools"); const checkV8dependants = path.join(buildDir, "check-v8-dependants.js"); @@ -580,7 +574,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject } if (!shouldUseNewRoutine) { - // TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov const projectRoot = this.getPlatformData(projectData).projectRoot; await this.cleanProject(projectRoot, projectData); } diff --git a/vendor/gradle-plugin/build.gradle b/vendor/gradle-plugin/build.gradle index a6ae420e71..9c9e9dde2a 100644 --- a/vendor/gradle-plugin/build.gradle +++ b/vendor/gradle-plugin/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.2' + classpath 'com.android.tools.build:gradle:{{runtimeAndroidPluginVersion}}' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties b/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties index 234361767e..ac497025b8 100644 --- a/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties +++ b/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-{{runtimeGradleVersion}}-bin.zip From 9ae5b0a5e4564cfc50f683a6ee0c7fb4bfab03a9 Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Wed, 4 Jul 2018 18:08:37 +0300 Subject: [PATCH 3/7] test: add unit tests for using the Gradle and Android plugin versions specified in the package.json of the runtime instead of hardcoded ones --- test/services/android-plugin-build-service.ts | 121 ++++++++++++++++-- 1 file changed, 110 insertions(+), 11 deletions(-) diff --git a/test/services/android-plugin-build-service.ts b/test/services/android-plugin-build-service.ts index b8dc65f670..31fb1aedee 100644 --- a/test/services/android-plugin-build-service.ts +++ b/test/services/android-plugin-build-service.ts @@ -8,17 +8,28 @@ import { Logger } from "../../lib/common/logger"; import * as ErrorsLib from "../../lib/common/errors"; import temp = require("temp"); import { INCLUDE_GRADLE_NAME } from "../../lib/constants"; -temp.track(); +import * as stubs from "../stubs"; +import { DevicePlatformsConstants } from "../../lib/common/mobile/device-platforms-constants"; +import { getShortPluginName } from "../../lib/common/helpers"; -describe('androiPluginBuildService', () => { +temp.track(); +describe.only('androiPluginBuildService', () => { let spawnFromEventCalled = false; - const createTestInjector = (): IInjector => { - const testInjector = new Yok(); + let testInjector: IInjector; + let fs: IFileSystem; + let androidBuildPluginService: AndroidPluginBuildService; + let tempFolder: string; + let pluginFolder: string; + function setupTestInjector(): IInjector { + testInjector = new Yok(); testInjector.register("fs", FsLib.FileSystem); testInjector.register("childProcess", { spawnFromEvent: async (command: string, args: string[], event: string, options?: any, spawnFromEventOptions?: ISpawnFromEventOptions): Promise => { + const finalAarName = `${getShortPluginName("my-plugin")}-release.aar`; + const aar = path.join(pluginFolder, getShortPluginName("my-plugin"), "build", "outputs", "aar", finalAarName); + fs.writeFile(aar, ""); spawnFromEventCalled = command.indexOf("gradlew") !== -1; return null; } @@ -41,15 +52,44 @@ describe('androiPluginBuildService', () => { executeBeforeHooks: async (commandName: string, hookArguments?: IDictionary): Promise => undefined, executeAfterHooks: async (commandName: string, hookArguments?: IDictionary): Promise => undefined }); + testInjector.register('projectDataService', stubs.ProjectDataService); + testInjector.register('platformService', { + getCurrentPlatformVersion: (platform: string, projectData: IProjectData): string => { + console.log("here?"); + return "4.1.2"; + } + }); + testInjector.register('devicePlatformsConstants', DevicePlatformsConstants); + setupNpm(); return testInjector; - }; + } - let testInjector: IInjector; - let fs: IFileSystem; - let androidBuildPluginService: AndroidPluginBuildService; - let tempFolder: string; - let pluginFolder: string; + function setupNpm(gradleVersion?: string, gradleAndroidVersion?: string): void { + testInjector.register('npm', { + getRegistryPackageData: async (packageName: string): Promise => { + const result: any = []; + result["dist-tags"] = { latest: '1.0.0' }; + result.versions = []; + result.versions['1.0.0'] = { + "name": packageName, + "gradle": { + "version": gradleVersion || "1.0.0", + "android": gradleAndroidVersion || "1.0.0" + } + }; + result.versions['4.1.2'] = { + "name": packageName, + "gradle": { + "version": "1.0.0", + "android": "1.0.0" + } + }; + + return result; + } + }); + } function setUpIncludeGradle() { fs = testInjector.resolve("fs"); @@ -107,7 +147,7 @@ dependencies { } before(() => { - testInjector = createTestInjector(); + setupTestInjector(); androidBuildPluginService = testInjector.resolve(AndroidPluginBuildService); }); @@ -135,6 +175,65 @@ dependencies { assert.isTrue(spawnFromEventCalled); }); + it('should use the latest runtime gradle versions when no project dir specified', async () => { + const gradleVersion = "1.2.3"; + const gradleAndroidPluginVersion = "4.5.6"; + setupNpm(gradleVersion, gradleAndroidPluginVersion); + androidBuildPluginService = testInjector.resolve(AndroidPluginBuildService); + setUpPluginNativeFolder(true, false, false); + const config: IBuildOptions = { + platformsAndroidDirPath: tempFolder, + pluginName: "my-plugin", + aarOutputDir: tempFolder, + tempPluginDirPath: pluginFolder + }; + + await androidBuildPluginService.buildAar(config); + + const gradleWrappersContent = fs.readText(path.join(pluginFolder, getShortPluginName("my-plugin"), "build.gradle").toString()); + const androidVersionRegex = /com\.android\.tools\.build\:gradle\:(.*)\'\n/g; + const actualAndroidVersion = androidVersionRegex.exec(gradleWrappersContent)[1]; + assert.equal(actualAndroidVersion, gradleAndroidPluginVersion); + + const buildGradleContent = fs.readText( + path.join(pluginFolder, getShortPluginName("my-plugin"), "gradle", "wrapper", "gradle-wrapper.properties").toString()); + const gradleVersionRegex = /gradle\-(.*)\-bin\.zip\n/g; + const actualGradleVersion = gradleVersionRegex.exec(buildGradleContent)[1]; + assert.equal(actualGradleVersion, gradleVersion); + + assert.isTrue(spawnFromEventCalled); + }); + + it.only('should use specified runtime gradle versions from the project dir', async () => { + const gradleVersion = "1.2.3"; + const gradleAndroidPluginVersion = "4.5.6"; + setupNpm(gradleVersion, gradleAndroidPluginVersion); + androidBuildPluginService = testInjector.resolve(AndroidPluginBuildService); + setUpPluginNativeFolder(true, false, false); + const config: IBuildOptions = { + platformsAndroidDirPath: tempFolder, + pluginName: "my-plugin", + aarOutputDir: tempFolder, + tempPluginDirPath: pluginFolder, + projectDir: tempFolder + }; + + await androidBuildPluginService.buildAar(config); + + const gradleWrappersContent = fs.readText(path.join(pluginFolder, getShortPluginName("my-plugin"), "build.gradle").toString()); + const androidVersionRegex = /com\.android\.tools\.build\:gradle\:(.*)\'\n/g; + const actualAndroidVersion = androidVersionRegex.exec(gradleWrappersContent)[1]; + assert.equal(actualAndroidVersion, "1.0.0"); + + const buildGradleContent = fs.readText( + path.join(pluginFolder, getShortPluginName("my-plugin"), "gradle", "wrapper", "gradle-wrapper.properties").toString()); + const gradleVersionRegex = /gradle\-(.*)\-bin\.zip\n/g; + const actualGradleVersion = gradleVersionRegex.exec(buildGradleContent)[1]; + assert.equal(actualGradleVersion, "1.0.0"); + + assert.isTrue(spawnFromEventCalled); + }); + it('if android manifest is missing', async () => { setUpPluginNativeFolder(false, true, true); const config: IBuildOptions = { From 9dcffbe77b7e6b28394d9b6bbf27ed544490d402 Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Wed, 4 Jul 2018 18:46:02 +0300 Subject: [PATCH 4/7] refactor: make the buildAar tests more readable --- lib/services/android-plugin-build-service.ts | 24 +- test/services/android-plugin-build-service.ts | 341 ++++++++++-------- 2 files changed, 204 insertions(+), 161 deletions(-) diff --git a/lib/services/android-plugin-build-service.ts b/lib/services/android-plugin-build-service.ts index 8f5dce3c01..281e93017d 100644 --- a/lib/services/android-plugin-build-service.ts +++ b/lib/services/android-plugin-build-service.ts @@ -240,18 +240,28 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { } private async getRuntimeGradleVersions(projectDir: string): Promise { - const runtimeVersions: IRuntimeGradleVersions = {}; const registryData = await this.$npm.getRegistryPackageData(TNS_ANDROID_RUNTIME_NAME); - let runtimeVersion: string = registryData["dist-tags"].latest; + let runtimeGradleVersions: IRuntimeGradleVersions = null; if (projectDir) { - runtimeVersion = this.$platformService.getCurrentPlatformVersion( + const projectRuntimeVersion = this.$platformService.getCurrentPlatformVersion( this.$devicePlatformsConstants.Android, this.$projectDataService.getProjectData(projectDir)); + runtimeGradleVersions = this.getGradleVersions(registryData.versions[projectRuntimeVersion]); } - const latestPackageData = registryData.versions[runtimeVersion]; - const packageJsonGradle = latestPackageData && latestPackageData.gradle; - if (packageJsonGradle) { + if (!runtimeGradleVersions) { + const latestRuntimeVersion = registryData["dist-tags"].latest; + runtimeGradleVersions = this.getGradleVersions(registryData.versions[latestRuntimeVersion]); + } + + return runtimeGradleVersions || {}; + } + + private getGradleVersions(packageData: any): IRuntimeGradleVersions { + const packageJsonGradle = packageData && packageData.gradle; + let runtimeVersions: IRuntimeGradleVersions = null; + if (packageJsonGradle && (packageJsonGradle.version || packageJsonGradle.android)) { + runtimeVersions = {}; runtimeVersions.gradleVersion = packageJsonGradle.version; runtimeVersions.gradleAndroidPluginVersion = packageJsonGradle.android; } @@ -271,7 +281,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}"; const gradleAndroidPluginVersion = version || "3.1.2"; - this.replaceFileContent(buildGradlePath, gradleAndroidPluginVersionPlaceholder,gradleAndroidPluginVersion); + this.replaceFileContent(buildGradlePath, gradleAndroidPluginVersionPlaceholder, gradleAndroidPluginVersion); } private replaceFileContent(filePath: string, content: string, replacement: string) { diff --git a/test/services/android-plugin-build-service.ts b/test/services/android-plugin-build-service.ts index 31fb1aedee..7d089587b0 100644 --- a/test/services/android-plugin-build-service.ts +++ b/test/services/android-plugin-build-service.ts @@ -14,21 +14,56 @@ import { getShortPluginName } from "../../lib/common/helpers"; temp.track(); -describe.only('androiPluginBuildService', () => { +describe.only('androidPluginBuildService', () => { + const pluginName = 'my-plugin'; + const shortPluginName = getShortPluginName(pluginName); let spawnFromEventCalled = false; - let testInjector: IInjector; let fs: IFileSystem; let androidBuildPluginService: AndroidPluginBuildService; let tempFolder: string; let pluginFolder: string; - function setupTestInjector(): IInjector { - testInjector = new Yok(); + function setup(options?: { + addManifest?: boolean, + addResFolder?: boolean, + addAssetsFolder?: boolean, + addLegacyIncludeGradle?: boolean, + addProjectDir?: boolean, + addProjectRuntime?: boolean, + latestRuntimeGradleVersion?: string, + latestRuntimeGradleAndroidVersion?: string, + projectRuntimeGradleVersion?: string, + projectRuntimeGradleAndroidVersion?: string, + }): IBuildOptions { + options = options || {}; + spawnFromEventCalled = false; + tempFolder = temp.mkdirSync("androidPluginBuildService-temp"); + pluginFolder = temp.mkdirSync("androidPluginBuildService-plugin"); + setupDI(options); + setupPluginFolders(options.addManifest, options.addResFolder, options.addAssetsFolder, options.addLegacyIncludeGradle); + + return { + platformsAndroidDirPath: pluginFolder, + pluginName: pluginName, + aarOutputDir: pluginFolder, + tempPluginDirPath: tempFolder, + projectDir: options.addProjectDir ? pluginFolder : null + }; + } + + function setupDI(options: { + addProjectRuntime?: boolean + latestRuntimeGradleVersion?: string, + latestRuntimeGradleAndroidVersion?: string, + projectRuntimeGradleVersion?: string, + projectRuntimeGradleAndroidVersion?: string, + }): void { + const testInjector: IInjector = new Yok(); testInjector.register("fs", FsLib.FileSystem); testInjector.register("childProcess", { - spawnFromEvent: async (command: string, args: string[], event: string, options?: any, spawnFromEventOptions?: ISpawnFromEventOptions): Promise => { - const finalAarName = `${getShortPluginName("my-plugin")}-release.aar`; - const aar = path.join(pluginFolder, getShortPluginName("my-plugin"), "build", "outputs", "aar", finalAarName); + spawnFromEvent: async (command: string): Promise => { + const finalAarName = `${shortPluginName}-release.aar`; + const aar = path.join(tempFolder, shortPluginName, "build", "outputs", "aar", finalAarName); fs.writeFile(aar, ""); spawnFromEventCalled = command.indexOf("gradlew") !== -1; return null; @@ -55,46 +90,49 @@ describe.only('androiPluginBuildService', () => { testInjector.register('projectDataService', stubs.ProjectDataService); testInjector.register('platformService', { getCurrentPlatformVersion: (platform: string, projectData: IProjectData): string => { - console.log("here?"); - return "4.1.2"; + return options.addProjectRuntime ? "1.0.0" : null; } }); testInjector.register('devicePlatformsConstants', DevicePlatformsConstants); - setupNpm(); + testInjector.register('npm', setupNpm(options)); - return testInjector; + fs = testInjector.resolve("fs"); + androidBuildPluginService = testInjector.resolve(AndroidPluginBuildService); } - function setupNpm(gradleVersion?: string, gradleAndroidVersion?: string): void { - testInjector.register('npm', { + function setupNpm(options: { + latestRuntimeGradleVersion?: string, + latestRuntimeGradleAndroidVersion?: string, + projectRuntimeGradleVersion?: string, + projectRuntimeGradleAndroidVersion?: string, + addProjectRuntime?: boolean + }): any { + return { getRegistryPackageData: async (packageName: string): Promise => { const result: any = []; - result["dist-tags"] = { latest: '1.0.0' }; + result["dist-tags"] = { latest: '4.1.2' }; result.versions = []; result.versions['1.0.0'] = { "name": packageName, "gradle": { - "version": gradleVersion || "1.0.0", - "android": gradleAndroidVersion || "1.0.0" + "version": options.projectRuntimeGradleVersion, + "android": options.projectRuntimeGradleAndroidVersion } }; result.versions['4.1.2'] = { "name": packageName, "gradle": { - "version": "1.0.0", - "android": "1.0.0" + "version": options.latestRuntimeGradleVersion, + "android": options.latestRuntimeGradleAndroidVersion } }; return result; } - }); + }; } function setUpIncludeGradle() { - fs = testInjector.resolve("fs"); - pluginFolder = temp.mkdirSync("AndroidProjectPropertiesManager-temp"); - const validIncludeGradleContent = `android { productFlavors { "nativescript-pro-ui" { @@ -114,11 +152,7 @@ dependencies { fs.writeFile(path.join(pluginFolder, INCLUDE_GRADLE_NAME), validIncludeGradleContent); } - function setUpPluginNativeFolder(manifestFile: boolean, resFolder: boolean, assetsFolder: boolean) { - fs = testInjector.resolve("fs"); - tempFolder = temp.mkdirSync("AndroidProjectPropertiesManager"); - pluginFolder = temp.mkdirSync("AndroidProjectPropertiesManager-temp"); - + function setupPluginFolders(manifestFile: boolean, resFolder: boolean, assetsFolder: boolean, addLegacyIncludeGradle: boolean) { const validAndroidManifestContent = ` `; @@ -130,182 +164,181 @@ dependencies { `; if (manifestFile) { - fs.writeFile(path.join(tempFolder, "AndroidManifest.xml"), validAndroidManifestContent); + fs.writeFile(path.join(pluginFolder, "AndroidManifest.xml"), validAndroidManifestContent); } if (resFolder) { - const valuesFolder = path.join(tempFolder, "res", "values"); + const valuesFolder = path.join(pluginFolder, "res", "values"); fs.createDirectory(valuesFolder); fs.writeFile(path.join(valuesFolder, "strings.xml"), validStringsXmlContent); } if (assetsFolder) { - const imagesFolder = path.join(tempFolder, "assets", "images"); + const imagesFolder = path.join(pluginFolder, "assets", "images"); fs.createDirectory(imagesFolder); fs.writeFile(path.join(imagesFolder, "myicon.png"), "123"); } + + if (addLegacyIncludeGradle) { + setUpIncludeGradle(); + } } - before(() => { - setupTestInjector(); - androidBuildPluginService = testInjector.resolve(AndroidPluginBuildService); - }); + describe('buildAar', () => { - beforeEach(() => { - spawnFromEventCalled = false; - }); + it('builds aar when all supported files are in the plugin', async () => { + const config: IBuildOptions = setup({ + addManifest: true, + addResFolder: true, + addAssetsFolder: true + }); - describe('builds aar', () => { - - it('if supported files are in plugin', async () => { - setUpPluginNativeFolder(true, true, true); - const config: IBuildOptions = { - platformsAndroidDirPath: tempFolder, - pluginName: "my-plugin", - aarOutputDir: tempFolder, - tempPluginDirPath: pluginFolder - }; - - try { - await androidBuildPluginService.buildAar(config); - } catch (e) { - /* intentionally left blank */ - } + await androidBuildPluginService.buildAar(config); assert.isTrue(spawnFromEventCalled); }); - it('should use the latest runtime gradle versions when no project dir specified', async () => { - const gradleVersion = "1.2.3"; - const gradleAndroidPluginVersion = "4.5.6"; - setupNpm(gradleVersion, gradleAndroidPluginVersion); - androidBuildPluginService = testInjector.resolve(AndroidPluginBuildService); - setUpPluginNativeFolder(true, false, false); - const config: IBuildOptions = { - platformsAndroidDirPath: tempFolder, - pluginName: "my-plugin", - aarOutputDir: tempFolder, - tempPluginDirPath: pluginFolder - }; + it('does not build aar when there are no supported files in the plugin', async () => { + const config: IBuildOptions = setup(); await androidBuildPluginService.buildAar(config); - const gradleWrappersContent = fs.readText(path.join(pluginFolder, getShortPluginName("my-plugin"), "build.gradle").toString()); - const androidVersionRegex = /com\.android\.tools\.build\:gradle\:(.*)\'\n/g; - const actualAndroidVersion = androidVersionRegex.exec(gradleWrappersContent)[1]; - assert.equal(actualAndroidVersion, gradleAndroidPluginVersion); + assert.isFalse(spawnFromEventCalled); + }); + + it('builds aar when there are res and assets folders', async () => { + const config: IBuildOptions = setup({ + addResFolder: true, + addAssetsFolder: true + }); - const buildGradleContent = fs.readText( - path.join(pluginFolder, getShortPluginName("my-plugin"), "gradle", "wrapper", "gradle-wrapper.properties").toString()); - const gradleVersionRegex = /gradle\-(.*)\-bin\.zip\n/g; - const actualGradleVersion = gradleVersionRegex.exec(buildGradleContent)[1]; - assert.equal(actualGradleVersion, gradleVersion); + await androidBuildPluginService.buildAar(config); assert.isTrue(spawnFromEventCalled); }); - it.only('should use specified runtime gradle versions from the project dir', async () => { - const gradleVersion = "1.2.3"; - const gradleAndroidPluginVersion = "4.5.6"; - setupNpm(gradleVersion, gradleAndroidPluginVersion); - androidBuildPluginService = testInjector.resolve(AndroidPluginBuildService); - setUpPluginNativeFolder(true, false, false); - const config: IBuildOptions = { - platformsAndroidDirPath: tempFolder, - pluginName: "my-plugin", - aarOutputDir: tempFolder, - tempPluginDirPath: pluginFolder, - projectDir: tempFolder - }; + it('builds aar when there is an android manifest', async () => { + const config: IBuildOptions = setup({ + addManifest: true + }); await androidBuildPluginService.buildAar(config); - const gradleWrappersContent = fs.readText(path.join(pluginFolder, getShortPluginName("my-plugin"), "build.gradle").toString()); - const androidVersionRegex = /com\.android\.tools\.build\:gradle\:(.*)\'\n/g; - const actualAndroidVersion = androidVersionRegex.exec(gradleWrappersContent)[1]; - assert.equal(actualAndroidVersion, "1.0.0"); + assert.isTrue(spawnFromEventCalled); + }); + + it('builds aar with the latest runtime gradle versions when no project dir is specified', async () => { + const expectedGradleVersion = "1.2.3"; + const expectedAndroidVersion = "4.5.6"; + const config: IBuildOptions = setup({ + addManifest: true, + latestRuntimeGradleVersion: expectedGradleVersion, + latestRuntimeGradleAndroidVersion: expectedAndroidVersion + }); - const buildGradleContent = fs.readText( - path.join(pluginFolder, getShortPluginName("my-plugin"), "gradle", "wrapper", "gradle-wrapper.properties").toString()); - const gradleVersionRegex = /gradle\-(.*)\-bin\.zip\n/g; - const actualGradleVersion = gradleVersionRegex.exec(buildGradleContent)[1]; - assert.equal(actualGradleVersion, "1.0.0"); + await androidBuildPluginService.buildAar(config); + const actualAndroidVersion = getGradleAndroidPluginVersion(); + const actualGradleVersion = getGradleVersion(); + assert.equal(actualAndroidVersion, expectedAndroidVersion); + assert.equal(actualGradleVersion, expectedGradleVersion); assert.isTrue(spawnFromEventCalled); }); - it('if android manifest is missing', async () => { - setUpPluginNativeFolder(false, true, true); - const config: IBuildOptions = { - platformsAndroidDirPath: tempFolder, - pluginName: "my-plugin", - aarOutputDir: tempFolder, - tempPluginDirPath: pluginFolder - }; - - try { - await androidBuildPluginService.buildAar(config); - } catch (e) { - /* intentionally left blank */ - } + it('builds aar with the latest runtime gradle versions when a project dir without runtime versions is specified', async () => { + const expectedGradleVersion = "4.4.4"; + const expectedAndroidVersion = "5.5.5"; + const config: IBuildOptions = setup({ + addManifest: true, + addProjectDir: true, + latestRuntimeGradleVersion: expectedGradleVersion, + latestRuntimeGradleAndroidVersion: expectedAndroidVersion, + addProjectRuntime: true, + projectRuntimeGradleVersion: null, + projectRuntimeGradleAndroidVersion: null + }); + + await androidBuildPluginService.buildAar(config); + const actualAndroidVersion = getGradleAndroidPluginVersion(); + const actualGradleVersion = getGradleVersion(); + assert.equal(actualGradleVersion, expectedGradleVersion); + assert.equal(actualAndroidVersion, expectedAndroidVersion); assert.isTrue(spawnFromEventCalled); }); - it('if there is only an android manifest file', async () => { - setUpPluginNativeFolder(true, false, false); - const config: IBuildOptions = { - platformsAndroidDirPath: tempFolder, - pluginName: "my-plugin", - aarOutputDir: tempFolder, - tempPluginDirPath: pluginFolder - }; - - try { - await androidBuildPluginService.buildAar(config); - } catch (e) { - /* intentionally left blank */ - } + it('builds aar with the specified runtime gradle versions when project runtime has gradle versions', async () => { + const expectedGradleVersion = "2.2.2"; + const expectedAndroidVersion = "3.3"; + const config: IBuildOptions = setup({ + addManifest: true, + addProjectDir: true, + latestRuntimeGradleVersion: "4.4.4", + latestRuntimeGradleAndroidVersion: "5.5.5", + addProjectRuntime: true, + projectRuntimeGradleVersion: expectedGradleVersion, + projectRuntimeGradleAndroidVersion: expectedAndroidVersion + }); + + await androidBuildPluginService.buildAar(config); + const actualAndroidVersion = getGradleAndroidPluginVersion(); + const actualGradleVersion = getGradleVersion(); + assert.equal(actualGradleVersion, expectedGradleVersion); + assert.equal(actualAndroidVersion, expectedAndroidVersion); assert.isTrue(spawnFromEventCalled); }); - }); - describe(`doesn't build aar `, () => { - it('if there are no files', async () => { - setUpPluginNativeFolder(false, false, false); - const config: IBuildOptions = { - platformsAndroidDirPath: tempFolder, - pluginName: "my-plugin", - aarOutputDir: tempFolder, - tempPluginDirPath: pluginFolder - }; - - try { - await androidBuildPluginService.buildAar(config); - } catch (e) { - /* intentionally left blank */ - } + it('builds aar with a hardcoded runtime gradle versions when a project runtime and the latest runtime do not have versions specified', async () => { + const expectedGradleVersion = "4.4"; + const expectedAndroidVersion = "3.1.2"; + const config: IBuildOptions = setup({ + addManifest: true, + addProjectDir: true, + latestRuntimeGradleVersion: null, + latestRuntimeGradleAndroidVersion: null, + addProjectRuntime: true, + projectRuntimeGradleVersion: null, + projectRuntimeGradleAndroidVersion: null + }); - assert.isFalse(spawnFromEventCalled); + await androidBuildPluginService.buildAar(config); + const actualAndroidVersion = getGradleAndroidPluginVersion(); + const actualGradleVersion = getGradleVersion(); + + assert.equal(actualGradleVersion, expectedGradleVersion); + assert.equal(actualAndroidVersion, expectedAndroidVersion); + assert.isTrue(spawnFromEventCalled); }); }); - describe(`handles include.gradle`, () => { + describe('migrateIncludeGradle', () => { it('if there is a legacy include.gradle file', async () => { - setUpIncludeGradle(); - const config: IBuildOptions = { - platformsAndroidDirPath: pluginFolder, - pluginName: "my-plugin", - aarOutputDir: pluginFolder, - tempPluginDirPath: pluginFolder - }; - - const includeGradleName = INCLUDE_GRADLE_NAME; + const config: IBuildOptions = setup({ + addLegacyIncludeGradle: true + }); + await androidBuildPluginService.migrateIncludeGradle(config); - const includeGradleContent = fs.readText(path.join(pluginFolder, includeGradleName).toString()); - const productFlavorsAreRemoved = includeGradleContent.indexOf("productFlavors") === -1; - assert.isTrue(productFlavorsAreRemoved); + + const includeGradleContent = fs.readText(path.join(pluginFolder, INCLUDE_GRADLE_NAME).toString()); + const areProductFlavorsRemoved = includeGradleContent.indexOf("productFlavors") === -1; + assert.isTrue(areProductFlavorsRemoved); }); }); + + function getGradleAndroidPluginVersion() { + const gradleWrappersContent = fs.readText(path.join(tempFolder, shortPluginName, "build.gradle")); + const androidVersionRegex = /com\.android\.tools\.build\:gradle\:(.*)\'\n/g; + const androidVersion = androidVersionRegex.exec(gradleWrappersContent)[1]; + + return androidVersion; + } + + function getGradleVersion() { + const buildGradleContent = fs.readText(path.join(tempFolder, shortPluginName, "gradle", "wrapper", "gradle-wrapper.properties")); + const gradleVersionRegex = /gradle\-(.*)\-bin\.zip\n/g; + const gradleVersion = gradleVersionRegex.exec(buildGradleContent)[1]; + + return gradleVersion; + } }); From 86f7f3896cbfcc82b6eea6391bd9ec178fda5d78 Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Thu, 5 Jul 2018 14:33:04 +0300 Subject: [PATCH 5/7] chore: add some trace logging when searching for the runtime Gradle versions --- lib/services/android-plugin-build-service.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/services/android-plugin-build-service.ts b/lib/services/android-plugin-build-service.ts index 281e93017d..b251b16b81 100644 --- a/lib/services/android-plugin-build-service.ts +++ b/lib/services/android-plugin-build-service.ts @@ -247,11 +247,13 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { this.$devicePlatformsConstants.Android, this.$projectDataService.getProjectData(projectDir)); runtimeGradleVersions = this.getGradleVersions(registryData.versions[projectRuntimeVersion]); + this.$logger.trace(`Got gradle versions ${JSON.stringify(runtimeGradleVersions)} from runtime v${projectRuntimeVersion}`); } if (!runtimeGradleVersions) { const latestRuntimeVersion = registryData["dist-tags"].latest; runtimeGradleVersions = this.getGradleVersions(registryData.versions[latestRuntimeVersion]); + this.$logger.trace(`Got gradle versions ${JSON.stringify(runtimeGradleVersions)} from the latest runtime v${latestRuntimeVersion}`); } return runtimeGradleVersions || {}; From dcc78bfafdc1c0185f4fb9bfe5d3cc7f732ca013 Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Thu, 5 Jul 2018 15:01:45 +0300 Subject: [PATCH 6/7] refactor: create a common injector stub --- lib/services/android-plugin-build-service.ts | 2 +- test/services/android-plugin-build-service.ts | 89 +++++++------------ test/stubs.ts | 33 ++++++- 3 files changed, 65 insertions(+), 59 deletions(-) diff --git a/lib/services/android-plugin-build-service.ts b/lib/services/android-plugin-build-service.ts index b251b16b81..57c09e11e7 100644 --- a/lib/services/android-plugin-build-service.ts +++ b/lib/services/android-plugin-build-service.ts @@ -259,7 +259,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { return runtimeGradleVersions || {}; } - private getGradleVersions(packageData: any): IRuntimeGradleVersions { + private getGradleVersions(packageData: { gradle: { version: string, android: string }}): IRuntimeGradleVersions { const packageJsonGradle = packageData && packageData.gradle; let runtimeVersions: IRuntimeGradleVersions = null; if (packageJsonGradle && (packageJsonGradle.version || packageJsonGradle.android)) { diff --git a/test/services/android-plugin-build-service.ts b/test/services/android-plugin-build-service.ts index 7d089587b0..e7367b0c49 100644 --- a/test/services/android-plugin-build-service.ts +++ b/test/services/android-plugin-build-service.ts @@ -1,20 +1,15 @@ import { AndroidPluginBuildService } from "../../lib/services/android-plugin-build-service"; -import { Yok } from "../../lib/common/yok"; import { assert } from "chai"; +import { INCLUDE_GRADLE_NAME } from "../../lib/constants"; +import { getShortPluginName } from "../../lib/common/helpers"; import * as FsLib from "../../lib/common/file-system"; import * as path from "path"; -import { HostInfo } from "../../lib/common/host-info"; -import { Logger } from "../../lib/common/logger"; -import * as ErrorsLib from "../../lib/common/errors"; -import temp = require("temp"); -import { INCLUDE_GRADLE_NAME } from "../../lib/constants"; import * as stubs from "../stubs"; -import { DevicePlatformsConstants } from "../../lib/common/mobile/device-platforms-constants"; -import { getShortPluginName } from "../../lib/common/helpers"; +import temp = require("temp"); temp.track(); -describe.only('androidPluginBuildService', () => { +describe('androidPluginBuildService', () => { const pluginName = 'my-plugin'; const shortPluginName = getShortPluginName(pluginName); let spawnFromEventCalled = false; @@ -40,7 +35,7 @@ describe.only('androidPluginBuildService', () => { tempFolder = temp.mkdirSync("androidPluginBuildService-temp"); pluginFolder = temp.mkdirSync("androidPluginBuildService-plugin"); setupDI(options); - setupPluginFolders(options.addManifest, options.addResFolder, options.addAssetsFolder, options.addLegacyIncludeGradle); + setupPluginFolders(options); return { platformsAndroidDirPath: pluginFolder, @@ -58,7 +53,7 @@ describe.only('androidPluginBuildService', () => { projectRuntimeGradleVersion?: string, projectRuntimeGradleAndroidVersion?: string, }): void { - const testInjector: IInjector = new Yok(); + const testInjector: IInjector = new stubs.InjectorStub(); testInjector.register("fs", FsLib.FileSystem); testInjector.register("childProcess", { spawnFromEvent: async (command: string): Promise => { @@ -69,31 +64,11 @@ describe.only('androidPluginBuildService', () => { return null; } }); - testInjector.register("hostInfo", HostInfo); - testInjector.register("androidToolsInfo", { - getToolsInfo: () => { - return {}; - }, - validateInfo: () => { - return true; - } - }); - testInjector.register("logger", Logger); - testInjector.register("errors", ErrorsLib.Errors); - testInjector.register("options", {}); - testInjector.register("config", {}); - testInjector.register("staticConfig", {}); - testInjector.register("hooksService", { - executeBeforeHooks: async (commandName: string, hookArguments?: IDictionary): Promise => undefined, - executeAfterHooks: async (commandName: string, hookArguments?: IDictionary): Promise => undefined - }); - testInjector.register('projectDataService', stubs.ProjectDataService); testInjector.register('platformService', { getCurrentPlatformVersion: (platform: string, projectData: IProjectData): string => { return options.addProjectRuntime ? "1.0.0" : null; } }); - testInjector.register('devicePlatformsConstants', DevicePlatformsConstants); testInjector.register('npm', setupNpm(options)); fs = testInjector.resolve("fs"); @@ -132,27 +107,12 @@ describe.only('androidPluginBuildService', () => { }; } - function setUpIncludeGradle() { - const validIncludeGradleContent = `android { - productFlavors { - "nativescript-pro-ui" { - dimension "nativescript-pro-ui" - } - } -} - -def supportVersion = project.hasProperty("supportVersion") ? project.supportVersion : "23.3.0" - -dependencies { - compile "com.android.support:appcompat-v7:$supportVersion" - compile "com.android.support:recyclerview-v7:$supportVersion" - compile "com.android.support:design:$supportVersion" -}`; - - fs.writeFile(path.join(pluginFolder, INCLUDE_GRADLE_NAME), validIncludeGradleContent); - } - - function setupPluginFolders(manifestFile: boolean, resFolder: boolean, assetsFolder: boolean, addLegacyIncludeGradle: boolean) { + function setupPluginFolders(options: { + addManifest?: boolean, + addResFolder?: boolean, + addAssetsFolder?: boolean, + addLegacyIncludeGradle?: boolean + }) { const validAndroidManifestContent = ` `; @@ -162,25 +122,40 @@ dependencies { name="string_name" >text_string `; + const validIncludeGradleContent = `android { + productFlavors { + "nativescript-pro-ui" { + dimension "nativescript-pro-ui" + } + } + } + + def supportVersion = project.hasProperty("supportVersion") ? project.supportVersion : "23.3.0" + + dependencies { + compile "com.android.support:appcompat-v7:$supportVersion" + compile "com.android.support:recyclerview-v7:$supportVersion" + compile "com.android.support:design:$supportVersion" + }`; - if (manifestFile) { + if (options.addManifest) { fs.writeFile(path.join(pluginFolder, "AndroidManifest.xml"), validAndroidManifestContent); } - if (resFolder) { + if (options.addResFolder) { const valuesFolder = path.join(pluginFolder, "res", "values"); fs.createDirectory(valuesFolder); fs.writeFile(path.join(valuesFolder, "strings.xml"), validStringsXmlContent); } - if (assetsFolder) { + if (options.addAssetsFolder) { const imagesFolder = path.join(pluginFolder, "assets", "images"); fs.createDirectory(imagesFolder); fs.writeFile(path.join(imagesFolder, "myicon.png"), "123"); } - if (addLegacyIncludeGradle) { - setUpIncludeGradle(); + if (options.addLegacyIncludeGradle) { + fs.writeFile(path.join(pluginFolder, INCLUDE_GRADLE_NAME), validIncludeGradleContent); } } diff --git a/test/stubs.ts b/test/stubs.ts index 86d123e7df..7ce0254f05 100644 --- a/test/stubs.ts +++ b/test/stubs.ts @@ -4,9 +4,11 @@ import * as util from "util"; import * as chai from "chai"; import { EventEmitter } from "events"; import { SpawnOptions } from "child_process"; - import * as path from "path"; import * as constants from "./../lib/constants"; +import { Yok } from "./../lib/common/yok"; +import { HostInfo } from "./../lib/common/host-info"; +import { DevicePlatformsConstants } from "./../lib/common/mobile/device-platforms-constants"; export class LoggerStub implements ILogger { setLevel(level: string): void { } @@ -851,3 +853,32 @@ export class AndroidResourcesMigrationServiceStub implements IAndroidResourcesMi return Promise.resolve(); } } + +export class InjectorStub extends Yok implements IInjector { + constructor() { + super(); + this.register("fs", FileSystemStub); + this.register("hostInfo", HostInfo); + this.register("androidToolsInfo", AndroidToolsInfoStub); + this.register("logger", LoggerStub); + this.register("errors", ErrorsStub); + this.register("options", {}); + this.register("config", {}); + this.register("staticConfig", {}); + this.register("hooksService", HooksServiceStub); + this.register('projectDataService', ProjectDataService); + this.register('devicePlatformsConstants', DevicePlatformsConstants); + this.register("emulatorPlatformService", EmulatorPlatformService); + this.register("androidResourcesMigrationService", AndroidResourcesMigrationServiceStub); + this.register("platformService", PlatformServiceStub); + this.register("commandsService", CommandsService); + this.register("projectChangesService", ProjectChangesService); + this.register('childProcess', ChildProcessStub); + this.register("liveSyncService", LiveSyncServiceStub); + this.register("prompter", PrompterStub); + this.register('platformsData', PlatformsDataStub); + this.register("androidPluginBuildService", AndroidPluginBuildServiceStub); + this.register('projectData', ProjectDataStub); + this.register('npmInstallationManager', NpmInstallationManagerStub); + } +} From 1a881a193a8ca7a001843254aed3da970b2634c2 Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Mon, 9 Jul 2018 17:38:13 +0300 Subject: [PATCH 7/7] chore: fix pull request comments --- lib/common | 2 +- lib/constants.ts | 5 ++ lib/node-package-manager.ts | 2 +- lib/services/android-plugin-build-service.ts | 50 +++++++++---------- test/services/android-plugin-build-service.ts | 16 +++--- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/lib/common b/lib/common index 2bdc7a2b87..cef91c4816 160000 --- a/lib/common +++ b/lib/common @@ -1 +1 @@ -Subproject commit 2bdc7a2b87e78dcc71e248802fc1b9d0869585de +Subproject commit cef91c4816d32aedfab92b49c7e4903db14b4936 diff --git a/lib/constants.ts b/lib/constants.ts index fbecd9a22e..7921094df0 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -207,4 +207,9 @@ export class Hooks { public static createProject = "createProject"; } +export class AndroidBuildDefaults { + public static GradleVersion = "4.4"; + public static GradleAndroidPluginVersion = "3.1.2"; +} + export const PACKAGE_PLACEHOLDER_NAME = "__PACKAGE__"; diff --git a/lib/node-package-manager.ts b/lib/node-package-manager.ts index 0bdadc059b..b8da3ee23a 100644 --- a/lib/node-package-manager.ts +++ b/lib/node-package-manager.ts @@ -123,7 +123,7 @@ export class NodePackageManager implements INodePackageManager { const url = `https://registry.npmjs.org/${packageName}`; this.$logger.trace(`Trying to get data from npm registry for package ${packageName}, url is: ${url}`); const responseData = (await this.$httpClient.httpRequest(url)).body; - this.$logger.trace(`Successfully received data from npm registry for package ${packageName}.`); + this.$logger.trace(`Successfully received data from npm registry for package ${packageName}. Response data is: ${responseData}`); const jsonData = JSON.parse(responseData); this.$logger.trace(`Successfully parsed data from npm registry for package ${packageName}.`); return jsonData; diff --git a/lib/services/android-plugin-build-service.ts b/lib/services/android-plugin-build-service.ts index 57c09e11e7..20baf645ba 100644 --- a/lib/services/android-plugin-build-service.ts +++ b/lib/services/android-plugin-build-service.ts @@ -1,5 +1,5 @@ import * as path from "path"; -import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR, TNS_ANDROID_RUNTIME_NAME } from "../constants"; +import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR, TNS_ANDROID_RUNTIME_NAME, AndroidBuildDefaults } from "../constants"; import { getShortPluginName, hook } from "../common/helpers"; import { Builder, parseString } from "xml2js"; import { ILogger } from "log4js"; @@ -24,7 +24,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { private $logger: ILogger, private $npm: INodePackageManager, private $projectDataService: IProjectDataService, - private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { } + private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants, + private $errors: IErrors) { } private static MANIFEST_ROOT = { $: { @@ -170,8 +171,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { public async buildAar(options: IBuildOptions): Promise { this.validateOptions(options); const manifestFilePath = this.getManifest(options.platformsAndroidDirPath); - const androidSourceSetDirectories = this.getAndroidSourceDirectories(options.platformsAndroidDirPath); - const shouldBuildAar = !!manifestFilePath || androidSourceSetDirectories.length > 0; + const androidSourceDirectories = this.getAndroidSourceDirectories(options.platformsAndroidDirPath); + const shouldBuildAar = !!manifestFilePath || androidSourceDirectories.length > 0; if (shouldBuildAar) { const shortPluginName = getShortPluginName(options.pluginName); @@ -179,7 +180,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { const pluginTempMainSrcDir = path.join(pluginTempDir, "src", "main"); await this.updateManifest(manifestFilePath, pluginTempMainSrcDir, shortPluginName); - this.copySourceSetDirectories(androidSourceSetDirectories, pluginTempMainSrcDir); + this.copySourceSetDirectories(androidSourceDirectories, pluginTempMainSrcDir); await this.setupGradle(pluginTempDir, options.platformsAndroidDirPath, options.projectDir); await this.buildPlugin({ pluginDir: pluginTempDir, pluginName: options.pluginName }); this.copyAar(shortPluginName, pluginTempDir, options.aarOutputDir); @@ -197,9 +198,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { try { androidManifestContent = this.$fs.readText(manifestFilePath); } catch (err) { - throw new Error( - `Failed to fs.readFileSync the manifest file located at ${manifestFilePath}` - ); + this.$errors.failWithoutHelp(`Failed to fs.readFileSync the manifest file located at ${manifestFilePath}. Error is: ${err.toString()}`); } updatedManifestContent = await this.updateManifestContent(androidManifestContent, defaultPackageName); @@ -211,15 +210,13 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { try { this.$fs.writeFile(pathToTempAndroidManifest, updatedManifestContent); } catch (e) { - throw new Error(`Failed to write the updated AndroidManifest in the new location - ${pathToTempAndroidManifest}`); + this.$errors.failWithoutHelp(`Failed to write the updated AndroidManifest in the new location - ${pathToTempAndroidManifest}. Error is: ${e.toString()}`); } } private copySourceSetDirectories(androidSourceSetDirectories: string[], pluginTempMainSrcDir: string): void { for (const dir of androidSourceSetDirectories) { - const dirNameParts = dir.split(path.sep); - // get only the last subdirectory of the entire path string. e.g. 'res', 'java', etc. - const dirName = dirNameParts[dirNameParts.length - 1]; + const dirName = path.basename(dir); const destination = path.join(pluginTempMainSrcDir, dirName); this.$fs.ensureDirectoryExists(destination); @@ -272,7 +269,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { } private replaceGradleVersion(pluginTempDir: string, version: string): void { - const gradleVersion = version || "4.4"; + const gradleVersion = version || AndroidBuildDefaults.GradleVersion; const gradleVersionPlaceholder = "{{runtimeGradleVersion}}"; const gradleWrapperPropertiesPath = path.join(pluginTempDir, "gradle", "wrapper", "gradle-wrapper.properties"); @@ -281,7 +278,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { private replaceGradleAndroidPluginVersion(buildGradlePath: string, version: string): void { const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}"; - const gradleAndroidPluginVersion = version || "3.1.2"; + const gradleAndroidPluginVersion = version || AndroidBuildDefaults.GradleAndroidPluginVersion; this.replaceFileContent(buildGradlePath, gradleAndroidPluginVersionPlaceholder, gradleAndroidPluginVersion); } @@ -297,10 +294,10 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { const includeGradlePath = path.join(platformsAndroidDirPath, INCLUDE_GRADLE_NAME); if (this.$fs.exists(includeGradlePath)) { const includeGradleContent = this.$fs.readText(includeGradlePath); - const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent); + const compileDependencies = this.getIncludeGradleCompileDependenciesScope(includeGradleContent); - if (repositoriesAndDependenciesScopes.length > 0) { - this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n")); + if (compileDependencies.length) { + this.$fs.appendFile(buildGradlePath, "\n" + compileDependencies.join("\n")); } } } @@ -315,10 +312,10 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { this.$fs.copyFile(pathToBuiltAar, path.join(aarOutputDir, `${shortPluginName}.aar`)); } } catch (e) { - throw new Error(`Failed to copy built aar to destination. ${e.message}`); + this.$errors.failWithoutHelp(`Failed to copy built aar to destination. ${e.message}`); } } else { - throw new Error(`No built aar found at ${pathToBuiltAar}`); + this.$errors.failWithoutHelp(`No built aar found at ${pathToBuiltAar}`); } } @@ -336,7 +333,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { try { includeGradleFileContent = this.$fs.readFile(includeGradleFilePath).toString(); } catch (err) { - throw new Error(`Failed to fs.readFileSync the include.gradle file located at ${includeGradleFilePath}`); + this.$errors.failWithoutHelp(`Failed to fs.readFileSync the include.gradle file located at ${includeGradleFilePath}. Error is: ${err.toString()}`); } const productFlavorsScope = this.getScope("productFlavors", includeGradleFileContent); @@ -347,7 +344,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { return true; } catch (e) { - throw new Error(`Failed to write the updated include.gradle in - ${includeGradleFilePath}`); + this.$errors.failWithoutHelp(`Failed to write the updated include.gradle ` + + `in - ${includeGradleFilePath}. Error is: ${e.toString()}`); } } @@ -375,13 +373,13 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { try { await this.$childProcess.spawnFromEvent(gradlew, localArgs, "close", { cwd: pluginBuildSettings.pluginDir }); } catch (err) { - throw new Error(`Failed to build plugin ${pluginBuildSettings.pluginName} : \n${err}`); + this.$errors.failWithoutHelp(`Failed to build plugin ${pluginBuildSettings.pluginName} : \n${err}`); } } private validateOptions(options: IBuildOptions): void { if (!options) { - throw new Error("Android plugin cannot be built without passing an 'options' object."); + this.$errors.failWithoutHelp("Android plugin cannot be built without passing an 'options' object."); } if (!options.pluginName) { @@ -393,7 +391,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { } if (!options.tempPluginDirPath) { - throw new Error("Android plugin cannot be built without passing the path to a directory where the temporary project should be built."); + this.$errors.failWithoutHelp("Android plugin cannot be built without passing the path to a directory where the temporary project should be built."); } this.validatePlatformsAndroidDirPathOption(options); @@ -401,11 +399,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService { private validatePlatformsAndroidDirPathOption(options: IBuildOptions): void { if (!options) { - throw new Error("Android plugin cannot be built without passing an 'options' object."); + this.$errors.failWithoutHelp("Android plugin cannot be built without passing an 'options' object."); } if (!options.platformsAndroidDirPath) { - throw new Error("Android plugin cannot be built without passing the path to the platforms/android dir."); + this.$errors.failWithoutHelp("Android plugin cannot be built without passing the path to the platforms/android dir."); } } diff --git a/test/services/android-plugin-build-service.ts b/test/services/android-plugin-build-service.ts index e7367b0c49..0e57af0368 100644 --- a/test/services/android-plugin-build-service.ts +++ b/test/services/android-plugin-build-service.ts @@ -1,6 +1,6 @@ import { AndroidPluginBuildService } from "../../lib/services/android-plugin-build-service"; import { assert } from "chai"; -import { INCLUDE_GRADLE_NAME } from "../../lib/constants"; +import { INCLUDE_GRADLE_NAME, AndroidBuildDefaults } from "../../lib/constants"; import { getShortPluginName } from "../../lib/common/helpers"; import * as FsLib from "../../lib/common/file-system"; import * as path from "path"; @@ -34,7 +34,7 @@ describe('androidPluginBuildService', () => { spawnFromEventCalled = false; tempFolder = temp.mkdirSync("androidPluginBuildService-temp"); pluginFolder = temp.mkdirSync("androidPluginBuildService-plugin"); - setupDI(options); + createTestInjector(options); setupPluginFolders(options); return { @@ -46,7 +46,7 @@ describe('androidPluginBuildService', () => { }; } - function setupDI(options: { + function createTestInjector(options: { addProjectRuntime?: boolean latestRuntimeGradleVersion?: string, latestRuntimeGradleAndroidVersion?: string, @@ -242,7 +242,7 @@ describe('androidPluginBuildService', () => { assert.isTrue(spawnFromEventCalled); }); - it('builds aar with the specified runtime gradle versions when project runtime has gradle versions', async () => { + it('builds aar with the specified runtime gradle versions when the project runtime has gradle versions', async () => { const expectedGradleVersion = "2.2.2"; const expectedAndroidVersion = "3.3"; const config: IBuildOptions = setup({ @@ -264,9 +264,7 @@ describe('androidPluginBuildService', () => { assert.isTrue(spawnFromEventCalled); }); - it('builds aar with a hardcoded runtime gradle versions when a project runtime and the latest runtime do not have versions specified', async () => { - const expectedGradleVersion = "4.4"; - const expectedAndroidVersion = "3.1.2"; + it('builds aar with the hardcoded gradle versions when the project runtime and the latest runtime do not have versions specified', async () => { const config: IBuildOptions = setup({ addManifest: true, addProjectDir: true, @@ -281,8 +279,8 @@ describe('androidPluginBuildService', () => { const actualAndroidVersion = getGradleAndroidPluginVersion(); const actualGradleVersion = getGradleVersion(); - assert.equal(actualGradleVersion, expectedGradleVersion); - assert.equal(actualAndroidVersion, expectedAndroidVersion); + assert.equal(actualGradleVersion, AndroidBuildDefaults.GradleVersion); + assert.equal(actualAndroidVersion, AndroidBuildDefaults.GradleAndroidPluginVersion); assert.isTrue(spawnFromEventCalled); }); });