Skip to content

Commit

Permalink
All profiles implicitly inherit from 'default', fixed plan bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jun 8, 2020
1 parent 05b3bfc commit cb92143
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
17 changes: 11 additions & 6 deletions rewrite-core/src/main/java/org/openrewrite/RefactorPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Arrays.stream;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.StreamSupport.stream;

public class RefactorPlan {
private final Map<String, Profile> profilesByName;
Expand All @@ -35,9 +35,14 @@ public RefactorPlan(Collection<Profile> profiles, Collection<SourceVisitor<?>> v
this.visitors = visitors;
}

public <S extends SourceFile, T extends SourceVisitor<S>> Collection<T> visitors(
Class<S> sourceType, String... profiles) {
List<Profile> loadedProfiles = stream(profiles)
public <T extends Tree, S extends SourceVisitor<T>> Collection<S> visitors(
Class<T> sourceType, String... profiles) {
return visitors(sourceType, Arrays.asList(profiles));
}

public <T extends Tree, S extends SourceVisitor<T>> Collection<S> visitors(
Class<T> sourceType, Iterable<String> profiles) {
List<Profile> loadedProfiles = stream(profiles.spliterator(), false)
.map(profilesByName::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());
Expand All @@ -59,9 +64,9 @@ public <S extends SourceFile, T extends SourceVisitor<S>> Collection<T> visitors
}
return true;
})
.map(v -> (T) v)
.filter(v -> loadedProfiles.stream().anyMatch(p -> p.accept(v).equals(Profile.FilterReply.ACCEPT)))
.map(v -> (S) v)
.map(v -> loadedProfiles.stream().reduce(v, (v2, profile) -> profile.configure(v2), (v1, v2) -> v1))
.filter(v -> loadedProfiles.stream().anyMatch(p -> p.accept(v).equals(Profile.FilterReply.ACCEPT)))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public Profile build(Collection<ProfileConfiguration> otherProfileConfigurations
.forEach(configs::add);
}

if(!name.equals("default") && configsByName.containsKey("default")) {
inOrderConfigurations.add(configsByName.get("default"));
}

return new Profile() {
@Override
public String getName() {
Expand Down Expand Up @@ -214,7 +218,7 @@ private Map<String, Object> propertyMap(String partialName, @Nullable Object con
}

private Profile.FilterReply accept(SourceVisitor<?> visitor) {
if (exclude.stream().anyMatch(i -> i.matcher(visitor.getName()).matches())) {
if (visitor.validate().isInvalid() || exclude.stream().anyMatch(i -> i.matcher(visitor.getName()).matches())) {
return Profile.FilterReply.DENY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ import org.openrewrite.text.ChangeText
import org.openrewrite.text.PlainText

class RefactorPlanTest {
val parent = ProfileConfiguration().apply {
private val parent = ProfileConfiguration().apply {
name = "parent"
setInclude(setOf("org.openrewrite.text.*"))
setConfigure(mapOf("org.openrewrite.text.ChangeText.toText" to "hi"));
}

val child = ProfileConfiguration().apply {
private val child = ProfileConfiguration().apply {
name = "child"
setExtend(setOf("parent"))
setConfigure(mapOf("org.openrewrite.text.ChangeText.toText" to "overridden"))
}

val planBuilder = RefactorPlan.builder()
private val planBuilder = RefactorPlan.builder()
.loadProfile(parent)
.loadProfile(child)
.visitor(ChangeText())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ internal class ProfileConfigurationTest {
assertThat(profile.configure(changeText).toText).isEqualTo("Hello Jon!")
}

@Test
fun everyProfileImplicitlyExtendsDefault() {
val default = ProfileConfiguration().apply {
name = "default"
setConfigure(
mapOf("org.openrewrite.text.ChangeText" to
mapOf("toText" to "Hello Jon!")
)
)
}

val profile = ProfileConfiguration().apply {
name = "hello-jon"
}.build(listOf(default))

assertThat(profile.configure(changeText).toText).isEqualTo("Hello Jon!")
}

@Test
fun propertyNameCombinedWithVisitorName() {
val profile = ProfileConfiguration().apply {
Expand Down

0 comments on commit cb92143

Please sign in to comment.