You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/12/11 12:07:21 UTC

[isis] branch master updated: ISIS-2473: automated example re-writing

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

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 6cd7427  ISIS-2473: automated example re-writing
6cd7427 is described below

commit 6cd742758d9a3f1a3b9b49b8ebdb8d3bd389baf4
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Dec 11 13:07:05 2020 +0100

    ISIS-2473: automated example re-writing
---
 .../j2adoc/test/ExampleReferenceFinder.java        |  90 ++++++++++++++++++
 .../isis/tooling/j2adoc/test/J2AdocTest.java       | 102 +++++++++++++++++----
 .../isis/tooling/j2adoc/test/ProjectSampler.java   |  14 +++
 3 files changed, 188 insertions(+), 18 deletions(-)

diff --git a/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/ExampleReferenceFinder.java b/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/ExampleReferenceFinder.java
new file mode 100644
index 0000000..1ee5348
--- /dev/null
+++ b/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/ExampleReferenceFinder.java
@@ -0,0 +1,90 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.tooling.j2adoc.test;
+
+import java.util.List;
+
+import org.apache.isis.commons.internal.collections._Lists;
+
+import lombok.ToString;
+import lombok.val;
+
+class ExampleReferenceFinder {
+    
+    @ToString(doNotUseGetters = true)
+    static class ExampleReference {
+        int exampleRef = -1;
+        int chapterStart = -1;
+        int chapterEnd = -1;
+        String name;
+        String shortName;
+    }
+    
+    static List<ExampleReference> find(Iterable<String> lines) {
+        val eRefs = _Lists.<ExampleReference>newArrayList();
+        
+        ExampleReference acc = new ExampleReference();
+        
+        int i = 0;
+        
+        for(val line : lines) {
+            if(line.contains("refguide:applib-svc:example$services/")) {
+                acc.exampleRef = i;
+                
+                val shortRef = line.substring(line.lastIndexOf("/")+1);
+                val name = shortRef.substring(0, shortRef.lastIndexOf(".java"));
+                acc.name = name;
+                
+                if(name.contains(".")) {
+                    acc.shortName = name.substring(name.lastIndexOf(".")+1);
+                } else {
+                    acc.shortName = name;
+                }
+                
+            } else if(line.startsWith("= ")
+                    || line.startsWith("== ")
+                    || line.startsWith("=== ")
+                    || line.startsWith("==== ")
+                    || line.startsWith("===== ")
+                    ) {
+                
+                if(acc.exampleRef==-1) {
+                    acc.chapterStart = i;    
+                } else if(acc.chapterEnd==-1) {
+                    acc.chapterEnd = i;
+                    //commit
+                    eRefs.add(acc);
+                    acc = new ExampleReference();
+                    acc.chapterStart = i;
+                }
+            }
+            i++;
+        }
+        
+        if(acc.exampleRef!=-1
+                && acc.chapterEnd==-1) {
+            acc.chapterEnd = i-1;
+            //commit
+            eRefs.add(acc);
+        }
+        
+        return eRefs; 
+    }
+
+}
diff --git a/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/J2AdocTest.java b/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/J2AdocTest.java
index b475ad3..d2eeb8d 100644
--- a/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/J2AdocTest.java
+++ b/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/J2AdocTest.java
@@ -21,16 +21,16 @@ package org.apache.isis.tooling.j2adoc.test;
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
+import java.util.List;
 import java.util.function.Consumer;
 
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 import org.apache.isis.commons.collections.Can;
-import org.apache.isis.commons.internal.base._Files;
 import org.apache.isis.commons.internal.base._Text;
+import org.apache.isis.commons.internal.collections._Lists;
 import org.apache.isis.commons.internal.collections._Sets;
-import org.apache.isis.commons.internal.functions._Predicates;
 import org.apache.isis.tooling.j2adoc.J2AdocContext;
 import org.apache.isis.tooling.j2adoc.util.AsciiDocIncludeTagFilter;
 import org.apache.isis.tooling.javamodel.AnalyzerConfigFactory;
