Skip to content

Commit

Permalink
Read build properties from src/main/resources/application.properties …
Browse files Browse the repository at this point in the history
…if present

Fixes #12498
  • Loading branch information
Artur- committed Dec 7, 2021
1 parent 3c211e2 commit f142f8a
Showing 1 changed file with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.vaadin.flow.plugin.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
Expand All @@ -25,7 +27,10 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -58,10 +63,6 @@
import static com.vaadin.flow.server.Constants.GENERATED_TOKEN;
import static com.vaadin.flow.server.Constants.NPM_TOKEN;
import static com.vaadin.flow.server.Constants.PROJECT_FRONTEND_GENERATED_DIR_TOKEN;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_ENABLE_DEV_SERVER;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_INITIAL_UIDL;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP;
import static com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FLOW_RESOURCES_FOLDER;
import static com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES;
import static com.vaadin.flow.server.frontend.FrontendUtils.TOKEN_FILE;
Expand All @@ -71,6 +72,15 @@
*/
public class BuildFrontendUtil {

private static final String[] USED_INIT_PARAMETERS = new String[] {
InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE,
InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP,
InitParameters.SERVLET_PARAMETER_INITIAL_UIDL,
InitParameters.SERVLET_PARAMETER_ENABLE_PNPM,
InitParameters.REQUIRE_HOME_NODE_EXECUTABLE,
InitParameters.BUILD_FOLDER };
private static Properties _applicationPropertiesValues;

/**
* Hide public constructor.
*/
Expand Down Expand Up @@ -201,11 +211,11 @@ public static File propagateBuildInfo(PluginAdapterBase adapter) {
File token = new File(adapter.servletResourceOutputDirectory(),
TOKEN_FILE);
JsonObject buildInfo = Json.createObject();
buildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE,
buildInfo.put(InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE,
adapter.productionMode());
buildInfo.put(SERVLET_PARAMETER_USE_V14_BOOTSTRAP,
buildInfo.put(InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP,
adapter.isUseDeprecatedV14Bootstrapping());
buildInfo.put(SERVLET_PARAMETER_INITIAL_UIDL,
buildInfo.put(InitParameters.SERVLET_PARAMETER_INITIAL_UIDL,
adapter.eagerServerLoad());
buildInfo.put(NPM_TOKEN, adapter.npmFolder().getAbsolutePath());
buildInfo.put(GENERATED_TOKEN,
Expand All @@ -223,8 +233,14 @@ public static File propagateBuildInfo(PluginAdapterBase adapter) {
buildInfo.put(PROJECT_FRONTEND_GENERATED_DIR_TOKEN,
adapter.generatedTsFolder().getAbsolutePath());

buildInfo.put(InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE,
adapter.productionMode());
buildInfo.put(InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP,
adapter.isUseDeprecatedV14Bootstrapping());
buildInfo.put(InitParameters.SERVLET_PARAMETER_INITIAL_UIDL,
adapter.eagerServerLoad());
buildInfo.put(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM,
adapter.pnpmEnable());
usePnpm(adapter));
buildInfo.put(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE,
adapter.requireHomeNodeExec());

Expand Down Expand Up @@ -258,6 +274,31 @@ public static File propagateBuildInfo(PluginAdapterBase adapter) {
}
}

private static Map<Object, Object> getApplicationProperties(
PluginAdapterBase adapter) {
adapter.logInfo("get app proprties");
if (_applicationPropertiesValues != null) {
return _applicationPropertiesValues;
}
_applicationPropertiesValues = new Properties();

File applicationPropertiesFile = new File(adapter.javaResourceFolder(),
"application.properties");
if (applicationPropertiesFile.exists()) {
try (FileInputStream stream = new FileInputStream(
applicationPropertiesFile)) {
adapter.logInfo("Loading app proprties");
_applicationPropertiesValues.load(stream);
} catch (IOException e) {
adapter.logError(
"Unable to read "
+ applicationPropertiesFile.getAbsolutePath(),
e);
}
}
return _applicationPropertiesValues;
}

/**
* runs the node-Updater.
*
Expand Down Expand Up @@ -305,7 +346,7 @@ public static void runNodeUpdater(PluginAdapterBuild adapter)
adapter.generateEmbeddableWebComponents())
.withTokenFile(
BuildFrontendUtil.getTokenFile(adapter))
.enablePnpm(adapter.pnpmEnable())
.enablePnpm(usePnpm(adapter))
.useGlobalPnpm(adapter.useGlobalPnpm())
.withFusionApplicationProperties(
adapter.applicationProperties())
Expand Down Expand Up @@ -333,6 +374,31 @@ public static void runNodeUpdater(PluginAdapterBuild adapter)
}
}

private static boolean usePnpm(PluginAdapterBase adapter) {
return getApplicationProperty(adapter,
InitParameters.SERVLET_PARAMETER_ENABLE_PNPM,
adapter.pnpmEnable());
}

private static boolean getApplicationProperty(PluginAdapterBase adapter,
String key, boolean defaultValue) {
Map<Object, Object> appProperties = getApplicationProperties(adapter);
String propKey = "vaadin." + key;
if (appProperties.containsKey(propKey)) {
adapter.logInfo("Checking " + propKey + " in prop");
Object value = appProperties.get(propKey);
if (value instanceof Boolean) {
return (Boolean) value;
} else {
adapter.logWarn("Expected " + propKey
+ " in application.properties to be a boolean, found '"
+ value + "'");
}
}

return defaultValue;
}

/**
* Execute the frontend build with the wanted build system.
*
Expand Down Expand Up @@ -464,18 +530,19 @@ public static void updateBuildFile(PluginAdapterBuild adapter) {
buildInfo.remove(NPM_TOKEN);
buildInfo.remove(GENERATED_TOKEN);
buildInfo.remove(FRONTEND_TOKEN);
buildInfo.remove(Constants.SERVLET_PARAMETER_ENABLE_PNPM);
buildInfo.remove(Constants.REQUIRE_HOME_NODE_EXECUTABLE);
buildInfo.remove(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM);
buildInfo.remove(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE);
buildInfo.remove(
Constants.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE);
InitParameters.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE);
buildInfo.remove(Constants.CONNECT_JAVA_SOURCE_FOLDER_TOKEN);
buildInfo.remove(Constants.JAVA_RESOURCE_FOLDER_TOKEN);
buildInfo.remove(Constants.CONNECT_APPLICATION_PROPERTIES_TOKEN);
buildInfo.remove(Constants.CONNECT_OPEN_API_FILE_TOKEN);
buildInfo.remove(Constants.PROJECT_FRONTEND_GENERATED_DIR_TOKEN);
buildInfo.remove(InitParameters.BUILD_FOLDER);

buildInfo.put(SERVLET_PARAMETER_ENABLE_DEV_SERVER, false);
buildInfo.put(InitParameters.SERVLET_PARAMETER_ENABLE_DEV_SERVER,
false);
FileUtils.write(tokenFile, JsonUtil.stringify(buildInfo, 2) + "\n",
StandardCharsets.UTF_8.name());
} catch (IOException e) {
Expand Down

0 comments on commit f142f8a

Please sign in to comment.