-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4269] An interface-based way to isolate profile code
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
Showing
4 changed files
with
92 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
rewrite-maven/src/main/java/org/openrewrite/maven/WithProfiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters