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        maven {
38            url 'http://dl.bintray.com/jetbrains/intellij-plugin-service'
39        }
40
41    }
42    dependencies {
43        classpath "gradle.plugin.org.jetbrains:gradle-intellij-plugin:0.1.10"
44    }
45}
46
47apply plugin: 'java'
48apply plugin: 'idea'
49apply plugin: 'antlr'
50
51
52version = '0.05'
53
54if (!('release' in gradle.startParameter.taskNames)) {
55    def versionSuffix
56    try {
57        def git = org.eclipse.jgit.api.Git.open(file('..'))
58        def head = git.getRepository().getRef('HEAD')
59        versionSuffix = head.getObjectId().abbreviate(8).name()
60
61        if (!git.status().call().clean) {
62            versionSuffix += '-dirty'
63        }
64    } catch (Exception ex) {
65        // In case we can't get the commit for some reason,
66        // just use -dev
67        versionSuffix = 'dev'
68    }
69
70    def baseVersion = version
71    version = baseVersion + '-' + versionSuffix
72} else {
73    if (System.env.JDK7_HOME == null && !JavaVersion.current().isJava7()) {
74        throw new InvalidUserDataException("bzzzzzzzt. Release builds must be performed with java 7. " +
75                "Either run gradle with java 7, or define the JDK7_HOME environment variable.")
76    }
77}
78
79if (System.env.JDK7_HOME != null) {
80    sourceCompatibility = 1.7
81    targetCompatibility = 1.7
82
83    tasks.withType(JavaCompile) {
84        doFirst {
85            options.fork = true
86            options.bootClasspath = "$System.env.JDK7_HOME/jre/lib/rt.jar"
87            options.bootClasspath += "$File.pathSeparator$System.env.JDK7_HOME/jre/lib/jsse.jar"
88        }
89    }
90}
91
92def sandboxDir = "${buildDir}/sandbox"
93
94if (!('idea' in gradle.startParameter.taskNames)) {
95
96    apply plugin: 'org.jetbrains.intellij'
97
98    intellij {
99        version 'IC-2016.3.5'
100        pluginName 'smalidea'
101
102        updateSinceUntilBuild false
103
104        sandboxDirectory sandboxDir
105    }
106
107    // This prints out the directories that can be used to configure a plugin sdk in IDEA, using
108    // the copy of IDEA downloaded by the org.jetbrains.intellij plugin
109    task ideaDirs() {
110        project.afterEvaluate {
111            if (intellij != null) {
112                println "IDEA Plugin jdk: ${intellij.ideaDependency.classes}"
113                println "sources: ${intellij.ideaDependency.getSources()}"
114            }
115        }
116    }
117
118    dependencies {
119        compile files("${System.properties['java.home']}/../lib/tools.jar")
120    }
121
122} else {
123    project(':') {
124        idea {
125            project {
126                ipr {
127                    withXml {
128                        def node = it.asNode()
129
130                        /*node.find { it.@name == 'ProjectRootManager' }
131                                .@'project-jdk-type' = 'IDEA JDK'*/
132
133                        def componentNode = node.find { it.@name == 'ProjectRunConfigurationManager' }
134                        if (componentNode == null) {
135                            componentNode = it.node.appendNode 'component', [name: 'ProjectRunConfigurationManager']
136                        }
137
138                        if (componentNode.find { it.@name == 'All smalidea tests' } == null) {
139                            componentNode.append(new XmlParser().parseText("""
140                                <configuration default="false" name="All smalidea tests" type="JUnit" factoryName="JUnit">
141                                  <extension name="coverage" enabled="false" merge="false" runner="idea" />
142                                  <module name="smalidea" />
143                                  <option name="TEST_OBJECT" value="directory" />
144                                  <option name="VM_PARAMETERS" value="-Didea.system.path=${sandboxDir}/config -Didea.system.path=${sandboxDir}/system-test -Didea.load.plugins.id=org.jf.smalidea" />
145                                  <option name="WORKING_DIRECTORY" value="file://\$PROJECT_DIR\$/smalidea" />
146                                  <option name="PASS_PARENT_ENVS" value="true" />
147                                  <option name="TEST_SEARCH_SCOPE">
148                                    <value defaultName="moduleWithDependencies" />
149                                  </option>
150                                  <dir value="\$PROJECT_DIR\$/smalidea/src/test/java" />
151                                </configuration>"""))
152                        }
153                    }
154                }
155            }
156        }
157    }
158
159    idea {
160        module {
161            jdkName = 'IDEA Plugin jdk'
162
163            excludeDirs -= buildDir
164            if (buildDir.exists()) {
165                excludeDirs.addAll(buildDir.listFiles())
166            }
167
168            for (sourceDir in (sourceDirs + testSourceDirs)) {
169                excludeDirs.remove(sourceDir);
170                while ((sourceDir = sourceDir.getParentFile()) != null) {
171                    excludeDirs.remove(sourceDir);
172                }
173            }
174
175            iml {
176                withXml {
177                    def node = it.node
178
179                    node.@type = 'PLUGIN_MODULE'
180
181                    def pluginUrl = 'file://$MODULE_DIR$/src/main/resources/META-INF/plugin.xml'
182
183                    def pluginNode = node.find { it.@name == 'DevKit.ModuleBuildProperties' }
184                    if (pluginNode == null) {
185                        node.appendNode 'component', [name: 'DevKit.ModuleBuildProperties',
186                                                      url : pluginUrl]
187                    } else {
188                        pluginNode.@url = pluginUrl
189                    }
190                }
191            }
192        }
193    }
194}
195
196repositories {
197    mavenLocal()
198    mavenCentral()
199}
200
201dependencies {
202    compile project(':smali')
203    compile depends.antlr_runtime
204    compile depends.gson
205
206    antlr depends.antlr
207}
208
209task extractTokens(type: org.gradle.api.tasks.Copy, dependsOn: ':smali:build') {
210    project.afterEvaluate {
211        def allArtifacts = configurations.default.resolvedConfiguration.resolvedArtifacts
212        def smaliArtifact = allArtifacts.find { it.moduleVersion.id.name.equals('smali') }
213
214        from(zipTree(smaliArtifact.file)) {
215            include '**/*.tokens'
216        }
217        into "${buildDir}/tokens"
218    }
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)
228ideaModule.dependsOn(generateGrammarSource)
229
230task release(dependsOn: 'buildPlugin') {
231}
232
233tasks.getByPath('idea').dependsOn(project(':').getTasksByName('idea', true).findAll({
234    it.project.name != 'smalidea'
235}))