feat(plugins): allow to set minimum required jadx version in plugin info (#2314)

This commit is contained in:
Skylot
2024-11-06 16:29:43 +00:00
parent 5d064d3e50
commit be6cb573b1
19 changed files with 453 additions and 96 deletions
@@ -5,6 +5,7 @@ import com.google.gson.annotations.SerializedName
import io.github.oshai.kotlinlogging.KotlinLogging
import jadx.api.JadxDecompiler
import jadx.core.Jadx
import jadx.core.plugins.versions.VersionComparator
import jadx.gui.settings.JadxUpdateChannel
import org.jetbrains.kotlin.konan.file.use
import java.io.InputStreamReader
@@ -1,72 +0,0 @@
package jadx.gui.update;
public class VersionComparator {
private VersionComparator() {
}
public static int checkAndCompare(String str1, String str2) {
return compare(clean(str1), clean(str2));
}
private static String clean(String str) {
if (str == null || str.isEmpty()) {
return "";
}
String result = str.trim().toLowerCase();
if (result.startsWith("jadx-gui-")) {
result = result.substring(9);
}
if (result.startsWith("jadx-")) {
result = result.substring(5);
}
if (result.charAt(0) == 'v') {
result = result.substring(1);
}
if (result.charAt(0) == 'r') {
result = result.substring(1);
int dot = result.indexOf('.');
if (dot != -1) {
result = result.substring(0, dot);
}
}
// treat a package version as part of version
result = result.replace('-', '.');
return result;
}
private static int compare(String str1, String str2) {
String[] s1 = str1.split("\\.");
int l1 = s1.length;
String[] s2 = str2.split("\\.");
int l2 = s2.length;
int i = 0;
// skip equals parts
while (i < l1 && i < l2) {
if (!s1[i].equals(s2[i])) {
break;
}
i++;
}
// compare first non-equal ordinal number
if (i < l1 && i < l2) {
return Integer.valueOf(s1[i]).compareTo(Integer.valueOf(s2[i]));
}
boolean checkFirst = l1 > l2;
boolean zeroTail = isZeroTail(checkFirst ? s1 : s2, i);
if (zeroTail) {
return 0;
}
return checkFirst ? 1 : -1;
}
private static boolean isZeroTail(String[] arr, int pos) {
for (int i = pos; i < arr.length; i++) {
if (Integer.parseInt(arr[i]) != 0) {
return false;
}
}
return true;
}
}
@@ -1,44 +0,0 @@
package jadx.gui.update;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class VersionComparatorTest {
@Test
public void testCompare() {
checkCompare("", "", 0);
checkCompare("1", "1", 0);
checkCompare("1", "2", -1);
checkCompare("1.1", "1.1", 0);
checkCompare("0.5", "0.5", 0);
checkCompare("0.5", "0.5.0", 0);
checkCompare("0.5", "0.5.00", 0);
checkCompare("0.5", "0.5.0.0", 0);
checkCompare("0.5", "0.5.0.1", -1);
checkCompare("0.5.0", "0.5.0", 0);
checkCompare("0.5.0", "0.5.1", -1);
checkCompare("0.5", "0.5.1", -1);
checkCompare("0.4.8", "0.5", -1);
checkCompare("0.4.8", "0.5.0", -1);
checkCompare("0.4.8", "0.6", -1);
checkCompare("1.3.3.1", "1.3.3", 1);
checkCompare("1.3.3-1", "1.3.3", 1);
checkCompare("1.3.3.1-1", "1.3.3", 1);
}
@Test
public void testCompareUnstable() {
checkCompare("r2190.ce527ed", "jadx-r2299.742d30d", -1);
}
private static void checkCompare(String a, String b, int result) {
assertThat(VersionComparator.checkAndCompare(a, b))
.as("Compare %s and %s expect %d", a, b, result)
.isEqualTo(result);
assertThat(VersionComparator.checkAndCompare(b, a))
.as("Compare %s and %s expect %d", b, a, -result)
.isEqualTo(-result);
}
}