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

Type-safe message bundles - watch localized files for changes during the dev mode #11223

Merged
merged 1 commit into from
Aug 5, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.objectweb.asm.Opcodes.ACC_FINAL;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
Expand Down Expand Up @@ -53,6 +54,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.gizmo.AssignableResultHandle;
import io.quarkus.gizmo.BranchResult;
import io.quarkus.gizmo.BytecodeCreator;
Expand Down Expand Up @@ -91,6 +93,7 @@ public class MessageBundleProcessor {
private static final String SUFFIX = "_Bundle";
private static final String BUNDLE_DEFAULT_KEY = "defaultKey";
private static final String BUNDLE_LOCALE = "locale";
private static final String MESSAGES = "messages";

static final DotName BUNDLE = DotName.createSimple(MessageBundle.class.getName());
static final DotName MESSAGE = DotName.createSimple(Message.class.getName());
Expand All @@ -116,13 +119,23 @@ List<MessageBundleBuildItem> processBundles(BeanArchiveIndexBuildItem beanArchiv
ApplicationArchivesBuildItem applicationArchivesBuildItem,
BuildProducer<GeneratedClassBuildItem> generatedClasses, BeanRegistrationPhaseBuildItem beanRegistration,
BuildProducer<BeanConfiguratorBuildItem> configurators,
BuildProducer<MessageBundleMethodBuildItem> messageTemplateMethods) throws IOException {
BuildProducer<MessageBundleMethodBuildItem> messageTemplateMethods,
BuildProducer<HotDeploymentWatchedFileBuildItem> watchedFiles) throws IOException {

IndexView index = beanArchiveIndex.getIndex();
Map<String, ClassInfo> found = new HashMap<>();
List<MessageBundleBuildItem> bundles = new ArrayList<>();
Set<Path> messageFiles = findMessageFiles(applicationArchivesBuildItem);

Path messagesPath = applicationArchivesBuildItem.getRootArchive().getChildPath(MESSAGES);
for (Path messageFile : messageFiles) {
String messageFilePath = messagesPath.relativize(messageFile).toString();
if (File.separatorChar != '/') {
messageFilePath = messageFilePath.replace(File.separatorChar, '/');
}
watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(MESSAGES + "/" + messageFilePath));
}

// First collect all interfaces annotated with @MessageBundle
for (AnnotationInstance bundleAnnotation : index.getAnnotations(BUNDLE)) {
if (bundleAnnotation.target().kind() == Kind.CLASS) {
Expand Down Expand Up @@ -794,8 +807,7 @@ private String getDefaultLocale(AnnotationInstance bundleAnnotation) {

private Set<Path> findMessageFiles(ApplicationArchivesBuildItem applicationArchivesBuildItem) throws IOException {
ApplicationArchive applicationArchive = applicationArchivesBuildItem.getRootArchive();
String basePath = "messages";
Path messagesPath = applicationArchive.getChildPath(basePath);
Path messagesPath = applicationArchive.getChildPath(MESSAGES);
if (messagesPath == null) {
return Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.qute.resteasy.deployment;

import java.util.Locale;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
Expand All @@ -8,6 +10,7 @@

import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.qute.i18n.MessageBundles;

@Path("hello")
public class AppMessageHelloResource {
Expand All @@ -20,4 +23,11 @@ public class AppMessageHelloResource {
public TemplateInstance get() {
return hello.instance();
}

@GET
@Path("de")
@Produces(MediaType.TEXT_PLAIN)
public TemplateInstance helloDe() {
return hello.instance().setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.GERMAN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public class MessageBundleDevModeTest {
@Test
public void testMessageBundles() {
when().get("/hello").then().body(is("Hello Georg!"));

TEST.modifySourceFile("AppMessages.java", (s -> s.replace("Hello", "Heya")));

when().get("/hello").then().body(is("Heya Georg!"));

when().get("/hello/de").then().body(is("Hallo Georg!"));
TEST.modifyResourceFile("messages/msg_de.properties", (s -> s.replace("Hallo", "Heya")));
when().get("/hello/de").then().body(is("Heya Georg!"));
}
}