-
Notifications
You must be signed in to change notification settings - Fork 434
Move file to another directory #5373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d95c33
Move file to another directory (resulting in renaming directories if …
Jenson3210 01db6d1
Reformat
Jenson3210 e122f58
Added license header
Jenson3210 1482660
Update rewrite-core/src/test/java/org/openrewrite/MoveFileTest.java
Jenson3210 f3ec044
Move files with folder support (no globbing in folder possible (yet?))
Jenson3210 8cc461e
Added windows/unix path handling / tests
Jenson3210 83b5ffa
Update MoveFile.java
Jenson3210 412e939
Added windows support for moveTo and folder
Jenson3210 60fa2b6
Adding some tests for project root guarding
Jenson3210 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.openrewrite; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.PathMatcher; | ||
import java.nio.file.Paths; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class MoveFile extends Recipe { | ||
@Option(displayName = "File matcher", | ||
description = "Matching files will be renamed. This is a glob expression.", | ||
example = "**/*.yml") | ||
String fileMatcher; | ||
|
||
@Option(displayName = "Move to", | ||
description = "Either a relative or absolute path. If relative, it is relative to the current file's directory.", | ||
example = "../yamls/") | ||
String moveTo; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Move a file"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Move a file to a different directory. The file name will remain the same."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new TreeVisitor<Tree, ExecutionContext>() { | ||
|
||
@Override | ||
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
if (tree instanceof SourceFile) { | ||
SourceFile sourceFile = (SourceFile) tree; | ||
Path sourcePath = sourceFile.getSourcePath(); | ||
Path currentFolder = sourcePath.getParent(); | ||
String fileName = sourcePath.getFileName().toString(); | ||
PathMatcher pathMatcher = sourcePath.getFileSystem().getPathMatcher("glob:" + fileMatcher); | ||
if (pathMatcher.matches(sourcePath)) { | ||
Path moveToSourcePath = null; | ||
String moveToPath = moveTo; | ||
if (moveTo.startsWith("/")) { | ||
moveToSourcePath = Paths.get(""); | ||
moveToPath = moveTo.substring(1); | ||
} else if (moveTo.startsWith("../")) { | ||
moveToSourcePath = currentFolder; | ||
while (moveToPath.startsWith("../")) { | ||
moveToSourcePath = moveToSourcePath.getParent(); | ||
if (moveToSourcePath == null) { | ||
break; | ||
} | ||
moveToPath = moveToPath.substring(3); | ||
} | ||
} else if (!currentFolder.endsWith(moveTo)) { | ||
moveToSourcePath = currentFolder; | ||
} | ||
if (moveToSourcePath != null) { | ||
moveToSourcePath = moveToSourcePath.resolve(moveToPath).resolve(fileName); | ||
if (!moveToSourcePath.equals(sourcePath)) { | ||
return ((SourceFile) tree).withSourcePath(moveToSourcePath.normalize()); | ||
} | ||
} | ||
} | ||
} | ||
return super.visit(tree, ctx); | ||
} | ||
}; | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
rewrite-core/src/test/java/org/openrewrite/MoveFileTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.openrewrite; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import java.nio.file.Paths; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.openrewrite.test.SourceSpecs.text; | ||
|
||
class MoveFileTest implements RewriteTest { | ||
|
||
|
||
@Test | ||
@DocumentExample | ||
void renameDirectory() { | ||
rewriteRun( | ||
spec -> spec.recipe(new MoveFile("**/application*.yml", "../resources")), | ||
text( | ||
"hello: world", | ||
"hello: world", | ||
spec -> | ||
spec | ||
.path("src/main/renameMe/application.yml") | ||
.afterRecipe(pt -> assertThat(pt.getSourcePath()).isEqualTo(Paths.get("src/main/resources/application.yml"))) | ||
), | ||
text( | ||
"hello: world", | ||
"hello: world", | ||
spec -> | ||
spec | ||
.path("src/main/renameMe/application-dev.yml") | ||
.afterRecipe(pt -> assertThat(pt.getSourcePath()).isEqualTo(Paths.get("src/main/resources/application-dev.yml"))) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void moveToSubDirectory() { | ||
rewriteRun( | ||
spec -> spec.recipe(new MoveFile("**/application*.yml", "profiles")), | ||
text( | ||
"hello: world", | ||
"hello: world", | ||
spec -> | ||
spec | ||
.path("src/main/resources/application.yml") | ||
.afterRecipe(pt -> assertThat(pt.getSourcePath()).isEqualTo(Paths.get("src/main/resources/profiles/application.yml"))) | ||
), | ||
text( | ||
"hello: world", | ||
"hello: world", | ||
spec -> | ||
spec | ||
.path("src/main/resources/application-dev.yml") | ||
.afterRecipe(pt -> assertThat(pt.getSourcePath()).isEqualTo(Paths.get("src/main/resources/profiles/application-dev.yml"))) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void moveToExactPath() { | ||
rewriteRun( | ||
spec -> spec.recipe(new MoveFile("**/application*.yml", "/profiles")), | ||
text( | ||
"hello: world", | ||
"hello: world", | ||
spec -> | ||
spec | ||
.path("src/main/resources/application.yml") | ||
.afterRecipe(pt -> assertThat(pt.getSourcePath()).isEqualTo(Paths.get("profiles/application.yml"))) | ||
), | ||
text( | ||
"hello: world", | ||
"hello: world", | ||
spec -> | ||
spec | ||
.path("src/main/resources/application-dev.yml") | ||
.afterRecipe(pt -> assertThat(pt.getSourcePath()).isEqualTo(Paths.get("profiles/application-dev.yml"))) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void ignoreNonMatchingFiles() { | ||
rewriteRun( | ||
spec -> spec.recipe(new MoveFile("**/renameMe/application*.yml", "../resources")), | ||
text( | ||
"hello: world", | ||
spec -> spec.path("src/main/renameMe/application.yaml") | ||
), | ||
text( | ||
"hello: world", | ||
spec -> spec.path("src/main/doNotRenameMe/application.yml") | ||
) | ||
); | ||
} | ||
} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.