1
1
import * 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" ;
3
3
import { getShortPluginName , hook } from "../common/helpers" ;
4
4
import { Builder , parseString } from "xml2js" ;
5
5
import { ILogger } from "log4js" ;
@@ -12,19 +12,25 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
12
12
return this . $injector . resolve ( "hooksService" ) ;
13
13
}
14
14
15
+ private get $platformService ( ) : IPlatformService {
16
+ return this . $injector . resolve ( "platformService" ) ;
17
+ }
18
+
15
19
constructor ( private $injector : IInjector ,
16
20
private $fs : IFileSystem ,
17
21
private $childProcess : IChildProcess ,
18
22
private $hostInfo : IHostInfo ,
19
23
private $androidToolsInfo : IAndroidToolsInfo ,
20
- private $logger : ILogger ) { }
24
+ private $logger : ILogger ,
25
+ private $npm : INodePackageManager ,
26
+ private $projectDataService : IProjectDataService ,
27
+ private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ) { }
21
28
22
29
private static MANIFEST_ROOT = {
23
30
$ : {
24
31
"xmlns:android" : "http://schemas.android.com/apk/res/android"
25
32
}
26
33
} ;
27
- private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin" ;
28
34
29
35
private getAndroidSourceDirectories ( source : string ) : Array < string > {
30
36
const directories = [ RESOURCES_DIR , "java" , ASSETS_DIR , "jniLibs" ] ;
@@ -174,7 +180,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
174
180
175
181
await this . updateManifest ( manifestFilePath , pluginTempMainSrcDir , shortPluginName ) ;
176
182
this . copySourceSetDirectories ( androidSourceSetDirectories , pluginTempMainSrcDir ) ;
177
- this . setupGradle ( pluginTempDir , options . platformsAndroidDirPath ) ;
183
+ await this . setupGradle ( pluginTempDir , options . platformsAndroidDirPath , options . projectDir ) ;
178
184
await this . buildPlugin ( { pluginDir : pluginTempDir , pluginName : options . pluginName } ) ;
179
185
this . copyAar ( shortPluginName , pluginTempDir , options . aarOutputDir ) ;
180
186
}
@@ -221,23 +227,67 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
221
227
}
222
228
}
223
229
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" ) ) ;
226
232
const allGradleTemplateFiles = path . join ( gradleTemplatePath , "*" ) ;
233
+ const buildGradlePath = path . join ( pluginTempDir , "build.gradle" ) ;
227
234
228
235
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 ) ;
231
282
}
232
283
233
- private addCompileDependencies ( platformsAndroidDirPath : string , pluginTempDir : string ) : void {
284
+ private addCompileDependencies ( platformsAndroidDirPath : string , buildGradlePath : string ) : void {
234
285
const includeGradlePath = path . join ( platformsAndroidDirPath , INCLUDE_GRADLE_NAME ) ;
235
286
if ( this . $fs . exists ( includeGradlePath ) ) {
236
287
const includeGradleContent = this . $fs . readText ( includeGradlePath ) ;
237
288
const repositoriesAndDependenciesScopes = this . getIncludeGradleCompileDependenciesScope ( includeGradleContent ) ;
238
289
239
290
if ( repositoriesAndDependenciesScopes . length > 0 ) {
240
- const buildGradlePath = path . join ( pluginTempDir , "build.gradle" ) ;
241
291
this . $fs . appendFile ( buildGradlePath , "\n" + repositoriesAndDependenciesScopes . join ( "\n" ) ) ;
242
292
}
243
293
}
0 commit comments