Skip to content
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

Added generate-profile with '--diff-from' param (not tested) #8

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import fr.chuckame.marlinfw.configurator.change.LineChangeFormatter;
import fr.chuckame.marlinfw.configurator.change.LineChangeManager;
import fr.chuckame.marlinfw.configurator.constant.Constant;
import fr.chuckame.marlinfw.configurator.constant.ProfilePropertiesChangeAdapter;
import fr.chuckame.marlinfw.configurator.profile.ProfileProperties;
import fr.chuckame.marlinfw.configurator.constant.ProfileAdapter;
import fr.chuckame.marlinfw.configurator.profile.ProfilePropertiesParser;
import fr.chuckame.marlinfw.configurator.util.ConsoleHelper;
import fr.chuckame.marlinfw.configurator.util.FileHelper;
Expand Down Expand Up @@ -38,7 +37,7 @@ public class ApplyCommand implements Command {
@Parameter(names = {"--yes", "-y"}, description = "when present, the changes will be saved without prompting the user")
private boolean applyWithoutPrompt;

private final ProfilePropertiesChangeAdapter changeAdapter;
private final ProfileAdapter profileAdapter;
private final LineChangeManager lineChangeManager;
private final LineChangeFormatter lineChangeFormatter;
private final ProfilePropertiesParser profilePropertiesParser;
Expand All @@ -47,19 +46,18 @@ public class ApplyCommand implements Command {

@Override
public Mono<Void> run() {
return fileHelper.listFiles(profilePaths)
.flatMap(profilePropertiesParser::parseFromFile)
.reduceWith(ProfileProperties::new, ProfileProperties::merge)
.map(changeAdapter::profileToConstants)
.flatMap(wantedConstants ->
prepareChanges(wantedConstants)
.flatMap(changes -> printChanges(changes)
.then(printUnusedConstants(changes, wantedConstants))
.then(doSave ? checkIfUserAgree().then(applyAndSaveChanges(changes)) : Mono.empty()))
);
return profilePropertiesParser
.parseFromFiles(profilePaths)
.map(profileAdapter::profileToConstants)
.flatMap(wantedConstants ->
prepareChanges(wantedConstants)
.flatMap(changes -> printChanges(changes)
.then(printUnusedConstants(changes, wantedConstants))
.then(applyAndSaveChangesIfNeeded(changes)))
);
}

public Mono<Map<Path, List<LineChange>>> prepareChanges(final Map<String, Constant> wantedConstants) {
private Mono<Map<Path, List<LineChange>>> prepareChanges(final Map<String, Constant> wantedConstants) {
return fileHelper.listFiles(filesPath)
.flatMap(filePath -> fileHelper.lines(filePath)
.index()
Expand Down Expand Up @@ -117,6 +115,13 @@ private Mono<Void> printUnusedConstants(final Map<Path, List<LineChange>> change
.then();
}

private Mono<Void> applyAndSaveChangesIfNeeded(final Map<Path, List<LineChange>> changes) {
if (!doSave) {
return Mono.empty();
}
return checkIfUserAgree().then(applyAndSaveChanges(changes));
}

private Mono<Void> checkIfUserAgree() {
if (applyWithoutPrompt) {
return Mono.empty();
Expand All @@ -128,7 +133,7 @@ private Mono<Void> checkIfUserAgree() {
.then();
}

public Mono<Void> applyAndSaveChanges(final Map<Path, List<LineChange>> changes) {
private Mono<Void> applyAndSaveChanges(final Map<Path, List<LineChange>> changes) {
return Flux.fromIterable(changes.entrySet())
.filter(e -> onlyChangedFile(e.getValue()))
.groupBy(Map.Entry::getKey, Map.Entry::getValue)
Expand All @@ -144,7 +149,7 @@ private boolean isModifyingChange(final LineChange change) {
return !LineChange.DiffEnum.DO_NOTHING.equals(change.getDiff());
}

public Flux<String> applyChanges(final Collection<LineChange> changes) {
private Flux<String> applyChanges(final Collection<LineChange> changes) {
return Flux.fromIterable(changes).flatMap(lineChangeManager::applyChange);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
import fr.chuckame.marlinfw.configurator.change.LineChangeFormatter;
import fr.chuckame.marlinfw.configurator.change.LineChangeManager;
import fr.chuckame.marlinfw.configurator.constant.Constant;
import fr.chuckame.marlinfw.configurator.constant.ConstantLineInterpreter;
import fr.chuckame.marlinfw.configurator.profile.ConstantHelper;
import fr.chuckame.marlinfw.configurator.util.ConsoleHelper;
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;
import java.util.List;
import java.util.Map;

@Component
@Parameters(commandNames = "diff", commandDescription = "Display differences between marlin configuration files")
Expand All @@ -30,26 +28,17 @@ public class DiffCommand implements Command {

private final LineChangeManager lineChangeManager;
private final LineChangeFormatter lineChangeFormatter;
private final FileHelper fileHelper;
private final ConstantHelper constantHelper;
private final ConsoleHelper consoleHelper;
private final ConstantLineInterpreter constantLineInterpreter;

@Override
public Mono<Void> run() {
return Mono.zip(getConstants(leftFiles), getConstants(rightFiles))
return Mono.zip(constantHelper.getConstants(leftFiles).collectMap(Constant::getName), constantHelper.getConstants(rightFiles).collectMap(Constant::getName))
.map(t -> Maps.difference(t.getT1(), t.getT2()))
.flatMap(this::printDiff)
.then();
}

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

private Mono<Void> printDiff(final MapDifference<String, Constant> diff) {
final var added = Flux.fromIterable(diff.entriesOnlyOnRight().values())
.map(addedConstant -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import fr.chuckame.marlinfw.configurator.constant.ConstantLineInterpreter;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import fr.chuckame.marlinfw.configurator.constant.Constant;
import fr.chuckame.marlinfw.configurator.profile.ConstantHelper;
import fr.chuckame.marlinfw.configurator.profile.ProfilePropertiesParser;
import fr.chuckame.marlinfw.configurator.util.ConsoleHelper;
import fr.chuckame.marlinfw.configurator.util.FileHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.file.Path;
Expand All @@ -20,25 +23,35 @@
public class GenerateProfileCommand implements Command {
@Parameter(required = true, description = "/path1 /path2 ...\tThe marlin constants folder or files paths")
private List<Path> filesPath;
@Parameter(names = {"--diff-from"}, description = "The marlin constants folder or files paths from where you want to make a diff. If gathered, the generated profile will contains only the diff between those files and the command files")
private List<Path> filesPathBase;
@Parameter(names = {"--output", "-o"}, required = true, description = "The output profile path, will be overwritten if already existing file. If 'console' is specified, the profile will just be printed to the console")
private Path profilePath;

private static final Path CONSOLE_OUTPUT = Path.of("console");

private final ProfilePropertiesParser profilePropertiesParser;
private final FileHelper fileHelper;
private final ConstantHelper constantHelper;
private final ConstantLineInterpreter constantLineInterpreter;
private final ConsoleHelper consoleHelper;

@Override
public Mono<Void> run() {
return constantHelper.constantsToProfile(fileHelper.listFiles(filesPath)
.flatMap(fileHelper::lines)
.flatMap(constantLineInterpreter::parseLine)
.map(ConstantLineInterpreter.ParsedConstant::getConstant))
final Flux<Constant> constants = CollectionUtils.isEmpty(filesPathBase) ? constantHelper.getConstants(filesPath) : getConstantsFromDiff();
return constantHelper.constantsToProfile(constants)
.flatMap(profile -> profilePath.equals(CONSOLE_OUTPUT) ?
profilePropertiesParser.writeToString(profile).doOnNext(consoleHelper::writeLine).then()
: profilePropertiesParser.writeToFile(profile, profilePath));
}

/**
* @return only constants that are not present from {@link #filesPathBase}, and only modified constants present on both sides
*/
private Flux<Constant> getConstantsFromDiff() {
return Mono.zip(constantHelper.getConstants(filesPathBase).collectMap(Constant::getName),
constantHelper.getConstants(filesPath).collectMap(Constant::getName))
.map(t -> Maps.difference(t.getT1(), t.getT2()))
.flatMapMany(diff -> Flux.fromIterable(diff.entriesDiffering().values())
.map(MapDifference.ValueDifference::leftValue)
.mergeWith(Flux.fromIterable(diff.entriesOnlyOnRight().values())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;

@Component
public class ProfilePropertiesChangeAdapter {
public class ProfileAdapter {
public Map<String, Constant> profileToConstants(final ProfileProperties profileProperties) {
final var wantedChanges = new HashMap<String, Constant>();
profileProperties.getDisabled().forEach(constantName -> wantedChanges.put(constantName, disabledConstant(constantName)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package fr.chuckame.marlinfw.configurator.profile;

import fr.chuckame.marlinfw.configurator.constant.Constant;
import fr.chuckame.marlinfw.configurator.constant.ConstantLineInterpreter;
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;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Component
@RequiredArgsConstructor
public class ConstantHelper {
private final FileHelper fileHelper;
private final ConstantLineInterpreter constantLineInterpreter;

public Mono<ProfileProperties> constantsToProfile(final Flux<Constant> constants) {
return constants.reduceWith(this::initEmptyProfile, this::addConstantToProfile);
}
Expand All @@ -28,4 +35,11 @@ private ProfileProperties addConstantToProfile(final ProfileProperties profile,
}
return profile;
}

public Flux<Constant> getConstants(final List<Path> files) {
return fileHelper.listFiles(files)
.flatMap(fileHelper::lines)
.flatMap(constantLineInterpreter::parseLine)
.map(ConstantLineInterpreter.ParsedConstant::getConstant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;
import java.util.List;

import static com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.INDENT_ARRAYS;
import static com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.MINIMIZE_QUOTES;
Expand All @@ -26,6 +27,12 @@ public class ProfilePropertiesParser {
private final ObjectMapper yamlParser = prepareYamlMapper();
private final FileHelper fileHelper;

public Mono<ProfileProperties> parseFromFiles(final List<Path> profileFilePaths) {
return fileHelper.listFiles(profileFilePaths)
.flatMap(this::parseFromFile)
.reduceWith(ProfileProperties::new, ProfileProperties::merge);
}

public Mono<ProfileProperties> parseFromFile(final Path profileFilePath) {
return fileHelper.read(profileFilePath)
.map(ExceptionUtils.wrap(bytes -> yamlParser.readValue(bytes, ProfileProperties.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import static org.assertj.core.api.Assertions.assertThat;

class ProfilePropertiesChangeAdapterTest {
private final ProfilePropertiesChangeAdapter changeAdapter = new ProfilePropertiesChangeAdapter();
class ProfileAdapterTest {
private final ProfileAdapter profileAdapter = new ProfileAdapter();

@Test
void getWantedConstantsShouldReturnEmptyMapWhenEmptyEnabledAndEmptyDisabled() {
Expand All @@ -19,7 +19,7 @@ void getWantedConstantsShouldReturnEmptyMapWhenEmptyEnabledAndEmptyDisabled() {
.disabled(List.of())
.build();

final var wantedConstants = changeAdapter.profileToConstants(profile);
final var wantedConstants = profileAdapter.profileToConstants(profile);

assertThat(wantedConstants).isEmpty();
}
Expand All @@ -31,7 +31,7 @@ void getWantedConstantsShouldReturnExpectedConstants() {
.disabled(List.of("c3", "c4"))
.build();

final var wantedConstants = changeAdapter.profileToConstants(profile);
final var wantedConstants = profileAdapter.profileToConstants(profile);

assertThat(wantedConstants)
.containsEntry("c1", Constant.builder().enabled(true).name("c1").value(null).build())
Expand Down