Skip to content

Commit

Permalink
fix: add Java version selector to Quarkus wizard
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon authored and angelozerr committed Jan 26, 2024
1 parent abc6df9 commit 58be88b
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.redhat.devtools.intellij.quarkus.projectWizard.QuarkusExtensionsModel;
import com.redhat.devtools.intellij.quarkus.projectWizard.QuarkusModel;
import com.redhat.devtools.intellij.quarkus.buildtool.BuildToolDelegate;
import org.jetbrains.jps.model.java.JdkVersionDetector;

public class QuarkusConstants {
public final static Key<BuildToolDelegate> WIZARD_TOOL_KEY = Key.create(QuarkusConstants.class.getPackage().getName() + ".tool");
Expand All @@ -24,8 +25,10 @@ public class QuarkusConstants {
public final static Key<String> WIZARD_CLASSNAME_KEY = Key.create(QuarkusConstants.class.getPackage().getName() + ".className");
public final static Key<String> WIZARD_PATH_KEY = Key.create(QuarkusConstants.class.getPackage().getName() + ".path");
public final static Key<QuarkusExtensionsModel> WIZARD_EXTENSIONS_MODEL_KEY = Key.create(QuarkusConstants.class.getPackage().getName() + ".model");
public final static Key<Integer> WIZARD_JAVA_VERSION_KEY = Key.create(QuarkusConstants.class.getPackage().getName() + ".javaVersion");
public final static Key<String> WIZARD_ENDPOINT_URL_KEY = Key.create(QuarkusConstants.class.getPackage().getName() + ".endpointURL");
public final static Key<QuarkusModel> WIZARD_QUARKUS_STREAMS = Key.create(QuarkusConstants.class.getPackage().getName() + ".streams");
public final static Key<JdkVersionDetector.JdkVersionInfo> WIZARD_JDK_INFO_KEY = Key.create(QuarkusConstants.class.getPackage().getName() + ".jdkVersionInfo");

public static final String CONFIG_ROOT_ANNOTATION = "io.quarkus.runtime.annotations.ConfigRoot";
public static final String CONFIG_GROUP_ANNOTATION = "io.quarkus.runtime.annotations.ConfigGroup";
Expand Down Expand Up @@ -102,6 +105,8 @@ public class QuarkusConstants {

public static final String CODE_CLASSNAME_PARAMETER_NAME = "className";

public static final String CODE_JAVA_VERSION_PARAMETER_NAME = "javaVersion";

public static final String CODE_PATH_PARAMETER_NAME = "path";

public static final String CODE_NO_EXAMPLES_NAME = "noExamples";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ private boolean checkRequestComplete() throws ExecutionException, InterruptedExc
@Override
public void updateDataModel() {
String endpointURL = getSelectedEndpointUrl();

if (!Comparing.strEqual(this.wizardContext.getUserData(QuarkusConstants.WIZARD_ENDPOINT_URL_KEY), endpointURL)) {
this.endpointURL.addCurrentTextToHistory();
this.wizardContext.putUserData(QuarkusConstants.WIZARD_ENDPOINT_URL_KEY, endpointURL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.intellij.util.Urls;
import com.intellij.util.io.HttpRequests;
import com.intellij.util.io.RequestBuilder;
import com.redhat.qute.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.zip.ZipUtil;
Expand All @@ -43,6 +44,20 @@
*/
public class QuarkusModelRegistry {

public static class CreateQuarkusProjectRequest {
public String endpoint;
public String tool;
public String groupId;
public String artifactId;
public String version;
public String className;
public String path;
public int javaVersion;
public QuarkusExtensionsModel model;
public File output;
public boolean codeStarts;
}

private static final Logger LOGGER = LoggerFactory.getLogger(QuarkusModelRegistry.class);
/**
* Default request timeout in seconds
Expand Down Expand Up @@ -127,7 +142,7 @@ public static QuarkusExtensionsModel loadExtensionsModel(String endPointURL, Str
request.setRequestProperty(CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_NAME, CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_VALUE);
}).connect(request -> {
try (Reader reader = request.getReader(indicator)) {
List<QuarkusExtension> extensions = mapper.readValue(reader, new TypeReference<List<QuarkusExtension>>() {
List<QuarkusExtension> extensions = mapper.readValue(reader, new TypeReference<>() {
});
QuarkusExtensionsModel newModel = new QuarkusExtensionsModel(key, extensions);
long elapsed = System.currentTimeMillis() - start;
Expand All @@ -139,18 +154,17 @@ public static QuarkusExtensionsModel loadExtensionsModel(String endPointURL, Str
});
}

public static void zip(String endpoint, String tool, String groupId, String artifactId, String version,
String className, String path, QuarkusExtensionsModel model, File output, boolean codeStarts) throws IOException {
Url url = Urls.newFromEncoded(normalizeURL(endpoint) + "/api/download");
String body = buildParameters(tool, groupId, artifactId, version, className, path, model, codeStarts);
public static void zip(CreateQuarkusProjectRequest createQuarkusProjectRequest) throws IOException {
Url url = Urls.newFromEncoded(normalizeURL(createQuarkusProjectRequest.endpoint) + "/api/download");
String body = buildParameters(createQuarkusProjectRequest);
RequestBuilder builder = HttpRequests.post(url.toString(), HttpRequests.JSON_CONTENT_TYPE).userAgent(QuarkusModelRegistry.USER_AGENT).tuner(connection -> {
connection.setRequestProperty(CODE_QUARKUS_IO_CLIENT_NAME_HEADER_NAME, CODE_QUARKUS_IO_CLIENT_NAME_HEADER_VALUE);
connection.setRequestProperty(CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_NAME, CODE_QUARKUS_IO_CLIENT_CONTACT_EMAIL_HEADER_VALUE);
});
try {
if (ApplicationManager.getApplication().executeOnPooledThread(() -> builder.connect(request -> {
request.write(body);
ZipUtil.unpack(request.getInputStream(), output, name -> {
ZipUtil.unpack(request.getInputStream(), createQuarkusProjectRequest.output, name -> {
int index = name.indexOf('/');
return name.substring(index);
});
Expand All @@ -163,26 +177,31 @@ public static void zip(String endpoint, String tool, String groupId, String arti
}
}

private static String buildParameters(String tool, String groupId, String artifactId, String version,
String className, String path, QuarkusExtensionsModel model,
boolean codeStarts) {
private static String buildParameters(CreateQuarkusProjectRequest createQuarkusProjectRequest) {
JsonObject json = new JsonObject();

json.addProperty(CODE_TOOL_PARAMETER_NAME, tool);
json.addProperty(CODE_GROUP_ID_PARAMETER_NAME, groupId);
json.addProperty(CODE_ARTIFACT_ID_PARAMETER_NAME, artifactId);
json.addProperty(CODE_VERSION_PARAMETER_NAME, version);
json.addProperty(CODE_CLASSNAME_PARAMETER_NAME, className);
json.addProperty(CODE_PATH_PARAMETER_NAME, path);
if (!codeStarts) {
json.addProperty(CODE_TOOL_PARAMETER_NAME, createQuarkusProjectRequest.tool);
json.addProperty(CODE_GROUP_ID_PARAMETER_NAME, createQuarkusProjectRequest.groupId);
json.addProperty(CODE_ARTIFACT_ID_PARAMETER_NAME, createQuarkusProjectRequest.artifactId);
json.addProperty(CODE_VERSION_PARAMETER_NAME, createQuarkusProjectRequest.version);
if (!StringUtils.isEmpty(createQuarkusProjectRequest.className)) {
json.addProperty(CODE_CLASSNAME_PARAMETER_NAME, createQuarkusProjectRequest.className);
}
if (!StringUtils.isEmpty(createQuarkusProjectRequest.path)) {
json.addProperty(CODE_PATH_PARAMETER_NAME, createQuarkusProjectRequest.path);
}
if (createQuarkusProjectRequest.javaVersion > 0) {
json.addProperty(CODE_JAVA_VERSION_PARAMETER_NAME, createQuarkusProjectRequest.javaVersion);
}
if (!createQuarkusProjectRequest.codeStarts) {
json.addProperty(CODE_NO_EXAMPLES_NAME, CODE_NO_EXAMPLES_DEFAULT);
}
JsonArray extensions = new JsonArray();
model.getCategories().stream().flatMap(category -> category.getExtensions().stream()).
createQuarkusProjectRequest.model.getCategories().stream().flatMap(category -> category.getExtensions().stream()).
filter(extension -> extension.isSelected() || extension.isDefaultExtension()).
forEach(extension -> extensions.add(extension.getId()));
json.add(CODE_EXTENSIONS_PARAMETER_NAME, extensions);
json.addProperty(CODE_STREAM_PARAMETER_NAME, model.getKey());
json.addProperty(CODE_STREAM_PARAMETER_NAME, createQuarkusProjectRequest.model.getKey());
return json.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,19 @@ public Module createModule(@NotNull ModifiableModuleModel moduleModel) throws In

private void processDownload() throws IOException {
File moduleFile = new File(getContentEntryPath());
QuarkusModelRegistry.zip(wizardContext.getUserData(QuarkusConstants.WIZARD_ENDPOINT_URL_KEY),
wizardContext.getUserData(QuarkusConstants.WIZARD_TOOL_KEY).asParameter(),
wizardContext.getUserData(QuarkusConstants.WIZARD_GROUPID_KEY),
wizardContext.getUserData(QuarkusConstants.WIZARD_ARTIFACTID_KEY),
wizardContext.getUserData(QuarkusConstants.WIZARD_VERSION_KEY),
wizardContext.getUserData(QuarkusConstants.WIZARD_CLASSNAME_KEY),
wizardContext.getUserData(QuarkusConstants.WIZARD_PATH_KEY),
wizardContext.getUserData(QuarkusConstants.WIZARD_EXTENSIONS_MODEL_KEY),
moduleFile,
wizardContext.getUserData(QuarkusConstants.WIZARD_EXAMPLE_KEY));
var createQuarkusProjectRequest = new QuarkusModelRegistry.CreateQuarkusProjectRequest();
createQuarkusProjectRequest.endpoint = wizardContext.getUserData(QuarkusConstants.WIZARD_ENDPOINT_URL_KEY);
createQuarkusProjectRequest.tool = wizardContext.getUserData(QuarkusConstants.WIZARD_TOOL_KEY).asParameter();
createQuarkusProjectRequest.groupId = wizardContext.getUserData(QuarkusConstants.WIZARD_GROUPID_KEY);
createQuarkusProjectRequest.artifactId = wizardContext.getUserData(QuarkusConstants.WIZARD_ARTIFACTID_KEY);
createQuarkusProjectRequest.version = wizardContext.getUserData(QuarkusConstants.WIZARD_VERSION_KEY);
createQuarkusProjectRequest.className = wizardContext.getUserData(QuarkusConstants.WIZARD_CLASSNAME_KEY);
createQuarkusProjectRequest.path = wizardContext.getUserData(QuarkusConstants.WIZARD_PATH_KEY);
createQuarkusProjectRequest.model = wizardContext.getUserData(QuarkusConstants.WIZARD_EXTENSIONS_MODEL_KEY);
createQuarkusProjectRequest.javaVersion = wizardContext.getUserData(QuarkusConstants.WIZARD_JAVA_VERSION_KEY);
createQuarkusProjectRequest.output = moduleFile;
createQuarkusProjectRequest.codeStarts = wizardContext.getUserData(QuarkusConstants.WIZARD_EXAMPLE_KEY);
QuarkusModelRegistry.zip(createQuarkusProjectRequest);
updateWrapperPermissions(moduleFile);
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(moduleFile);
RefreshQueue.getInstance().refresh(true, true, (Runnable) null, new VirtualFile[]{vf});
Expand Down
Loading

0 comments on commit 58be88b

Please sign in to comment.