Skip to content

Commit

Permalink
Fix live-reload (#261)
Browse files Browse the repository at this point in the history
* Fix live-reload

* Future collection
  • Loading branch information
ia3andy authored Nov 8, 2024
1 parent 7d852a2 commit 04fb911
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 26 deletions.
Binary file modified docs/modules/ROOT/assets/images/roq-how-it-works.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Roq allows to easily create a static website or blog using Quarkus super-powers.

== How it works

// https://excalidraw.com/#json=FbBiH5H7j5qFS-XZ96TDI,GIDI6Ss_p_VFKo62ip9t4A
// https://excalidraw.com/#json=pZssfxY47ooeLKkHeH0cM,7jxUkcUdHu3WcR1ktCRFow
image::roq-how-it-works.png[Roq - How it works]

[TIP]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.paths.PathVisit;
import io.quarkus.qute.deployment.TemplatePathBuildItem;
import io.quarkus.qute.deployment.TemplateRootBuildItem;
import io.quarkus.runtime.configuration.ConfigurationException;
Expand Down Expand Up @@ -150,8 +151,14 @@ public List<RoqFrontMatterRawTemplateBuildItem> resolveItems(RoqProjectBuildItem
}

roqProject.consumePathFromRoqResourceDir(config.contentDir(),
l -> scanContent(mapper, config, markups, watch, dataModifications, items, l.getPath()));
roqProject.consumePathFromRoqResourceDir(config.staticDir(), l -> scanStatic(config, staticFilesProducer, l.getPath()));
l -> {
watchResourceDir(watch, l);
scanContent(mapper, config, markups, watch, dataModifications, items, l.getPath());
});
roqProject.consumePathFromRoqResourceDir(config.staticDir(), l -> {
watchResourceDir(watch, l);
scanStatic(config, staticFilesProducer, l.getPath());
});
return items;
}

Expand All @@ -165,12 +172,19 @@ private static Consumer<Path> createRoqDirConsumer(YAMLMapper mapper, RoqSiteCon
if (!Files.isDirectory(root)) {
return;
}

// We scan Qute templates manually outside of resources for now
scanTemplates(config, watch, templatePathProducer, root.resolve(TEMPLATES_DIR));
scanLayouts(mapper, config, markups, watch, dataModifications, items, root.resolve(TEMPLATES_DIR),
final Path templatesDir = root.resolve(TEMPLATES_DIR);
watchDirectory(templatesDir, watch);
scanTemplates(config, watch, templatePathProducer, templatesDir);
scanLayouts(mapper, config, markups, watch, dataModifications, items, templatesDir,
TemplateType.LAYOUT);
scanContent(mapper, config, markups, watch, dataModifications, items, root.resolve(config.contentDir()));
scanStatic(config, staticFilesProducer, root.resolve(config.staticDir()));
final Path contentDir = root.resolve(config.contentDir());
watchDirectory(contentDir, watch);
scanContent(mapper, config, markups, watch, dataModifications, items, contentDir);
final Path staticDir = root.resolve(config.staticDir());
watchDirectory(staticDir, watch);
scanStatic(config, staticFilesProducer, staticDir);
};
}

Expand Down Expand Up @@ -202,6 +216,7 @@ private static void scanContent(YAMLMapper mapper, RoqSiteConfig config,
if (!Files.isDirectory(contentDir)) {
return;
}

// scan content
final Map<String, ConfiguredCollection> collections = config.collections().stream()
.collect(Collectors.toMap(ConfiguredCollection::id, Function.identity()));
Expand All @@ -212,10 +227,10 @@ private static void scanContent(YAMLMapper mapper, RoqSiteConfig config,
.forEach(p -> {
final String dirName = contentDir.relativize(p).getName(0).toString();
if (collections.containsKey(dirName)) {
addBuildItem(contentDir, items, mapper, config, markups, dataModifications, watch,
addBuildItem(contentDir, items, mapper, config, markups, dataModifications,
collections.get(dirName), TemplateType.DOCUMENT_PAGE).accept(p);
} else {
addBuildItem(contentDir, items, mapper, config, markups, dataModifications, watch, null,
addBuildItem(contentDir, items, mapper, config, markups, dataModifications, null,
TemplateType.NORMAL_PAGE).accept(p);
}
});
Expand All @@ -226,6 +241,30 @@ private static void scanContent(YAMLMapper mapper, RoqSiteConfig config,

}

private static void watchDirectory(Path dir, BuildProducer<HotDeploymentWatchedFileBuildItem> watch) {
if (!Files.isDirectory(dir)) {
return;
}
try (var stream = Files.walk(dir)) {
stream.forEach(f -> {
watch.produce(HotDeploymentWatchedFileBuildItem.builder().setLocation(f.toAbsolutePath().toString()).build());
if (LOGGER.isDebugEnabled()) {
LOGGER.debugf("Watching %s for changes", f);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void watchResourceDir(BuildProducer<HotDeploymentWatchedFileBuildItem> watch, PathVisit l) {
final String dir = l.getRelativePath();
watch.produce(HotDeploymentWatchedFileBuildItem.builder().setLocationPredicate(p -> p.startsWith(dir)).build());
if (LOGGER.isDebugEnabled()) {
LOGGER.debugf("Watching resources %s for changes", dir);
}
}

private static void scanLayouts(YAMLMapper mapper,
RoqSiteConfig config,
Map<String, QuteMarkupSection> markups,
Expand All @@ -245,7 +284,7 @@ private static void scanLayouts(YAMLMapper mapper,
try (Stream<Path> stream = Files.walk(layoutsDir)) {
final Consumer<Path> layoutsConsumer = addBuildItem(templatesRoot, items, mapper, config, markups,
dataModifications,
watch, null,
null,
type);
stream
.filter(Files::isRegularFile)
Expand All @@ -271,7 +310,6 @@ private static void scanTemplates(RoqSiteConfig config,
if (!Files.isDirectory(templatesRoot)) {
return;
}

// scan templates
try (Stream<Path> stream = Files.walk(templatesRoot)) {
stream
Expand All @@ -284,8 +322,6 @@ private static void scanTemplates(RoqSiteConfig config,
}
// add Qute templates
try {
watch.produce(HotDeploymentWatchedFileBuildItem.builder().setLocation(p.toAbsolutePath().toString())
.build());
final String link = toUnixPath(templatesRoot.relativize(p).toString());
templatePathProducer.produce(TemplatePathBuildItem.builder()
.path(link)
Expand All @@ -309,11 +345,9 @@ private static Consumer<Path> addBuildItem(Path root,
RoqSiteConfig config,
Map<String, QuteMarkupSection> markups,
List<RoqFrontMatterDataModificationBuildItem> dataModifications,
BuildProducer<HotDeploymentWatchedFileBuildItem> watch,
ConfiguredCollection collection,
TemplateType type) {
return file -> {
watch.produce(HotDeploymentWatchedFileBuildItem.builder().setLocation(file.toAbsolutePath().toString()).build());
String sourcePath = toUnixPath(root.relativize(file).toString());
String quteTemplatePath = ROQ_GENERATED_QUTE_PREFIX + removeExtension(sourcePath)
+ resolveOutputExtension(markups, sourcePath);
Expand All @@ -338,7 +372,8 @@ private static Consumer<Path> addBuildItem(Path root,
fm.getString(LAYOUT_KEY));
final String content = stripFrontMatter(fullContent);
ZonedDateTime date = parsePublishDate(file, fm, config.dateFormat(), config.timeZone());
if (date != null && !config.future() && date.isAfter(ZonedDateTime.now())) {
final boolean noFuture = !config.future() && (collection == null || !collection.future());
if (date != null && noFuture && date.isAfter(ZonedDateTime.now())) {
return;
}
String dateString = date.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.roq.frontmatter.runtime.config;

public record ConfiguredCollection(String id, boolean hidden) {
public record ConfiguredCollection(String id, boolean hidden, boolean future) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface RoqSiteConfig {
String CONTENT_DIR = "content";
String STATIC_DIR = "static";
String IGNORED_FILES = "**/_**,_**,.**";
List<ConfiguredCollection> DEFAULT_COLLECTIONS = List.of(new ConfiguredCollection("posts", false));
List<ConfiguredCollection> DEFAULT_COLLECTIONS = List.of(new ConfiguredCollection("posts", false, false));

/**
* The root path of your site (e.g. /blog) relative the quarkus http root path.
Expand Down Expand Up @@ -75,7 +75,7 @@ public interface RoqSiteConfig {
boolean generator();

/**
* Show future pages
* Show future documents
*/
@WithDefault("false")
boolean future();
Expand Down Expand Up @@ -122,7 +122,7 @@ default List<ConfiguredCollection> collections() {
return DEFAULT_COLLECTIONS;
}
return collectionsMap().entrySet().stream().filter(e -> e.getValue().enabled())
.map(e -> new ConfiguredCollection(e.getKey(), e.getValue().hidden())).toList();
.map(e -> new ConfiguredCollection(e.getKey(), e.getValue().hidden(), e.getValue().future())).toList();
}

interface CollectionConfig {
Expand All @@ -133,6 +133,12 @@ interface CollectionConfig {
@WithDefault("true")
boolean enabled();

/**
* Show future documents (overrides global future for this collection)
*/
@WithDefault("false")
boolean future();

/**
* If true, the collection won't be available on path but consumable as data.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkiverse.roq.frontmatter.runtime.model;

import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -49,7 +50,7 @@ public DocumentPage prevPage(DocumentPage page) {
/**
* Get the sub-list of documents depending on the given paginator
*/
public List<DocumentPage> paginated(Paginator paginator) {
public Collection<DocumentPage> paginated(Paginator paginator) {
if (paginator == null) {
return this;
}
Expand All @@ -58,6 +59,20 @@ public List<DocumentPage> paginated(Paginator paginator) {
Math.min(this.size(), (zeroBasedCurrent * paginator.limit()) + paginator.limit()));
}

/**
* @return future documents
*/
public Collection<DocumentPage> future() {
return stream().filter(d -> d.date().isAfter(ZonedDateTime.now())).toList();
}

/**
* @return past documents
*/
public Collection<DocumentPage> past() {
return stream().filter(d -> d.date().isBefore(ZonedDateTime.now())).toList();
}

/**
* Retrieves a list of non-null values from the pages for the specified keys.
* This method searches through all the pages for each of the provided keys and
Expand All @@ -66,7 +81,7 @@ public List<DocumentPage> paginated(Paginator paginator) {
* @param keys the keys to search for in the pages' data. Multiple keys can be passed.
* @return a {@code List<Object>} containing all non-null values found in the pages for the specified keys.
*/
public List<Object> by(String... keys) {
public Collection<Object> by(String... keys) {
return this.stream()
.flatMap(page -> Arrays.stream(keys)
.map(page::data) // Get the data for each key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
{#seo page site /}
{#rss site /}

<!-- Edit site and author settings in `_config.yml` to make the social details your own -->
<base href="{site.url.root.relative}">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="shortcut icon" href="{site.imagesDirUrl.resolve('site-icon.svg')}" type="image/svg+xml">
<!-- Chrome, Firefox OS and Opera -->
<meta name="theme-color" content="#263959">
<!-- Windows Phone -->
<meta name="msapplication-navbutton-color" content="#263959">
<!-- iOS Safari -->
<meta name="apple-mobile-web-app-status-bar-style" content="#263959">
<link rel="stylesheet" href="{site.url.fromRoot('/static/bundle/roq.css')}">

Expand Down

0 comments on commit 04fb911

Please sign in to comment.