@@ -94,39 +94,105 @@ class J2AdocTest {
     @Test @Disabled
     void adocDocMining() throws IOException {
         
-        val adocFiles = 
-                _Files.searchFiles(
-                        ProjectSampler.apacheIsisRoot(), 
-                        _Predicates.alwaysTrue(), 
-                        file->file.getName().endsWith(".adoc"));
+        val adocFiles = ProjectSampler.adocFiles(ProjectSampler.apacheIsisRoot());
      
         val names = _Sets.<String>newTreeSet();
         
         Can.ofCollection(adocFiles)
         .stream()
+        .filter(source->source.toString().contains("XmlSnapshotService"))
         .forEach(file->parseAdoc(file, names::add));
         
         names.forEach(System.out::println);
     }
     
-    private void parseAdoc(final @NonNull File file, Consumer<String> onShortName) {
+    private void parseAdoc(final @NonNull File file, Consumer<String> onName) {
         val lines = _Text.readLinesFromFile(file, StandardCharsets.UTF_8);
         
-        lines.stream()
-        .filter(line->line.contains("refguide:applib-svc:example$services/"))
-        .forEach(line->{
-            //System.out.println("--- " + file);
-            
-            //val shortRef = line.substring(line.indexOf("/")+1);
-            val shortRef = line.substring(line.lastIndexOf("/")+1);
-            val shortName = shortRef.substring(0, shortRef.lastIndexOf(".java"));
-            
-            onShortName.accept(shortName);//+".java");
+        ExampleReferenceFinder.find(lines)
+        .forEach(exRef->{
+            onName.accept(exRef.name);
         });
+    }
+    
+    @Test
+    void adocExampleProcessing() throws IOException {
+        
+        val adocFiles = ProjectSampler.adocFiles(ProjectSampler.apacheIsisRoot());
      
+        Can.ofCollection(adocFiles)
+        .stream()
+        //.filter(source->source.toString().contains("FactoryService"))
+        .forEach(file->processAdocExampleTags(file));
+    }
+    
+    
+    private static void processAdocExampleTags(File source) {
+        
+        val lines = _Text.readLinesFromFile(source, StandardCharsets.UTF_8);
+        
+        val exampleRefs = ExampleReferenceFinder.find(lines);
+        if(exampleRefs.isEmpty()) {
+            return;
+        }
+        
+        System.out.println(exampleRefs);
         
+        val fixedLines = _Lists.<String>newArrayList();
         
+        val it = lines.iterator();
+        String line;
+        int i = 0;
+        
+        for(val exRef : exampleRefs) {
+            
+            // seek chapter start
+            while(i<exRef.chapterStart) {
+                line = it.next();
+                fixedLines.add(line);
+                ++i;
+            }
+            
+            appendHeader(exRef.name, fixedLines);
+            
+            // seek chapter end
+            while(i<exRef.chapterEnd) {
+                line = it.next();
+                fixedLines.add(line);
+                ++i;
+            }
+            
+            appendFooter(fixedLines);
+            
+        }
+        
+        // seek document end
+        while(it.hasNext()) {
+            fixedLines.add(it.next());
+        }
+        
+        _Text.writeLinesToFile(fixedLines, source, StandardCharsets.UTF_8);
+
     }
 
+    private static void appendHeader(String key, List<String> lines) {
+        lines.add("== API");
+        lines.add("");
+        lines.add(String.format("include::system:generated:page$index/%s.adoc[leveloffset=+2]", key));
+        lines.add("");
+        lines.add("TODO example migration");
+        lines.add("");
+        lines.add(".Deprecated Docs");
+        lines.add("[WARNING]");
+        lines.add("================================");
+        lines.add("");
+    }
+    
+    private static void appendFooter(List<String> lines) {
+        lines.add("");
+        lines.add("================================");
+        lines.add("");
+    }
+    
     
 }
diff --git a/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/ProjectSampler.java b/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/ProjectSampler.java
index db0f8b8..19ecbad 100644
--- a/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/ProjectSampler.java
+++ b/tooling/java2adoc/src/test/java/org/apache/isis/tooling/j2adoc/test/ProjectSampler.java
@@ -19,6 +19,12 @@
 package org.apache.isis.tooling.j2adoc.test;
 
 import java.io.File;
+import java.util.Set;
+
+import org.apache.isis.commons.internal.base._Files;
+import org.apache.isis.commons.internal.functions._Predicates;
+
+import lombok.SneakyThrows;
 
 final class ProjectSampler {
 
@@ -33,5 +39,13 @@ final class ProjectSampler {
     static File apacheIsisApplib() {
         return new File(apacheIsisRoot(), "api/applib");
     }
+
+    @SneakyThrows
+    public static Set<File> adocFiles(File folder) {
+        return _Files.searchFiles(
+                    ProjectSampler.apacheIsisRoot(), 
+                        _Predicates.alwaysTrue(), 
+                        file->file.getName().endsWith(".adoc"));
+    }
     
 }