Skip to content

Commit

Permalink
added @RequiresPlugin annotations to JobParent
Browse files Browse the repository at this point in the history
  • Loading branch information
daspilker committed Apr 1, 2015
1 parent dad6d4b commit 9fd4d8b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PluginASTTransformation implements ASTTransformation {
void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
sourceUnit.AST?.classes*.methods.flatten().each { MethodNode method ->
method.getAnnotations(REQUIRES_PLUGIN_ANNOTATION).each { AnnotationNode requiresPluginAnnotation ->
if (!method.declaringClass.getField('jobManagement')) {
if (!method.declaringClass.getField('jobManagement') && !method.declaringClass.getField('jm')) {
sourceUnit.errorCollector.addError(
"no jobManagement field in $method.declaringClass",
Token.newString(
Expand All @@ -43,11 +43,12 @@ class PluginASTTransformation implements ASTTransformation {
sourceUnit
)
}
String jobManagementVariable = method.declaringClass.getField('jobManagement') ? 'jobManagement' : 'jm'

MethodCallExpression pluginCheckStatement
if (requiresPluginAnnotation.members.minimumVersion) {
pluginCheckStatement = new MethodCallExpression(
new VariableExpression('jobManagement'),
new VariableExpression(jobManagementVariable),
new ConstantExpression('requireMinimumPluginVersion'),
new ArgumentListExpression(
requiresPluginAnnotation.members.id,
Expand All @@ -56,7 +57,7 @@ class PluginASTTransformation implements ASTTransformation {
)
} else {
pluginCheckStatement = new MethodCallExpression(
new VariableExpression('jobManagement'),
new VariableExpression(jobManagementVariable),
new ConstantExpression('requirePlugin'),
new ArgumentListExpression(
requiresPluginAnnotation.members.id,
Expand Down
14 changes: 14 additions & 0 deletions job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/JobParent.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ abstract class JobParent extends Script implements DslFactory {
}

@Override
@RequiresPlugin(id = 'build-flow-plugin')
BuildFlowJob buildFlowJob(String name, @DslContext(BuildFlowJob) Closure closure = null) {
processJob(name, BuildFlowJob, closure)
}
Expand All @@ -44,16 +45,19 @@ abstract class JobParent extends Script implements DslFactory {
}

@Override
@RequiresPlugin(id = 'maven-plugin')
MavenJob mavenJob(String name, @DslContext(MavenJob) Closure closure = null) {
processJob(name, MavenJob, closure)
}

@Override
@RequiresPlugin(id = 'jenkins-multijob-plugin')
MultiJob multiJob(String name, @DslContext(MultiJob) Closure closure = null) {
processJob(name, MultiJob, closure)
}

@Override
@RequiresPlugin(id = 'workflow-aggregator')
WorkflowJob workflowJob(String name, @DslContext(WorkflowJob) Closure closure = null) {
processJob(name, WorkflowJob, closure)
}
Expand Down Expand Up @@ -91,26 +95,31 @@ abstract class JobParent extends Script implements DslFactory {
}

@Override
@RequiresPlugin(id = 'sectioned-view')
SectionedView sectionedView(String name, @DslContext(SectionedView) Closure closure = null) {
processView(name, SectionedView, closure)
}

@Override
@RequiresPlugin(id = 'nested-view')
NestedView nestedView(String name, @DslContext(NestedView) Closure closure = null) {
processView(name, NestedView, closure)
}

@Override
@RequiresPlugin(id = 'delivery-pipeline-plugin')
DeliveryPipelineView deliveryPipelineView(String name, @DslContext(DeliveryPipelineView) Closure closure = null) {
processView(name, DeliveryPipelineView, closure)
}

@Override
@RequiresPlugin(id = 'build-pipeline-plugin')
BuildPipelineView buildPipelineView(String name, @DslContext(BuildPipelineView) Closure closure = null) {
processView(name, BuildPipelineView, closure)
}

@Override
@RequiresPlugin(id = 'build-monitor-plugin')
BuildMonitorView buildMonitorView(String name, @DslContext(BuildMonitorView) Closure closure = null) {
processView(name, BuildMonitorView, closure)
}
Expand Down Expand Up @@ -143,6 +152,7 @@ abstract class JobParent extends Script implements DslFactory {

@Override
@Deprecated
@RequiresPlugin(id = 'cloudbees-folder')
Folder folder(@DslContext(Folder) Closure closure) {
jm.logDeprecationWarning()

Expand All @@ -153,6 +163,7 @@ abstract class JobParent extends Script implements DslFactory {
}

@Override
@RequiresPlugin(id = 'cloudbees-folder')
Folder folder(String name, @DslContext(Folder) Closure closure = null) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), 'name must be specified')

Expand All @@ -165,16 +176,19 @@ abstract class JobParent extends Script implements DslFactory {
folder
}

@RequiresPlugin(id = 'config-file-provider')
ConfigFile customConfigFile(String name, @DslContext(ConfigFile) Closure closure = null) {
processConfigFile(name, ConfigFileType.Custom, closure)
}

@RequiresPlugin(id = 'config-file-provider')
ConfigFile mavenSettingsConfigFile(String name, @DslContext(ConfigFile) Closure closure = null) {
processConfigFile(name, ConfigFileType.MavenSettings, closure)
}

@Override
@Deprecated
@RequiresPlugin(id = 'config-file-provider')
ConfigFile configFile(Map<String, Object> arguments = [:], @DslContext(ConfigFile) Closure closure) {
jm.logDeprecationWarning()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package javaposse.jobdsl.dsl
package javaposse.jobdsl.dsl;

import java.lang.annotation.Documented
import java.lang.annotation.ElementType
import java.lang.annotation.Target
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

/**
* Indicates that a plugin must be installed to use the features provided by the annotated DSL method. A minimum
* version can be specified as a lower bound for the version of the required plugin.
*/
@Target([ElementType.METHOD])
@Target(ElementType.METHOD)
@Documented
@interface RequiresPlugin {
/**
* The Plugin ID or short name of the required plugin.
*/
String id()
String id();

/**
* The least acceptable version of the required plugin. Optional, any version will be accepted if none is given.
*/
String minimumVersion() default ''
String minimumVersion() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class JobParentSpec extends Specification {
view instanceof BuildPipelineView
parent.referencedViews.contains(view)
view.node.description[0].text() == 'foo'
_ * jobManagement.requirePlugin('build-pipeline-plugin')
}

def 'build pipeline view without closure'() {
Expand All @@ -105,6 +106,7 @@ class JobParentSpec extends Specification {
view.name == 'test'
view instanceof BuildPipelineView
parent.referencedViews.contains(view)
_ * jobManagement.requirePlugin('build-pipeline-plugin')
}

def 'build monitor view deprecated variant'() {
Expand All @@ -131,6 +133,7 @@ class JobParentSpec extends Specification {
view instanceof BuildMonitorView
parent.referencedViews.contains(view)
view.node.description[0].text() == 'foo'
_ * jobManagement.requirePlugin('build-monitor-plugin')
}

def 'build monitor view without closure'() {
Expand All @@ -141,6 +144,7 @@ class JobParentSpec extends Specification {
view.name == 'test'
view instanceof BuildMonitorView
parent.referencedViews.contains(view)
_ * jobManagement.requirePlugin('build-monitor-plugin')
}

def 'sectioned view deprecated variant'() {
Expand All @@ -167,6 +171,7 @@ class JobParentSpec extends Specification {
view instanceof SectionedView
parent.referencedViews.contains(view)
view.node.description[0].text() == 'foo'
_ * jobManagement.requirePlugin('sectioned-view')
}

def 'sectioned view without closure'() {
Expand All @@ -177,6 +182,7 @@ class JobParentSpec extends Specification {
view.name == 'test'
view instanceof SectionedView
parent.referencedViews.contains(view)
_ * jobManagement.requirePlugin('sectioned-view')
}

def 'nested view deprecated variant'() {
Expand All @@ -203,6 +209,7 @@ class JobParentSpec extends Specification {
view instanceof NestedView
parent.referencedViews.contains(view)
view.node.description[0].text() == 'foo'
_ * jobManagement.requirePlugin('nested-view')
}

def 'nested view without closure'() {
Expand All @@ -213,6 +220,7 @@ class JobParentSpec extends Specification {
view.name == 'test'
view instanceof NestedView
parent.referencedViews.contains(view)
_ * jobManagement.requirePlugin('nested-view')
}

def 'delivery pipeline view deprecated variant'() {
Expand All @@ -239,6 +247,7 @@ class JobParentSpec extends Specification {
view instanceof DeliveryPipelineView
parent.referencedViews.contains(view)
view.node.description[0].text() == 'foo'
_ * jobManagement.requirePlugin('delivery-pipeline-plugin')
}

def 'delivery pipeline view without closure'() {
Expand All @@ -249,6 +258,7 @@ class JobParentSpec extends Specification {
view.name == 'test'
view instanceof DeliveryPipelineView
parent.referencedViews.contains(view)
_ * jobManagement.requirePlugin('delivery-pipeline-plugin')
}

def 'folder deprecated variant'() {
Expand All @@ -261,6 +271,7 @@ class JobParentSpec extends Specification {
folder.name == 'test'
parent.referencedJobs.contains(folder)
2 * jobManagement.logDeprecationWarning()
_ * jobManagement.requirePlugin('cloudbees-folder')
}

def 'folder'() {
Expand All @@ -273,6 +284,7 @@ class JobParentSpec extends Specification {
folder.name == 'test'
parent.referencedJobs.contains(folder)
folder.node.displayName[0].text() == 'foo'
_ * jobManagement.requirePlugin('cloudbees-folder')
}

def 'folder without closure'() {
Expand All @@ -282,6 +294,7 @@ class JobParentSpec extends Specification {
then:
folder.name == 'test'
parent.referencedJobs.contains(folder)
_ * jobManagement.requirePlugin('cloudbees-folder')
}

def 'default config file deprecated variant'() {
Expand All @@ -295,6 +308,7 @@ class JobParentSpec extends Specification {
configFile.type == ConfigFileType.Custom
parent.referencedConfigFiles.contains(configFile)
2 * jobManagement.logDeprecationWarning()
_ * jobManagement.requirePlugin('config-file-provider')
}

def 'custom config file deprecated variant'() {
Expand All @@ -308,6 +322,7 @@ class JobParentSpec extends Specification {
configFile.type == ConfigFileType.Custom
parent.referencedConfigFiles.contains(configFile)
2 * jobManagement.logDeprecationWarning()
_ * jobManagement.requirePlugin('config-file-provider')
}

def 'custom config file'() {
Expand All @@ -321,6 +336,7 @@ class JobParentSpec extends Specification {
configFile.type == ConfigFileType.Custom
configFile.comment == 'foo'
parent.referencedConfigFiles.contains(configFile)
_ * jobManagement.requirePlugin('config-file-provider')
}

def 'custom config file without closure'() {
Expand All @@ -331,6 +347,7 @@ class JobParentSpec extends Specification {
configFile.name == 'test'
configFile.type == ConfigFileType.Custom
parent.referencedConfigFiles.contains(configFile)
_ * jobManagement.requirePlugin('config-file-provider')
}

def 'Maven settings config file deprecated variant'() {
Expand All @@ -344,6 +361,7 @@ class JobParentSpec extends Specification {
configFile.type == ConfigFileType.MavenSettings
parent.referencedConfigFiles.contains(configFile)
2 * jobManagement.logDeprecationWarning()
_ * jobManagement.requirePlugin('config-file-provider')
}

def 'Maven settings config file'() {
Expand All @@ -357,6 +375,7 @@ class JobParentSpec extends Specification {
configFile.type == ConfigFileType.MavenSettings
configFile.comment == 'foo'
parent.referencedConfigFiles.contains(configFile)
_ * jobManagement.requirePlugin('config-file-provider')
}

def 'Maven settings config file without closure'() {
Expand All @@ -367,6 +386,7 @@ class JobParentSpec extends Specification {
configFile.name == 'test'
configFile.type == ConfigFileType.MavenSettings
parent.referencedConfigFiles.contains(configFile)
_ * jobManagement.requirePlugin('config-file-provider')
}

def 'readFileInWorkspace from seed job'() {
Expand Down Expand Up @@ -500,6 +520,7 @@ class JobParentSpec extends Specification {
then:
job.name == 'test'
parent.referencedJobs.contains(job)
_ * jobManagement.requirePlugin('build-flow-plugin')
}

def 'matrixJob deprecated variant'() {
Expand Down Expand Up @@ -544,6 +565,7 @@ class JobParentSpec extends Specification {
then:
job.name == 'test'
parent.referencedJobs.contains(job)
_ * jobManagement.requirePlugin('maven-plugin')
}

def 'multiJob deprecated variant'() {
Expand All @@ -566,6 +588,7 @@ class JobParentSpec extends Specification {
then:
job.name == 'test'
parent.referencedJobs.contains(job)
_ * jobManagement.requirePlugin('jenkins-multijob-plugin')
}

def 'workflow deprecated variant'() {
Expand All @@ -588,5 +611,6 @@ class JobParentSpec extends Specification {
then:
job.name == 'test'
parent.referencedJobs.contains(job)
_ * jobManagement.requirePlugin('workflow-aggregator')
}
}
3 changes: 2 additions & 1 deletion job-dsl-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ buildscript {
maven {
url 'http://repo.jenkins-ci.org/releases/'
}
mavenLocal()
}
dependencies {
classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.10.1'
classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.10.2-SNAPSHOT'
}
}

Expand Down
Loading

0 comments on commit 9fd4d8b

Please sign in to comment.