Search in sources :

Example 11 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class BuildFrontendUtil method runNodeUpdater.

/**
 * runs the node-Updater.
 *
 * @param adapter
 *            - the PluginAdapterBase.
 * @throws ExecutionFailedException
 *             - a ExecutionFailedException.
 * @throws URISyntaxException
 *             - - Could not build an URI from nodeDownloadRoot().
 */
public static void runNodeUpdater(PluginAdapterBuild adapter) throws ExecutionFailedException, URISyntaxException {
    Set<File> jarFiles = adapter.getJarFiles();
    File flowResourcesFolder = new File(adapter.npmFolder(), Paths.get(adapter.buildFolder(), DEFAULT_FLOW_RESOURCES_FOLDER).toString());
    final URI nodeDownloadRootURI;
    nodeDownloadRootURI = adapter.nodeDownloadRoot();
    ClassFinder classFinder = adapter.getClassFinder();
    Lookup lookup = adapter.createLookup(classFinder);
    try {
        new NodeTasks.Builder(lookup, adapter.npmFolder(), adapter.generatedFolder(), adapter.frontendDirectory(), adapter.buildFolder()).runNpmInstall(adapter.runNpmInstall()).withWebpack(adapter.webpackOutputDirectory(), adapter.servletResourceOutputDirectory()).useV14Bootstrap(adapter.isUseDeprecatedV14Bootstrapping()).enablePackagesUpdate(true).useByteCodeScanner(adapter.optimizeBundle()).withFlowResourcesFolder(flowResourcesFolder).copyResources(jarFiles).copyTemplates(true).copyLocalResources(adapter.frontendResourcesDirectory()).enableImportsUpdate(true).withEmbeddableWebComponents(adapter.generateEmbeddableWebComponents()).withTokenFile(BuildFrontendUtil.getTokenFile(adapter)).enablePnpm(adapter.pnpmEnable()).useGlobalPnpm(adapter.useGlobalPnpm()).withApplicationProperties(adapter.applicationProperties()).withEndpointSourceFolder(adapter.javaSourceFolder()).withEndpointGeneratedOpenAPIFile(adapter.openApiJsonFile()).withFrontendGeneratedFolder(adapter.generatedTsFolder()).withHomeNodeExecRequired(adapter.requireHomeNodeExec()).withNodeVersion(adapter.nodeVersion()).withNodeDownloadRoot(nodeDownloadRootURI).setNodeAutoUpdate(adapter.nodeAutoUpdate()).setJavaResourceFolder(adapter.javaResourceFolder()).withPostinstallPackages(adapter.postinstallPackages()).build().execute();
    } catch (ExecutionFailedException exception) {
        throw exception;
    } catch (Throwable throwable) {
        // NOSONAR Intentionally throwable
        throw new ExecutionFailedException("Error occured during goal execution: " + throwable.getMessage() + "Please run Maven with the -e switch (or Gradle with the --stacktrace switch), to learn the full stack trace.", throwable);
    }
}
Also used : ExecutionFailedException(com.vaadin.flow.server.ExecutionFailedException) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) ReflectionsClassFinder(com.vaadin.flow.server.scanner.ReflectionsClassFinder) Lookup(com.vaadin.flow.di.Lookup) File(java.io.File) URI(java.net.URI)

Example 12 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class EndpointsValidator method process.

@Override
public void process(Set<Class<?>> classSet, ServletContext servletContext) throws ServletException {
    if (classSet == null) {
        // an endpoint and dependencies are not added to the project.
        return;
    }
    ClassFinder finder = new DefaultClassFinder(classSet);
    Set<Class<?>> endpoints = finder.getAnnotatedClasses(Endpoint.class);
    if (!endpoints.isEmpty()) {
        try {
            finder.loadClass(classToCheck);
        } catch (ClassNotFoundException e) {
            throw new ServletException("ERROR: Vaadin endpoints only work for Spring " + "enabled projects.\n" + "This is not a spring application but there are Vaadin endpoints in these classes: " + endpoints.stream().map(Class::getName).collect(Collectors.joining("\n    - ")), e);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder)

Example 13 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class TaskRunPnpmInstallTest method getGeneratedVersionsContent.

private JsonObject getGeneratedVersionsContent(File versions) throws IOException {
    ClassFinder classFinder = getClassFinder();
    Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON)).thenReturn(versions.toURI().toURL());
    String path = getNodeUpdater().generateVersionsJson();
    File generatedVersionsFile = new File(getNodeUpdater().npmFolder, path);
    return Json.parse(FileUtils.readFileToString(generatedVersionsFile, StandardCharsets.UTF_8));
}
Also used : ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) File(java.io.File)

Example 14 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class TaskRunPnpmInstallTest method runPnpmInstall_versionsJsonIsFound_pnpmHookFileIsGenerated.

