JavadocArchiver.java 4.2 KB
Newer Older
K
kohsuke 已提交
1 2
package hudson.tasks;

K
kohsuke 已提交
3
import hudson.FilePath;
K
kohsuke 已提交
4
import hudson.Launcher;
K
kohsuke 已提交
5
import hudson.Util;
6
import hudson.util.FormFieldValidator;
K
kohsuke 已提交
7 8 9 10
import hudson.model.Action;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
11
import hudson.model.DirectoryBrowserSupport;
K
kohsuke 已提交
12 13
import hudson.model.Project;
import hudson.model.ProminentProjectAction;
K
kohsuke 已提交
14
import hudson.model.Result;
15
import hudson.model.Actionable;
16
import hudson.model.AbstractItem;
K
kohsuke 已提交
17 18 19 20 21 22 23 24
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;

/**
25
 * Saves Javadoc for the project and publish them. 
K
kohsuke 已提交
26 27 28
 *
 * @author Kohsuke Kawaguchi
 */
K
kohsuke 已提交
29
public class JavadocArchiver extends Publisher {
K
kohsuke 已提交
30
    /**
31
     * Path to the Javadoc directory in the workspace.
K
kohsuke 已提交
32 33 34 35 36 37 38 39 40 41 42 43
     */
    private final String javadocDir;

    public JavadocArchiver(String javadocDir) {
        this.javadocDir = javadocDir;
    }

    public String getJavadocDir() {
        return javadocDir;
    }

    /**
44
     * Gets the directory where the Javadoc is stored for the given project.
K
kohsuke 已提交
45
     */
46
    private static File getJavadocDir(AbstractItem project) {
K
kohsuke 已提交
47 48 49
        return new File(project.getRootDir(),"javadoc");
    }

50
    public boolean perform(Build<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException {
51
        listener.getLogger().println("Publishing Javadoc");
K
kohsuke 已提交
52

K
kohsuke 已提交
53 54
        FilePath javadoc = build.getParent().getWorkspace().child(javadocDir);
        FilePath target = new FilePath(getJavadocDir(build.getParent()));
K
kohsuke 已提交
55

K
kohsuke 已提交
56
        try {
K
kohsuke 已提交
57 58 59 60 61 62
            // if the build has failed, then there's not much point in reporting an error
            // saying javadoc directory doesn't exist. We want the user to focus on the real error,
            // which is the build failure.
            if(build.getResult().isWorseOrEqualTo(Result.FAILURE) && !javadoc.exists())
                return true;

K
kohsuke 已提交
63 64 65 66 67 68
            javadoc.copyRecursiveTo("**/*",target);
        } catch (IOException e) {
            Util.displayIOException(e,listener);
            e.printStackTrace(listener.fatalError("Unable to copy Javadoc from "+javadoc+" to "+target));
            build.setResult(Result.FAILURE);
        }
K
kohsuke 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81

        return true;
    }

    public Action getProjectAction(Project project) {
        return new JavadocAction(project);
    }

    public Descriptor<Publisher> getDescriptor() {
        return DESCRIPTOR;
    }


82
    public static final Descriptor<Publisher> DESCRIPTOR = new DescriptorImpl();
K
kohsuke 已提交
83

84
    public static final class JavadocAction extends Actionable implements ProminentProjectAction {
85
        private final AbstractItem project;
K
kohsuke 已提交
86

87
        public JavadocAction(AbstractItem project) {
K
kohsuke 已提交
88 89 90 91 92 93 94 95
            this.project = project;
        }

        public String getUrlName() {
            return "javadoc";
        }

        public String getDisplayName() {
K
kohsuke 已提交
96 97 98 99
            if(new File(getJavadocDir(project),"help-doc.html").exists())
                return "Javadoc";
            else
                return "Document";
K
kohsuke 已提交
100 101 102
        }

        public String getIconFileName() {
K
kohsuke 已提交
103 104 105 106 107
            if(getJavadocDir(project).exists())
                return "help.gif";
            else
                // hide it since we don't have javadoc yet.
                return null;
K
kohsuke 已提交
108 109
        }

K
kohsuke 已提交
110
        public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, InterruptedException {
111
            new DirectoryBrowserSupport(this).serveFile(req, rsp, new FilePath(getJavadocDir(project)), "help.gif", false);
K
kohsuke 已提交
112 113
        }
    }
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    public static class DescriptorImpl extends Descriptor<Publisher> {
        private DescriptorImpl() {
            super(JavadocArchiver.class);
        }

        public String getDisplayName() {
            return "Publish Javadoc";
        }

        public Publisher newInstance(StaplerRequest req) {
            return new JavadocArchiver(req.getParameter("javadoc_dir"));
        }

        /**
         * Performs on-the-fly validation on the file mask wildcard.
         */
        public void doCheck(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
            new FormFieldValidator.WorkspaceDirectory(req,rsp).process();
        }
    }
K
kohsuke 已提交
135
}