Skip to content

Commit

Permalink
Add rewrite HtmlImports step (#6109)
Browse files Browse the repository at this point in the history
* Rename the goal

* Flatten frontend directory

* Correct the logic to search for themed files inside node_modules

* Add IT test for client side imported components theming

* Revert "Add IT test for client side imported components theming"

This reverts commit 0cfb723.

* Revert "Correct the logic to search for themed files inside node_modules"

This reverts commit 8e65792.

* Rewrite HtmlImports to JsModules

Fixes #6092

* Rewrite the content of the file even if it doesn't match the declaration
pattern.

* Add parameter for html import rewrite strategy

* Add unit tests for rewrite HtmlImports step.

* Fix code review comments

* Add javadocs and fix minor things in code

* Apply better replacement suggestion by code review
  • Loading branch information
Denis authored and Johannes Eriksson committed Jul 26, 2019
1 parent 233b24d commit 4014d19
Show file tree
Hide file tree
Showing 12 changed files with 811 additions and 11 deletions.
26 changes: 25 additions & 1 deletion flow-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand Down Expand Up @@ -108,5 +109,28 @@
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/test/java</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

import com.vaadin.flow.component.dependency.HtmlImport;
import com.vaadin.flow.plugin.common.FlowPluginFrontendUtils;
import com.vaadin.flow.plugin.migration.CopyMigratedResourcesStep;
import com.vaadin.flow.plugin.migration.CopyResourcesStep;
import com.vaadin.flow.plugin.migration.CreateMigrationJsonsStep;
import com.vaadin.flow.plugin.migration.RewriteHtmlImportsStep;
import com.vaadin.flow.server.frontend.FrontendUtils;

import elemental.json.Json;
Expand All @@ -54,11 +58,19 @@
* @author Vaadin Ltd
*
*/
@Mojo(name = "migrate", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
@Mojo(name = "migrate-to-p3", requiresDependencyResolution = ResolutionScope.COMPILE, defaultPhase = LifecyclePhase.PROCESS_CLASSES)
public class MigrateMojo extends AbstractMojo {

private static final String DEPENDENCIES = "dependencies";

/**
* The strategy to rewrite {@link HtmlImport} annotations.
*
*/
public static enum HtmlImportsRewriteStrategy {
ALWAYS, SKIP, SKIP_ON_ERROR;
}

/**
* A list of directories with files to migrate.
*/
Expand Down Expand Up @@ -96,6 +108,24 @@ public class MigrateMojo extends AbstractMojo {
@Parameter(defaultValue = "true")
private boolean ignoreModulizerErrors;

/**
* Allows to specify the strategy to use to rewrite {@link HtmlImport}
* annotations in Java files.
* <p>
* Three values are available:
* <ul>
* <li>ALWAYS : if chosen then {@link HtmlImport} will be always rewritten
* regardless of migration of the import files content
* <li>SKIP : if chosen then neither {@link HtmlImport} annotation will be
* rewritten
* <li>SKIP_ON_ERROR : if chosen then {@link HtmlImport} annotations will be
* rewritten only if there are no errors during migration of imported files
* content
* </ul>
*/
@Parameter(defaultValue = "ALWAYS")
private HtmlImportsRewriteStrategy htmlImportsRewrite;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
prepareMigrationDirectory();
Expand Down Expand Up @@ -195,6 +225,20 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (!modulizerHasErrors && !keepOriginal) {
removeOriginalResources(paths);
}

switch (htmlImportsRewrite) {
case SKIP:
break;
case ALWAYS:
rewrite();
break;
case SKIP_ON_ERROR:
if (!modulizerHasErrors) {
rewrite();
}
break;
}

}

private void prepareMigrationDirectory() {
Expand Down Expand Up @@ -371,14 +415,18 @@ private void cleanUp(File dir) throws IOException {
private String[] getResources() {
if (resources == null) {
File webApp = new File(project.getBasedir(), "src/main/webapp");
File frontend = new File(webApp, "frontend");
if (frontend.exists() && webApp.listFiles().length == 1) {
resources = new String[] { frontend.getPath() };
} else {
resources = new String[] { webApp.getPath() };
}
resources = new String[] { webApp.getPath() };
}
return resources;
}

private void rewrite() {
RewriteHtmlImportsStep step = new RewriteHtmlImportsStep(
new File(project.getBuild().getOutputDirectory()),
FlowPluginFrontendUtils.getClassFinder(project),
project.getCompileSourceRoots().stream().map(File::new)
.collect(Collectors.toList()));
step.rewrite();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ List<String> getVisitedPaths() {

private Path getTarget(Path source) {
Path relativePath = sourceRoot.relativize(source);
String topLevel = relativePath.getName(0).toString();
if ("frontend".equals(topLevel)) {
if (relativePath.getNameCount() == 1) {
return targetRoot;
}
relativePath = relativePath.subpath(1,
relativePath.getNameCount());
}
return targetRoot.resolve(relativePath);
}
}
Expand Down
Loading

0 comments on commit 4014d19

Please sign in to comment.