Skip to content

Commit

Permalink
Rename existing target directory when its name case does not match th…
Browse files Browse the repository at this point in the history
…e specified target name. Fixes: #303
  • Loading branch information
pway99 committed Feb 23, 2022
1 parent e8c9816 commit 2893424
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/java/org/openrewrite/maven/RewriteRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,24 @@ public void execute() throws MojoExecutionException {
// Should we try to use git to move the file first, and only if that fails fall back to this?
assert result.getBefore() != null;
Path originalLocation = results.getProjectRoot().resolve(result.getBefore().getSourcePath());
File originalParentDir = originalLocation.toFile().getParentFile();
boolean deleteSucceeded = originalLocation.toFile().delete();
if (!deleteSucceeded) {
throw new IOException("Unable to delete file " + originalLocation.toAbsolutePath());
}
assert result.getAfter() != null;
// Ensure directories exist in case something was moved into a hitherto non-existent package
Path afterLocation = results.getProjectRoot().resolve(result.getAfter().getSourcePath());
File parentDir = afterLocation.toFile().getParentFile();
//noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
File afterParentDir = afterLocation.toFile().getParentFile();
// Rename the directory if its name case has been changed, e.g. camel case to lower case.
if (afterParentDir.exists() && afterParentDir.getAbsolutePath().equalsIgnoreCase((originalParentDir.getAbsolutePath()))
&& !afterParentDir.getAbsolutePath().equals(originalParentDir.getAbsolutePath())) {
if (!originalParentDir.renameTo(afterParentDir)) {
throw new RuntimeException("Unable to rename directory from " + originalParentDir.getAbsolutePath() + " To: " + afterParentDir.getAbsolutePath());
}
} else if (!afterParentDir.exists() && !afterParentDir.mkdirs()) {
throw new RuntimeException("Unable to create directory " + afterParentDir.getAbsolutePath());
}
try (BufferedWriter sourceFileWriter = Files.newBufferedWriter(afterLocation)) {
sourceFileWriter.write(result.getAfter().printAll());
}
Expand Down

0 comments on commit 2893424

Please sign in to comment.