Last active
August 29, 2015 14:04
-
-
Save darylteo/9cdb9b8e32be4197e72e to your computer and use it in GitHub Desktop.
Simple Plugin in Java for Gradle
This file contains hidden or 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
// usage | |
ssh { | |
// dsl form | |
options { | |
foo 'bar' | |
name project.name | |
flag(false) // also possible | |
} | |
// property form | |
options.hello = 'world' | |
} | |
println ssh.options | |
// { foo: "bar", name: "??", flag: false, hello: "world" } |
This file contains hidden or 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
package org.company.gradle; | |
import groovy.lang.Closure; | |
import groovy.lang.MissingMethodException; | |
import org.gradle.api.Plugin; | |
import org.gradle.api.Project; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by dteo on 18/07/2014. | |
*/ | |
class SSHPlugin implements Plugin<Project> { | |
@Override | |
public void apply(Project project) { | |
// this namespaces your configuration to ssh { } | |
project.getExtensions().create("ssh", SSHConfiguration.class); | |
} | |
} | |
// this will need to be an extension that you create in your plugin itself | |
class SSHConfiguration { | |
private SSHOptions options = new SSHOptions(); | |
public SSHOptions getOptions() { | |
return this.options; | |
} | |
// this will accept ssh { options { } } | |
public void options(Closure closure) { | |
closure.setResolveStrategy(Closure.DELEGATE_FIRST); | |
closure.setDelegate(this.options); | |
closure.call(this.options); | |
} | |
} | |
class SSHOptions extends GroovyObjectSupport { | |
private Map<String, Object> args = new HashMap<String,Object>(); | |
public String get(String name) { | |
return this.args.get(name); | |
} | |
public Map<String, String> getAll() { | |
return this.args; | |
} | |
public void set(String name, Object value) { | |
if (value != null) { | |
this.args.put(name, value.toString()); | |
} else { | |
this.args.remove(name); | |
} | |
} | |
public Object invokeMethod(String name, Object values) { | |
Object[] args = (Object[]) values; | |
if (args == null || args.length != 1) { | |
throw new MissingMethodException(name, SSHOptions.class, args); | |
} | |
this.args.put(name, args[0]); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/groovy/org/gradle/api/internal/artifacts/dsl/dependencies/DefaultDependencyHandler.java