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 2022/10/22 11:42:33 UTC

[isis] branch master updated: ISIS-3142: replace Rename.groovy script with more subtle jshell script

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 aa7419eca6 ISIS-3142: replace Rename.groovy script with more subtle jshell script
aa7419eca6 is described below

commit aa7419eca68b42dadf8e6e0e5837ec9c13cec929
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sat Oct 22 13:42:28 2022 +0200

    ISIS-3142: replace Rename.groovy script with more subtle jshell script
    
    - more fine grained control
---
 scripts/ci/rename-all-published-sources.jsh | 333 ++++++++++++++++++++++++++++
 scripts/rename/.gitignore                   |   1 -
 scripts/rename/src/Rename.groovy            | 204 -----------------
 3 files changed, 333 insertions(+), 205 deletions(-)

diff --git a/scripts/ci/rename-all-published-sources.jsh b/scripts/ci/rename-all-published-sources.jsh
new file mode 100644
index 0000000000..1cc243ea2f
--- /dev/null
+++ b/scripts/ci/rename-all-published-sources.jsh
@@ -0,0 +1,333 @@
+/*
+ *  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.
+ */
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.FileVisitOption;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+/**
+ * Renames project from oldName to newName.
+ *
+ * <p>
+ * This doesn't rewrite the contents of the following (binary) extensions:
+ * <ul>
+ *     <li>.jar</li>
+ *     <li>.zip</li>
+ *     <li>.pptx</li>
+ *     <li>.docx</li>
+ *     <li>.xlsx</li>
+ *     <li>.odt</li>
+ *     <li>.rtf</li>
+ *     <li>.pdf</li>
+ * </ul>
+ */
+class RenameProject {
+
+    static final boolean DRY_RUN = false;
+
+    // to obtain all the suffixes:
+    // find . -type f | sed -rn 's|.*/[^/]+\.([^/.]+)$|\1|p' | sort -u
+    static final List<String> EXTENSIONS = List.of(
+            "NOTICE",
+            "STATUS",
+            "MF",
+            "TXT",
+            "adoc",
+            "bat",
+            "cfg",
+            "css",
+            "dcl",
+            "dtd",
+            "factories",
+            "feature",
+            "fxml",
+            "gql",
+            "graphqls",
+            "hbs",
+            "html",
+            "importorder",
+            "info",
+            "ini",
+            "java",
+            "jdo",
+            "js",
+            "json",
+            "kt",
+            "kts",
+            "ldif",
+            "list",
+            "md",
+            "orm",
+            "po",
+            "pot",
+            "properties",
+            "puml",
+            "rdf",
+            "sh",
+            "soc",
+            "svg",
+            "template",
+            "thtml",
+            "ts",
+            "txt",
+            "xml",
+            "xsd",
+            "yaml",
+            "yml"
+            );
+
+    static final List<String> PATH_EXLUSIONS = List.of(
+            "/build/",
+            "/target/",
+            "/antora/", "/adoc/", // not published in its legacy form
+            "/scripts/ci/", // don't touch
+            "/."
+            );
+
+    static final List<String> UNCONDITIONAL_INCLUSIONS = List.of(
+            "/META-INF/services/");
+
+    public RenameProject(final File root, final String oldName, final String newName) {
+        this.root = root;
+        this.fromLower = oldName.toLowerCase();
+        this.toLower = newName.toLowerCase();
+        this.fromUpper = oldName.toUpperCase();
+        this.toUpper = newName.toUpperCase();
+        this.fromTitle = capitalize(fromLower);
+        this.toTitle = capitalize(toLower);
+    }
+
+    final File root;
+
+    final String fromLower;
+    final String toLower;
+    final String fromUpper;
+    final String toUpper;
+    final String fromTitle;
+    final String toTitle;
+
+    static String capitalize(final String s) { return s.substring(0, 1).toUpperCase() + s.substring(1); }
+
+    public void renameAllFiles() throws IOException {
+        Files.find(root.toPath(), Integer.MAX_VALUE, (path, attr)->{
+            if(isPathExcluded(path.toFile())) {
+                return false;
+            }
+            return !attr.isDirectory();
+        }, FileVisitOption.FOLLOW_LINKS)
+        .map(Path::toFile)
+        //.filter(file->fileNameEndsWithSupportedExtension(file)) // rename files unconditionally
+        .forEach(file->{
+            renameIfRequired(file);
+        });
+    }
+
+    private void renameIfRequired(final File file) {
+        var relativeFilePathRenamed = pathRelativeToRoot(file)
+                .replace(fromTitle, toTitle)
+                .replace(fromUpper, toUpper)
+                .replace("\\" + fromLower, "\\" + toLower)
+                .replace("/" + fromLower, "/" + toLower)
+                .replace("-" + fromLower, "-" + toLower)
+                .replace("_" + fromLower, "_" + toLower)
+                .replace("." + fromLower, "." + toLower);
+
+        var filePathRenamed = pathOf(root) + relativeFilePathRenamed;
+
+        if (!filePathRenamed.equals(pathOf(file))) {
+
+            System.err.printf("rename %s -> %s%n", pathRelativeToRoot(file), relativeFilePathRenamed);
+            //System.err.printf("rename %s -> %s%n", pathOf(file), filePathRenamed);
+
+            if(!DRY_RUN) {
+                var fileRename = new File(filePathRenamed);
+                var parentFile = fileRename.getParentFile();
+                parentFile.mkdirs();
+                try {
+                    Files.move(file.toPath(), fileRename.toPath());
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    public void rewriteAllFileContents() throws IOException {
+
+        Files.find(root.toPath(), Integer.MAX_VALUE, (path, attr)->{
+            return !attr.isDirectory();
+        }, FileVisitOption.FOLLOW_LINKS)
+        .map(Path::toFile)
+        .filter(File::exists)
+        .forEach(file->{
+            if(isPathExcluded(file)) {
+                return;
+            }
+            if(isPathUnconditionallyIncluded(file)
+                    || fileNameEndsWithSupportedExtension(file)) {
+                try {
+                    rewriteIfRequired(file);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+    }
+
+    private void rewriteIfRequired(final File file) throws FileNotFoundException, IOException {
+        var lines = readLinesFromFile(file, StandardCharsets.UTF_8);
+        var newLines = new ArrayList<String>(lines.size());
+
+        final int linesChangedCount =
+            lines.stream().mapToInt(line->{
+                var newLine = line
+                        .replace(fromLower, toLower)
+                        .replace(fromUpper, toUpper)
+                        .replace(fromTitle, toTitle);
+                newLines.add(newLine);
+                return line.equals(newLine)
+                        ? 0
+                        : 1;
+            })
+            .sum();
+
+        if (linesChangedCount>0) {
+            System.err.printf("rewriting %s%n", pathRelativeToRoot(file));
+            if(!DRY_RUN) {
+                writeLinesToFile(newLines, file, StandardCharsets.UTF_8);
+            }
+        }
+    }
+
+    private static boolean fileNameEndsWithSupportedExtension(final File file) {
+        for (String ext : EXTENSIONS) {
+            if (file.getName().endsWith("." + ext)
+                    || file.getName().equals(ext)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static boolean isPathUnconditionallyIncluded(final File file) {
+        var path = pathOf(file);
+        for (String incl : UNCONDITIONAL_INCLUSIONS) {
+            if (path.contains(incl)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static boolean isPathExcluded(final File file) {
+        var path = pathOf(file);
+        for (String excl : PATH_EXLUSIONS) {
+            if (path.contains(excl)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private String pathRelativeToRoot(final File file) {
+        var prefix = pathOf(root);
+        var path = pathOf(file);
+        if(!path.startsWith(prefix)) {
+            throw new IllegalArgumentException("file not subpath of root");
+        }
+        return path.substring(prefix.length());
+    }
+
+    private static String pathOf(final File file) {
+        return file.getAbsolutePath().replace('\\', '/');
+    }
+
+    // -- READING
+
+    private static List<String> readLines(
+            final InputStream input,
+            final Charset charset){
+        if(input==null) {
+            return List.of();
+        }
+        var lines = new ArrayList<String>();
+        try(Scanner scanner = new Scanner(input, charset.name())){
+            scanner.useDelimiter("\\n");
+            while(scanner.hasNext()) {
+                var line = scanner.next();
+                //line = line.replace("\r", ""); // preserve windows specific line terminal
+                lines.add(line);
+            }
+        }
+        return lines;
+    }
+
+    private static List<String> readLinesFromFile(
+            final File file,
+            final Charset charset) throws FileNotFoundException, IOException {
+        try(var input = new FileInputStream(file)){
+            return readLines(input, charset);
+        }
+    }
+
+    // -- WRITING
+
+    static void writeLinesToFile(
+            final Iterable<String> lines,
+            final File file,
+            final Charset charset) throws FileNotFoundException, IOException {
+
+        try(var bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))) {
+            for(var line : lines) {
+                bw.append(line).append("\n");
+            }
+        }
+    }
+
+}
+
+var rootPath = "" + System.getenv("PROJECT_LEGACY_ROOT_PATH");
+if(rootPath.isBlank() 
+        || ! new File(rootPath).exists()) {
+    throw new IllegalArgumentException("env PROJECT_LEGACY_ROOT_PATH must point to an existing directory");
+}
+
+var root = new File(rootPath);
+
+var renamer = new RenameProject(root, "causeway", "isis");
+System.out.printf("processing root %s%n", root.getAbsolutePath());
+
+renamer.renameAllFiles();
+renamer.rewriteAllFileContents();
+
+System.out.println("done.");
+
+/exit
diff --git a/scripts/rename/.gitignore b/scripts/rename/.gitignore
deleted file mode 100644
index c3af857904..0000000000
--- a/scripts/rename/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-lib/
diff --git a/scripts/rename/src/Rename.groovy b/scripts/rename/src/Rename.groovy
deleted file mode 100644
index 2dcc4b1259..0000000000
--- a/scripts/rename/src/Rename.groovy
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- *  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.
- *
- */
-
-/**
- * Renames project from 'isis' to '...'
- *
- * usage:
- * <pre>
- *    pushd scripts/rename/src
- *    groovy Rename.groovy
- *    popd
- * </pre>
- *
- * <p>
- * This doesn't rewrite the contents of the following (binary) extensions:
- *
- * <ul>
- *     <li>.jar
- *     <li> .zip
- *     <li> .pptx
- *     <li> .docx
- *     <li> .xlsx
- *     <li> .odt
- *     <li> .rtf
- *     <li> .pdf
- * </ul>
- */
-class Rename {
-
-  // root of the repo
-  static final ROOT_DIR = "../../.."
-  //static final ROOT_DIR = "../../.." + "/persistence/jdo/metamodel/src/main/resources/META-INF/services"
-
-  static final FROM_LOWERCASE = "isis"
-  static final TO_LOWERCASE = "causeway"
-
-  static final FROM_TITLECASE = FROM_LOWERCASE.capitalize()
-  static final TO_TITLECASE = TO_LOWERCASE.capitalize()
-
-  static final FROM_UPPERCASE = FROM_LOWERCASE.toUpperCase()
-  static final TO_UPPERCASE = TO_LOWERCASE.toUpperCase()
-
-
-  // to obtain all the suffixes:
-  // find . -type f | sed -rn 's|.*/[^/]+\.([^/.]+)$|\1|p' | sort -u
-  static final EXTENSIONS = [
-          "NOTICE",
-          "STATUS",
-          "ConstraintValidator",
-          "CausewayBeanTypeClassifier",
-          "MF",
-          "TXT",
-          "UriBuilderPlugin",
-          "adoc",
-          "bat",
-          "cfg",
-          "css",
-          "dcl",
-          "dtd",
-          "factories",
-          "feature",
-          "fxml",
-          "gql",
-          "graphqls",
-          "hbs",
-          "html",
-          "importorder",
-          "info",
-          "ini",
-          "java",
-          "jdo",
-          "js",
-          "json",
-          "kt",
-          "kts",
-          "ldif",
-          "list",
-          "md",
-          "po",
-          "pot",
-          "properties",
-          "puml",
-          "rdf",
-          "sh",
-          "soc",
-          "svg",
-          "template",
-          "thtml",
-          "ts",
-          "txt",
-          "xml",
-          "xsd",
-          "yaml",
-          "yml"
-  ]
-
-  def renameAllFiles() {
-
-    def currentDir = new File(ROOT_DIR)
-
-    currentDir.eachFileRecurse { File file ->
-      if (file.isDirectory()) {
-        return
-      }
-      renameIfRequired(file)
-    }
-  }
-
-  def renameIfRequired(File file) {
-    def filePathRename = file.path
-            .replaceAll(FROM_TITLECASE, TO_TITLECASE)
-            .replaceAll(FROM_UPPERCASE, TO_UPPERCASE)
-            .replaceAll("\\\\" + FROM_LOWERCASE, "\\\\" + TO_LOWERCASE)
-            .replaceAll("/" + FROM_LOWERCASE, "/" + TO_LOWERCASE)
-            .replaceAll("-" + FROM_LOWERCASE, "-" + TO_LOWERCASE)
-            .replaceAll("_" + FROM_LOWERCASE, "_" + TO_LOWERCASE)
-            .replaceAll("[.]" + FROM_LOWERCASE, "." + TO_LOWERCASE)
-
-    if (!filePathRename.equals(file.path)) {
-
-      println "${file.path} -> ${filePathRename}"
-
-      def fileRename = new File(filePathRename)
-      def parentFile = fileRename.parentFile
-      parentFile.mkdirs()
-
-      file.renameTo(fileRename)
-    }
-  }
-
-  def rewriteAllFileContents() {
-
-    def currentDir = new File(ROOT_DIR)
-
-    currentDir.eachFileRecurse { file ->
-      // println "${file.path}"
-      if (!file.exists()) {
-        return
-      }
-      if (file.isDirectory()) {
-        return
-      }
-      if (file.path.contains(".git\\" )) {
-        return
-      }
-
-      if(fileNameEndsWithSupportedExtension(file)) {
-        rewriteIfRequired(file)
-      }
-    }
-  }
-
-  private void rewriteIfRequired(File file) {
-    def fileText = file.text
-    def updatedFileText = fileText
-            .replaceAll(FROM_LOWERCASE, TO_LOWERCASE)
-            .replaceAll(FROM_UPPERCASE, TO_UPPERCASE)
-            .replaceAll(FROM_TITLECASE, TO_TITLECASE)
-
-    if (!fileText.equals(updatedFileText)) {
-
-      println "rewriting ${file.path}"
-
-      file.write(updatedFileText)
-    }
-  }
-
-
-  private boolean fileNameEndsWithSupportedExtension(File file) {
-    for (ext in EXTENSIONS) {
-      if (file.name.endsWith(ext)) {
-        return true
-      }
-    }
-    return false
-  }
-
-}
-
-static void main(String[] args) {
-  def rr = new Rename()
-
-  rr.renameAllFiles()
-  rr.rewriteAllFileContents()
-}
-
-