@Test
public void runPnpmInstall_versionsJsonIsFound_pnpmHookFileIsGenerated() throws IOException, ExecutionFailedException {
    ClassFinder classFinder = getClassFinder();
    File versions = temporaryFolder.newFile();
    FileUtils.write(versions, "{}", StandardCharsets.UTF_8);
    Mockito.when(classFinder.getResource(Constants.VAADIN_VERSIONS_JSON)).thenReturn(versions.toURI().toURL());
    TaskRunNpmInstall task = createTask();
    getNodeUpdater().modified = true;
    getNodeUpdater().versionsPath = "./versions.json";
    task.execute();
    File file = new File(getNodeUpdater().npmFolder, "pnpmfile.js");
    Assert.assertTrue(file.exists());
    String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    MatcherAssert.assertThat(content, CoreMatchers.containsString("JSON.parse(fs.readFileSync"));
}
Also used : ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) File(java.io.File) Test(org.junit.Test)

Example 15 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class NodeUpdateImportsTest method noFallBackScanner_fallbackIsNotImportedEvenIfTheFileExists.

@Test
public void noFallBackScanner_fallbackIsNotImportedEvenIfTheFileExists() throws Exception {
    Stream<Class<?>> classes = Stream.concat(Stream.of(NodeTestComponents.class.getDeclaredClasses()), Stream.of(ExtraNodeTestComponents.class.getDeclaredClasses()));
    ClassFinder classFinder = new DefaultClassFinder(new URLClassLoader(getClassPath()), classes.toArray(Class<?>[]::new));
    // create fallback imports file:
    // it is present after generated but the user is now running
    // everything without fallback. The file should not be included into
    // the imports
    fallBackImportsFile.mkdirs();
    fallBackImportsFile.createNewFile();
    Assert.assertTrue(fallBackImportsFile.exists());
    updater = new TaskUpdateImports(classFinder, new FrontendDependenciesScannerFactory().createScanner(false, classFinder, true), finder -> null, tmpRoot, generatedPath, frontendDirectory, tokenFile, null, false, TARGET, true, false, Mockito.mock(FeatureFlags.class)) {

        @Override
        Logger log() {
            return logger;
        }
    };
    updater.execute();
    assertTrue(importsFile.exists());
    String mainContent = FileUtils.readFileToString(importsFile, Charset.defaultCharset());
    // fallback file is not imported in generated-flow-imports
    MatcherAssert.assertThat(mainContent, CoreMatchers.not(CoreMatchers.containsString(FrontendUtils.FALLBACK_IMPORTS_NAME)));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DEFAULT_FRONTEND_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) DEFAULT_GENERATED_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR) IMPORTS_D_TS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_D_TS_NAME) HashSet(java.util.HashSet) URLClassLoader(java.net.URLClassLoader) Charset(java.nio.charset.Charset) FrontendDependenciesScannerFactory(com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner.FrontendDependenciesScannerFactory) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) Logger(org.slf4j.Logger) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) NODE_MODULES(com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) TARGET(com.vaadin.flow.server.Constants.TARGET) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) IMPORTS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) MatcherAssert(org.hamcrest.MatcherAssert) Rule(org.junit.Rule) Paths(java.nio.file.Paths) FeatureFlags(com.vaadin.experimental.FeatureFlags) JsonObject(elemental.json.JsonObject) FLOW_NPM_PACKAGE_NAME(com.vaadin.flow.server.frontend.FrontendUtils.FLOW_NPM_PACKAGE_NAME) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) FrontendDependenciesScannerFactory(com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner.FrontendDependenciesScannerFactory) URLClassLoader(java.net.URLClassLoader) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) Logger(org.slf4j.Logger) Test(org.junit.Test)

Aggregations

ClassFinder (com.vaadin.flow.server.frontend.scanner.ClassFinder)23 File (java.io.File)20 Test (org.junit.Test)14 FeatureFlags (com.vaadin.experimental.FeatureFlags)9 Before (org.junit.Before)9 Lookup (com.vaadin.flow.di.Lookup)8 DefaultClassFinder (com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder)8 IOException (java.io.IOException)8 TARGET (com.vaadin.flow.server.Constants.TARGET)7 DEFAULT_FRONTEND_DIR (com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR)7 DEFAULT_GENERATED_DIR (com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR)7 IMPORTS_NAME (com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME)7 NODE_MODULES (com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES)7 Charset (java.nio.charset.Charset)7 Paths (java.nio.file.Paths)7 Set (java.util.Set)7 FileUtils (org.apache.commons.io.FileUtils)7 Assert (org.junit.Assert)7 Rule (org.junit.Rule)7 ExpectedException (org.junit.rules.ExpectedException)7