view src/main/groovy/org/ssdt_ohio/gradle/tasks/UserDoc.groovy @ 17:3741247de37a

USASR-1307: add helper to resolve template classpath problem
author smith@nwoca.org
date Thu, 01 Jan 2015 00:01:02 +0000
parents e3c55e83c9a4
children b14418c2ce40
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
import org.ssdt_ohio.gradle.doc.tools.UserDocHelper

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);
    }

    String windowTitle

    String docTitle

    Boolean publicScope = true
    Boolean protectedScope = false
    Boolean packageScope = false
    Boolean privateScope = false

    @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 : "");

        properties.put('userdocHelper',new UserDocHelper())

        def tool = new GroovyDocTool(new ClasspathResourceManager(this.getClass().getClassLoader()),
                [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 UserDocTemplateInfo.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 UserDocTemplateInfo.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 UserDocTemplateInfo.DEFAULT_CLASS_TEMPLATES;
    }
}