-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Improve uninstall/--replace if dir in use
- Loading branch information
Showing
12 changed files
with
245 additions
and
28 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
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
47 changes: 47 additions & 0 deletions
47
fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/SemVerHelper.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,47 @@ | ||
/** | ||
* Copyright 2023 Open Text. | ||
* | ||
* The only warranties for products and services of Open Text | ||
* and its affiliates and licensors (“Open Text”) are as may | ||
* be set forth in the express warranty statements accompanying | ||
* such products and services. Nothing herein should be construed | ||
* as constituting an additional warranty. Open Text shall not be | ||
* liable for technical or editorial errors or omissions contained | ||
* herein. The information contained herein is subject to change | ||
* without notice. | ||
*/ | ||
package com.fortify.cli.common.util; | ||
|
||
import java.lang.module.ModuleDescriptor.Version; | ||
import java.util.regex.Pattern; | ||
|
||
public final class SemVerHelper { | ||
private static final Pattern semverPattern = Pattern.compile("([1-9]\\d*)\\.(\\d+)\\.(\\d+)(?:-(.*))?"); | ||
/** | ||
* Loosely compare two semantic versions, returning -1 if first semver is lower than | ||
* the second, 0 if they are the same, or 1 if first semver is higher than the | ||
* second. Null, blank, or non-semver values are always considered lower than | ||
* true semvers. | ||
* | ||
* @param semver1 | ||
* @param semver2 | ||
* @return | ||
*/ | ||
public static final int compare(String semver1, String semver2) { | ||
var semver1Matcher = semverPattern.matcher(semver1==null?"":semver1); | ||
var semver2Matcher = semverPattern.matcher(semver2==null?"":semver2); | ||
if ( (semver1==null && semver2==null) || semver1.equals(semver2) ) { | ||
return 0; | ||
} else if ( !semver1Matcher.matches() && !semver2Matcher.matches() ) { | ||
return semver1.compareTo(semver2); | ||
} else if ( semver1Matcher.matches() && !semver2Matcher.matches() ) { | ||
return 1; | ||
} else if ( !semver1Matcher.matches() && semver2Matcher.matches() ) { | ||
return -1; | ||
} else { | ||
var version1 = Version.parse(semver1); | ||
var version2 = Version.parse(semver2); | ||
return version1.compareTo(version2); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
fcli-core/fcli-common/src/test/java/com/fortify/cli/common/util/SemVerHelperTest.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,48 @@ | ||
/** | ||
* Copyright 2023 Open Text. | ||
* | ||
* The only warranties for products and services of Open Text | ||
* and its affiliates and licensors (“Open Text”) are as may | ||
* be set forth in the express warranty statements accompanying | ||
* such products and services. Nothing herein should be construed | ||
* as constituting an additional warranty. Open Text shall not be | ||
* liable for technical or editorial errors or omissions contained | ||
* herein. The information contained herein is subject to change | ||
* without notice. | ||
*/ | ||
package com.fortify.cli.common.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
/** | ||
* | ||
* @author Ruud Senden | ||
*/ | ||
public class SemVerHelperTest { | ||
@ParameterizedTest | ||
@CsvSource({ | ||
",,0", | ||
"a,a,0", | ||
"1.2.3,1.2.3,0", | ||
"a,b,-1", | ||
"b,a,1", | ||
"a,1.2.3,-1", | ||
"1.2.3,a,1", | ||
"a,1.2.3-alpha1,-1", | ||
"1.2.3-alpha1,a,1", | ||
"1.2.0,1.2.1,-1", | ||
"1.2.1,1.2.0,1", | ||
"1.2.0,1.3.0,-1", | ||
"1.3.0,1.2.0,1", | ||
"1.0.0,2.0.0,-1", | ||
"2.0.0,1.0.0,1", | ||
"1.2.0,1.2.0-alpha1,1", | ||
"1.2.0-alpha1,1.2.0,-1" | ||
}) | ||
public void testSemVerCompare(String semver1, String semver2, int expectedResult) throws Exception { | ||
assertEquals(expectedResult, SemVerHelper.compare(semver1, semver2)); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.