Skip to content

Commit cb94028

Browse files
fix: Apply parent theme located in project frontend folder (#16218) (CP: 24.0) (#16263)
* fix: Apply parent theme located in project frontend folder (#16218) * fix: Apply parent theme located in project frontend folder * Clarifications in comments and info message * extract a new method for frontend utils setup * fix version --------- Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com> Co-authored-by: Mikhail Shabarov <mikhail@vaadin.com>
1 parent 4511c13 commit cb94028

19 files changed

Lines changed: 528 additions & 250 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,17 @@ private static <T> Collection<T> getStylesheetReferences(
16841684
}
16851685
}
16861686

1687-
// Secondly, add a link for the project's custom theme, if it exists
1687+
// Secondly, check if a parent theme is in the project's frontend/themes
1688+
// folder and add a link if any
1689+
Optional<String> parentThemeName = FrontendUtils
1690+
.getParentThemeNameInFrontend(frontendFolder, themeName.get());
1691+
if (parentThemeName.isPresent()) {
1692+
T reference = referenceProvider.apply(parentThemeName.get(),
1693+
fileName);
1694+
references.add(reference);
1695+
}
1696+
1697+
// Finally, add a link for the project's custom theme, if it exists
16881698
// and not yet added by the packaged themes loop
16891699
if (!projectCustomThemeAdded) {
16901700
T reference = referenceProvider.apply(themeName.get(), fileName);

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendUtils.java‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static com.vaadin.flow.server.Constants.COMPATIBILITY_RESOURCES_FRONTEND_DEFAULT;
1919
import static com.vaadin.flow.server.Constants.DEV_BUNDLE_JAR_PATH;
2020
import static com.vaadin.flow.server.Constants.RESOURCES_FRONTEND_DEFAULT;
21-
import static com.vaadin.flow.server.Constants.PROJECT_FRONTEND_GENERATED_DIR_TOKEN;
2221
import static com.vaadin.flow.server.Constants.VAADIN_WEBAPP_RESOURCES;
2322
import static com.vaadin.flow.server.frontend.FrontendTools.INSTALL_NODE_LOCALLY;
2423
import static java.lang.String.format;
@@ -34,7 +33,6 @@
3433
import java.nio.file.Files;
3534
import java.nio.file.LinkOption;
3635
import java.nio.file.Path;
37-
import java.nio.file.Paths;
3836
import java.nio.file.attribute.BasicFileAttributes;
3937
import java.util.ArrayList;
4038
import java.util.Arrays;
@@ -60,7 +58,9 @@
6058
import com.vaadin.flow.server.VaadinService;
6159
import com.vaadin.flow.server.VaadinServlet;
6260
import com.vaadin.flow.server.frontend.FallbackChunk.CssImportData;
61+
import com.vaadin.flow.theme.ThemeDefinition;
6362

63+
import elemental.json.Json;
6464
import elemental.json.JsonArray;
6565
import elemental.json.JsonObject;
6666
import jakarta.servlet.ServletContext;
@@ -1267,4 +1267,58 @@ public static Optional<String> getThemeName(File projectFolder)
12671267
}
12681268
}
12691269

