Created
April 14, 2018 16:27
-
-
Save jmini/ea039fea055fd32f0e87cb0ac21e1970 to your computer and use it in GitHub Desktop.
Simple tool to do java renamings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package rename; | |
| import java.nio.file.Path; | |
| import java.util.List; | |
| import java.util.Map; | |
| public class Input { | |
| final Path root; | |
| final Map<String, String> projectNames; | |
| final String oldRootPackageName; | |
| final String newRootPackageName; | |
| final List<String> fileExtensions; | |
| public Input(Path root, Map<String, String> projectNames, String oldRootPackageName, String newRootPackageName, List<String> fileExtensions) { | |
| super(); | |
| this.root = root; | |
| this.projectNames = projectNames; | |
| this.oldRootPackageName = oldRootPackageName; | |
| this.newRootPackageName = newRootPackageName; | |
| this.fileExtensions = fileExtensions; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package rename; | |
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| import java.util.List; | |
| import java.util.Map.Entry; | |
| public class Rename { | |
| public static void rename(Input input) throws IOException { | |
| moveFolder(input); | |
| moveSourceFilesToMatchNewPackageName(input); | |
| replacePackagesInFiles(input); | |
| System.out.println("DONE"); | |
| } | |
| private static void moveFolder(Input input) throws IOException { | |
| for (Entry<String, String> e : input.projectNames.entrySet()) { | |
| String oldProjectName = e.getKey(); | |
| String newProjectName = e.getValue(); | |
| Path oldProjectFolder = input.root.resolve(oldProjectName); | |
| Path newProjectFolder = input.root.resolve(newProjectName); | |
| moveFolderWithChecks(oldProjectFolder, newProjectFolder); | |
| } | |
| } | |
| private static void moveFolderWithChecks(Path oldFolder, Path newFolder) throws IOException { | |
| if (!oldFolder.equals(newFolder)) { | |
| if (!Files.isDirectory(oldFolder)) { | |
| throw new IllegalStateException("Expecting a directory: " + oldFolder.toString()); | |
| } | |
| if (Files.exists(newFolder)) { | |
| throw new IllegalStateException("This directory should not exist: " + newFolder.toString()); | |
| } | |
| Files.createDirectories(newFolder.getParent()); | |
| Files.move(oldFolder, newFolder); | |
| System.out.println("'" + oldFolder + "' moved to '" + newFolder + "'"); | |
| } | |
| } | |
| private static void moveFileWithChecks(Path oldFolder, Path newFolder) { | |
| if (!oldFolder.equals(newFolder)) { | |
| if (!Files.isRegularFile(oldFolder)) { | |
| throw new IllegalStateException("Expecting a file: " + oldFolder.toString()); | |
| } | |
| if (Files.exists(newFolder)) { | |
| throw new IllegalStateException("This directory should not exist: " + newFolder.toString()); | |
| } | |
| try { | |
| Files.move(oldFolder, newFolder); | |
| System.out.println("'" + oldFolder + "' moved to '" + newFolder + "'"); | |
| } | |
| catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| private static void moveServicesFiles(Path folder, String oldPrefix, String newPrefix) throws IOException { | |
| if (Files.isDirectory(folder)) { | |
| Files.walk(folder) // | |
| .filter(f -> f.toFile().isFile() && f.toFile().getName().startsWith(oldPrefix)) // | |
| .forEach(f -> moveFileWithChecks(f, f.getParent().resolve(f.toFile().getName().replace(oldPrefix, newPrefix)))); | |
| } | |
| } | |
| private static void moveSourceFilesToMatchNewPackageName(Input input) throws IOException { | |
| for (String newProjectName : input.projectNames.values()) { | |
| Path newProjectFolder = input.root.resolve(newProjectName); | |
| Path srcFolder = newProjectFolder.resolve("src").resolve("main").resolve("java"); | |
| moveSourceFolderToMatchNewPackageName(srcFolder, input.oldRootPackageName, input.newRootPackageName); | |
| Path testFolder = newProjectFolder.resolve("src").resolve("test").resolve("java"); | |
| moveSourceFolderToMatchNewPackageName(testFolder, input.oldRootPackageName, input.newRootPackageName); | |
| Path servicesFolder = newProjectFolder.resolve("src").resolve("main").resolve("resources").resolve("META-INF").resolve("services"); | |
| moveServicesFiles(servicesFolder, input.oldRootPackageName, input.newRootPackageName); | |
| } | |
| } | |
| private static void moveSourceFolderToMatchNewPackageName(Path srcFolder, String oldRootPackageName, String newRootPackageName) throws IOException { | |
| if (Files.isDirectory(srcFolder)) { | |
| Path oldFolder = findFolderForPackageName(srcFolder, oldRootPackageName); | |
| Path newFolder = findFolderForPackageName(srcFolder, newRootPackageName); | |
| if (Files.isDirectory(oldFolder)) { | |
| moveFolderWithChecks(oldFolder, newFolder); | |
| } | |
| } | |
| } | |
| private static Path findFolderForPackageName(Path srcFolder, String packageName) { | |
| Path result = srcFolder; | |
| for (String subFolder : packageName.split("\\.")) { | |
| result = result.resolve(subFolder); | |
| } | |
| return result; | |
| } | |
| private static void replacePackagesInFiles(Input input) throws IOException { | |
| for (String newProjectName : input.projectNames.values()) { | |
| Path newProjectFolder = input.root.resolve(newProjectName); | |
| Files.walk(newProjectFolder) // | |
| .filter(f -> f.toFile().isFile() && includeFile(f, input.fileExtensions)) // | |
| .forEach(f -> replaceInFile(f, input.oldRootPackageName, input.newRootPackageName)); | |
| } | |
| } | |
| private static boolean includeFile(Path f, List<String> endWith) { | |
| String fileName = f.toFile().getName(); | |
| for (String s : endWith) { | |
| if (fileName.endsWith(s)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| private static void replaceInFile(final Path file, String oldString, String newString) { | |
| try { | |
| String content = new String(Files.readAllBytes(file)); | |
| content = content.replace(oldString, newString); | |
| Files.write(file, content.getBytes()); | |
| System.out.println("Replace in file '" + file + "'"); | |
| } | |
| catch (final IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment