Skip to content

Commit

Permalink
Remove Old Dev UI: Core Configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Aug 3, 2023
1 parent 6e65c47 commit d56a90b
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 1,129 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.quarkus.devui.deployment.menu;

import static io.quarkus.vertx.http.deployment.devmode.console.ConfigEditorProcessor.cleanUpAsciiDocIfNecessary;
import static io.quarkus.vertx.http.deployment.devmode.console.ConfigEditorProcessor.isSetByDevServices;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand All @@ -25,13 +27,12 @@
import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem;
import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.devui.deployment.InternalPageBuildItem;
import io.quarkus.devui.runtime.config.ConfigDescription;
import io.quarkus.devui.runtime.config.ConfigDescriptionBean;
import io.quarkus.devui.runtime.config.ConfigDevUiRecorder;
import io.quarkus.devui.runtime.config.ConfigDevUIRecorder;
import io.quarkus.devui.runtime.config.ConfigJsonRPCService;
import io.quarkus.devui.spi.JsonRPCProvidersBuildItem;
import io.quarkus.devui.spi.page.Page;
import io.quarkus.vertx.http.deployment.devmode.console.ConfigEditorProcessor;
import io.quarkus.vertx.http.runtime.devmode.ConfigDescription;

/**
* This creates Extensions Page
Expand Down Expand Up @@ -66,7 +67,7 @@ InternalPageBuildItem createConfigurationPages(
@Record(ExecutionTime.STATIC_INIT)
void registerConfigs(List<ConfigDescriptionBuildItem> configDescriptionBuildItems,
Optional<DevServicesLauncherConfigResultBuildItem> devServicesLauncherConfig,
ConfigDevUiRecorder recorder) {
ConfigDevUIRecorder recorder) {

List<ConfigDescription> configDescriptions = new ArrayList<>();
for (ConfigDescriptionBuildItem item : configDescriptionBuildItems) {
Expand All @@ -93,16 +94,16 @@ void registerConfigs(List<ConfigDescriptionBuildItem> configDescriptionBuildItem
void registerJsonRpcService(
BuildProducer<JsonRPCProvidersBuildItem> jsonRPCProvidersProducer,
BuildProducer<SyntheticBeanBuildItem> syntheticBeanProducer,
ConfigDevUiRecorder recorder) {
ConfigDevUIRecorder recorder) {

DevConsoleManager.register("config-update-property", map -> {
Map<String, String> values = Collections.singletonMap(map.get("name"), map.get("value"));
ConfigEditorProcessor.updateConfig(values, false);
updateConfig(values);
return null;
});
DevConsoleManager.register("config-set-properties", value -> {
String content = value.get("content");
ConfigEditorProcessor.setConfig(content, false);
setConfig(content);
return null;
});

Expand Down Expand Up @@ -131,4 +132,98 @@ private static String formatJavadoc(String val) {
val = val.replace("@deprecated", "<br><strong>Deprecated</strong>");
return val;
}

private static String cleanUpAsciiDocIfNecessary(String docs) {
if (docs == null || !docs.toLowerCase(Locale.ROOT).contains("@asciidoclet")) {
return docs;
}
// TODO #26199 Ideally we'd use a proper AsciiDoc renderer, but for now we'll just clean it up a bit.
return docs.replace("@asciidoclet", "")
// Avoid problems with links.
.replace("<<", "&lt;&lt;")
.replace(">>", "&gt;&gt;")
// Try to render line breaks... kind of.
.replace("\n\n", "<p>")
.replace("\n", "<br>");
}

private static boolean isSetByDevServices(Optional<DevServicesLauncherConfigResultBuildItem> devServicesLauncherConfig,
String propertyName) {
if (devServicesLauncherConfig.isPresent()) {
return devServicesLauncherConfig.get().getConfig().containsKey(propertyName);
}
return false;
}

private static void updateConfig(Map<String, String> values) {
if (values != null && !values.isEmpty()) {
try {
Path configPath = getConfigPath();
List<String> lines = Files.readAllLines(configPath);
for (Map.Entry<String, String> entry : values.entrySet()) {
String name = entry.getKey();
String value = entry.getValue();
int nameLine = -1;
for (int i = 0, linesSize = lines.size(); i < linesSize; i++) {
String line = lines.get(i);
if (line.startsWith(name + "=")) {
nameLine = i;
break;
}
}
if (nameLine != -1) {
if (value.isEmpty()) {
lines.remove(nameLine);
} else {
lines.set(nameLine, name + "=" + value);
}
} else {
if (!value.isEmpty()) {
lines.add(name + "=" + value);
}
}
}

try (BufferedWriter writer = Files.newBufferedWriter(configPath)) {
for (String i : lines) {
writer.write(i);
writer.newLine();
}
}
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}

private static void setConfig(String value) {
try {
Path configPath = getConfigPath();
try (BufferedWriter writer = Files.newBufferedWriter(configPath)) {
if (value == null || value.isEmpty()) {
writer.newLine();
} else {
writer.write(value);
}
}
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

private static Path getConfigPath() throws IOException {
List<Path> resourcesDir = DevConsoleManager.getHotReplacementContext().getResourcesDir();
if (resourcesDir.isEmpty()) {
throw new IllegalStateException("Unable to manage configurations - no resource directory found");
}

// In the current project only
Path path = resourcesDir.get(0);
Path configPath = path.resolve("application.properties");
if (!Files.exists(configPath)) {
Files.createDirectories(configPath.getParent());
configPath = Files.createFile(path.resolve("application.properties"));
}
return configPath;
}
}
Loading

0 comments on commit d56a90b

Please sign in to comment.