Search in sources :

Example 1 with GroovyObjectSupport

use of groovy.lang.GroovyObjectSupport in project gradle by gradle.

the class AntGroovydoc method getGroovyVersion.

private String getGroovyVersion(List<File> combinedClasspath) {
    File temp;
    final String tempPath;
    try {
        temp = File.createTempFile("temp", "");
        String p = temp.getCanonicalPath();
        tempPath = File.separatorChar == '/' ? p : p.replace(File.separatorChar, '/');
        temp.deleteOnExit();
    } catch (IOException e) {
        throw new GradleException("Unable to create temp file needed for Groovydoc", e);
    }
    ant.withClasspath(combinedClasspath).execute(new Closure<Object>(this, this) {

        @SuppressWarnings("UnusedDeclaration")
        public Object doCall(Object it) {
            final GroovyObjectSupport antBuilder = (GroovyObjectSupport) it;
            antBuilder.invokeMethod("taskdef", ImmutableMap.of("name", "groovy", "classname", "org.codehaus.groovy.ant.Groovy"));
            antBuilder.invokeMethod("groovy", new Object[] { "new File('" + tempPath + "').text = GroovySystem.version" });
            return null;
        }
    });
    try {
        return Files.toString(temp, Charset.defaultCharset()).trim();
    } catch (IOException e) {
        throw new GradleException("Unable to find Groovy version needed for Groovydoc", e);
    }
}
Also used : GradleException(org.gradle.api.GradleException) GroovyObjectSupport(groovy.lang.GroovyObjectSupport) IOException(java.io.IOException) File(java.io.File)

Example 2 with GroovyObjectSupport

use of groovy.lang.GroovyObjectSupport in project groovity by disney.

the class Closure method tag.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Object tag(Map attributes, final groovy.lang.Closure body) throws Exception {
    Object var = attributes.get(VAR);
    Object args = attributes.get(ARGS);
    if (args instanceof groovy.lang.Closure) {
        args = ((groovy.lang.Closure) args).call();
    }
    final List argNames;
    if (args == null) {
        argNames = new ArrayList<String>();
    } else if (args instanceof List) {
        argNames = (List) args;
    } else {
        argNames = Arrays.asList(args);
    }
    groovy.lang.Closure theClosure = new groovy.lang.Closure(body.getOwner(), body.getDelegate()) {

        public Object doCall(Object[] args) {
            groovy.lang.Closure myBody = (groovy.lang.Closure) body.clone();
            myBody.setDelegate(new GroovyObjectSupport() {

                HashMap argMap = new HashMap();

                public void setProperty(String name, Object value) {
                    if (argNames.contains(name)) {
                        argMap.put(name, value);
                    } else {
                        throw new MissingPropertyException(name);
                    }
                }

                public Object getProperty(String name) {
                    if (argNames.contains(name)) {
                        return argMap.get(name);
                    } else {
                        throw new MissingPropertyException(name);
                    }
                }
            });
            myBody.setResolveStrategy(DELEGATE_FIRST);
            // here we assign runtime args as properties of the closure
            for (int i = 0; i < argNames.size(); i++) {
                Object arg = null;
                if (args != null && i < args.length) {
                    arg = args[i];
                }
                // System.out.println("Setting property "+argNames.get(i).toString()+" to "+arg);
                myBody.setProperty(argNames.get(i).toString(), arg);
            }
            return myBody.call();
        }
    };
    if (var != null) {
        bind(body, var.toString(), theClosure);
    }
    return theClosure;
}
Also used : HashMap(java.util.HashMap) GroovyObjectSupport(groovy.lang.GroovyObjectSupport) MissingPropertyException(groovy.lang.MissingPropertyException) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

GroovyObjectSupport (groovy.lang.GroovyObjectSupport)2 MissingPropertyException (groovy.lang.MissingPropertyException)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 GradleException (org.gradle.api.GradleException)1