This is an automated email from the ASF dual-hosted git repository. danhaywood pushed a commit to branch ISIS-3078 in repository https://gitbox.apache.org/repos/asf/isis.git
commit ba13aec3b10fda56f841ad9daee80a455a444c02 Author: Dan Haywood <[email protected]> AuthorDate: Mon Jun 20 14:21:35 2022 +0100 ISIS-3078: adds script to perform bulk rename --- scripts/rename/.gitignore | 1 + scripts/rename/src/Main.groovy | 176 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/scripts/rename/.gitignore b/scripts/rename/.gitignore new file mode 100644 index 0000000000..c3af857904 --- /dev/null +++ b/scripts/rename/.gitignore @@ -0,0 +1 @@ +lib/ diff --git a/scripts/rename/src/Main.groovy b/scripts/rename/src/Main.groovy new file mode 100644 index 0000000000..e2a67c7ec5 --- /dev/null +++ b/scripts/rename/src/Main.groovy @@ -0,0 +1,176 @@ +/** + * This doesn't rewrite the contents of the following (binary) extensions: + * + * .jar + * .zip + * + * .pptx + * .docx + * .xlsx + * + * .odt + * .rtf + * .pdf + * + */ +class RenameAndRewrite { + + // 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 RenameAndRewrite() + + rr.renameAllFiles() + rr.rewriteAllFileContents() +} +
