Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #22: Add config for site and data directories #23

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions common-deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkiverse.roq</groupId>
<artifactId>quarkus-roq-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>quarkus-roq-common-deployment</artifactId>
<name>Quarkus Roq - Common - Deployment</name>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkiverse.roq.deployment.config;

import java.util.Objects;

import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.roq")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public interface RoqBuildConfig {

String DEFAULT_SITE_DIR = "src/main/site";
String DEFAULT_DATA_DIR = "data/";

/**
* Path to the static root directory (relative to the project root).
*/
@WithDefault(DEFAULT_SITE_DIR)
String siteDir();

/**
* Path to the static data directory (relative to the site directory `quarkus.roq.site-dir`).
*/
@WithDefault(DEFAULT_DATA_DIR)
String dataDir();

static boolean isEqual(RoqBuildConfig q1, RoqBuildConfig q2) {
if (!Objects.equals(q1.siteDir(), q2.siteDir())) {
return false;
}
if (!Objects.equals(q1.dataDir(), q2.dataDir())) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.quarkiverse.roq.deployment.config;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.StringJoiner;

/**
* Container to store resolved directory locations.
*/
public class RoqProjectDirectories {

/**
* The root directory of the project
*/
private final Path rootDir;

/**
* The site directory of the project defaults to /src/main/site
*/
private final Path siteDir;

/**
* The data directory defaults to "data/" relative to root directory.
*/
private final Path dataDir;

public RoqProjectDirectories(Path rootDir, Path siteDir, Path dataDir) {
this.rootDir = rootDir;
this.siteDir = siteDir;
this.dataDir = dataDir;
}

public Path getRootDir() {
return rootDir;
}

public Path getSiteDir() {
return siteDir;
}

public Path getDataDir() {
return dataDir;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
RoqProjectDirectories that = (RoqProjectDirectories) o;
return Objects.equals(getRootDir(), that.getRootDir()) && Objects.equals(getSiteDir(), that.getSiteDir())
&& Objects.equals(getDataDir(), that.getDataDir());
}

@Override
public int hashCode() {
return Objects.hash(getRootDir(), getSiteDir(), getDataDir());
}

@Override
public String toString() {
return new StringJoiner(", ", RoqProjectDirectories.class.getSimpleName() + "[", "]")
.add("rootDir=" + rootDir)
.add("siteDir=" + siteDir)
.add("dataDir=" + dataDir)
.toString();
}

public static Path findProjectRoot(Path outputDirectory) {
Path currentPath = outputDirectory;
do {
if (Files.exists(currentPath.resolve(Paths.get("src", "main")))
|| Files.exists(currentPath.resolve(Paths.get("config", "application.properties")))
|| Files.exists(currentPath.resolve(Paths.get("config", "application.yaml")))
|| Files.exists(currentPath.resolve(Paths.get("config", "application.yml")))) {
return currentPath.normalize();
}
if (currentPath.getParent() != null && Files.exists(currentPath.getParent())) {
currentPath = currentPath.getParent();
} else {
return null;
}
} while (true);
}
}
7 changes: 6 additions & 1 deletion deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<artifactId>quarkus-roq</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.roq</groupId>
<artifactId>quarkus-roq-common-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-deployment</artifactId>
Expand Down Expand Up @@ -47,4 +52,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

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

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jboss.logging.Logger;

import io.quarkiverse.roq.deployment.config.RoqBuildConfig;
import io.quarkiverse.roq.deployment.config.RoqProjectDirectories;
import io.quarkiverse.roq.runtime.FixedStaticPagesProvider;
import io.quarkiverse.roq.runtime.RoqGenerator;
import io.quarkiverse.roq.runtime.RoqRecorder;
Expand All @@ -21,6 +28,7 @@

class RoqProcessor {

private static final Logger LOG = Logger.getLogger(RoqProcessor.class);
private static final String FEATURE = "roq";

@BuildStep
Expand Down Expand Up @@ -54,4 +62,46 @@ void initHandler(List<NotFoundPageDisplayableEndpointBuildItem> notFoundPageDisp
recorder.setOutputTarget(outputTarget.getOutputDirectory().toAbsolutePath().toString());
}

/**
* Resolves the project directories based on the provided configuration and output target.
*
* @param config the build configuration
* @param outputTarget the output target build item
* @return a {@link RoqProjectDirectories} object containing the resolved project root, site root, and data root
* directories, or {@code null} if the site root directory is not found
* @throws IllegalStateException if the project root is not found and the site directory is not absolute
*/
private static RoqProjectDirectories resolveProjectDirs(RoqBuildConfig config, OutputTargetBuildItem outputTarget) {
Path projectRoot = RoqProjectDirectories.findProjectRoot(outputTarget.getOutputDirectory());
Path configuredSiteDirPath = Paths.get(config.siteDir().trim());

if (projectRoot == null || !Files.isDirectory(projectRoot)) {
if (configuredSiteDirPath.isAbsolute() && Files.isDirectory(configuredSiteDirPath)) {
configuredSiteDirPath = configuredSiteDirPath.normalize();
} else {
throw new IllegalStateException(
"If not absolute, the Site directory is resolved relative to the project root, but Roq was not able to find the project root.");
}
}

final Path siteRoot = projectRoot.resolve(configuredSiteDirPath).normalize();

if (!Files.isDirectory(siteRoot)) {
LOG.warnf(
"Roq directory not found 'quarkus.roq.site-dir=%s' resolved to '%s'. It is recommended to remove the quarkus-roq extension if not used.",
config.siteDir(), siteRoot.toAbsolutePath());
return null;
}

Path dataRoot = siteRoot.resolve(config.dataDir().trim()).normalize();

if (!Files.isDirectory(dataRoot)) {
LOG.debugf("Roq directory not found 'quarkus.roq.data-dir=%s' resolved to '%s'.", config.dataDir(),
dataRoot.toAbsolutePath());
dataRoot = null;
}

return new RoqProjectDirectories(projectRoot, siteRoot, dataRoot);
}

}
Loading
Loading