11import * as path from "path" ;
2- import { MANIFEST_FILE_NAME , INCLUDE_GRADLE_NAME , ASSETS_DIR , RESOURCES_DIR } from "../constants" ;
2+ import { MANIFEST_FILE_NAME , INCLUDE_GRADLE_NAME , ASSETS_DIR , RESOURCES_DIR , TNS_ANDROID_RUNTIME_NAME } from "../constants" ;
33import { getShortPluginName , hook } from "../common/helpers" ;
44import { Builder , parseString } from "xml2js" ;
55import { ILogger } from "log4js" ;
@@ -12,19 +12,25 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
1212 return this . $injector . resolve ( "hooksService" ) ;
1313 }
1414
15+ private get $platformService ( ) : IPlatformService {
16+ return this . $injector . resolve ( "platformService" ) ;
17+ }
18+
1519 constructor ( private $injector : IInjector ,
1620 private $fs : IFileSystem ,
1721 private $childProcess : IChildProcess ,
1822 private $hostInfo : IHostInfo ,
1923 private $androidToolsInfo : IAndroidToolsInfo ,
20- private $logger : ILogger ) { }
24+ private $logger : ILogger ,
25+ private $npm : INodePackageManager ,
26+ private $projectDataService : IProjectDataService ,
27+ private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ) { }
2128
2229 private static MANIFEST_ROOT = {
2330 $ : {
2431 "xmlns:android" : "http://schemas.android.com/apk/res/android"
2532 }
2633 } ;
27- private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin" ;
2834
2935 private getAndroidSourceDirectories ( source : string ) : Array < string > {
3036 const directories = [ RESOURCES_DIR , "java" , ASSETS_DIR , "jniLibs" ] ;
@@ -174,7 +180,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
174180
175181 await this . updateManifest ( manifestFilePath , pluginTempMainSrcDir , shortPluginName ) ;
176182 this . copySourceSetDirectories ( androidSourceSetDirectories , pluginTempMainSrcDir ) ;
177- this . setupGradle ( pluginTempDir , options . platformsAndroidDirPath ) ;
183+ await this . setupGradle ( pluginTempDir , options . platformsAndroidDirPath , options . projectDir ) ;
178184 await this . buildPlugin ( { pluginDir : pluginTempDir , pluginName : options . pluginName } ) ;
179185 this . copyAar ( shortPluginName , pluginTempDir , options . aarOutputDir ) ;
180186 }
@@ -221,23 +227,67 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
221227 }
222228 }
223229
224- private setupGradle ( pluginTempDir : string , platformsAndroidDirPath : string ) : void {
225- const gradleTemplatePath = path . resolve ( path . join ( __dirname , AndroidPluginBuildService . ANDROID_PLUGIN_GRADLE_TEMPLATE ) ) ;
230+ private async setupGradle ( pluginTempDir : string , platformsAndroidDirPath : string , projectDir : string ) : Promise < void > {
231+ const gradleTemplatePath = path . resolve ( path . join ( __dirname , "../../vendor/gradle-plugin" ) ) ;
226232 const allGradleTemplateFiles = path . join ( gradleTemplatePath , "*" ) ;
233+ const buildGradlePath = path . join ( pluginTempDir , "build.gradle" ) ;
227234
228235 this . $fs . copyFile ( allGradleTemplateFiles , pluginTempDir ) ;
229- this . addCompileDependencies ( platformsAndroidDirPath , pluginTempDir ) ;
230- // TODO: replace placeholders in target destination
236+ this . addCompileDependencies ( platformsAndroidDirPath , buildGradlePath ) ;
237+ const runtimeGradleVersions = await this . getRuntimeGradleVersions ( projectDir ) ;
238+ this . replaceGradleVersion ( pluginTempDir , runtimeGradleVersions . gradleVersion ) ;
239+ this . replaceGradleAndroidPluginVersion ( buildGradlePath , runtimeGradleVersions . gradleAndroidPluginVersion ) ;
240+ }
241+
242+ private async getRuntimeGradleVersions ( projectDir : string ) : Promise < IRuntimeGradleVersions > {
243+ const runtimeVersions : IRuntimeGradleVersions = { } ;
244+ const registryData = await this . $npm . getRegistryPackageData ( TNS_ANDROID_RUNTIME_NAME ) ;
245+ let runtimeVersion : string = registryData [ "dist-tags" ] . latest ;
246+ if ( projectDir ) {
247+ runtimeVersion = this . $platformService . getCurrentPlatformVersion (
248+ this . $devicePlatformsConstants . Android ,
249+ this . $projectDataService . getProjectData ( projectDir ) ) ;
250+ }
251+
252+ const latestPackageData = registryData . versions [ runtimeVersion ] ;
253+ const packageJsonGradle = latestPackageData && latestPackageData . gradle ;
254+ if ( packageJsonGradle ) {
255+ runtimeVersions . gradleVersion = packageJsonGradle . version ;
256+ runtimeVersions . gradleAndroidPluginVersion = packageJsonGradle . android ;
257+ }
258+
259+ return runtimeVersions ;
260+ }
261+
262+ private replaceGradleVersion ( pluginTempDir : string , version : string ) : void {
263+ const gradleVersion = version || "4.4" ;
264+ const gradleVersionPlaceholder = "{{runtimeGradleVersion}}" ;
265+ const gradleWrapperPropertiesPath = path . join ( pluginTempDir , "gradle" , "wrapper" , "gradle-wrapper.properties" ) ;
266+
267+ this . replaceFileContent ( gradleWrapperPropertiesPath , gradleVersionPlaceholder , gradleVersion ) ;
268+ }
269+
270+ private replaceGradleAndroidPluginVersion ( buildGradlePath : string , version : string ) : void {
271+ const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}" ;
272+ const gradleAndroidPluginVersion = version || "3.1.2" ;
273+
274+ this . replaceFileContent ( buildGradlePath , gradleAndroidPluginVersionPlaceholder , gradleAndroidPluginVersion ) ;
275+ }
276+
277+ private replaceFileContent ( filePath : string , content : string , replacement : string ) {
278+ const fileContent = this . $fs . readText ( filePath ) ;
279+ const contentRegex = new RegExp ( content , "g" ) ;
280+ const replacedFileContent = fileContent . replace ( contentRegex , replacement ) ;
281+ this . $fs . writeFile ( filePath , replacedFileContent ) ;
231282 }
232283
233- private addCompileDependencies ( platformsAndroidDirPath : string , pluginTempDir : string ) : void {
284+ private addCompileDependencies ( platformsAndroidDirPath : string , buildGradlePath : string ) : void {
234285 const includeGradlePath = path . join ( platformsAndroidDirPath , INCLUDE_GRADLE_NAME ) ;
235286 if ( this . $fs . exists ( includeGradlePath ) ) {
236287 const includeGradleContent = this . $fs . readText ( includeGradlePath ) ;
237288 const repositoriesAndDependenciesScopes = this . getIncludeGradleCompileDependenciesScope ( includeGradleContent ) ;
238289
239290 if ( repositoriesAndDependenciesScopes . length > 0 ) {
240- const buildGradlePath = path . join ( pluginTempDir , "build.gradle" ) ;
241291 this . $fs . appendFile ( buildGradlePath , "\n" + repositoriesAndDependenciesScopes . join ( "\n" ) ) ;
242292 }
243293 }
0 commit comments