1270+
public static Optional<JsonObject> getThemeJsonInFrontend(
1271+
File frontendFolder, String themeName) throws IOException {
1272+
File themeJsonFile = Path.of(frontendFolder.getAbsolutePath(),
1273+
Constants.APPLICATION_THEME_ROOT, themeName, "theme.json")
1274+
.toFile();
1275+
if (themeJsonFile.exists()) {
1276+
String content = FileUtils.readFileToString(themeJsonFile,
1277+
StandardCharsets.UTF_8);
1278+
content = content.replaceAll("\\r\\n", "\n");
1279+
return Optional.of(Json.parse(content));
1280+
}
1281+
return Optional.empty();
1282+
}
1283+
1284+
public static Optional<JsonObject> getThemeJsonInFrontend(Options options,
1285+
ThemeDefinition themeDefinition) throws IOException {
1286+
if (themeDefinition != null) {
1287+
String themeName = themeDefinition.getName();
1288+
return getThemeJsonInFrontend(options.getFrontendDirectory(),
1289+
themeName);
1290+
}
1291+
return Optional.empty();
1292+
}
1293+
1294+
public static Optional<String> getParentThemeNameInFrontend(
1295+
File frontendFolder, JsonObject themeJson) {
1296+
if (themeJson != null) {
1297+
if (themeJson.hasKey("parent")) {
1298+
String parentThemeName = themeJson.getString("parent");
1299+
File parentThemeFolder = Path
1300+
.of(frontendFolder.getAbsolutePath(),
1301+
Constants.APPLICATION_THEME_ROOT,
1302+
parentThemeName)
1303+
.toFile();
1304+
if (parentThemeFolder.exists()) {
1305+
return Optional.of(parentThemeName);
1306+
}
1307+
}
1308+
}
1309+
return Optional.empty();
1310+
}
1311+
1312+
public static Optional<String> getParentThemeNameInFrontend(
1313+
File frontendFolder, String projectCustomThemeName)
1314+
throws IOException {
1315+
Optional<JsonObject> themeJson = getThemeJsonInFrontend(frontendFolder,
1316+
projectCustomThemeName);
1317+
if (themeJson.isPresent()) {
1318+
return getParentThemeNameInFrontend(frontendFolder,
1319+
themeJson.get());
1320+
}
1321+
return Optional.empty();
1322+
}
1323+
12701324
}

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunDevBundleBuild.java‎

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,17 @@ private static boolean themeConfigurationChanged(Options options,
270270
throws IOException {
271271
Map<String, JsonObject> themeJsonContents = new HashMap<>();
272272

273-
if (options.getJarFiles() == null) {
274-
return false;
273+
if (options.getJarFiles() != null) {
274+
options.getJarFiles().stream().filter(File::exists)
275+
.filter(file -> !file.isDirectory())
276+
.forEach(jarFile -> getPackagedThemeJsonContents(jarFile,
277+
themeJsonContents));
275278
}
276279

277-
options.getJarFiles().stream().filter(File::exists)
278-
.filter(file -> !file.isDirectory())
279-
.forEach(jarFile -> getPackagedThemeJsonContents(jarFile,
280-
themeJsonContents));
281-
282280
ThemeDefinition themeDefinition = frontendDependencies
283281
.getThemeDefinition();
284-
Optional<JsonObject> projectThemeJson = getContentForProjectThemeJson(
285-
options, themeDefinition);
282+
Optional<JsonObject> projectThemeJson = FrontendUtils
283+
.getThemeJsonInFrontend(options, themeDefinition);
286284

287285
JsonObject contentsInStats = statsJson.getObject("themeJsonContents");
288286
if (contentsInStats == null && (!themeJsonContents.isEmpty()
@@ -307,16 +305,29 @@ private static boolean themeConfigurationChanged(Options options,
307305
}
308306

309307
List<String> missedKeys = new ArrayList<>();
310-
JsonObject content = Json.parse(contentsInStats.getString(key));
311-
if (!objectIncludesEntry(content, projectThemeJson.get(),
308+
JsonObject themeJsonContent = Json
309+
.parse(contentsInStats.getString(key));
310+
if (!objectIncludesEntry(themeJsonContent, projectThemeJson.get(),
312311
missedKeys)) {
313312
getLogger().info(
314313
"Project custom theme '{}' has imports/assets in 'theme.json' not present in the bundle",
315314
projectThemeName);
316-
Collections.reverse(missedKeys);
317-
logChangedFiles(missedKeys, "Detected missed entries:");
315+
logMissedEntries(missedKeys);
318316
return true;
319317
}
318+
319+
Optional<String> parentThemeInFrontend = FrontendUtils
320+
.getParentThemeNameInFrontend(
321+
options.getFrontendDirectory(),
322+
projectThemeJson.get());
323+
if (parentThemeInFrontend.isPresent()) {
324+
String parentThemeName = parentThemeInFrontend.get();
325+
Optional<JsonObject> themeJson = FrontendUtils
326+
.getThemeJsonInFrontend(options.getFrontendDirectory(),
327+
parentThemeName);
328+
themeJson.ifPresent(jsonObject -> themeJsonContents
329+
.put(parentThemeName, jsonObject));
330+
}
320331
}
321332

322333
for (Map.Entry<String, JsonObject> themeContent : themeJsonContents
@@ -333,8 +344,9 @@ private static boolean themeConfigurationChanged(Options options,
333344
if (!objectIncludesEntry(content, themeContent.getValue(),
334345
missedKeys)) {
335346
getLogger().info(
336-
"Packaged custom theme '{}' has imports/assets in 'theme.json' not present in the bundle",
347+
"Parent theme '{}' has imports/assets in 'theme.json' not present in the bundle",
337348
themeContent.getKey());
349+
logMissedEntries(missedKeys);
338350
return true;
339351
}
340352
}
@@ -564,6 +576,11 @@ private static void logChangedFiles(List<String> frontendFiles,
564576
getLogger().info(message, handledFiles);
565577
}
566578

579+
private static void logMissedEntries(List<String> missedKeys) {
580+
Collections.reverse(missedKeys);
581+
logChangedFiles(missedKeys, "Detected missed entries:");
582+
}
583+
567584
private static void compareFrontendHashes(JsonObject frontendHashes,
568585
List<String> faultyContent, String frontendFilePath,
569586
String frontendFileContent) {
@@ -856,24 +873,6 @@ private static void getPackagedThemeJsonContents(File jarFileToLookup,
856873
}
857874
}
858875

859-
private static Optional<JsonObject> getContentForProjectThemeJson(
860-
Options options, ThemeDefinition themeDefinition)
861-
throws IOException {
862-
if (themeDefinition != null) {
863-
String themeName = themeDefinition.getName();
864-
File projectThemeJson = new File(options.getFrontendDirectory(),
865-
Constants.APPLICATION_THEME_ROOT + "/" + themeName + "/"
866-
+ "theme.json");
867-
if (projectThemeJson.exists()) {
868-
String content = FileUtils.readFileToString(projectThemeJson,
869-
StandardCharsets.UTF_8);
870-
content = content.replaceAll("\\r\\n", "\n");
871-
return Optional.of(Json.parse(content));
872-
}
873-
}
874-
return Optional.empty();
875-
}
876-
877876
private static Logger getLogger() {
878877
return LoggerFactory.getLogger(TaskRunDevBundleBuild.class);
879878
}

‎flow-server/src/main/resources/vite.generated.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,17 @@ function statsExtracterPlugin(): PluginOption {
282282

283283
const projectThemeJson = path.resolve(frontendFolder, settings.themeFolder, settings.themeName, "theme.json")
284284
if (existsSync(projectThemeJson)) {
285-
themeJsonContents[ settings.themeName ] = readFileSync(projectThemeJson, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
285+
const themeJson = readFileSync(projectThemeJson, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
286+
themeJsonContents[ settings.themeName ] = themeJson;
287+
const themeJsonObject = JSON.parse(themeJson);
288+
if (themeJsonObject.parent) {
289+
const parentThemeJson = path.resolve(frontendFolder, settings.themeFolder, themeJsonObject.parent, "theme.json");
290+
if (existsSync(parentThemeJson)) {
291+
themeJsonContents[ themeJsonObject.parent ] = readFileSync(parentThemeJson, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
292+
}
293+
}
286294
}
287295

288-
289296
let webComponents: string[] = [];
290297
if (webComponentTags) {
291298
webComponents = webComponentTags.split(";");

0 commit comments

Comments
 (0)