Skip to content

Commit 687ed7d

Browse files
committed
create include.gradle for plugins coming from npm @scopes
1 parent e683d82 commit 687ed7d

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

build-artifacts/project-template-gradle/build.gradle

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def isWinOs = System.properties['os.name'].toLowerCase().contains('windows')
4040
def metadataParams = new LinkedList <String> ()
4141
def allJarPaths = new LinkedList <String> ()
4242
def configurationsDir = "$projectDir/configurations"
43-
def createPluginConfigFile = false
43+
def shouldCreatePluginConfigFile = false
4444
def configStage = "\n:config phase: "
4545
def nodeModulesDir = "../../node_modules/"
4646
def libDir = "$projectDir/../../lib/Android/"
@@ -55,7 +55,7 @@ def asbgProject = project(":asbg")
5555
asbgProject.ext.outDir = new File("$projectDir", "src/main/java")
5656
asbgProject.ext.jsCodeDir = new File("$projectDir", "src/main/assets/app")
5757

58-
def compiteCompileSdkVersion () {
58+
def computeCompileSdkVersion () {
5959
if(project.hasProperty("compileSdk")) {
6060
return compileSdk
6161
}
@@ -118,14 +118,14 @@ task getDevDependencies {
118118
////////////////////////////////////////////////////////////////////////////////////
119119

120120
android {
121-
compileSdkVersion compiteCompileSdkVersion()
121+
compileSdkVersion computeCompileSdkVersion()
122122
buildToolsVersion computeBuildToolsVersion()
123123

124124
defaultConfig {
125125
minSdkVersion 17
126126
targetSdkVersion computeTargetSdkVersion()
127127
ndk {
128-
abiFilters "armeabi-v7a", "x86"
128+
abiFilters "armeabi-v7a", "x86"
129129
}
130130
}
131131

@@ -183,14 +183,14 @@ repositories {
183183
}
184184

185185
dependencies {
186-
def suppotVer = "22.2.0";
186+
def supportVer = "22.2.0";
187187
if(project.hasProperty("supportVersion")) {
188-
suppotVer = supportVersion
188+
supportVer = supportVersion
189189
}
190190

191-
compile "com.android.support:support-v4:$suppotVer"
192-
compile "com.android.support:appcompat-v7:$suppotVer"
193-
debugCompile "com.android.support:design:$suppotVer"
191+
compile "com.android.support:support-v4:$supportVer"
192+
compile "com.android.support:appcompat-v7:$supportVer"
193+
debugCompile "com.android.support:design:$supportVer"
194194

195195
// take all jars within the libs dir
196196
compile fileTree(dir: "$projectDir/libs", include: ["**/*.jar"])
@@ -268,7 +268,7 @@ def updateProductFlavorsContent(flavor, dimensionName, oldContent) {
268268
def closingQuotes = oldContent.indexOf('"', openingQuoutes + 1);
269269
if(closingQuotes == -1) {
270270
closingQuotes = oldContent.indexOf("'", openingQuoutes + 1);
271-
}
271+
}
272272

273273
index = closingQuotes + 1;
274274
}
@@ -282,8 +282,7 @@ def updateProductFlavorsContent(flavor, dimensionName, oldContent) {
282282
return newContent;
283283
}
284284

285-
def createProductFlavorsContent(flavor, dimensionName, includeAndroidContent = true)
286-
{
285+
def createProductFlavorsContent(flavor, dimensionName, includeAndroidContent = true) {
287286
if (includeAndroidContent)
288287
{
289288
def content = """
@@ -317,12 +316,11 @@ def createIncludeFile (filePath, flavor, dimensionName) {
317316
defaultIncludeFile.text = createProductFlavorsContent(flavor, dimensionName);
318317
}
319318

320-
def sanatizeDimensionName(str) {
319+
def sanitizeDimensionName(str) {
321320
return str.replaceAll(/\W/, "")
322321
}
323322

324-
def replaceProductFlavorInContent(content, dimension, flavor)
325-
{
323+
def replaceProductFlavorInContent(content, dimension, flavor) {
326324
def indexStart = content.indexOf("productFlavors");
327325
def index = indexStart + "productFlavors".length();
328326
def indexEnd = -1;
@@ -373,17 +371,16 @@ def replaceProductFlavorInContent(content, dimension, flavor)
373371
}
374372
}
375373
}
376-
//make sure the include.gradle file, produced by the user, has only allowed characters in dimension attribute and remove any invalid characters if necessary
377-
def updateIncludeGradleFile(targetFile, dimensionName, flavor)
378-
{
374+
375+
// make sure the include.gradle file provided by the user has only allowed characters in dimension attribute and remove any invalid characters if necessary
376+
def updateIncludeGradleFile(targetFile, dimensionName, flavor) {
379377
def fileEntry = new File(targetFile.getAbsolutePath());
380378
def content = fileEntry.text;
381379
def replacedContent = replaceProductFlavorInContent(content, dimensionName, flavor);
382380
fileEntry.text = replacedContent;
383381
}
384382

385-
def renamePluginDirToFlavorName(directory, flavor)
386-
{
383+
def renamePluginDirToFlavorName(directory, flavor) {
387384
def parentName = directory.getName();
388385
def parentFile = new File("src", parentName);
389386
if (parentFile.exists())
@@ -401,44 +398,73 @@ task createDefaultIncludeFiles {
401398
println "$configStage createDefaultIncludeFiles"
402399
def ft = file(configurationsDir)
403400

404-
ft.listFiles().each { fl ->
405-
406-
if(fl.isDirectory()) {
407-
def fileName = fl.name
408-
def dimensionName = sanatizeDimensionName(fileName)
409-
createPluginConfigFile = true
410-
def foundIncludeFile = false
411-
412-
def flavor = "F" + flavorNumber++
413-
println "\t+found plugins: " + fileName
414-
fl.listFiles().each { subFile ->
415-
416-
if(subFile.name == "include.gradle") {
417-
foundIncludeFile = true
418-
updateIncludeGradleFile(subFile, dimensionName, flavor)
419-
renamePluginDirToFlavorName(subFile.getParentFile(), flavor);
401+
ft.listFiles().each { file ->
402+
if (file.isDirectory()) {
403+
shouldCreatePluginConfigFile = true
404+
def hasChildrenDirs = false
405+
file.listFiles().each { subFile ->
406+
if (subFile.isDirectory()) {
407+
hasChildrenDirs = true
420408
}
421409
}
422-
423-
flavorNames.add('"' + dimensionName + '"')
424-
425-
if(!foundIncludeFile) {
426-
createIncludeFile(fl.getAbsolutePath() , flavor, dimensionName)
427-
renamePluginDirToFlavorName(fl, flavor);
428-
}
410+
411+
// if plugin is scoped - traverse its children directories
412+
// e.g. @scope/plugin-with-android-aars
413+
if (hasChildrenDirs) {
414+
file.listFiles().each { subFile ->
415+
if (subFile.isDirectory()) {
416+
flavorNumber++
417+
createIncludeGradleForPlugin(subFile, flavorNumber, flavorNames)
418+
}
419+
}
420+
} else {
421+
flavorNumber++
422+
createIncludeGradleForPlugin(file, flavorNumber, flavorNames)
423+
}
429424
}
430425
}
431426
}
432427

428+
def createIncludeGradleForPlugin(file, flavorNumber, flavorNames) {
429+
def parentDir = new File(file.getParent())
430+
def parentName = parentDir.name
431+
def dirToRename = file
432+
433+
if (parentName.indexOf("@") == 0) {
434+
dirToRename = new File(parentName + "_" + file.name)
435+
}
436+
437+
def foundIncludeFile = false
438+
def fileName = file.name
439+
def dimensionName = sanitizeDimensionName(fileName)
440+
441+
def flavor = "F" + flavorNumber
442+
println "\t+found plugins: " + fileName
443+
file.listFiles().each { subFile ->
444+
if (subFile.name == "include.gradle") {
445+
foundIncludeFile = true
446+
updateIncludeGradleFile(subFile, dimensionName, flavor)
447+
renamePluginDirToFlavorName(dirToRename, flavor);
448+
}
449+
}
450+
451+
flavorNames.add('"' + dimensionName + '"')
452+
453+
if (!foundIncludeFile) {
454+
createIncludeFile(file.getAbsolutePath() , flavor, dimensionName)
455+
renamePluginDirToFlavorName(dirToRename, flavor);
456+
}
457+
}
458+
433459
task createPluginsConfigFile {
434460
description "creates product flavor config file based on what plugins are added"
435461

436-
if(configDir.exists()) {
462+
if (configDir.exists()) {
437463
println "$configStage createPluginsConfigFile"
438464

439465
def flavorsFile = new File("$configurationsDir/include.gradle")
440466

441-
if(createPluginConfigFile) {
467+
if(shouldCreatePluginConfigFile) {
442468
println "\t Creating product flavors include.gradle file in $configurationsDir folder..."
443469
def flavors = flavorNames.join(", ")
444470

0 commit comments

Comments
 (0)