Skip to content

Commit f791234

Browse files
therepanicfmbenhassine
authored andcommitted
Remove commons-io dependency from PathSearch
In this commit, we replace the use of commons-io in PathSearch with api from jdk. Closes: gh-1215 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
1 parent 97121ac commit f791234

File tree

4 files changed

+61
-34
lines changed

4 files changed

+61
-34
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<spring-boot.version>4.0.0-SNAPSHOT</spring-boot.version>
5353
<jline.version>3.30.6</jline.version>
5454
<antlr-st4.version>4.3.4</antlr-st4.version>
55-
<commons-io.version>2.20.0</commons-io.version>
5655
<jakarta.validation-api.version>3.1.1</jakarta.validation-api.version>
5756
<jspecify.version>1.0.0</jspecify.version>
5857

spring-shell-core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@
5656
<artifactId>reactor-core</artifactId>
5757
<version>${reactor.version}</version>
5858
</dependency>
59-
<dependency>
60-
<groupId>commons-io</groupId>
61-
<artifactId>commons-io</artifactId>
62-
<version>${commons-io.version}</version>
63-
</dependency>
6459
<dependency>
6560
<groupId>jakarta.validation</groupId>
6661
<artifactId>jakarta.validation-api</artifactId>

spring-shell-core/src/main/java/org/springframework/shell/core/tui/component/PathSearch.java

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package org.springframework.shell.core.tui.component;
1717

1818
import java.io.IOException;
19+
import java.nio.file.FileSystems;
1920
import java.nio.file.FileVisitResult;
2021
import java.nio.file.Files;
2122
import java.nio.file.Path;
2223
import java.nio.file.Paths;
24+
import java.nio.file.PathMatcher;
25+
import java.nio.file.SimpleFileVisitor;
2326
import java.nio.file.attribute.BasicFileAttributes;
2427
import java.util.ArrayList;
2528
import java.util.Collections;
@@ -34,11 +37,6 @@
3437
import java.util.stream.Collectors;
3538
import java.util.stream.Stream;
3639

37-
import org.apache.commons.io.file.AccumulatorPathVisitor;
38-
import org.apache.commons.io.file.Counters;
39-
import org.apache.commons.io.filefilter.IOFileFilter;
40-
import org.apache.commons.io.filefilter.NotFileFilter;
41-
import org.apache.commons.io.filefilter.WildcardFileFilter;
4240
import org.jline.keymap.BindingReader;
4341
import org.jline.keymap.KeyMap;
4442
import org.jline.terminal.Terminal;
@@ -73,6 +71,7 @@
7371
*
7472
* @author Janne Valkealahti
7573
* @author Piotr Olaszewski
74+
* @author Andrey Litvitski
7675
*/
7776
public class PathSearch extends AbstractTextComponent<Path, PathSearchContext> {
7877

@@ -614,16 +613,15 @@ public PathScannerResult apply(String input, PathSearchContext context) {
614613
Path path = Path.of(p);
615614
log.debug(String.format("Walking input %s for path %s", input, path));
616615
Files.walkFileTree(path, visitor);
617-
log.debug(String.format("walked files %s dirs %s", visitor.getPathCounters().getFileCounter().get(),
618-
visitor.getPathCounters().getDirectoryCounter().get()));
616+
log.debug(String.format("walked files %s dirs %s", visitor.getFileCount(), visitor.getDirCount()));
619617
}
620618
catch (Exception e) {
621619
log.debug("PathSearchPathVisitor caused exception", e);
622620
}
623621

624622
// match and score candidates
625623
Set<ScoredPath> treeSet = new HashSet<ScoredPath>();
626-
Stream.concat(visitor.getFileList().stream(), visitor.getDirList().stream()).forEach(p -> {
624+
Stream.concat(visitor.getFiles().stream(), visitor.getDirs().stream()).forEach(p -> {
627625
SearchMatchResult result;
628626
if (StringUtils.hasText(match)) {
629627
SearchMatch searchMatch = SearchMatch.builder()
@@ -643,37 +641,81 @@ public PathScannerResult apply(String input, PathSearchContext context) {
643641
return treeSet.stream()
644642
.sorted()
645643
.limit(context.getPathSearchConfig().getMaxPathsSearch())
646-
.collect(Collectors.collectingAndThen(Collectors.toList(),
647-
list -> PathScannerResult.of(list, visitor.getPathCounters().getDirectoryCounter().get(),
648-
visitor.getPathCounters().getFileCounter().get(), StringUtils.hasText(match))));
644+
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> PathScannerResult.of(list,
645+
visitor.getDirCount(), visitor.getFileCount(), StringUtils.hasText(match))));
649646
}
650647

651648
}
652649

653650
/**
654-
* Extension to AccumulatorPathVisitor which allows to break out from scanning when
655-
* enough results are found.
651+
* Extension to SimpleFileVisitor which allows to break out from scanning when enough
652+
* results are found.
656653
*/
657-
private static class PathSearchPathVisitor extends AccumulatorPathVisitor {
654+
private static class PathSearchPathVisitor extends SimpleFileVisitor<Path> {
658655

659656
private final int limitFiles;
660657

661-
private final static IOFileFilter DNFILTER = new NotFileFilter(new WildcardFileFilter(".*"));
658+
private long fileCount;
659+
660+
private long dirCount;
661+
662+
private final List<Path> files = new ArrayList<>();
663+
664+
private final List<Path> dirs = new ArrayList<>();
665+
666+
private final PathMatcher hiddenMatcher = FileSystems.getDefault().getPathMatcher("glob:.*");
662667

663668
PathSearchPathVisitor(int limitFiles) {
664-
super(Counters.longPathCounters(), DNFILTER, DNFILTER);
665669
this.limitFiles = limitFiles;
666670
}
667671

672+
private boolean accept(Path path) {
673+
Path name = path.getFileName();
674+
if (name == null) {
675+
return false;
676+
}
677+
return !this.hiddenMatcher.matches(name);
678+
}
679+
680+
@Override
681+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
682+
if (!accept(dir)) {
683+
return FileVisitResult.SKIP_SUBTREE;
684+
}
685+
this.dirs.add(dir);
686+
this.dirCount++;
687+
return FileVisitResult.CONTINUE;
688+
}
689+
668690
@Override
669691
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
670692
FileVisitResult result = super.visitFile(file, attributes);
671-
if (getPathCounters().getFileCounter().get() >= this.limitFiles) {
672-
return FileVisitResult.TERMINATE;
693+
if (accept(file)) {
694+
this.files.add(file);
695+
this.fileCount++;
696+
if (this.fileCount >= this.limitFiles) {
697+
return FileVisitResult.TERMINATE;
698+
}
673699
}
674700
return result;
675701
}
676702

703+
public long getDirCount() {
704+
return this.dirCount;
705+
}
706+
707+
public List<Path> getFiles() {
708+
return this.files;
709+
}
710+
711+
public List<Path> getDirs() {
712+
return this.dirs;
713+
}
714+
715+
public long getFileCount() {
716+
return this.fileCount;
717+
}
718+
677719
}
678720

679721
}

spring-shell-core/src/test/java/org/springframework/shell/core/tui/component/PathSearchTests.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,7 @@ static Stream<Arguments> testParts() {
4949
List.of(PartText.of("01", false), PartText.of("2", true), PartText.of("3", true),
5050
PartText.of("456789", false))),
5151
Arguments.of("0123456789", new int[] { 8, 9 },
52-
List.of(PartText.of("01234567", false), PartText.of("8", true), PartText.of("9", true))),
53-
Arguments.of(
54-
"spring-shell-core/build/test-results/test/TEST-org.springframework.shell.support.search.FuzzyMatchV2SearchMatchAlgorithmTests.xml",
55-
new int[] { 13, 33, 59, 67, 73 },
56-
List.of(PartText.of("spring-shell-", false), PartText.of("c", true),
57-
PartText.of("ore/build/test-resu", false), PartText.of("l", true),
58-
PartText.of("ts/test/TEST-org.springfr", false), PartText.of("a", true),
59-
PartText.of("mework.", false), PartText.of("s", true), PartText.of("hell.", false),
60-
PartText.of("s", true),
61-
PartText.of("upport.search.FuzzyMatchV2SearchMatchAlgorithmTests.xml", false))));
52+
List.of(PartText.of("01234567", false), PartText.of("8", true), PartText.of("9", true))));
6253
}
6354

6455
@ParameterizedTest

0 commit comments

Comments
 (0)