You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2019/01/05 05:29:48 UTC

[tomee-site-generator] 01/02: TOMEE-2342: Generate Javadoc for each version

This is an automated email from the ASF dual-hosted git repository.

dblevins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee-site-generator.git

commit d8175832aa7792fe6415d89bb93951f4778edc28
Author: David Blevins <da...@gmail.com>
AuthorDate: Fri Jan 4 21:28:36 2019 -0800

    TOMEE-2342: Generate Javadoc for each version
---
 .../java/org/apache/tomee/website/Javadocs.java    | 97 +++++++++++++++++++++-
 .../java/org/apache/tomee/website/Sources.java     | 25 ++++++
 .../org/apache/tomee/website/VersionIndex.java     |  1 +
 .../org/apache/tomee/website/VersionsIndex.java    |  2 +
 4 files changed, 124 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/tomee/website/Javadocs.java b/src/main/java/org/apache/tomee/website/Javadocs.java
index 6320c42..da36eaf 100644
--- a/src/main/java/org/apache/tomee/website/Javadocs.java
+++ b/src/main/java/org/apache/tomee/website/Javadocs.java
@@ -16,6 +16,18 @@
  */
 package org.apache.tomee.website;
 
+import org.apache.openejb.loader.Files;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.util.Pipe;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+
+import static org.apache.openejb.loader.Files.mkdirs;
+
 /**
  * For each git repo (source) it will collects the `src/main/java` contents
  * into one large source tree and use the Javadoc proccessor directly in code
@@ -63,6 +75,89 @@ public class Javadocs {
      */
     public void prepare(final Source source) {
 
-        // do the magic
+        final File javaSources = mkdirs(new File(String.format("target/javadocs/%s-src", source.getName())));
+
+        try {
+            java.nio.file.Files.walk(source.getDir().toPath())
+                    .map(Path::toFile)
+                    .filter(File::isFile)
+                    .filter(this::isJava)
+                    .filter(this::srcMainJava)
+                    .filter(file -> !file.getAbsolutePath().contains("/tck/"))
+                    .filter(file -> !file.getAbsolutePath().contains("/itests/"))
+                    .filter(file -> !file.getAbsolutePath().contains("/examples/"))
+                    .filter(file -> !file.getAbsolutePath().contains("-example/"))
+                    .filter(file -> !file.getAbsolutePath().contains("/archetype-resources/"))
+                    .forEach(file -> {
+                        try {
+                            final String relativePath = file.getAbsolutePath().replaceAll(".*/src/main/java/", "");
+                            final File dest = new File(javaSources, relativePath);
+                            Files.mkdirs(dest.getParentFile());
+                            IO.copy(file, dest);
+                        } catch (IOException e) {
+                            throw new IllegalStateException(e);
+                        }
+                    });
+        } catch (IOException e) {
+            throw new IllegalStateException("Failed to aggregate java sources");
+        }
+
+        final ProcessBuilder cmd = new ProcessBuilder(
+                getJavadocCommand().getAbsolutePath(),
+                "-sourcepath",
+                javaSources.getAbsolutePath(),
+                "-d",
+                sources.getGeneratedDestFor(source, "javadoc").getAbsolutePath()
+        );
+
+        Stream.of(javaSources.listFiles())
+                .filter(File::isDirectory)
+                .forEach(file -> {
+                    cmd.command().add("-subpackages");
+                    cmd.command().add(file.getName());
+                });
+
+        try {
+            Pipe.pipe(cmd.start());
+        } catch (IOException e) {
+            throw new IllegalStateException("Command failed");
+        }
+    }
+
+    public static File getJavadocCommand() {
+
+        final File java_home = System.getenv("JAVA_HOME") != null ? new File(System.getenv("JAVA_HOME")) : new File("");
+        final File javaHome = new File(System.getProperty("java.home"));
+
+        final Supplier<File>[] locations = new Supplier[]{
+                () -> new File(java_home, "bin/javadoc"),
+                () -> new File(java_home, "bin/javadoc.exe"),
+                () -> new File(java_home.getParentFile(), "bin/javadoc"),
+                () -> new File(java_home.getParentFile(), "bin/javadoc.exe"),
+                () -> new File(javaHome, "bin/javadoc"),
+                () -> new File(javaHome, "bin/javadoc.exe"),
+                () -> new File(javaHome.getParentFile(), "bin/javadoc"),
+                () -> new File(javaHome.getParentFile(), "bin/javadoc.exe"),
+        };
+
+        return Stream.of(locations)
+                .map(Supplier::get)
+                .filter(File::exists)
+                .filter(File::canExecute)
+                .findFirst().orElseThrow(() -> new IllegalStateException("Cannot find javadoc command"));
+    }
+
+    public static void main(String[] args) {
+    }
+
+    private static void copy(final File file, File javaSources) {
+    }
+
+    private boolean srcMainJava(final File file) {
+        return file.getAbsolutePath().contains("src/main/java/");
+    }
+
+    private boolean isJava(final File file) {
+        return file.getName().endsWith(".java");
     }
 }
