-
Notifications
You must be signed in to change notification settings - Fork 0
/
activities-constants.gradle
120 lines (93 loc) · 5.62 KB
/
activities-constants.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.regex.Matcher
import java.util.regex.Pattern
def generatedSrcDir = "$buildDir/generated/src/main/kotlin/"
android.sourceSets.main.java.srcDirs += ['src/main/kotlin', "$generatedSrcDir"]
def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern
if (tskReqStr.contains("assemble")) {
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
} else {
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
}
Matcher matcher = pattern.matcher(tskReqStr)
if (matcher.find())
return matcher.group(1).toLowerCase()
else {
return ""
}
}
static String toCamelCase(String text) {
return text.toLowerCase().replaceAll("(_)([A-Za-z0-9])", { Object[] it -> it[2].toUpperCase() })
}
task activitiesConstants {
doLast {
// Map<String,String> with :
// key = The name of the module concatenated to the name of the activity
// value = List of manifests of the module
def moduleManifests = [:]
rootProject.allprojects.each { p ->
p.plugins.withId("com.android.base") {
// find all main manifests and currentFlavor manifests
moduleManifests[p.name] = p.android.sourceSets
.findAll { it.name in ["main", currentFlavor] }*.manifest.srcFile
.findAll { it.exists() } // keep only existing manifests
}
}
println "\nList of manifests in module(s) :"
moduleManifests.each { key, values -> println "$key = $values" }
// Map<String,String> with :
// key = The name of the module concatenated to the name of the activity
// value = The canonical name of the activity
def activities = [:]
moduleManifests.each { module, manifests ->
manifests.each { file ->
def manifest = new XmlSlurper().parse(file)
def manifestPackage = manifest.@package.text()
manifest.application.activity.each { activity ->
def activityName = activity."@android:name".text()
def variableName = "${activityName.split("\\.").last()}".replaceAll(/\B[A-Z]/) { '_' + it }.toUpperCase()
activityName = activityName.startsWith(".") ? "$manifestPackage$activityName" : activityName
if (activities[module] == null) activities[module] = [:]
activities[module] += [(variableName): activityName]
}
}
}
if (activities.size() > 0) {
def manifestPackage = android.defaultConfig.applicationId
if (manifestPackage == null) {
// if applicationId is not set in build.gradle
// we retrieve the applicationId from the AndroidManifest.xml
manifestPackage = new XmlSlurper().parse(android.sourceSets.main.manifest.srcFile).@package.text()
}
def genSrcDir = file("$generatedSrcDir/${manifestPackage.replaceAll("\\.", "\\/")}")
genSrcDir.mkdirs()
def activityConstantKt = file("${genSrcDir.path}/ActivitiesConstant.kt")
activityConstantKt.write "package $manifestPackage\n\n"
activityConstantKt << "import android.app.Activity\n"
activityConstantKt << "import android.content.Context\n"
activityConstantKt << "import android.content.Intent\n"
activityConstantKt << "import androidx.fragment.app.Fragment\n\n"
activityConstantKt << "class ActivitiesConstant {\n\n"
println "\nActivities constants available -> ${activityConstantKt.path}"
activities.keySet().each { module ->
activityConstantKt << " object ${module.capitalize()} {\n"
activities[module].each { variable, value ->
activityConstantKt << " const val $variable: String = \"$value\"\n"
activityConstantKt << " fun Context.${toCamelCase(variable)}(block: Intent.() -> Unit = {}) = Intent().apply { setClassName(this@${toCamelCase(variable)}, $variable) }.apply(block)\n"
activityConstantKt << " fun Context.start${toCamelCase(variable).capitalize()}(block: Intent.() -> Unit = {}) = startActivity(${toCamelCase(variable)}(block))\n"
activityConstantKt << " fun Activity.start${toCamelCase(variable).capitalize()}ForResult(block: Intent.() -> Unit = {}, requestCode: Int) = startActivityForResult(${toCamelCase(variable)}(block), requestCode)\n"
activityConstantKt << " fun Fragment.start${toCamelCase(variable).capitalize()}ForResult(block: Intent.() -> Unit = {}, requestCode: Int) = context?.run { startActivityForResult(${toCamelCase(variable)}(block), requestCode) }\n"
activityConstantKt << " fun Activity.start${toCamelCase(variable).capitalize()}ForResult(requestCode: Int, block: Intent.() -> Unit = {}) = startActivityForResult(${toCamelCase(variable)}(block), requestCode)\n"
activityConstantKt << " fun Fragment.start${toCamelCase(variable).capitalize()}ForResult(requestCode: Int, block: Intent.() -> Unit = {}) = context?.run { startActivityForResult(${toCamelCase(variable)}(block), requestCode) }\n"
activityConstantKt << " \n"
}
activityConstantKt << " }\n\n"
}
activityConstantKt << "}\n"
println activityConstantKt.readLines().join("\n")
}
}
}
preBuild.dependsOn activitiesConstants