Skip to content

Commit

Permalink
Merge pull request #13 from quarkiverse/batch
Browse files Browse the repository at this point in the history
Introduce batch mode
  • Loading branch information
ia3andy authored Jun 14, 2024
2 parents 3feecec + 1da7e31 commit e0d748f
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@

import static java.util.function.Predicate.not;

import java.nio.file.Path;
import java.util.*;

import jakarta.inject.Singleton;

import io.quarkiverse.statiq.runtime.FixedStaticPagesProvider;
import io.quarkiverse.statiq.runtime.StatiqGenerator;
import io.quarkiverse.statiq.runtime.StatiqGeneratorConfig;
import io.quarkiverse.statiq.runtime.StatiqRecorder;
import io.quarkiverse.statiq.runtime.*;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.vertx.http.deployment.NonApplicationRootPathBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.deployment.devmode.NotFoundPageDisplayableEndpointBuildItem;
import io.quarkus.vertx.http.deployment.spi.StaticResourcesBuildItem;

Expand All @@ -33,18 +24,6 @@ FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
SyntheticBeanBuildItem produceGeneratorConfig(StatiqConfig config, OutputTargetBuildItem outputTarget,
StatiqRecorder recorder) {
final Path outputDir = outputTarget.getOutputDirectory().resolve(config.outputDir());
return SyntheticBeanBuildItem.configure(StatiqGeneratorConfig.class)
.scope(Singleton.class)
.runtimeValue(recorder.createGeneratorConfig(config.fixed().orElse(List.of()),
outputDir.toAbsolutePath().toString()))
.done();
}

@BuildStep
void produceBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeanProducer) {
additionalBeanProducer.produce(AdditionalBeanBuildItem.unremovableOf(StatiqGenerator.class));
Expand All @@ -55,6 +34,7 @@ void produceBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeanProducer)
@Record(ExecutionTime.RUNTIME_INIT)
void initHandler(List<NotFoundPageDisplayableEndpointBuildItem> notFoundPageDisplayableEndpoints,
StaticResourcesBuildItem staticResourcesBuildItem,
OutputTargetBuildItem outputTarget,
StatiqRecorder recorder) {
Set<String> staticPaths = new HashSet<>();
if (staticResourcesBuildItem != null) {
Expand All @@ -67,23 +47,7 @@ void initHandler(List<NotFoundPageDisplayableEndpointBuildItem> notFoundPageDisp
.toList());
}
recorder.setStatiqPages(staticPaths);
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void initHandler(BuildProducer<RouteBuildItem> routes,
NonApplicationRootPathBuildItem nonApplicationRootPath,
StatiqRecorder recorder) {

final RouteBuildItem route = nonApplicationRootPath.routeBuilder()
.management()
.blockingRoute()
.route("statiq/generate")
.handler(recorder.createGenerateHandler())
.build();

routes.produce(route);

recorder.setOutputTarget(outputTarget.getOutputDirectory().toAbsolutePath().toString());
}

}
71 changes: 42 additions & 29 deletions deployment/src/main/resources/dev-ui/qwc-statiq.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '@vaadin/checkbox';
import '@vaadin/grid';
import { columnBodyRenderer } from '@vaadin/grid/lit.js';
import '@vaadin/grid/vaadin-grid-sort-column.js';
import '@qomponent/qui-alert';

