use of com.vaadin.flow.server.frontend.NodeTasks.Builder in project flow by vaadin.
the class NodeTasksTest method should_BeAbleToCustomizeFolders.
@Test
public void should_BeAbleToCustomizeFolders() throws Exception {
System.setProperty(PARAM_FRONTEND_DIR, "my_custom_sources_folder");
System.setProperty(PARAM_GENERATED_DIR, "my/custom/generated/folder");
Lookup mockedLookup = mock(Lookup.class);
Mockito.doReturn(new DefaultClassFinder(this.getClass().getClassLoader())).when(mockedLookup).lookup(ClassFinder.class);
Builder builder = new Builder(mockedLookup, new File(userDir), TARGET).enablePackagesUpdate(false).enableImportsUpdate(true).runNpmInstall(false).withEmbeddableWebComponents(false);
Assert.assertEquals(new File(userDir, "my_custom_sources_folder").getAbsolutePath(), ((File) getFieldValue(builder, "frontendDirectory")).getAbsolutePath());
Assert.assertEquals(new File(userDir, "my/custom/generated/folder").getAbsolutePath(), ((File) getFieldValue(builder, "generatedFolder")).getAbsolutePath());
builder.build().execute();
Assert.assertTrue(new File(userDir, "my/custom/generated/folder/" + IMPORTS_NAME).exists());
}
use of com.vaadin.flow.server.frontend.NodeTasks.Builder in project flow by vaadin.
the class NodeTasksTest method should_generateServiceWorkerWhenPwa.
@Test
public void should_generateServiceWorkerWhenPwa() throws Exception {
Lookup mockedLookup = mock(Lookup.class);
Mockito.doReturn(new DefaultClassFinder(this.getClass().getClassLoader())).when(mockedLookup).lookup(ClassFinder.class);
Builder builder = new Builder(mockedLookup, new File(userDir), TARGET).enablePackagesUpdate(false).enableImportsUpdate(true).runNpmInstall(false).withEmbeddableWebComponents(false);
Assert.assertEquals(new File(userDir, DEFAULT_FRONTEND_DIR).getAbsolutePath(), ((File) getFieldValue(builder, "frontendDirectory")).getAbsolutePath());
Assert.assertEquals(new File(new File(userDir, TARGET), DEFAULT_GENERATED_DIR).getAbsolutePath(), ((File) getFieldValue(builder, "generatedFolder")).getAbsolutePath());
builder.build().execute();
Assert.assertTrue(new File(userDir, Paths.get(TARGET, DEFAULT_GENERATED_DIR, IMPORTS_NAME).toString()).exists());
}
use of com.vaadin.flow.server.frontend.NodeTasks.Builder in project flow by vaadin.
the class NodeTasksTest method should_GenerateTsConfigAndTsDefinitions_When_Vaadin14BootstrapMode.
@Test
public void should_GenerateTsConfigAndTsDefinitions_When_Vaadin14BootstrapMode() throws ExecutionFailedException {
Lookup mockedLookup = mock(Lookup.class);
Mockito.doReturn(new DefaultClassFinder(this.getClass().getClassLoader())).when(mockedLookup).lookup(ClassFinder.class);
Builder builder = new Builder(mockedLookup, new File(userDir), TARGET).enablePackagesUpdate(false).useV14Bootstrap(true).enableImportsUpdate(true).runNpmInstall(false).withEmbeddableWebComponents(false).useV14Bootstrap(false);
builder.build().execute();
Assert.assertTrue(new File(userDir, "tsconfig.json").exists());
Assert.assertTrue(new File(userDir, "types.d.ts").exists());
}
use of com.vaadin.flow.server.frontend.NodeTasks.Builder in project flow by vaadin.
the class DevModeInitializer method initDevModeHandler.
/**
* Initialize the devmode server if not in production mode or compatibility
* mode.
*
* @param classes
* classes to check for npm- and js modules
* @param context
* VaadinContext we are running in
* @return the initialized dev mode handler or {@code null} if none was
* created
*
* @throws VaadinInitializerException
* if dev mode can't be initialized
*/
public static DevModeHandler initDevModeHandler(Set<Class<?>> classes, VaadinContext context) throws VaadinInitializerException {
ApplicationConfiguration config = ApplicationConfiguration.get(context);
if (config.isProductionMode()) {
log().debug("Skipping DEV MODE because PRODUCTION MODE is set.");
return null;
}
if (!config.enableDevServer()) {
log().debug("Skipping DEV MODE because dev server shouldn't be enabled.");
return null;
}
// This needs to be set as there is no "current service" available in
// this call
FeatureFlags.get(context).setPropertiesLocation(config.getJavaResourceFolder());
String baseDir = config.getStringProperty(FrontendUtils.PROJECT_BASEDIR, null);
if (baseDir == null) {
baseDir = getBaseDirectoryFallback();
}
// Initialize the usage statistics if enabled
if (config.isUsageStatisticsEnabled()) {
StatisticsStorage storage = new StatisticsStorage();
DevModeUsageStatistics.init(baseDir, storage, new StatisticsSender(storage));
}
String generatedDir = System.getProperty(PARAM_GENERATED_DIR, Paths.get(config.getBuildFolder(), DEFAULT_GENERATED_DIR).toString());
String frontendFolder = config.getStringProperty(PARAM_FRONTEND_DIR, System.getProperty(PARAM_FRONTEND_DIR, DEFAULT_FRONTEND_DIR));
File flowResourcesFolder = new File(baseDir, config.getFlowResourcesFolder());
Lookup lookupFromContext = context.getAttribute(Lookup.class);
Lookup lookupForClassFinder = Lookup.of(new DevModeClassFinder(classes), ClassFinder.class);
Lookup lookup = Lookup.compose(lookupForClassFinder, lookupFromContext);
Builder builder = new NodeTasks.Builder(lookup, new File(baseDir), new File(generatedDir), new File(frontendFolder), config.getBuildFolder());
log().info("Starting dev-mode updaters in {} folder.", builder.getNpmFolder());
if (!builder.getGeneratedFolder().exists()) {
try {
FileUtils.forceMkdir(builder.getGeneratedFolder());
} catch (IOException e) {
throw new UncheckedIOException(String.format("Failed to create directory '%s'", builder.getGeneratedFolder()), e);
}
}
File generatedPackages = new File(builder.getGeneratedFolder(), PACKAGE_JSON);
// Regenerate webpack configuration, as it may be necessary to
// update it
// TODO: make sure target directories are aligned with build
// config,
// see https://github.com/vaadin/flow/issues/9082
File target = new File(baseDir, config.getBuildFolder());
builder.withWebpack(Paths.get(target.getPath(), "classes", VAADIN_WEBAPP_RESOURCES).toFile(), Paths.get(target.getPath(), "classes", VAADIN_SERVLET_RESOURCES).toFile());
builder.useV14Bootstrap(config.useV14Bootstrap());
if (!config.useV14Bootstrap() && isEndpointServiceAvailable(lookup)) {
String connectJavaSourceFolder = config.getStringProperty(CONNECT_JAVA_SOURCE_FOLDER_TOKEN, Paths.get(baseDir, DEFAULT_CONNECT_JAVA_SOURCE_FOLDER).toString());
String connectApplicationProperties = config.getStringProperty(CONNECT_APPLICATION_PROPERTIES_TOKEN, Paths.get(baseDir, DEFAULT_CONNECT_APPLICATION_PROPERTIES).toString());
String connectOpenApiJsonFile = config.getStringProperty(CONNECT_OPEN_API_FILE_TOKEN, Paths.get(baseDir, config.getBuildFolder(), DEFAULT_CONNECT_OPENAPI_JSON_FILE).toString());
builder.withEndpointSourceFolder(new File(connectJavaSourceFolder)).withApplicationProperties(new File(connectApplicationProperties)).withEndpointGeneratedOpenAPIFile(new File(connectOpenApiJsonFile));
}
// generate those
if (!new File(builder.getNpmFolder(), PACKAGE_JSON).exists() || !generatedPackages.exists()) {
builder.createMissingPackageJson(true);
}
Set<File> frontendLocations = getFrontendLocationsFromClassloader(DevModeStartupListener.class.getClassLoader());
boolean useByteCodeScanner = config.getBooleanProperty(SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE, Boolean.parseBoolean(System.getProperty(SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE, Boolean.FALSE.toString())));
boolean enablePnpm = config.isPnpmEnabled();
boolean useGlobalPnpm = config.isGlobalPnpm();
boolean useHomeNodeExec = config.getBooleanProperty(InitParameters.REQUIRE_HOME_NODE_EXECUTABLE, false);
String[] additionalPostinstallPackages = config.getStringProperty(InitParameters.ADDITIONAL_POSTINSTALL_PACKAGES, "").split(",");
String frontendGeneratedFolder = config.getStringProperty(PROJECT_FRONTEND_GENERATED_DIR_TOKEN, Paths.get(baseDir, DEFAULT_PROJECT_FRONTEND_GENERATED_DIR).toString());
JsonObject tokenFileData = Json.createObject();
NodeTasks tasks = builder.enablePackagesUpdate(true).useByteCodeScanner(useByteCodeScanner).withFlowResourcesFolder(flowResourcesFolder).withFrontendGeneratedFolder(new File(frontendGeneratedFolder)).copyResources(frontendLocations).copyLocalResources(new File(baseDir, Constants.LOCAL_FRONTEND_RESOURCES_PATH)).enableImportsUpdate(true).runNpmInstall(true).populateTokenFileData(tokenFileData).withEmbeddableWebComponents(true).enablePnpm(enablePnpm).useGlobalPnpm(useGlobalPnpm).withHomeNodeExecRequired(useHomeNodeExec).withProductionMode(config.isProductionMode()).withPostinstallPackages(Arrays.asList(additionalPostinstallPackages)).build();
Runnable runnable = () -> runNodeTasks(context, tokenFileData, tasks);
CompletableFuture<Void> nodeTasksFuture = CompletableFuture.runAsync(runnable);
Lookup devServerLookup = Lookup.compose(lookup, Lookup.of(config, ApplicationConfiguration.class));
if (FeatureFlags.get(context).isEnabled(FeatureFlags.VITE)) {
return new ViteHandler(devServerLookup, 0, builder.getNpmFolder(), nodeTasksFuture);
} else {
return new WebpackHandler(devServerLookup, 0, builder.getNpmFolder(), nodeTasksFuture);
}
}
use of com.vaadin.flow.server.frontend.NodeTasks.Builder in project flow by vaadin.
the class NodeTasksEndpointTest method setup.
@Before
public void setup() {
userDir = temporaryFolder.getRoot().getAbsolutePath();
System.setProperty("user.dir", userDir);
System.clearProperty(PARAM_FRONTEND_DIR);
System.clearProperty(PARAM_GENERATED_DIR);
File src = new File(getClass().getClassLoader().getResource("java").getFile());
dir = new File(userDir);
File json = new File(dir, "api-file.json");
Lookup mockLookup = Mockito.mock(Lookup.class);
Mockito.doReturn(new EndpointGeneratorTaskFactoryImpl()).when(mockLookup).lookup(EndpointGeneratorTaskFactory.class);
Mockito.doReturn(new DefaultClassFinder(Collections.singleton(ConnectEndpointsForTesting.class))).when(mockLookup).lookup(ClassFinder.class);
builder = new Builder(mockLookup, dir, TARGET).enablePackagesUpdate(false).enableImportsUpdate(false).withEmbeddableWebComponents(false).withEndpointSourceFolder(src).withEndpointGeneratedOpenAPIFile(json).withFrontendGeneratedFolder(new File(dir, "api"));
}
Aggregations