Skip to content

Commit

Permalink
Given paths can include directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuckame committed Nov 2, 2020
1 parent 78db0e9 commit 1b6da36
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'fr.chuckame.marlinfw'
version '1.0.0'
version '1.1.0'
sourceCompatibility = '11'

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ private Mono<Void> checkIfUserAgree() {
}

public Mono<Map<Path, List<LineChange>>> prepareChanges(final List<Path> filesPath, final Map<String, Constant> wantedConstants) {
return Flux.fromIterable(filesPath)
.flatMap(filePath -> fileHelper.lines(filePath)
.index()
.concatMap(line -> lineChangeManager.prepareChange(line.getT2(), line.getT1().intValue(), wantedConstants))
.collectList()
.zipWith(Mono.just(filePath)))
.collectMap(Tuple2::getT2, Tuple2::getT1);
return fileHelper.listFiles(filesPath)
.flatMap(filePath -> fileHelper.lines(filePath)
.index()
.concatMap(line -> lineChangeManager.prepareChange(line.getT2(), line.getT1().intValue(), wantedConstants))
.collectList()
.zipWith(Mono.just(filePath)))
.collectMap(Tuple2::getT2, Tuple2::getT1);
}

public Mono<Void> applyAndSaveChanges(final Map<Path, List<LineChange>> changes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ private Mono<Void> printDiff(final MapDifference<String, Constant> diff) {
}

private Mono<Map<String, Constant>> getConstants(final List<Path> files) {
return Flux.fromIterable(files)
.flatMap(fileHelper::lines)
.flatMap(constantLineInterpreter::parseLine)
.map(ConstantLineInterpreter.ParsedConstant::getConstant)
.collectMap(Constant::getName);
return fileHelper.listFiles(files)
.flatMap(fileHelper::lines)
.flatMap(constantLineInterpreter::parseLine)
.map(ConstantLineInterpreter.ParsedConstant::getConstant)
.collectMap(Constant::getName);
}

private Mono<Void> printChanges(final Map<Path, List<LineChange>> changes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import fr.chuckame.marlinfw.configurator.util.FileHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.file.Path;
Expand All @@ -34,10 +33,10 @@ public class GenerateProfileCommand implements Command {

@Override
public Mono<Void> run() {
return constantHelper.constantsToProfile(Flux.fromIterable(filesPath)
.flatMap(fileHelper::lines)
.flatMap(constantLineInterpreter::parseLine)
.map(ConstantLineInterpreter.ParsedConstant::getConstant))
return constantHelper.constantsToProfile(fileHelper.listFiles(filesPath)
.flatMap(fileHelper::lines)
.flatMap(constantLineInterpreter::parseLine)
.map(ConstantLineInterpreter.ParsedConstant::getConstant))
.flatMap(profile -> profilePath.equals(CONSOLE_OUTPUT) ?
profilePropertiesParser.writeToString(profile).doOnNext(consoleHelper::writeLine).then()
: profilePropertiesParser.writeToFile(profile, profilePath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,29 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Supplier;
import java.util.stream.StreamSupport;

@Component
public class FileHelper {
public Flux<Path> listFiles(final List<Path> paths) {
return Flux.fromIterable(paths)
.flatMap(path -> {
if (Files.isDirectory(path)) {
return toFlux(ExceptionUtils.wrap(() -> Files.newDirectoryStream(path).spliterator()));
}
return Mono.just(path);
})
.filter(Files::isRegularFile)
;
}

private <T> Flux<T> toFlux(final Supplier<Spliterator<T>> iterator) {
return Flux.fromStream(() -> StreamSupport.stream(iterator.get(), false));

}

public Flux<String> lines(final Path file) {
return Flux.fromStream(ExceptionUtils.wrap(() -> Files.lines(file)));
}
Expand Down

0 comments on commit 1b6da36

Please sign in to comment.