Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from abayer/directive-generator
[JENKINS-49893] Add a Declarative directive snippet generator
- Loading branch information
Showing
110 changed files
with
4,797 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/AbstractDirective.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2018, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.pipeline.modeldefinition.generator; | ||
|
||
import hudson.model.AbstractDescribableImpl; | ||
import hudson.model.Descriptor; | ||
|
||
public abstract class AbstractDirective<T extends AbstractDirective<T>> extends AbstractDescribableImpl<T> { | ||
public final String toGroovy(boolean indent) { | ||
Descriptor d = getDescriptor(); | ||
if (d instanceof DirectiveDescriptor) { | ||
if (indent) { | ||
return ((DirectiveDescriptor) d).toIndentedGroovy(this); | ||
} else { | ||
return ((DirectiveDescriptor) d).toGroovy(this); | ||
} | ||
} else { | ||
return "// Not a valid descriptor"; | ||
} | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...rc/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/AgentDirective.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2018, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.pipeline.modeldefinition.generator; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Descriptor; | ||
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent; | ||
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor; | ||
import org.jenkinsci.plugins.structs.SymbolLookup; | ||
import org.jenkinsci.plugins.structs.describable.DescribableModel; | ||
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable; | ||
import org.jenkinsci.plugins.workflow.cps.Snippetizer; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AgentDirective extends AbstractDirective<AgentDirective> { | ||
private DeclarativeAgent agent; | ||
|
||
@DataBoundConstructor | ||
public AgentDirective(DeclarativeAgent agent) { | ||
this.agent = agent; | ||
} | ||
|
||
public DeclarativeAgent getAgent() { | ||
return agent; | ||
} | ||
|
||
@Extension | ||
public static class DescriptorImpl extends DirectiveDescriptor<AgentDirective> { | ||
@Override | ||
@Nonnull | ||
public String getName() { | ||
return "agent"; | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public String getDisplayName() { | ||
return "Agent"; | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public List<Descriptor> getDescriptors() { | ||
List<Descriptor> descriptors = new ArrayList<>(); | ||
for (DeclarativeAgentDescriptor td : DeclarativeAgentDescriptor.all()) { | ||
if (!SymbolLookup.getSymbolValue(td).isEmpty()) { | ||
descriptors.add(td); | ||
} | ||
} | ||
|
||
return descriptors; | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public String toGroovy(@Nonnull AgentDirective directive) { | ||
if (directive.agent != null) { | ||
DeclarativeAgentDescriptor desc = directive.agent.getDescriptor(); | ||
|
||
UninstantiatedDescribable ud = UninstantiatedDescribable.from(directive.agent); | ||
DescribableModel model = ud.getModel(); | ||
if (model != null) { | ||
StringBuilder result = new StringBuilder(); | ||
if (DeclarativeAgentDescriptor.zeroArgModels().containsKey(desc.getName())) { | ||
// agent none or agent any | ||
result.append("agent ").append(desc.getName()); | ||
} else { | ||
result.append("agent {\n"); | ||
if (DeclarativeAgentDescriptor.noRequiredArgsModels().containsKey(desc.getName()) && | ||
ud.getArguments().entrySet().stream().allMatch(e -> e.getValue() == null | ||
|| (e.getValue() instanceof String && e.getValue().equals("")))) { | ||
// agent { dockerfile true } | ||
result.append(desc.getName()).append(" true\n"); | ||
} else if (model.hasSingleRequiredParameter() && ud.getArguments().size() == 1) { | ||
// agent { label 'foo' } or agent { docker 'image' } | ||
result.append(Snippetizer.object2Groovy(ud)).append("\n"); | ||
} else { | ||
// Multiple arguments etc | ||
result.append(desc.getName()).append(" ").append(DirectiveGenerator.mapToClosure(ud.getArguments())); | ||
} | ||
result.append("}"); | ||
} | ||
result.append("\n"); | ||
return result.toString(); | ||
} | ||
} | ||
|
||
return "// No valid agent defined\n"; | ||
} | ||
} | ||
} |
Oops, something went wrong.