Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created November 8, 2022 16:48
Show Gist options
  • Save maxandersen/c93a1abe7832bc6b244967557eda9fe1 to your computer and use it in GitHub Desktop.
Save maxandersen/c93a1abe7832bc6b244967557eda9fe1 to your computer and use it in GitHub Desktop.
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11+
import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
class q3upgrade {
static String recipeURL = "https://raw.githubusercontent.com/quarkusio/quarkus/main/jakarta/quarkus3.yml";
private static String OS = System.getProperty("os.name").toLowerCase();
public static boolean isWindows() {
return OS.contains("win");
}
public static void main(String[] args) throws IOException {
if(!Files.exists(Path.of(mvnCommand()))) {
System.err.println("Cannot locate " + mvnCommand() + ", currently only support Maven for migration. Is the current directory in root of your Quarkus project?");
System.exit(-1);
}
Path tempfile = downloadRecipe();
ProcessBuilder processBuilder = new ProcessBuilder();
String[] command = new String[] {
mvnCommand(),
"org.openrewrite.maven:rewrite-maven-plugin:4.36.0:run",
"-Drewrite.configLocation=" + tempfile.toAbsolutePath(),
"-DactiveRecipes=io.quarkus.openrewrite.Quarkus3"
};
System.out.println("Executing:\n" + String.join(" ", command) );
processBuilder.command(command);
try {
Process process = processBuilder.start();
BufferedReader reader =
new BufferedReader(new java.io.InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
out.println(line);
}
int exitCode = process.waitFor();
out.println("\nExited with error code : " + exitCode);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static String mvnCommand() {
return isWindows() ? "./mvnw.cmd" : "./mvnw";
}
private static Path downloadRecipe() throws MalformedURLException, IOException, ProtocolException {
URL url = new URL(recipeURL);
out.println("Downloading " + recipeURL + " to temporary file.");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
Path tempfile = null;
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
try(Scanner s = new Scanner(responseStream).useDelimiter("\\A")) {
String response = s.hasNext() ? s.next() : "";
tempfile = Files.createTempFile("quarkus3", "yml");
Files.writeString(tempfile,response);
}
return tempfile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment