view src/main/groovy/org/ssdt_ohio/gradle/tasks/UserDoc.groovy @ 13:a628135958e7

USASR-1307: inititial userdoc plugin/task to produce customizable groovy doc
author Dave Smith <smith@nwoca.org>
date Tue, 10 Dec 2013 22:53:57 +0000
parents
children 9de72de14ab3
line wrap: on
line source
package org.ssdt_ohio.gradle.tasks

import org.codehaus.groovy.tools.groovydoc.ClasspathResourceManager
import org.codehaus.groovy.tools.groovydoc.FileOutputTool
import org.codehaus.groovy.tools.groovydoc.GroovyDocTool
import org.codehaus.groovy.tools.groovydoc.gstringTemplates.GroovyDocTemplateInfo
import org.gradle.api.InvalidUserDataException
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileVisitDetails
import org.gradle.api.internal.ClassPathRegistry
import org.gradle.api.logging.LogLevel
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.TaskAction

class UserDoc extends SourceTask {

    private FileCollection groovyClasspath;

    private FileCollection classpath;

    private File destinationDir;

    ClassPathRegistry classPathRegistry

    public UserDoc() {
        getLogging().captureStandardOutput(LogLevel.INFO);
        classPathRegistry = getServices().get(ClassPathRegistry.class);
    }

    @TaskAction
    protected void generate() {
        checkGroovyClasspathNonEmpty(getGroovyClasspath().getFiles());

        def tmpDir = new File(project.buildDir, "tmp/userdoc")
        logger.debug("userdoc tmpDir: $tmpDir")
        project.delete tmpDir
        logger.debug("userdoc source: ${super.source.collect { it.toString() }}")
        project.copy {
            from super.getSource()
            into tmpDir
        }

        def args = [:]
        args.sourcepath = tmpDir.toString()
        args.destdir = destinationDir

        Properties properties = new Properties();
//        properties.setProperty("windowTitle", windowTitle);
//        properties.setProperty("docTitle", docTitle);
//        properties.setProperty("footer", footer);
//        properties.setProperty("header", header);
//        checkScopeProperties(properties);
//        properties.setProperty("publicScope", publicScope.toString());
//        properties.setProperty("protectedScope", protectedScope.toString());
//        properties.setProperty("packageScope", packageScope.toString());
//        properties.setProperty("privateScope", privateScope.toString());
//        properties.setProperty("author", author.toString());
//        properties.setProperty("processScripts", processScripts.toString());
//        properties.setProperty("includeMainForScripts", includeMainForScripts.toString());
//        properties.setProperty("overviewFile", overviewFile != null ? overviewFile.getAbsolutePath() : "");
//        properties.setProperty("charset", charset != null ? charset : "");
//        properties.setProperty("fileEncoding", fileEncoding != null ? fileEncoding : "");
//

        def tool = new GroovyDocTool(new ClasspathResourceManager(),
                [tmpDir.getPath()] as String[],
                getDocTemplates(),
                getPackageTemplates(),
                getClassTemplates(), [],
                properties)

        def addFiles = []

        super.getSource().visit { FileVisitDetails d ->
            if (d.file.isFile()) {
                addFiles << d.getRelativePath().toString()
            }
        }

        tool.add(addFiles)
        tool.renderToOutput(new FileOutputTool(), destinationDir.getCanonicalPath())
    }

    private void checkGroovyClasspathNonEmpty(Collection<File> classpath) {
        if (classpath.isEmpty()) {
            throw new InvalidUserDataException("You must assign a Groovy library to the groovy configuration!");
        }
    }

    /**
     * Returns the directory to generate the documentation into.
     *
     * @return The directory to generate the documentation into
     */
    @OutputDirectory
    public File getDestinationDir() {
        return destinationDir;
    }

    /**
     * Sets the directory to generate the documentation into.
     */
    public void setDestinationDir(File destinationDir) {
        this.destinationDir = destinationDir;
    }

    /**
     * Returns the classpath containing the Groovy library to be used.
     *
     * @return The classpath containing the Groovy library to be used
     */
    @InputFiles
    public FileCollection getGroovyClasspath() {
        return groovyClasspath;
    }

    /**
     * Sets the classpath containing the Groovy library to be used.
     */
    public void setGroovyClasspath(FileCollection groovyClasspath) {
        this.groovyClasspath = groovyClasspath;
    }

    /**
     * Returns the classpath used to locate classes referenced by the documented sources.
     *
     * @return The classpath used to locate classes referenced by the documented sources
     */
    @InputFiles
    public FileCollection getClasspath() {
        return classpath;
    }

    /**
     * Sets the classpath used to locate classes referenced by the documented sources.
     */
    public void setClasspath(FileCollection classpath) {
        this.classpath = classpath;
    }

    /**
     * Creates and returns an array of package template classpath entries.
     * <p>
     * This method is meant to be overridden by custom GroovyDoc implementations, using custom package templates.
     *
     * @return an array of package templates, whereas each entry is resolved as classpath entry, defaults to
     * {@link GroovyDocTemplateInfo#DEFAULT_PACKAGE_TEMPLATES}.
     */
    protected String[] getPackageTemplates() {
        return GroovyDocTemplateInfo.DEFAULT_PACKAGE_TEMPLATES;
    }

    /**
     * Creates and returns an array of doc template classpath entries.
     * <p>
     * This method is meant to be overridden by custom GroovyDoc implementations, using custom doc templates.
     *
     * @return an array of doc templates, whereas each entry is resolved as classpath entry, defaults to
     * {@link GroovyDocTemplateInfo#DEFAULT_DOC_TEMPLATES}.
     */
    protected String[] getDocTemplates() {
        return GroovyDocTemplateInfo.DEFAULT_DOC_TEMPLATES;
    }

    /**
     * Creates and returns an array of class template classpath entries.
     * <p>
     * This method is meant to be overridden by custom GroovyDoc implementations, using custom class templates.
     *
     * @return an array of class templates, whereas each entry is resolved as classpath entry, defaults to
     * {@link GroovyDocTemplateInfo#DEFAULT_CLASS_TEMPLATES}.
     */
    protected String[] getClassTemplates() {
        return GroovyDocTemplateInfo.DEFAULT_CLASS_TEMPLATES;
    }
}