Skip to content

Commit

Permalink
[4269] An interface-based way to isolate profile code
Browse files Browse the repository at this point in the history
Honestly, I'd probably just copy the code for now.  If it weren't for
having to have listProfiles due to lombok-generated
`MavenSettings#getProfile`, which has a different signature than we
need, I'd probably go with this.  But having `getProfiles` and
`listProfiles` in the same class is really messy.

Even so, I'll push it and let the rewrite folks decide.

 #4269
  • Loading branch information
Samuel Cox committed Jul 31, 2024
1 parent d003201 commit f9bea1a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.nio.file.Paths;
import java.util.*;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static org.openrewrite.maven.tree.MavenRepository.MAVEN_LOCAL_DEFAULT;
Expand All @@ -51,7 +50,7 @@
@Data
@AllArgsConstructor
@JacksonXmlRootElement(localName = "settings")
public class MavenSettings {
public class MavenSettings implements WithProfiles<MavenSettings.Profile> {
@Nullable
String localRepository;

Expand Down Expand Up @@ -84,27 +83,12 @@ public MavenSettings(@Nullable String localRepository, @Nullable Profiles profil
this.servers = servers;
}

public List<Profile> activeProfiles(final Iterable<String> userSpecifiedProfiles) {
if (this.profiles == null) {
@Override
public List<Profile> listProfiles() {
if (profiles == null) {
return Collections.emptyList();
}

final List<Profile> explicitActiveProfiles =
profiles.getProfiles().stream()
.filter(p -> p.isActive(userSpecifiedProfiles))
.collect(Collectors.toList());

// activeByDefault profiles should be active even if they don't exist
// in userSpecifiedProfiles _unless_ a profile was activated by the
// user or is activated by its activation value (except for 'activeByDefault')
if (!explicitActiveProfiles.isEmpty()) {
return explicitActiveProfiles;
}

return profiles.getProfiles().stream()
.filter(p -> p.getActivation() != null &&
Boolean.TRUE.equals(p.getActivation().getActiveByDefault()))
.collect(Collectors.toList());
return profiles.getProfiles();
}

public static @Nullable MavenSettings parse(Parser.Input source, ExecutionContext ctx) {
Expand Down Expand Up @@ -334,7 +318,7 @@ public ActiveProfiles merge(@Nullable ActiveProfiles activeProfiles) {

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@Data
public static class Profile {
public static class Profile implements WithProfiles.Profile {
@Nullable
String id;

Expand All @@ -344,6 +328,21 @@ public static class Profile {
@Nullable
RawRepositories repositories;

@Override
public String id() {
return id;
}

@Override
public boolean isActive() {
return activation != null && activation.isActive();
}

@Override
public boolean isActiveByDefault() {
return activation != null && Boolean.TRUE.equals(activation.getActiveByDefault());
}

/**
* Returns true if this profile was activated either by the supplied active profiles
* or by activation property, <i>but not solely by activeByDefault</i>.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.openrewrite.maven;

import java.util.List;
import java.util.stream.Collectors;

public interface WithProfiles<P extends WithProfiles.Profile> {
interface Profile {
String id();
boolean isActive();
boolean isActiveByDefault();
}

List<P> listProfiles();

default List<P> activeProfiles(final Iterable<String> userSpecifiedProfiles) {
final List<P> profiles = listProfiles();

final List<P> explicitActiveProfiles =
profiles.stream()
.filter(p -> isActivated(p, userSpecifiedProfiles))
.collect(Collectors.toList());

// activeByDefault profiles should be active even if they don't exist
// in userSpecifiedProfiles _unless_ a profile was activated by the
// user or is activated by its activation value (except for 'activeByDefault')
if (!explicitActiveProfiles.isEmpty()) {
return explicitActiveProfiles;
}

return profiles.stream()
.filter(p -> Boolean.TRUE.equals(p.isActiveByDefault()))
.collect(Collectors.toList());
}

default boolean isActivated(final P profile, final Iterable<String> userSpecifiedProfiles) {
if (profile.id() != null) {
for (String activeProfile : userSpecifiedProfiles) {
if (activeProfile.trim().equals(profile.id())) {
return true;
}
}
}
return profile.isActive();
}

}
28 changes: 8 additions & 20 deletions rewrite-maven/src/main/java/org/openrewrite/maven/tree/Pom.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.WithProfiles;
import org.openrewrite.maven.internal.MavenPomDownloader;

import java.nio.file.Path;
Expand All @@ -48,7 +49,7 @@
@With
@Builder
@AllArgsConstructor
public class Pom {
public class Pom implements WithProfiles<Profile> {

private static final List<String> JAR_PACKAGING_TYPES = Arrays.asList("jar", "bundle");

Expand Down Expand Up @@ -104,14 +105,19 @@ public static int getModelVersion() {
List<License> licenses = emptyList();

@Builder.Default
List<Profile> profiles = emptyList();
List<org.openrewrite.maven.tree.Profile> profiles = emptyList();

@Builder.Default
List<Plugin> plugins = emptyList();

@Builder.Default
List<Plugin> pluginManagement = emptyList();

@Override
public List<org.openrewrite.maven.tree.Profile> listProfiles() {
return profiles;
}

public String getGroupId() {
return gav.getGroupId();
}
Expand Down Expand Up @@ -156,24 +162,6 @@ public List<MavenRepository> getEffectiveRepositories() {
.collect(Collectors.toList());
}

public List<Profile> activeProfiles(final Iterable<String> userSpecifiedProfiles) {
final List<Profile> explicitActiveProfiles =
getProfiles().stream()
.filter(p -> p.isActive(userSpecifiedProfiles))
.collect(Collectors.toList());

// activeByDefault profiles should be active even if they don't exist
// in userSpecifiedProfiles _unless_ a profile was active.
if (!explicitActiveProfiles.isEmpty()) {
return explicitActiveProfiles;
}

return getProfiles().stream()
.filter(p -> p.getActivation() != null &&
Boolean.TRUE.equals(p.getActivation().getActiveByDefault()))
.collect(Collectors.toList());
}

/**
* @param downloader A POM downloader to download dependencies and parents.
* @param ctx An execution context containing any maven-specific requirements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.Value;
import lombok.With;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.WithProfiles;

import java.util.List;
import java.util.Map;
Expand All @@ -29,7 +30,7 @@
*/
@Value
@With
public class Profile {
public class Profile implements WithProfiles.Profile {
@Nullable
String id;

Expand All @@ -44,6 +45,21 @@ public class Profile {
List<Plugin> plugins;
List<Plugin> pluginManagement;

@Override
public String id() {
return id;
}

@Override
public boolean isActive() {
return activation != null && activation.isActive();
}

@Override
public boolean isActiveByDefault() {
return activation != null && Boolean.TRUE.equals(activation.getActiveByDefault());
}

/**
* Returns true if this profile was activated either by the supplied active profiles
* or by activation property, <i>but not solely by activeByDefault</i>.
Expand Down

0 comments on commit f9bea1a

Please sign in to comment.