1import groovy.xml.QName
2
3buildscript {
4    ext.dataBindingRootFolder = project.projectDir
5    apply from: 'propLoader.gradle'
6    ext.addRepos(repositories)
7    if (ext.dataBindingConfig.addRemoteRepos) {
8        dependencies {
9            classpath "com.android.databinding:localizemaven:${dataBindingConfig.extraPluginsVersion}"
10        }
11    }
12    dependencies {
13        classpath 'net.sf.proguard:proguard-gradle:5.2.1'
14    }
15}
16
17subprojects {
18    if (dataBindingConfig.isIndependent) {
19        apply plugin: 'maven'
20        if (dataBindingConfig.addRemoteRepos && !dataBindingConfig.forGradlePlugin) {
21            apply plugin: 'com.android.databinding.localizemaven'
22        }
23    } else {
24        apply from: "${project.projectDir}/../../buildSrc/base/baseJava.gradle"
25    }
26
27    if (it.name != "compilationTests") {
28        group = dataBindingConfig.group
29        version = dataBindingConfig.version
30        uploadArchives {
31            repositories {
32                mavenDeployer {
33                    repository(url: "file://${dataBindingConfig.mavenRepoDir}")
34                }
35            }
36        }
37    }
38
39    buildscript {
40        addRepos(repositories)
41    }
42
43}
44
45if (dataBindingConfig.addRemoteRepos && !dataBindingConfig.forGradlePlugin) {
46    rootProject.localizeMaven {
47        localRepoDir = file(dataBindingConfig.megaRepoDir)
48        otherRepoDirs = dataBindingConfig.localRepositories
49    }
50}
51
52task deleteRepo(type: Delete) {
53    delete "${dataBindingConfig.mavenRepoDir}/${dataBindingConfig.group.replace('.', '/')}"
54}
55
56task deletePrebuildFolder(type: Delete) {
57    delete "${dataBindingConfig.prebuildFolder}"
58}
59
60task deleteEap(type: Delete) {
61    delete "${dataBindingConfig.eapOutDir}"
62}
63
64
65def buildExtensionsTask = project.tasks.create "buildExtensionsTask", Exec
66buildExtensionsTask.workingDir file('extensions').getAbsolutePath()
67buildExtensionsTask.commandLine './gradlew'
68buildExtensionsTask.args 'clean', 'uploadArchives', '--info', '--stacktrace',
69        "-PuseReleaseVersion=${dataBindingConfig.inReleaseBuild ? 'true' : 'false'}",
70        "-PaddRemoteRepos=${dataBindingConfig.addRemoteRepos ? 'true' : 'false'}",
71        "-PforGradlePlugin=${dataBindingConfig.forGradlePlugin ? 'true' : 'false'}"
72buildExtensionsTask.dependsOn subprojects.uploadArchives
73
74def prepareExtensionPrebuilds = project.tasks.create "prepareExtensionPrebuilds", Exec
75prepareExtensionPrebuilds.workingDir file('extensions').getAbsolutePath()
76prepareExtensionPrebuilds.commandLine './gradlew'
77prepareExtensionPrebuilds.args 'clean', 'preparePrebuilds', '--info', '--stacktrace',
78        "-PuseReleaseVersion=${dataBindingConfig.inReleaseBuild ? 'true' : 'false'}",
79        "-PaddRemoteRepos=${dataBindingConfig.addRemoteRepos ? 'true' : 'false'}",
80        "-PforGradlePlugin=${dataBindingConfig.forGradlePlugin ? 'true' : 'false'}"
81prepareExtensionPrebuilds.dependsOn subprojects.uploadArchives
82
83file('integration-tests').listFiles().findAll { it.isDirectory() }.each {
84    println("Creating run test task for  ${it.getAbsolutePath()}.")
85    def testTask = project.tasks.create "runTestsOf${it.getName().capitalize()}", Exec
86    testTask.workingDir it.getAbsolutePath()
87    //on linux
88    testTask.commandLine './gradlew'
89    testTask.args 'clean', 'connectedCheck', '--info', '--stacktrace',
90            "-PuseReleaseVersion=${dataBindingConfig.inReleaseBuild ? 'true' : 'false'}",
91            "-PaddRemoteRepos=${dataBindingConfig.addRemoteRepos ? 'true' : 'false'}",
92            "-PforGradlePlugin=${dataBindingConfig.forGradlePlugin ? 'true' : 'false'}"
93    testTask.dependsOn subprojects.uploadArchives
94}
95
96task runIntegrationTests {
97    dependsOn tasks.findAll { task -> task.name.startsWith('runTestsOf') }
98}
99
100task runAllTests {
101    dependsOn runIntegrationTests
102}
103
104task preparePrebuilds() {
105    dependsOn deletePrebuildFolder
106    dependsOn prepareExtensionPrebuilds
107}
108
109allprojects {
110    afterEvaluate { project ->
111        runAllTests.dependsOn project.tasks.findAll {task -> task.name.equals('test')}
112        runAllTests.dependsOn project.tasks.findAll {task -> task.name.equals('connectedCheck')}
113        preparePrebuilds.dependsOn project.tasks.findAll {task -> task.name.startsWith('prebuild')}
114    }
115}
116
117subprojects.uploadArchives.each { it.shouldRunAfter deleteRepo  }
118subprojects.uploadArchives.each { it.shouldRunAfter deletePrebuildFolder  }
119
120buildExtensionsTask.shouldRunAfter deleteRepo
121tasks['runTestsOfMultiModuleTestApp'].dependsOn tasks['runTestsOfIndependentLibrary']
122
123
124task rebuildRepo() {
125    dependsOn deleteRepo
126    dependsOn subprojects.uploadArchives
127}
128
129task copySamplesToEap(type : Copy) {
130    mustRunAfter deleteEap
131    from ("$projectDir/samples") {
132        exclude "**/build"
133        exclude "**/local.properties"
134    }
135    into "${dataBindingConfig.eapOutDir}/samples"
136}
137
138
139task copyMavenRepoToEap(type : Copy) {
140    mustRunAfter deleteEap
141    dependsOn rebuildRepo
142    from(dataBindingConfig.mavenRepoDir)
143    into "${dataBindingConfig.eapOutDir}/${dataBindingConfig.mavenRepoName}"
144}
145
146tasks.create(name : 'createEapdataBindingConfigFile') << {
147    def propsFile = new File("${dataBindingConfig.eapOutDir}/databinding.properties")
148    Properties props = new Properties()
149    props.setProperty('version', dataBindingConfig.version)
150    props.setProperty('mavenRepoName', dataBindingConfig.mavenRepoName)
151    props.store(propsFile.newWriter(), null)
152}
153
154task batchEAP() {
155    dependsOn deleteEap
156    dependsOn copyMavenRepoToEap
157    dependsOn copySamplesToEap
158    dependsOn createEapdataBindingConfigFile
159}
160
161def findChildByTag(node, tag) {
162    return node.children().find {
163        it.name().getLocalPart().equals(tag)
164    }
165}
166
167def fullJar(project) {
168    def localizeTask = project.parent.tasks.findByName('localizeDependencies')
169    if (localizeTask != null) {
170        localizeTask.dependsOn project.tasks.findByName('buildLicenseNotice')
171    }
172    if (!dataBindingConfig.inReleaseBuild) {
173        return
174    }
175    def jarName = project.uploadArchives.repositories.mavenDeployer.pom.artifactId
176    def workingDir = "${project.buildDir}/intermediates/fullJar"
177    def fatJar = "${workingDir}/${jarName}-fat.jar"
178    def proguardJar = "${workingDir}/${jarName}-proguard.jar"
179    def jarJar = project.jar.archivePath
180    def runProguard = dataBindingConfig.runProguard
181
182    project.configurations {
183        jarJarArchives
184    }
185
186    project.tasks.create(name: 'fatJar', type: Jar) {
187        baseName = jarName + '-fat'
188        doFirst {
189            file(workingDir).mkdirs()
190        }
191        def deps = new HashSet<ResolvedDependency>()
192        project.configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each {
193            if (!it.moduleGroup.startsWith('com.android.tools')) {
194                deps.addAll(it.allModuleArtifacts)
195            }
196        }
197        from { deps.findAll { !it.name.contains('baseLibrary') && !it.name.contains("juniversalchardet")}.collect {
198            it.file.isDirectory() ? it.file : project.zipTree(it.file)
199        } } {
200            exclude "META-INF/maven/**"
201            exclude "META-INF/MANIFEST.MF"
202            exclude "META-INF/LICENSE.txt"
203            exclude "META-INF/NOTICE.txt"
204            exclude "META-INF/services/javax.annotation.processing.Processor"
205            exclude "**/*.stg"
206        }
207        archiveName "${baseName}.jar"
208        destinationDir = new File(workingDir)
209        with project.jar
210    }
211    /**
212     * not used unless jarJarFile is changed to use proguarded version instead.
213     */
214    project.tasks.create(name: 'proguard', type: proguard.gradle.ProGuardTask) {
215        dependsOn 'fatJar'
216
217        injars  fatJar
218        outjars proguardJar
219
220        configuration '../proguard.cfg'
221    }
222
223    project.tasks.create(name: 'jarJarFile') {
224        dependsOn runProguard ? 'proguard' : 'fatJar'
225        dependsOn project.jar
226        def inputLibrary = runProguard ? proguardJar : fatJar
227        def outputLibrary = jarJar
228        inputs.file(inputLibrary)
229        outputs.file(outputLibrary)
230
231        doLast {
232            def jarJarLibrary = new File(dataBindingConfig.externalPrebuiltsBase,
233                    'tools/common/m2/repository/com/googlecode/jarjar/jarjar/1.4/jarjar-1.4.jar').
234                    getCanonicalPath()
235            // in Ant
236            ant.taskdef(name: "jarjarIt",
237                    classname: 'com.tonicsystems.jarjar.JarJarTask',
238                    classpath: jarJarLibrary)
239            // start jarjar task
240            ant.jarjarIt(jarfile: outputLibrary) {
241                // input is our inputLibrary
242                zipfileset(src: inputLibrary)
243                // rule to repackage antlr to new package
244                rule pattern: 'org.antlr.**', result: 'com.google.repacked.org.antlr.@1'
245                rule pattern: 'org.abego.treelayout.**', result: 'com.google.repacked.treelayout.@1'
246                // rule to repackage commons
247                rule pattern: 'org.apache.**', result: 'com.google.repacked.apache.@1'
248                rule pattern: 'kotlin.**', result: 'com.google.repacked.kotlin.@1'
249            }
250        }
251    }
252
253    def setupUpload = { uploadTask ->
254        uploadTask.dependsOn 'jarJarFile'
255        uploadTask.repositories {
256                mavenDeployer {
257                    pom.whenConfigured { pom ->
258                        pom.dependencies.removeAll { dep ->
259                            def isBaseLibrary = dep.groupId == 'com.android.databinding' &&
260                                    dep.artifactId == 'baseLibrary'
261                            def isGradle = dep.groupId == 'com.android.tools.build' &&
262                                    dep.artifactId == 'gradle'
263                            def isChardet = dep.groupId == 'com.googlecode.juniversalchardet' &&
264                                    dep.artifactId == 'juniversalchardet'
265                            return !isBaseLibrary && !isGradle && !isChardet
266                        }
267                    }
268                }
269            }
270        uploadTask.outputs.upToDateWhen { false } // force it to re-run all the time.
271    }
272
273    if (project.publishLocal != null) {
274        setupUpload(project.publishLocal)
275    }
276    if (project.uploadArchives != null) {
277        setupUpload(project.uploadArchives)
278    }
279
280    project.bintrayUpload.dependsOn 'jarJarFile'
281    project.publishing.publications.mavenJava(MavenPublication) {
282            pom.withXml {
283                def deps = findChildByTag(asNode(), "dependencies")
284                if (deps == null) {
285                    throw new IllegalArgumentException("cannot find dependencies in pom file")
286                }
287                def unwanted = deps.children().findAll {
288                    def groupId = findChildByTag(it, "groupId").localText()[0]
289                    def artifactId = findChildByTag(it, "artifactId").localText()[0]
290                    def isBaseLibrary = groupId == 'com.android.databinding' &&
291                            artifactId == 'baseLibrary'
292                    def isGradle = groupId == 'com.android.tools.build' && artifactId == 'gradle'
293                    def isChardet = groupId == 'com.googlecode.juniversalchardet' &&
294                            artifactId == 'juniversalchardet'
295                    return !isBaseLibrary && !isGradle && !isChardet
296                }
297                unwanted.each {
298                    deps.remove(it)
299                }
300            }
301    }
302}
303