1import java.util.regex.Pattern
2
3allprojects {
4    // Expose the per-object-directory configuration to all projects.
5    ext {
6        mozconfig = gradle.mozconfig
7        topsrcdir = gradle.mozconfig.topsrcdir
8        topobjdir = gradle.mozconfig.topobjdir
9    }
10
11    repositories {
12        if (gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORY) {
13            maven {
14                url gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORY
15            }
16        }
17    }
18}
19
20buildDir "${topobjdir}/gradle/build"
21
22buildscript {
23    repositories {
24        if (gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORY) {
25            maven {
26                url gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORY
27            }
28        }
29        // For android-sdk-manager SNAPSHOT releases.
30        maven {
31            url "file://${gradle.mozconfig.topsrcdir}/mobile/android/gradle/m2repo"
32        }
33    }
34
35    dependencies {
36        classpath 'com.android.tools.build:gradle:2.1.3'
37        classpath('com.stanfy.spoon:spoon-gradle-plugin:1.0.4') {
38            // Without these, we get errors linting.
39            exclude module: 'guava'
40        }
41        // Provided in tree.
42        classpath 'com.jakewharton.sdkmanager:gradle-plugin:1.5.0-SNAPSHOT'
43    }
44}
45
46task generateCodeAndResources(type:Exec) {
47    workingDir "${topobjdir}"
48
49    commandLine mozconfig.substs.GMAKE
50    args '-C'
51    args "${topobjdir}/mobile/android/base"
52    args 'gradle-targets'
53
54    // Only show the output if something went wrong.
55    ignoreExitValue = true
56    standardOutput = new ByteArrayOutputStream()
57    errorOutput = standardOutput
58    doLast {
59        if (execResult.exitValue != 0) {
60            throw new GradleException("Process '${commandLine}' finished with non-zero exit value ${execResult.exitValue}:\n\n${standardOutput.toString()}")
61        }
62    }
63}
64
65// Skip unit test for all build variants, unless if it was specifically requested by user.
66// The enabled property for the unit test tasks is reset based on the command line task names just before the task execution.
67// I bet there is a easier/cleaner way to do this, but this gets the job done for now.
68Pattern pattern = Pattern.compile('.*test(.+UnitTest)?.*')
69boolean startTasksIncludeTest = gradle.startParameter.taskNames.any {
70    taskName ->
71        taskName.matches(pattern)
72}
73gradle.taskGraph.beforeTask {
74    Task task ->
75        if (task.name.matches(pattern)) {
76            task.enabled = startTasksIncludeTest
77        }
78}
79
80afterEvaluate {
81    subprojects {
82        if (!hasProperty('android')) {
83            return
84        }
85        android.applicationVariants.all {
86            preBuild.dependsOn rootProject.generateCodeAndResources
87        }
88        android.libraryVariants.all {
89            preBuild.dependsOn rootProject.generateCodeAndResources
90        }
91    }
92}
93
94apply plugin: 'idea'
95
96idea {
97    project {
98        languageLevel = '1.7'
99    }
100
101    module {
102        // Object directories take a huge amount of time for IntelliJ to index.
103        // Exclude them.  Convention is that object directories start with obj.
104        // IntelliJ is clever and will not exclude the parts of the object
105        // directory that are referenced, if there are any.  In practice,
106        // indexing the entirety of the tree is taking too long, so exclude all
107        // but mobile/.
108        def topsrcdirURI = file(topsrcdir).toURI()
109        excludeDirs += files(file(topsrcdir)
110            .listFiles({it.isDirectory()} as FileFilter)
111            .collect({topsrcdirURI.relativize(it.toURI()).toString()}) // Relative paths.
112            .findAll({!it.equals('mobile/')}))
113
114        // If topobjdir is below topsrcdir, hide only some portions of that tree.
115        def topobjdirURI = file(topobjdir).toURI()
116        if (!topsrcdirURI.relativize(topobjdirURI).isAbsolute()) {
117            excludeDirs -= file(topobjdir)
118            excludeDirs += files(file(topobjdir).listFiles())
119            excludeDirs -= file("${topobjdir}/gradle")
120        }
121
122        if (!mozconfig.substs.MOZ_INSTALL_TRACKING) {
123            excludeDirs += file("${topsrcdir}/mobile/android/thirdparty/com/adjust")
124        }
125    }
126}
127
128task wrapper(type: Wrapper) {
129}
130