[MNG-5726] Support regular expression matching in profile activation for (#1421)

OS version

This requires using the reserved prefix "regex:" in the version element.
This commit is contained in:
Konrad Windszus 2024-02-28 19:04:58 +01:00 committed by GitHub
parent ab6ec5bd74
commit cf60940497
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 204 additions and 42 deletions

View File

@ -21,6 +21,8 @@ package org.apache.maven.model.profile.activation;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Locale;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationOS;
import org.apache.maven.model.Profile;
@ -37,6 +39,8 @@ import org.apache.maven.utils.Os;
@Singleton
public class OperatingSystemProfileActivator implements ProfileActivator {
private static final String REGEX_PREFIX = "regex:";
@Override
public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
Activation activation = profile.getActivation();
@ -53,17 +57,21 @@ public class OperatingSystemProfileActivator implements ProfileActivator {
boolean active = ensureAtLeastOneNonNull(os);
String actualOsName = context.getSystemProperties().get("os.name").toLowerCase(Locale.ENGLISH);
String actualOsArch = context.getSystemProperties().get("os.arch").toLowerCase(Locale.ENGLISH);
String actualOsVersion = context.getSystemProperties().get("os.version").toLowerCase(Locale.ENGLISH);
if (active && os.getFamily() != null) {
active = determineFamilyMatch(os.getFamily());
active = determineFamilyMatch(os.getFamily(), actualOsName);
}
if (active && os.getName() != null) {
active = determineNameMatch(os.getName());
active = determineNameMatch(os.getName(), actualOsName);
}
if (active && os.getArch() != null) {
active = determineArchMatch(os.getArch());
active = determineArchMatch(os.getArch(), actualOsArch);
}
if (active && os.getVersion() != null) {
active = determineVersionMatch(os.getVersion());
active = determineVersionMatch(os.getVersion(), actualOsVersion);
}
return active;
@ -86,8 +94,25 @@ public class OperatingSystemProfileActivator implements ProfileActivator {
return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
}
private boolean determineVersionMatch(String version) {
String test = version;
private boolean determineVersionMatch(String expectedVersion, String actualVersion) {
String test = expectedVersion;
boolean reverse = false;
final boolean result;
if (test.startsWith(REGEX_PREFIX)) {
result = actualVersion.matches(test.substring(REGEX_PREFIX.length()));
} else {
if (test.startsWith("!")) {
reverse = true;
test = test.substring(1);
}
result = actualVersion.equals(test);
}
return reverse != result;
}
private boolean determineArchMatch(String expectedArch, String actualArch) {
String test = expectedArch;
boolean reverse = false;
if (test.startsWith("!")) {
@ -95,13 +120,13 @@ public class OperatingSystemProfileActivator implements ProfileActivator {
test = test.substring(1);
}
boolean result = Os.OS_VERSION.equals(test);
boolean result = actualArch.equals(test);
return reverse != result;
}
private boolean determineArchMatch(String arch) {
String test = arch;
private boolean determineNameMatch(String expectedName, String actualName) {
String test = expectedName;
boolean reverse = false;
if (test.startsWith("!")) {
@ -109,26 +134,12 @@ public class OperatingSystemProfileActivator implements ProfileActivator {
test = test.substring(1);
}
boolean result = Os.OS_ARCH.equals(test);
boolean result = actualName.equals(test);
return reverse != result;
}
private boolean determineNameMatch(String name) {
String test = name;
boolean reverse = false;
if (test.startsWith("!")) {
reverse = true;
test = test.substring(1);
}
boolean result = Os.OS_NAME.equals(test);
return reverse != result;
}
private boolean determineFamilyMatch(String family) {
private boolean determineFamilyMatch(String family, String actualName) {
String test = family;
boolean reverse = false;
@ -137,7 +148,7 @@ public class OperatingSystemProfileActivator implements ProfileActivator {
test = test.substring(1);
}
boolean result = Os.isFamily(test);
boolean result = Os.isFamily(test, actualName);
return reverse != result;
}

View File

@ -148,18 +148,31 @@ public class Os {
*
*/
public static boolean isFamily(String family) {
return isFamily(family, OS_NAME);
}
/**
* Determines if the OS on which Maven is executing matches the
* given OS family derived from the given OS name
*
* @param family the family to check for
* @param actualOsName the OS name to check against
* @return true if the OS matches
*
*/
public static boolean isFamily(String family, String actualOsName) {
// windows probing logic relies on the word 'windows' in the OS
boolean isWindows = OS_NAME.contains(FAMILY_WINDOWS);
boolean isWindows = actualOsName.contains(FAMILY_WINDOWS);
boolean is9x = false;
boolean isNT = false;
if (isWindows) {
// there are only four 9x platforms that we look for
is9x = (OS_NAME.contains("95")
|| OS_NAME.contains("98")
|| OS_NAME.contains("me")
is9x = (actualOsName.contains("95")
|| actualOsName.contains("98")
|| actualOsName.contains("me")
// wince isn't really 9x, but crippled enough to
// be a muchness. Maven doesnt run on CE, anyway.
|| OS_NAME.contains("ce"));
|| actualOsName.contains("ce"));
isNT = !is9x;
}
switch (family) {
@ -170,27 +183,27 @@ public class Os {
case FAMILY_NT:
return isWindows && isNT;
case FAMILY_OS2:
return OS_NAME.contains(FAMILY_OS2);
return actualOsName.contains(FAMILY_OS2);
case FAMILY_NETWARE:
return OS_NAME.contains(FAMILY_NETWARE);
return actualOsName.contains(FAMILY_NETWARE);
case FAMILY_DOS:
return PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE) && !isWindows;
return PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows;
case FAMILY_MAC:
return OS_NAME.contains(FAMILY_MAC) || OS_NAME.contains(DARWIN);
return actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN);
case FAMILY_TANDEM:
return OS_NAME.contains("nonstop_kernel");
return actualOsName.contains("nonstop_kernel");
case FAMILY_UNIX:
return PATH_SEP.equals(":")
&& !isFamily(FAMILY_OPENVMS)
&& (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x"));
&& !isFamily(FAMILY_OPENVMS, actualOsName)
&& (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x"));
case FAMILY_ZOS:
return OS_NAME.contains(FAMILY_ZOS) || OS_NAME.contains(FAMILY_OS390);
return actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390);
case FAMILY_OS400:
return OS_NAME.contains(FAMILY_OS400);
return actualOsName.contains(FAMILY_OS400);
case FAMILY_OPENVMS:
return OS_NAME.contains(FAMILY_OPENVMS);
return actualOsName.contains(FAMILY_OPENVMS);
default:
return OS_NAME.contains(family.toLowerCase(Locale.US));
return actualOsName.contains(family.toLowerCase(Locale.US));
}
}

View File

@ -0,0 +1,138 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.model.profile.activation;
import java.util.Properties;
import org.apache.maven.api.model.Activation;
import org.apache.maven.api.model.ActivationOS;
import org.apache.maven.api.model.Profile;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Tests {@link OperatingSystemProfileActivator}.
*
*/
class OperatingSystemProfileActivatorTest extends AbstractProfileActivatorTest<OperatingSystemProfileActivator> {
@Override
@BeforeEach
void setUp() throws Exception {
activator = new OperatingSystemProfileActivator();
}
private Profile newProfile(ActivationOS.Builder activationBuilder) {
Activation a = Activation.newBuilder().os(activationBuilder.build()).build();
Profile p = Profile.newBuilder().activation(a).build();
return p;
}
private Properties newProperties(String osName, String osVersion, String osArch) {
Properties props = new Properties();
props.setProperty("os.name", osName);
props.setProperty("os.version", osVersion);
props.setProperty("os.arch", osArch);
return props;
}
@Test
void testVersionStringComparison() throws Exception {
Profile profile = newProfile(ActivationOS.newBuilder().version("6.5.0-1014-aws"));
assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
assertActivation(false, profile, newContext(null, newProperties("linux", "3.1.0", "amd64")));
}
@Test
void testVersionRegexMatching() throws Exception {
Profile profile = newProfile(ActivationOS.newBuilder().version("regex:.*aws"));
assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
assertActivation(false, profile, newContext(null, newProperties("linux", "3.1.0", "amd64")));
}
@Test
void testName() {
Profile profile = newProfile(ActivationOS.newBuilder().name("windows"));
assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
}
@Test
void testNegatedName() {
Profile profile = newProfile(ActivationOS.newBuilder().name("!windows"));
assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(false, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
}
@Test
void testArch() {
Profile profile = newProfile(ActivationOS.newBuilder().arch("amd64"));
assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(false, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
}
@Test
void testNegatedArch() {
Profile profile = newProfile(ActivationOS.newBuilder().arch("!amd64"));
assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
}
@Test
void testFamily() {
Profile profile = newProfile(ActivationOS.newBuilder().family("windows"));
assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(true, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
}
@Test
void testNegatedFamily() {
Profile profile = newProfile(ActivationOS.newBuilder().family("!windows"));
assertActivation(true, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(false, profile, newContext(null, newProperties("windows", "6.5.0-1014-aws", "aarch64")));
}
@Test
void testAllOsConditions() {
Profile profile = newProfile(ActivationOS.newBuilder()
.family("windows")
.name("windows")
.arch("aarch64")
.version("99"));
assertActivation(false, profile, newContext(null, newProperties("linux", "6.5.0-1014-aws", "amd64")));
assertActivation(false, profile, newContext(null, newProperties("windows", "1", "aarch64")));
assertActivation(false, profile, newContext(null, newProperties("windows", "99", "amd64")));
assertActivation(true, profile, newContext(null, newProperties("windows", "99", "aarch64")));
}
}