Skip to content

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 9 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/MoveFile.java
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 rewrite-core/src/test/java/org/openrewrite/MoveFileTest.java
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")
)
);
}
}
Loading