diff --git a/.gitignore b/.gitignore index 19e5b7d9..c508c0c3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ .settings bin build +!buildSrc/src/main/groovy/org/springframework/restdocs/build/ target .idea *.iml \ No newline at end of file diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy new file mode 100644 index 00000000..2ff80d71 --- /dev/null +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy @@ -0,0 +1,71 @@ +/* + * Copyright 2014-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.build + +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.api.tasks.Exec +import org.gradle.api.tasks.GradleBuild + +public class SampleBuildConfigurer { + + private final String name + + private String workingDir + + SampleBuildConfigurer(String name) { + this.name = name + } + + void workingDir(String workingDir) { + this.workingDir = workingDir + } + + Task createTask(Project project, Object... dependencies) { + Task mavenBuild = mavenBuild(project, dependencies) + Task gradleBuild = gradleBuild(project, dependencies) + Task sampleBuild = project.tasks.create name + sampleBuild.description = "Builds the ${name} sample" + sampleBuild.group = "Build" + sampleBuild.dependsOn mavenBuild, gradleBuild + return sampleBuild + } + + private Task mavenBuild(Project project, Object... dependencies) { + Task mavenBuild = project.tasks.create("${name}Maven", Exec) + mavenBuild.description = "Builds the ${name} sample with Maven" + mavenBuild.group = "Build" + mavenBuild.workingDir = this.workingDir + String suffix = File.separatorChar == '/' ? '' : '.bat' + mavenBuild.commandLine = [System.env.MAVEN_HOME ? + "${System.env.MAVEN_HOME}/bin/mvn${suffix}" : "mvn${suffix}", + 'clean', 'package'] + mavenBuild.dependsOn dependencies + return mavenBuild + } + + private Task gradleBuild(Project project, Object... dependencies) { + Task gradleBuild = project.tasks.create("${name}Gradle", GradleBuild) + gradleBuild.description = "Builds the ${name} sample with Gradle" + gradleBuild.group = "Build" + gradleBuild.dir = this.workingDir + gradleBuild.tasks = ['clean', 'build'] + gradleBuild.dependsOn dependencies + return gradleBuild + } + +} \ No newline at end of file diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesExtension.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesExtension.groovy new file mode 100644 index 00000000..c86a3104 --- /dev/null +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesExtension.groovy @@ -0,0 +1,49 @@ +/* + * Copyright 2014-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.build + +import org.gradle.api.Project +import org.gradle.api.Task + +public class SamplesExtension { + + Project Project + + Task buildSamplesTask + + Object[] dependencies = [] + + SamplesExtension(Project project, Task buildSamplesTask) { + this.project = project + this.buildSamplesTask = buildSamplesTask + } + + void dependOn(Object... paths) { + this.dependencies += paths + } + + def methodMissing(String name, args) { + SampleBuildConfigurer configurer = new SampleBuildConfigurer(name) + Closure closure = args[0] + closure.delegate = configurer + closure.resolveStrategy = Closure.DELEGATE_FIRST + closure.call() + Task task = configurer.createTask(this.project, this.dependencies) + this.buildSamplesTask.dependsOn task + } + +} \ No newline at end of file diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesPlugin.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesPlugin.groovy new file mode 100644 index 00000000..09af86d8 --- /dev/null +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesPlugin.groovy @@ -0,0 +1,31 @@ +/* + * Copyright 2014-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.build + +import org.gradle.api.Plugin +import org.gradle.api.Project + +public class SamplesPlugin implements Plugin { + + public void apply(Project project) { + def buildSamplesTask = project.tasks.create('buildSamples') + buildSamplesTask.description = 'Builds the configured samples' + buildSamplesTask.group = 'Build' + project.extensions.create('samples', SamplesExtension, project, buildSamplesTask) + } + +} \ No newline at end of file