diff --git a/src/main/java/org/apache/tomee/website/Sources.java b/src/main/java/org/apache/tomee/website/Sources.java
index 0b481bd..be892da 100644
--- a/src/main/java/org/apache/tomee/website/Sources.java
+++ b/src/main/java/org/apache/tomee/website/Sources.java
@@ -138,6 +138,18 @@ public class Sources {
 
         VersionsIndex.prepare(this);
     }
+    /**
+     * This is the heart of the code to merge several documentation
+     * sources into one tree.
+     */
+    public void post() {
+        final Javadocs javadocs = new Javadocs(this);
+
+        sources.stream()
+                .peek(javadocs::prepare)
+                .forEach(Sources::done);
+        ;
+    }
 
     public File getJbakeContentDestFor(final Source source, final String... parts) {
         final File content = new File(jbake, "content");
@@ -153,6 +165,19 @@ public class Sources {
         return dir;
     }
 
+    public File getGeneratedDestFor(final Source source, final String... parts) {
+        File dir = new File(generated, source.getName());
+
+        for (final String part : parts) {
+            dir = new File(dir, part);
+        }
+
+        if (!dir.exists()) {
+            if (!dir.mkdirs()) throw new RuntimeException("Could not make directory: " + dir.getAbsolutePath());
+        }
+        return dir;
+    }
+
     private static void done(final Source source) {
         System.out.println("Done " + source);
     }
diff --git a/src/main/java/org/apache/tomee/website/VersionIndex.java b/src/main/java/org/apache/tomee/website/VersionIndex.java
index a2d0116..0008428 100644
--- a/src/main/java/org/apache/tomee/website/VersionIndex.java
+++ b/src/main/java/org/apache/tomee/website/VersionIndex.java
@@ -50,6 +50,7 @@ public class VersionIndex {
             if (examples.exists() && examples.listFiles().length > 0) {
                 index.append(" - link:examples[Examples]\n");
             }
+            index.append(" - link:javadoc[Javadoc]\n");
 
             IO.copy(IO.read(index.toString()), new File(docs.getParentFile(), "index.adoc"));
 
diff --git a/src/main/java/org/apache/tomee/website/VersionsIndex.java b/src/main/java/org/apache/tomee/website/VersionsIndex.java
index eaf3b0a..a96e56e 100644
--- a/src/main/java/org/apache/tomee/website/VersionsIndex.java
+++ b/src/main/java/org/apache/tomee/website/VersionsIndex.java
@@ -56,6 +56,8 @@ public class VersionsIndex {
                 if (examples.exists() && examples.listFiles().length > 0) {
                     index.append(" - link:").append(source.getName()).append("/examples[Examples]\n");
                 }
+                index.append(" - link:").append(source.getName()).append("/javadoc[Javadoc]\n");
+
                 index.append("\n\n");
             }