export class QwcStatiq extends LitElement {

Expand Down Expand Up @@ -66,38 +67,50 @@ export class QwcStatiq extends LitElement {

_renderTable() {
return html`
<div class="menubar">
<a href="/q/statiq/generate" target="_blank">
<vaadin-button id="start-cnt-testing-btn" theme="tertiary" tabindex="0" role="button">
<vaadin-icon icon="font-awesome-solid:play"></vaadin-icon>
Generate
</vaadin-button>
</a>
</div>
<vaadin-grid .items="${this._pages}" class="datatable" theme="no-border">
<vaadin-grid-column auto-width
header="Path"
flex-grow="1"
${columnBodyRenderer(this._pathRenderer, [])}>
</vaadin-grid-column>
<vaadin-grid-column auto-width
header="File"
flex-grow="1"
${columnBodyRenderer(this._fileRenderer, [])}>
</vaadin-grid-column>
<vaadin-grid-column path="type" flex-grow="0" width="12em"></vaadin-grid-column>
<vaadin-grid-column header="Link"
width="6em"
flex-grow="0"
${columnBodyRenderer(this._linkRenderer, [])}>
</vaadin-grid-column>
</vaadin-grid>
<div class="menubar">
<qui-alert level="warning">
<div>
<p>Generating in dev-mode is not using the production application. <br/> Run <code>QUARKUS_STATIQ_BATCH=true
java -jar target/quarkus-app/quarkus-run.jar</code></p>
<br/>
<vaadin-button @click="${this._generate}" id="start-cnt-testing-btn" theme="tertiary" tabindex="0"
role="button">
<vaadin-icon icon="font-awesome-solid:play"></vaadin-icon>
Generate
</vaadin-button>
</div>
</qui-alert>
</div>
<vaadin-grid .items="${this._pages}" class="datatable" theme="no-border">
<vaadin-grid-column auto-width
header="Path"
flex-grow="1"
${columnBodyRenderer(this._pathRenderer, [])}>
</vaadin-grid-column>
<vaadin-grid-column auto-width
header="File"
flex-grow="1"
${columnBodyRenderer(this._fileRenderer, [])}>
</vaadin-grid-column>
<vaadin-grid-column path="type" flex-grow="0" width="12em"></vaadin-grid-column>
<vaadin-grid-column header="Link"
width="6em"
flex-grow="0"
${columnBodyRenderer(this._linkRenderer, [])}>
</vaadin-grid-column>
</vaadin-grid>
`;
}

_generate() {
this.jsonRpc.generate().then(jsonRpcResponse => {
alert("Statiq generation succeeded in directory: " + jsonRpcResponse.result);
});
}

_fileRenderer(page) {
return html`${page.outputPath}`;
}
Expand Down
38 changes: 36 additions & 2 deletions docs/modules/ROOT/pages/includes/quarkus-statiq.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ h|[[quarkus-statiq_configuration]]link:#quarkus-statiq_configuration[Configurati
h|Type
h|Default

a|icon:lock[title=Fixed at build time] [[quarkus-statiq_quarkus-statiq-fixed]]`link:#quarkus-statiq_quarkus-statiq-fixed[quarkus.statiq.fixed]`
a| [[quarkus-statiq_quarkus-statiq-fixed]]`link:#quarkus-statiq_quarkus-statiq-fixed[quarkus.statiq.fixed]`


[.description]
Expand All @@ -27,7 +27,7 @@ endif::add-copy-button-to-env-var[]
|


a|icon:lock[title=Fixed at build time] [[quarkus-statiq_quarkus-statiq-output-dir]]`link:#quarkus-statiq_quarkus-statiq-output-dir[quarkus.statiq.output-dir]`
a| [[quarkus-statiq_quarkus-statiq-output-dir]]`link:#quarkus-statiq_quarkus-statiq-output-dir[quarkus.statiq.output-dir]`


[.description]
Expand All @@ -43,4 +43,38 @@ endif::add-copy-button-to-env-var[]
--|string
|`statiq`


a| [[quarkus-statiq_quarkus-statiq-batch]]`link:#quarkus-statiq_quarkus-statiq-batch[quarkus.statiq.batch]`


[.description]
--
Build as a CLI to export the static website

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_STATIQ_BATCH+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_STATIQ_BATCH+++`
endif::add-copy-button-to-env-var[]
--|boolean
|`false`


a| [[quarkus-statiq_quarkus-statiq-timeout]]`link:#quarkus-statiq_quarkus-statiq-timeout[quarkus.statiq.timeout]`


[.description]
--
Timeout for generation in seconds

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_STATIQ_TIMEOUT+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_STATIQ_TIMEOUT+++`
endif::add-copy-button-to-env-var[]
--|long
|`30`

|===
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public String hello(@QueryParam("name") String name) {
@Transactional
StatiqPages produce() {
return new StatiqPages(List.of(
new StatiqPage("/statiq?name=foo"),
new StatiqPage("/statiq?name=bar")));
StatiqPage.builder().html("/statiq?name=foo-html").build(),
StatiqPage.builder().path("/statiq?name=foo").build(),
StatiqPage.builder().path("/statiq?name=bar").build()));
}

}
5 changes: 1 addition & 4 deletions integration-tests/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
quarkus.statiq.fixed=/,/static/**,/assets/**,/some-page
quarkus.management.enabled=true
%dev.quarkus.management.enabled=false
quarkus.management.test-port=9000
quarkus.statiq.fixed=/,/static/**,/assets/**,/some-page
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkiverse.statiq.it;

import static io.restassured.RestAssured.given;
import static java.nio.file.Files.exists;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -9,37 +8,21 @@

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.main.Launch;
import io.quarkus.test.junit.main.QuarkusMainTest;

@QuarkusTest
@QuarkusMainTest
public class StatiqResourceTest {

@Test
public void testHelloEndpoint() {
given()
.when().get("/statiq?name=statiq")
.then()
.statusCode(200)
.body(is("Hello statiq"));
}

@Test
@Launch(value = {}, exitCode = 0)
public void testGenerate() {
given()
.baseUri("http://localhost:9000")
.when().get("/q/statiq/generate")
.then()
.statusCode(200)
.body(startsWith("Generated in:"))
.body(endsWith("/target/statiq"));

assertTrue(exists(Path.of("target/statiq/index.html")));
assertTrue(exists(Path.of("target/statiq/some-page")));
assertTrue(exists(Path.of("target/statiq/statiq-name-foo-html/index.html")));
assertTrue(exists(Path.of("target/statiq/statiq-name-bar")));
assertTrue(exists(Path.of("target/statiq/statiq-name-foo")));
assertTrue(exists(Path.of("target/statiq/assets/vector.svg")));

// FIXME: this will work with next web-bundler release
//assertTrue(exists(Path.of("target/statiq/static/logo.svg")));
assertTrue(exists(Path.of("target/statiq/static/logo.svg")));
}
}
2 changes: 2 additions & 0 deletions integration-tests/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.statiq.batch=true
quarkus.log.category."io.quarkiverse.statiq".level=DEBUG
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.version>3.10.1</quarkus.version>
<quarkus.version>3.11.2</quarkus.version>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,40 @@

@Singleton
public class FixedStaticPagesProvider {
private static String targetDir;
@Inject
StatiqGeneratorConfig config;
StatiqConfig config;

private static volatile Set<String> staticPaths;

public static void setStaticPaths(Set<String> staticPaths) {
FixedStaticPagesProvider.staticPaths = staticPaths;
}

public static void setOutputTarget(String targetDir) {
FixedStaticPagesProvider.targetDir = targetDir;
}

public static String targetDir() {
return targetDir;
}

@Produces
@Singleton
StatiqPages produce() {
List<StatiqPage> statiqPages = new ArrayList<>();
for (String p : config.fixedPaths) {
for (String p : config.fixed().orElse(List.of())) {

if (!isGlobPattern(p) && p.startsWith("/")) {
// fixed paths are directly added
statiqPages.add(new StatiqPage(p, PageType.FIXED));
statiqPages.add(StatiqPage.builder().path(p).fixed().build());
continue;
}
// Try to detect fixed paths from glob pattern
for (String staticPath : staticPaths) {
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + p);
if (matcher.matches(Path.of(staticPath))) {
statiqPages.add(new StatiqPage(staticPath, PageType.FIXED));
statiqPages.add(StatiqPage.builder().fixed().path(staticPath).build());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.statiq.deployment;
package io.quarkiverse.statiq.runtime;

import java.util.List;
import java.util.Optional;
Expand All @@ -9,7 +9,7 @@
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.statiq")
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public interface StatiqConfig {

/**
Expand All @@ -25,4 +25,15 @@ public interface StatiqConfig {
@WithDefault("statiq")
String outputDir();

/**
* Build as a CLI to export the static website
*/
@WithDefault("false")
boolean batch();

/**
* Timeout for generation in seconds
*/
@WithDefault("30")
long timeout();
}
Loading

0 comments on commit e0d748f

Please sign in to comment.