1+ /* ********************************************************************
2+ Copyright 2015 the Flapi authors
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ ********************************************************************/
16+
17+ package unquietcode.tools.flapi.plugin
18+
19+ import org.gradle.api.Plugin
20+ import org.gradle.api.Project
21+
22+ /**
23+ * Gradle plugin for generating and compiling descriptors.
24+ *
25+ * @author Ben Fagin
26+ * @version 2015-02-03
27+ */
28+ public class GradlePlugin implements Plugin<Project > {
29+
30+ @Override
31+ public void apply (Project project ) {
32+ FlapiPluginExtension properties = project. extensions. create(" flapi" , FlapiPluginExtension )
33+ project. apply plugin : ' java'
34+
35+ def flapiTask = project. task(' flapi' ) << {
36+ runTask(project, properties)
37+ }
38+
39+ def testClassesTask = project. tasks. getByName(' testClasses' )
40+
41+ // Add a dependency to the 'flapi' task for all existing 'testClasses' dependencies.
42+ testClassesTask. getTaskDependencies(). getDependencies(testClassesTask). each {
43+ flapiTask. dependsOn(it)
44+ }
45+
46+ // Add the 'flapi' task as a dependency of 'testClasses'.
47+ testClassesTask. dependsOn(flapiTask)
48+ }
49+
50+ private void runTask (Project project , FlapiPluginExtension properties ) {
51+
52+ // default for sources directory
53+ if (isEmpty(properties. sourcesDirectory)) {
54+ properties. sourcesDirectory = project. buildDir. getAbsolutePath()+ File . separator+ " generated-sources"
55+ }
56+
57+ // default for classes directory
58+ if (isEmpty(properties. classesDirectory)) {
59+ properties. classesDirectory = project. sourceSets. main. output. classesDir. getPath()
60+ }
61+
62+ // set up shared plugin helper
63+ final PluginHelper helper = new PluginHelper (properties. classesDirectory, properties. sourcesDirectory) {
64+ protected @Override Exception handleError (String message , Throwable cause ) throws Exception {
65+ throw new RuntimeException (message, cause);
66+ }
67+
68+ protected @Override Exception handleFailure (String message , Throwable cause ) throws Exception {
69+ throw new Exception (message, cause);
70+ }
71+
72+ protected @Override void logInfo (String message ) {
73+ project. logger. info(message)
74+ }
75+
76+ protected @Override void logWarn (String message ) {
77+ project. logger. warn(message)
78+ }
79+
80+ protected @Override void logError (String message ) {
81+ project. logger. error(message)
82+ }
83+
84+ protected @Override URLClassLoader getCompiledClassloader () throws Exception {
85+ List<URL > urls = new ArrayList<> ();
86+
87+ // dependencies
88+ for (def path : project. configurations[' testCompile' ]) {
89+ urls. add(path. toURI(). toURL())
90+ }
91+
92+ // sources
93+ for (def sourceSet : project. sourceSets) {
94+ urls. add(sourceSet. output. classesDir. toURI(). toURL())
95+ }
96+
97+ return new URLClassLoader (urls. toArray(new URL [urls. size()]), getClass(). getClassLoader());
98+ }
99+ };
100+
101+ // configure it
102+ helper. setIncludeRuntime(properties. includeRuntime)
103+ helper. setWriteClasses(properties. writeClasses)
104+ helper. setWriteSources(properties. writeSources)
105+
106+ // run it
107+ helper. processDescriptors(properties. descriptorClasses)
108+ }
109+
110+ private static boolean isEmpty (String s ) {
111+ return s == null || s. trim(). isEmpty()
112+ }
113+ }
0 commit comments