-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40844 from phillip-kruger/info
Show readme in Dev UI
- Loading branch information
Showing
6 changed files
with
306 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...vertx-http/deployment/src/main/java/io/quarkus/devui/deployment/menu/ReadmeProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.devui.deployment.menu; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
import io.quarkus.deployment.IsDevelopment; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.devui.deployment.InternalPageBuildItem; | ||
import io.quarkus.devui.runtime.readme.ReadmeJsonRPCService; | ||
import io.quarkus.devui.spi.JsonRPCProvidersBuildItem; | ||
import io.quarkus.devui.spi.page.Page; | ||
|
||
/** | ||
* This creates Readme Page | ||
*/ | ||
public class ReadmeProcessor { | ||
|
||
private static final String NS = "devui-readme"; | ||
|
||
@BuildStep(onlyIf = IsDevelopment.class) | ||
void createReadmePage(BuildProducer<InternalPageBuildItem> internalPageProducer) { | ||
|
||
String readme = getContents("README.md") | ||
.orElse(getContents("readme.md") | ||
.orElse(null)); | ||
|
||
if (readme != null) { | ||
InternalPageBuildItem readmePage = new InternalPageBuildItem("Readme", 51); | ||
|
||
readmePage.addBuildTimeData("readme", readme); | ||
|
||
readmePage.addPage(Page.webComponentPageBuilder() | ||
.namespace(NS) | ||
.title("Readme") | ||
.icon("font-awesome-brands:readme") | ||
.componentLink("qwc-readme.js")); | ||
|
||
internalPageProducer.produce(readmePage); | ||
} | ||
} | ||
|
||
@BuildStep(onlyIf = IsDevelopment.class) | ||
JsonRPCProvidersBuildItem createJsonRPCServiceForCache() { | ||
return new JsonRPCProvidersBuildItem(NS, ReadmeJsonRPCService.class); | ||
} | ||
|
||
private Optional<String> getContents(String name) { | ||
Path p = Path.of(name); | ||
if (Files.exists(p)) { | ||
try { | ||
return Optional.of(Files.readString(p)); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
extensions/vertx-http/dev-ui-resources/src/main/resources/dev-ui/qwc/qwc-readme.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { LitElement, html, css} from 'lit'; | ||
import MarkdownIt from 'markdown-it'; | ||
import { unsafeHTML } from 'lit/directives/unsafe-html.js'; | ||
import { JsonRpc } from 'jsonrpc'; | ||
import { readme } from 'devui-data'; | ||
|
||
/** | ||
* This component shows the Readme page | ||
*/ | ||
export class QwcReadme extends LitElement { | ||
|
||
jsonRpc = new JsonRpc("devui-readme", true); | ||
|
||
static styles = css` | ||
.readme { | ||
padding: 15px; | ||
} | ||
a { | ||
color:var(--quarkus-blue); | ||
} | ||
`; | ||
|
||
static properties = { | ||
_readme: {state:true}, | ||
}; | ||
|
||
constructor() { | ||
super(); | ||
this.md = new MarkdownIt(); | ||
this._readme = readme; | ||
} | ||
|
||
connectedCallback() { | ||
super.connectedCallback(); | ||
this._observer = this.jsonRpc.streamReadme().onNext(jsonRpcResponse => { | ||
this._readme = jsonRpcResponse.result; | ||
}); | ||
} | ||
|
||
render() { | ||
if(this._readme){ | ||
const htmlContent = this.md.render(this._readme); | ||
return html`<div class="readme">${unsafeHTML(htmlContent)}</div>`; | ||
} | ||
} | ||
|
||
} | ||
customElements.define('qwc-readme', QwcReadme); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...ertx-http/runtime/src/main/java/io/quarkus/devui/runtime/readme/ReadmeJsonRPCService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package io.quarkus.devui.runtime.readme; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardWatchEventKinds; | ||
import java.nio.file.WatchEvent; | ||
import java.nio.file.WatchKey; | ||
import java.nio.file.WatchService; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.mutiny.infrastructure.Infrastructure; | ||
import io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor; | ||
import io.smallrye.mutiny.subscription.Cancellable; | ||
|
||
@ApplicationScoped | ||
public class ReadmeJsonRPCService { | ||
private WatchService watchService = null; | ||
private Cancellable cancellable; | ||
private Path path = null; | ||
private final BroadcastProcessor<String> readmeStream = BroadcastProcessor.create(); | ||
|
||
@PostConstruct | ||
public void init() { | ||
this.path = getPath("README.md") | ||
.orElse(getPath("readme.md") | ||
.orElse(null)); | ||
if (this.path != null) { | ||
this.path = this.path.toAbsolutePath(); | ||
Path parentDir = this.path.getParent(); | ||
try { | ||
watchService = FileSystems.getDefault().newWatchService(); | ||
parentDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, | ||
StandardWatchEventKinds.ENTRY_MODIFY); | ||
|
||
this.cancellable = Multi.createFrom().emitter(emitter -> { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
WatchKey key; | ||
try { | ||
key = watchService.take(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
return; | ||
} | ||
List<WatchEvent<?>> events = key.pollEvents(); | ||
for (WatchEvent<?> event : events) { | ||
WatchEvent.Kind<?> kind = event.kind(); | ||
Path changed = parentDir.resolve((Path) event.context()); | ||
|
||
if (changed.equals(this.path)) { | ||
emitter.emit(event); | ||
} | ||
} | ||
boolean valid = key.reset(); | ||
if (!valid) { | ||
emitter.complete(); | ||
break; | ||
} | ||
} | ||
}).runSubscriptionOn(Infrastructure.getDefaultExecutor()) | ||
.onItem().transform(event -> { | ||
readmeStream.onNext(getContent()); | ||
return this.path; | ||
}).subscribe().with((t) -> { | ||
|
||
}); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
@PreDestroy | ||
public void cleanup() { | ||
if (cancellable != null) { | ||
cancellable.cancel(); | ||
} | ||
try { | ||
if (watchService != null) { | ||
watchService.close(); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private String getContent() { | ||
try { | ||
return Files.readString(this.path); | ||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
} | ||
|
||
public Multi<String> streamReadme() { | ||
return readmeStream; | ||
} | ||
|
||
private Optional<Path> getPath(String name) { | ||
Path p = Path.of(name); | ||
if (Files.exists(p)) { | ||
return Optional.of(p); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
} |