2525import org .openrewrite .text .PlainText ;
2626import org .openrewrite .text .PlainTextParser ;
2727
28- import java .util .*;
28+ import java .util .Arrays ;
29+ import java .util .List ;
2930import java .util .regex .Matcher ;
3031import java .util .regex .Pattern ;
3132
@@ -40,27 +41,18 @@ public class UpdateSdkMan extends Recipe {
4041 * Retrieve from https://api.sdkman.io/2/candidates/java/linuxx64/versions/all
4142 * TODO: Investifate the best way to get the latest versions from SDKMAN
4243 */
43- private static final String SDKMAN_CANDIDATES_LIST = "11.0.14.1-jbr,11.0.15-trava,11.0.25-albba,11.0.25-amzn,11.0.25-kona,11.0.25-librca,11.0.25-ms,11.0.25-sapmchn,11.0.25-sem,11.0.25-tem,11.0.25-zulu,11.0.25.fx-librca,11.0.25.fx-zulu,17.0.12-graal,17.0.12-jbr,17.0.12-oracle,17.0.13-albba,17.0.13-amzn,17.0.13-kona,17.0.13-librca,17.0.13-ms,17.0.13-sapmchn,17.0.13-sem,17.0.13-tem,17.0.13-zulu,17.0.13.crac-librca,17.0.13.crac-zulu,17.0.13.fx-librca,17.0.13.fx-zulu,17.0.9-graalce,21.0.2-graalce,21.0.2-open,21.0.5-amzn,21.0.5-graal,21.0.5-jbr,21.0.5-kona,21.0.5-librca,21.0.5-ms,21.0.5-oracle,21.0.5-sapmchn,21.0.5-sem,21.0.5-tem,21.0.5-zulu,21.0.5.crac-librca,21.0.5.crac-zulu,21.0.5.fx-librca,21.0.5.fx-zulu,22.0.2-oracle,22.1.0.1.r11-gln,22.1.0.1.r17-gln,22.3.5.r11-nik,22.3.5.r17-mandrel,22.3.5.r17-nik,23-open,23.0.1-amzn,23.0.1-graal,23.0.1-graalce,23.0.1-librca,23.0.1-oracle,23.0.1-sapmchn,23.0.1-tem,23.0.1-zulu,23.0.1.crac-zulu,23.0.1.fx-librca,23.0.1.fx-zulu,23.0.6.fx-nik,23.0.6.r17-mandrel,23.0.6.r17-nik,23.1.5.fx-nik,23.1.5.r21-mandrel,23.1.5.r21-nik,24.0.2.r22-mandrel,24.1.1.r23-mandrel,24.1.1.r23-nik,24.ea.22-graal,24.ea.23-graal,24.ea.24-graal,24.ea.26-open,24.ea.27-open,24.ea.28-open,24.ea.29-open,25.ea.1-graal,25.ea.1-open,25.ea.2-open,25.ea.3-open,6.0.119-zulu,7.0.352-zulu,8.0.282-trava,8.0.432-albba,8.0.432-amzn,8.0.432-kona,8.0.432-librca,8.0.432-sem,8.0.432-tem,8.0.432-zulu,8.0.432.fx-librca,8.0.432.fx-zulu" ;
44- private static final Map <String , Set <String >> sdkmanJDKs = Arrays .stream (SDKMAN_CANDIDATES_LIST .split ("," ))
45- .collect (HashMap ::new , (map , candidate ) -> {
46- String [] parts = candidate .split ("-" );
47- String dist = parts [1 ];
48- String version = parts [0 ];
49- map .computeIfAbsent (dist , k -> new HashSet <>()).add (version );
50- }, HashMap ::putAll );
44+ private static final List <String > SDKMAN_CANDIDATES_LIST = Arrays .asList (
45+ "11.0.14.1-jbr,11.0.15-trava,11.0.25-albba,11.0.25-amzn,11.0.25-kona,11.0.25-librca,11.0.25-ms,11.0.25-sapmchn,11.0.25-sem,11.0.25-tem,11.0.25-zulu,11.0.25.fx-librca,11.0.25.fx-zulu,17.0.12-graal,17.0.12-jbr,17.0.12-oracle,17.0.13-albba,17.0.13-amzn,17.0.13-kona,17.0.13-librca,17.0.13-ms,17.0.13-sapmchn,17.0.13-sem,17.0.13-tem,17.0.13-zulu,17.0.13.crac-librca,17.0.13.crac-zulu,17.0.13.fx-librca,17.0.13.fx-zulu,17.0.9-graalce,21.0.2-graalce,21.0.2-open,21.0.5-amzn,21.0.5-graal,21.0.5-jbr,21.0.5-kona,21.0.5-librca,21.0.5-ms,21.0.5-oracle,21.0.5-sapmchn,21.0.5-sem,21.0.5-tem,21.0.5-zulu,21.0.5.crac-librca,21.0.5.crac-zulu,21.0.5.fx-librca,21.0.5.fx-zulu,22.0.2-oracle,22.1.0.1.r11-gln,22.1.0.1.r17-gln,22.3.5.r11-nik,22.3.5.r17-mandrel,22.3.5.r17-nik,23-open,23.0.1-amzn,23.0.1-graal,23.0.1-graalce,23.0.1-librca,23.0.1-oracle,23.0.1-sapmchn,23.0.1-tem,23.0.1-zulu,23.0.1.crac-zulu,23.0.1.fx-librca,23.0.1.fx-zulu,23.0.6.fx-nik,23.0.6.r17-mandrel,23.0.6.r17-nik,23.1.5.fx-nik,23.1.5.r21-mandrel,23.1.5.r21-nik,24.0.2.r22-mandrel,24.1.1.r23-mandrel,24.1.1.r23-nik,24.ea.22-graal,24.ea.23-graal,24.ea.24-graal,24.ea.26-open,24.ea.27-open,24.ea.28-open,24.ea.29-open,25.ea.1-graal,25.ea.1-open,25.ea.2-open,25.ea.3-open,6.0.119-zulu,7.0.352-zulu,8.0.282-trava,8.0.432-albba,8.0.432-amzn,8.0.432-kona,8.0.432-librca,8.0.432-sem,8.0.432-tem,8.0.432-zulu,8.0.432.fx-librca,8.0.432.fx-zulu"
46+ .split ("," ));
5147
5248 @ Option (displayName = "Java version" , description = "The Java version to update to." , example = "17" )
5349 @ Nullable
5450 String newVersion ;
5551
56- @ Option (displayName = "Distribution" , description = "The JVM distribution to use." , example = "open " )
52+ @ Option (displayName = "Distribution" , description = "The JVM distribution to use." , example = "tem " )
5753 @ Nullable
5854 String newDistribution ;
5955
60- private static String getMajorVersion (String version ) {
61- return version .split ("\\ ." )[0 ];
62- }
63-
6456 @ Override
6557 public String getDisplayName () {
6658 return "Update SDKMan Java version" ;
@@ -75,42 +67,11 @@ public String getDescription() {
7567 "Note that these must correspond to valid SDKMAN distributions." ;
7668 }
7769
78- private Optional <String > getNewJDK (String currentVersion , String currentDist ) {
79- if (currentVersion .equals (newVersion ) && currentDist .equals (newDistribution )) {
80- return Optional .empty ();
81- }
82-
83- String dist = newDistribution == null ? currentDist : newDistribution ;
84- String ver = newVersion == null ? currentVersion : newVersion ;
85-
86- if (!sdkmanJDKs .containsKey (dist )) {
87- return Optional .empty ();
88- }
89-
90- Optional <String > sdkmanVersion = sdkmanJDKs .get (dist ).stream ().filter (v -> v .startsWith (ver ) || v .startsWith (getMajorVersion (ver ))).reduce ((first , second ) -> {
91- if (first .equals (ver )) {
92- return first ;
93- } else if (second .equals (ver )) {
94- return second ;
95- } else {
96- try {
97- int [] firstParts = Arrays .stream (first .split ("\\ ." )).mapToInt (Integer ::parseInt ).toArray ();
98- int [] secondParts = Arrays .stream (second .split ("\\ ." )).mapToInt (Integer ::parseInt ).toArray ();
99-
100- for (int i = 1 ; i < Math .min (firstParts .length , secondParts .length ); i ++) {
101- if (firstParts [i ] > secondParts [i ]) {
102- return first ;
103- } else if (firstParts [i ] < secondParts [i ]) {
104- return second ;
105- }
106- }
107- } catch (NumberFormatException e ) {
108- // Ignore
109- }
110- return first ;
111- }
112- });
113- return sdkmanVersion .map (s -> s + "-" + dist );
70+ @ Override
71+ public Validated <Object > validate (ExecutionContext ctx ) {
72+ return super .validate (ctx )
73+ .and (Validated .required ("newVersion" , newVersion )
74+ .or (Validated .required ("newDistribution" , newDistribution )));
11475 }
11576
11677 @ Override
@@ -119,30 +80,26 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
11980 @ Override
12081 public Tree visit (@ Nullable Tree tree , ExecutionContext ctx ) {
12182 SourceFile sourceFile = (SourceFile ) requireNonNull (tree );
122- if (sourceFile instanceof Quark || sourceFile instanceof Remote || sourceFile instanceof Binary || ( newVersion == null && newDistribution == null ) ) {
83+ if (sourceFile instanceof Quark || sourceFile instanceof Remote || sourceFile instanceof Binary ) {
12384 return sourceFile ;
12485 }
12586 PlainText plainText = PlainTextParser .convert (sourceFile );
12687
12788 // Define a regex pattern to extract the version and distribution
128- Pattern pattern = Pattern .compile ("java=([ \\ d.]+)-([a-zA-Z]+ )" );
89+ Pattern pattern = Pattern .compile ("java=(.*?)([.a-z]*-.* )" );
12990 Matcher matcher = pattern .matcher (plainText .getText ());
130-
131- if (!matcher .find ()) {
132- return sourceFile ;
133- }
134-
135- String currentVersion = matcher .group (1 ); // Extract the version (e.g., "21.0.2")
136- String currentDist = matcher .group (2 ); // Extract the distribution (e.g., "tem")
137-
138- Optional <String > newJdk = getNewJDK (currentVersion , currentDist );
139- if (newJdk .isPresent ()) {
140- return plainText .withText (matcher .replaceFirst ("java=" + newJdk .get ()));
91+ if (matcher .find ()) {
92+ String ver = newVersion == null ? matcher .group (1 ) : newVersion ;
93+ String dist = newDistribution == null ? matcher .group (2 ) : newDistribution ;
94+ for (String candidate : SDKMAN_CANDIDATES_LIST ) {
95+ if (candidate .startsWith (ver ) && candidate .endsWith (dist )) {
96+ return plainText .withText (matcher .replaceFirst ("java=" + candidate ));
97+ }
98+ }
14199 }
142100 return sourceFile ;
143101 }
144102 };
145-
146103 return Preconditions .check (new FindSourceFiles (SDKMAN_CONFIG_FILE_PATTERN ), visitor );
147104 }
148105}
0 commit comments