Skip to content

Commit

Permalink
[4269] Moving profile activation to pom/mavensettings where
Browse files Browse the repository at this point in the history
we can look at other profiles state in order to determine what to do
with `activeByDefault` profiles.

I'm not happy that I've copied the code in multiple locations.  The
method of isolating code to a static method doesn't work so well and my
attempts at doing some interface/inheritance modeling is not working
so well with lombok, which I've never really worked with.

Lastly, I've added some tests related to profile deactivation.  If
there's code that handles that, I haven't found it.  The code I've
added here does not.

 #4269
  • Loading branch information
Samuel Cox committed Jul 18, 2024
1 parent de608ea commit 494402f
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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 Down Expand Up @@ -83,6 +84,29 @@ public MavenSettings(@Nullable String localRepository, @Nullable Profiles profil
this.servers = servers;
}

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

final List<Profile> explicitActiveProfiles =
profiles.getProfiles().stream()
.filter(p -> p.isActivated(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());
}

public static @Nullable MavenSettings parse(Parser.Input source, ExecutionContext ctx) {
try {
return new Interpolator().interpolate(
Expand Down Expand Up @@ -153,17 +177,22 @@ public MavenSettings merge(@Nullable MavenSettings installSettings) {
}

public List<RawRepositories.Repository> getActiveRepositories(Iterable<String> activeProfiles) {
LinkedHashMap<String, RawRepositories.Repository> activeRepositories = new LinkedHashMap<>();
List<String> allProfiles = new ArrayList<>();
if (activeProfiles != null) {
for (String prof : activeProfiles) {
allProfiles.add(prof);
}
}

if (profiles != null) {
for (Profile profile : profiles.getProfiles()) {
if (profile.isActive(activeProfiles) || (this.activeProfiles != null &&
profile.isActive(this.activeProfiles.getActiveProfiles()))) {
if (profile.repositories != null) {
for (RawRepositories.Repository repository : profile.repositories.getRepositories()) {
activeRepositories.put(repository.getId(), repository);
}
}
if (this.activeProfiles != null && this.activeProfiles.getActiveProfiles() != null) {
allProfiles.addAll(this.activeProfiles.getActiveProfiles());
}

LinkedHashMap<String, RawRepositories.Repository> activeRepositories = new LinkedHashMap<>();
for (Profile activeProfile : activeProfiles(allProfiles)) {
if (activeProfile.repositories != null) {
for (RawRepositories.Repository repository : activeProfile.repositories.getRepositories()) {
activeRepositories.put(repository.getId(), repository);
}
}
}
Expand Down Expand Up @@ -315,13 +344,24 @@ public static class Profile {
@Nullable
RawRepositories repositories;

public boolean isActive(Iterable<String> activeProfiles) {
return ProfileActivation.isActive(id, activeProfiles, activation);
@SuppressWarnings("unused")
public boolean isActivated(String... activeProfiles) {
return isActivated(Arrays.asList(activeProfiles));
}

@SuppressWarnings("unused")
public boolean isActive(String... activeProfiles) {
return isActive(Arrays.asList(activeProfiles));
/**
* Returns true if this profile was activated either by the supplied active profiles
* or by activation property, <i>but not solely by activeByDefault</i>.
*/
boolean isActivated(Iterable<String> activeProfiles) {
if (getId() != null) {
for (String activeProfile : activeProfiles) {
if (activeProfile.trim().equals(getId())) {
return true;
}
}
}
return getActivation() != null && getActivation().isActive();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -158,13 +157,9 @@ public List<MavenRepository> getEffectiveRepositories() {
}

public List<Profile> activeProfiles(final Iterable<String> userSpecifiedProfiles) {
// pre-compute? I think this is immutable, but not sure

// TODO This is should probably be coded differently or have methods renamed/etc.
// I just did this quickly before vacation starts:)
final List<Profile> explicitActiveProfiles =
getProfiles().stream()
.filter(p -> p.isActive(userSpecifiedProfiles))
.filter(p -> p.isActivated(userSpecifiedProfiles))
.collect(Collectors.toList());

// activeByDefault profiles should be active even if they don't exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ public class Profile {
List<Plugin> plugins;
List<Plugin> pluginManagement;

public boolean isActive(Iterable<String> activeProfiles) {
return ProfileActivation.isActive(id, activeProfiles, activation);
/**
* Returns true if this profile was activated either by the supplied active profiles
* or by activation property, <i>but not solely by activeByDefault</i>.
*/
boolean isActivated(Iterable<String> activeProfiles) {
if (getId() != null) {
for (String activeProfile : activeProfiles) {
if (activeProfile.trim().equals(getId())) {
return true;
}
}
}
return getActivation() != null && getActivation().isActive();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,7 @@ public class ProfileActivation {
@Nullable
Property property;

public static boolean isActive(@Nullable String id, Iterable<String> activeProfiles,
@Nullable ProfileActivation activation) {
if (id != null) {
for (String activeProfile : activeProfiles) {
if (activeProfile.trim().equals(id)) {
return true;
}
}
}
return activation != null &&
(activation.isActive() ||
// Active by default is *only* enabled when no other profile is marked active by any other mechanism
// So even this check for any other explicit activation is overly broad
(Boolean.TRUE.equals(activation.getActiveByDefault()) && !activeProfiles.iterator().hasNext()));
}

// TODO rename these as well?
public boolean isActive() {
return isActiveByJdk() || isActiveByProperty();
}
Expand Down
Loading

0 comments on commit 494402f

Please sign in to comment.