1/*
2 * Copyright 2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32buildscript {
33    repositories {
34        maven {
35            url "https://plugins.gradle.org/m2/"
36        }
37    }
38    dependencies {
39        classpath 'gradle.plugin.org.jetbrains:gradle-intellij-plugin:0.0.40'
40    }
41}
42
43apply plugin: 'java'
44apply plugin: 'idea'
45apply plugin: 'antlr'
46
47version = '0.03'
48
49if (!('release' in gradle.startParameter.taskNames)) {
50    def versionSuffix
51    try {
52        def git = org.eclipse.jgit.api.Git.open(file('..'))
53        def head = git.getRepository().getRef('HEAD')
54        versionSuffix = head.getObjectId().abbreviate(8).name()
55
56        if (!git.status().call().clean) {
57            versionSuffix += '-dirty'
58        }
59    } catch (Exception ex) {
60        // In case we can't get the commit for some reason,
61        // just use -dev
62        versionSuffix = 'dev'
63    }
64
65    def baseVersion = version
66    version = baseVersion + '-' + versionSuffix
67} else {
68    if (System.env.JDK7_HOME == null && !JavaVersion.current().isJava7()) {
69        throw new InvalidUserDataException("bzzzzzzzt. Release builds must be performed with java 7. " +
70                "Either run gradle with java 7, or define the JDK7_HOME environment variable.")
71    }
72}
73
74if (System.env.JDK7_HOME != null) {
75    sourceCompatibility = 1.7
76    targetCompatibility = 1.7
77
78    tasks.withType(JavaCompile) {
79        doFirst {
80            options.fork = true
81            options.bootClasspath = "$System.env.JDK7_HOME/jre/lib/rt.jar"
82            options.bootClasspath += "$File.pathSeparator$System.env.JDK7_HOME/jre/lib/jsse.jar"
83        }
84    }
85}
86
87def sandboxDir = "${buildDir}/sandbox"
88
89// We don't want to use the org.jetbrains.intellij plugin when generating the idea project files,
90// so that idea classes aren't included as project dependencies, since they will already exist
91// in the plugin sdk defined for the project
92if (!('idea' in gradle.startParameter.taskNames)) {
93    apply plugin: 'org.jetbrains.intellij'
94
95    intellij {
96        version 'IC-15.0.6'
97        pluginName 'smalidea'
98
99        updateSinceUntilBuild false
100
101        sandboxDirectory sandboxDir
102    }
103
104    // This prints out the directories that can be used to configure a plugin sdk in IDEA, using
105    // the copy of IDEA downloaded by the org.jetbrains.intellij plugin
106    task ideaDirs() {
107        project.afterEvaluate {
108            if (intellij != null) {
109                println "IDEA Plugin jdk: ${intellij.ideaDirectory}"
110                println "sources: ${project.configurations['intellij-sources'].files[0]}"
111            }
112        }
113    }
114
115    dependencies {
116        compile files("${System.properties['java.home']}/../lib/tools.jar")
117    }
118} else {
119    // If we're running the idea task, let's make sure nothing else is being run, since
120    // we have to use a special configuration for the idea task
121    if (gradle.startParameter.taskNames.size() > 1) {
122        throw new InvalidUserDataException("The idea task must be run by itself.")
123    }
124
125    project(':') {
126        idea {
127            project {
128                ipr {
129                    withXml {
130                        def node = it.asNode()
131
132                        /*node.find { it.@name == 'ProjectRootManager' }
133                                .@'project-jdk-type' = 'IDEA JDK'*/
134
135                        def componentNode = node.find { it.@name == 'ProjectRunConfigurationManager' }
136                        if (componentNode == null) {
137                            componentNode = it.node.appendNode 'component', [name: 'ProjectRunConfigurationManager']
138                        }
139
140                        if (componentNode.find { it.@name == 'All smalidea tests' } == null) {
141                            componentNode.append(new XmlParser().parseText("""
142                                <configuration default="false" name="All smalidea tests" type="JUnit" factoryName="JUnit">
143                                  <extension name="coverage" enabled="false" merge="false" runner="idea" />
144                                  <module name="smalidea" />
145                                  <option name="TEST_OBJECT" value="directory" />
146                                  <option name="VM_PARAMETERS" value="-Didea.system.path=${sandboxDir}/config -Didea.system.path=${sandboxDir}/system-test -Didea.load.plugins.id=org.jf.smalidea" />
147                                  <option name="WORKING_DIRECTORY" value="file://\$PROJECT_DIR\$/smalidea" />
148                                  <option name="PASS_PARENT_ENVS" value="true" />
149                                  <option name="TEST_SEARCH_SCOPE">
150                                    <value defaultName="moduleWithDependencies" />
151                                  </option>
152                                  <dir value="\$PROJECT_DIR\$/smalidea/src/test/java" />
153                                </configuration>"""))
154                        }
155                    }
156                }
157            }
158        }
159    }
160
161    idea {
162        module {
163            jdkName = 'IDEA Plugin jdk'
164
165            excludeDirs -= buildDir
166            if (buildDir.exists()) {
167                excludeDirs.addAll(buildDir.listFiles())
168            }
169
170            for (sourceDir in (sourceDirs + testSourceDirs)) {
171                excludeDirs.remove(sourceDir);
172                while ((sourceDir = sourceDir.getParentFile()) != null) {
173                    excludeDirs.remove(sourceDir);
174                }
175            }
176
177            iml {
178                withXml {
179                    def node = it.node
180
181                    node.@type = 'PLUGIN_MODULE'
182
183                    def pluginUrl = 'file://$MODULE_DIR$/src/main/resources/META-INF/plugin.xml'
184
185                    def pluginNode = node.find { it.@name == 'DevKit.ModuleBuildProperties' }
186                    if (pluginNode == null) {
187                        node.appendNode 'component', [name: 'DevKit.ModuleBuildProperties',
188                                                      url : pluginUrl]
189                    } else {
190                        pluginNode.@url = pluginUrl
191                    }
192                }
193            }
194        }
195    }
196}
197
198repositories {
199    mavenLocal()
200    mavenCentral()
201}
202
203dependencies {
204    compile project(':smali')
205    compile depends.antlr_runtime
206    compile depends.gson
207
208    antlr depends.antlr
209}
210
211task extractTokens(type: org.gradle.api.tasks.Copy, dependsOn: ':smali:build') {
212    def allArtifacts = configurations.default.resolvedConfiguration.resolvedArtifacts
213    def smaliArtifact = allArtifacts.find { it.moduleVersion.id.name.equals('smali') }
214
215    from(zipTree(smaliArtifact.file)) {
216        include '**/*.tokens'
217    }
218    into "${buildDir}/tokens"
219}
220
221generateGrammarSource {
222    def tokensDir = file("${buildDir}/tokens/org/jf/smali")
223    inputs.file new File(tokensDir, 'smaliParser.tokens')
224    setArguments(['-lib', tokensDir.path])
225    outputDirectory(file("${buildDir}/generated-src/antlr/main/org/jf/smalidea"))
226}
227generateGrammarSource.dependsOn(extractTokens)
228
229ideaModule.dependsOn(generateGrammarSource)
230
231task release(dependsOn: 'buildPlugin') {
232}
233
234tasks.getByPath('idea').dependsOn(project(':').getTasksByName('idea', true).findAll({
235    it.project.name != 'smalidea'
236}))