Skip to content

Commit 51892f3

Browse files
committed
#25 gradle plugin for using Flapi within a gradle project
1 parent 912a79b commit 51892f3

File tree

7 files changed

+242
-2
lines changed

7 files changed

+242
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apply plugin: 'java'
2+
apply plugin: 'maven'
3+
4+
group = 'unquietcode.tools.flapi.test'
5+
version = '0.0-DEVELOPMENT'
6+
sourceCompatibility = '1.7'
7+
8+
repositories {
9+
mavenLocal()
10+
}
11+
12+
dependencies {
13+
compile 'unquietcode.tools.flapi.test:flapi-build-test-producer:0.0-DEVELOPMENT'
14+
testCompile 'junit:junit:4.11'
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apply plugin: 'java'
2+
apply plugin: 'maven'
3+
4+
group = 'unquietcode.tools.flapi.test'
5+
version = '0.0-DEVELOPMENT'
6+
sourceCompatibility = '1.7'
7+
8+
// add the gradle plugin
9+
buildscript {
10+
repositories {
11+
mavenLocal()
12+
}
13+
14+
dependencies {
15+
classpath 'unquietcode.tools.flapi:flapi-gradle-plugin:0.0-DEVELOPMENT'
16+
}
17+
}
18+
apply plugin: 'com.unquietcode.flapi.plugin'
19+
20+
21+
// ------------------------------------------------------- //
22+
23+
24+
repositories {
25+
mavenLocal()
26+
}
27+
28+
dependencies {
29+
testCompile 'unquietcode.tools.flapi:flapi:0.0-DEVELOPMENT'
30+
}
31+
32+
flapi {
33+
descriptorClasses = ['unquietcode.tools.flapi.plugin.TestDescriptor']
34+
includeRuntime = true
35+
}

flapi-gradle-plugin/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: 'groovy'
2+
apply plugin: 'maven'
3+
4+
group = 'unquietcode.tools.flapi'
5+
version = '0.0-DEVELOPMENT'
6+
sourceCompatibility = '1.7'
7+
8+
repositories {
9+
mavenLocal()
10+
}
11+
12+
dependencies {
13+
compile gradleApi()
14+
compile localGroovy()
15+
16+
compile 'unquietcode.tools.flapi:flapi-plugin:0.0-DEVELOPMENT'
17+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
/**
20+
* Configuration closure for the Flapi Gradle plugin.
21+
*
22+
* @author Ben Fagin
23+
* @version 2015-03-17
24+
*/
25+
public class FlapiPluginExtension {
26+
27+
/**
28+
* The list of {@link unquietcode.tools.flapi.DescriptorMaker} classes.
29+
*/
30+
String[] descriptorClasses;
31+
32+
/**
33+
* The directory to which the generated classes
34+
* will be written.
35+
*/
36+
String classesDirectory;
37+
38+
/**
39+
* The directory to which the generated sources
40+
* will be written.
41+
*/
42+
String sourcesDirectory;
43+
44+
/**
45+
* If true, the runtime classes will be written
46+
* out alongside the generated classes.
47+
*/
48+
boolean includeRuntime = true
49+
50+
/**
51+
* If true, the sources will be written.
52+
*/
53+
boolean writeSources = true
54+
55+
/**
56+
* If true, the sources will be compiled and written.
57+
*/
58+
boolean writeClasses = true
59+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=unquietcode.tools.flapi.plugin.GradlePlugin

flapi-plugin/src/main/java/unquietcode/tools/flapi/plugin/PluginHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public abstract class PluginHelper {
5050
private boolean includeRuntime = true;
5151

5252
public PluginHelper(String classesDirectory, String sourcesDirectory) {
53-
this.classesDirectory = Objects.requireNonNull(classesDirectory);
54-
this.sourcesDirectory = Objects.requireNonNull(sourcesDirectory);
53+
this.classesDirectory = Objects.requireNonNull(classesDirectory, "classes directory is required");
54+
this.sourcesDirectory = Objects.requireNonNull(sourcesDirectory, "sources directory is required");
5555
}
5656

5757
public void setWriteClasses(boolean writeClasses) {

0 commit comments

Comments
 (0)