Skip to content

Commit

Permalink
chore!: Remove compatibility mode from API (#16306)
Browse files Browse the repository at this point in the history
* chore: Remove compatibility mode from API

* Fix javadoc
  • Loading branch information
Artur- committed Mar 20, 2023
1 parent 1d169ad commit ddce81f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 104 deletions.
Expand Up @@ -93,8 +93,7 @@ public Set<File> generateWebComponents(File outputDirectory,
final String themeName = theme == null ? "" : theme.getName();
return WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(writerClass,
exporterRelatedClasses, outputDirectory, false,
themeName);
exporterRelatedClasses, outputDirectory, themeName);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(
"Unable to locate a required class using custom class "
Expand Down
Expand Up @@ -71,13 +71,8 @@ private static String getStringResource(String name) {
}
}

private static String getTemplate(boolean compatibilityMode) {
String templateHead;
if (compatibilityMode) {
templateHead = getStringResource(HTML_TEMPLATE);
} else {
templateHead = getStringResource(JS_TEMPLATE);
}
private static String getTemplate() {
String templateHead = getStringResource(JS_TEMPLATE);
String scriptTemplate = getStringResource(SCRIPT_TEMPLATE);
return templateHead.replace("_script_template_", scriptTemplate);
}
Expand All @@ -89,25 +84,21 @@ private static String getTemplate(boolean compatibilityMode) {
* web component exporter factory, not {@code null}
* @param frontendURI
* the frontend resources URI, not {@code null}
* @param compatibilityMode
* {@code true} to generate Polymer2 template, {@code false} to
* generate Polymer3 template
* @param themeName
* the theme defined using {@link Theme} or {@code null} if not
* defined
* @return generated web component html/JS to be served to the client
*/
public static String generateModule(
WebComponentExporterFactory<? extends Component> factory,
String frontendURI, boolean compatibilityMode, String themeName) {
String frontendURI, String themeName) {
Objects.requireNonNull(factory);
Objects.requireNonNull(frontendURI);

WebComponentConfiguration<? extends Component> config = new WebComponentExporter.WebComponentConfigurationFactory()
.create(factory.create());

return generateModule(config, frontendURI, false, compatibilityMode,
themeName);
return generateModule(config, frontendURI, false, themeName);
}

/**
Expand All @@ -117,28 +108,24 @@ public static String generateModule(
* web component class implementation, not {@code null}
* @param frontendURI
* the frontend resources URI, not {@code null}
* @param compatibilityMode
* {@code true} to generate Polymer2 template, {@code false} to
* generate Polymer3 template
* @param themeName
* the theme defined using {@link Theme} or {@code null} if not
* defined
* @return generated web component html/JS to be served to the client
*/
public static String generateModule(
WebComponentConfiguration<? extends Component> webComponentConfiguration,
String frontendURI, boolean compatibilityMode, String themeName) {
String frontendURI, String themeName) {
Objects.requireNonNull(webComponentConfiguration);
Objects.requireNonNull(frontendURI);

return generateModule(webComponentConfiguration, frontendURI, true,
compatibilityMode, themeName);
themeName);
}

private static String generateModule(
WebComponentConfiguration<? extends Component> webComponentConfiguration,
String frontendURI, boolean generateUiImport,
boolean compatibilityMode, String themeName) {
String frontendURI, boolean generateUiImport, String themeName) {
Objects.requireNonNull(webComponentConfiguration);
Objects.requireNonNull(frontendURI);

Expand All @@ -149,7 +136,7 @@ private static String generateModule(
webComponentConfiguration.getTag(), propertyDataSet,
frontendURI, generateUiImport, themeName);

String template = getTemplate(compatibilityMode);
String template = getTemplate();
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
template = template.replace("_" + replacement.getKey() + "_",
replacement.getValue());
Expand Down
Expand Up @@ -66,9 +66,6 @@ private WebComponentModulesWriter() {
* classes
* @param outputDirectory
* target directory for the generated web component module files
* @param compatibilityMode
* {@code true} to generated html modules, {@code false} to
* generate JavaScript modules
* @param themeName
* the theme defined using {@link Theme} or {@code null} if not
* defined
Expand All @@ -80,7 +77,7 @@ private WebComponentModulesWriter() {
*/
private static Set<File> writeWebComponentsToDirectory( // NOSONAR
Set<Class<?>> exporterClasses, File outputDirectory,
boolean compatibilityMode, String themeName) {
String themeName) {
// this method is used via reflection by DirectoryWriter
Objects.requireNonNull(exporterClasses,
"Parameter 'exporterClasses' must not be null");
Expand All @@ -95,7 +92,7 @@ private static Set<File> writeWebComponentsToDirectory( // NOSONAR

return WebComponentExporterUtils.getFactories(exporterClasses).stream()
.map(factory -> writeWebComponentToDirectory(factory,
outputDirectory, compatibilityMode, themeName))
outputDirectory, themeName))
.collect(Collectors.toSet());
}

Expand All @@ -114,16 +111,16 @@ private static Set<File> writeWebComponentsToDirectory( // NOSONAR
*/
private static File writeWebComponentToDirectory(
WebComponentExporterFactory<?> factory, File outputDirectory,
boolean compatibilityMode, String themeName) {
String themeName) {
String tag = getTag(factory);

String fileName = compatibilityMode ? tag + ".html" : tag + ".js";
String fileName = tag + ".js";
Path generatedFile = outputDirectory.toPath().resolve(fileName);
try {
FileUtils.forceMkdir(generatedFile.getParent().toFile());
Files.write(generatedFile,
Collections.singletonList(generateModule(factory,
compatibilityMode, themeName)),
Collections
.singletonList(generateModule(factory, themeName)),
StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UncheckedIOException(String.format(
Expand All @@ -135,9 +132,8 @@ private static File writeWebComponentToDirectory(

private static String generateModule(
WebComponentExporterFactory<? extends Component> factory,
boolean compatibilityMode, String themeName) {
return WebComponentGenerator.generateModule(factory, "../",
compatibilityMode, themeName);
String themeName) {
return WebComponentGenerator.generateModule(factory, "../", themeName);
}

private static String getTag(
Expand All @@ -158,7 +154,7 @@ public static final class DirectoryWriter implements Serializable {

/**
* Calls
* {@link #writeWebComponentsToDirectory(java.util.Set, java.io.File, boolean, java.lang.String)}
* {@link #writeWebComponentsToDirectory(java.util.Set, java.io.File, java.lang.String)}
* via reflection on the supplied {@code writer}. The {@code writer} and
* {@code exporterClasses} must be loaded with the same class loader.
*
Expand All @@ -172,9 +168,6 @@ public static final class DirectoryWriter implements Serializable {
* @param outputDirectory
* target directory for the generated web component module
* files
* @param compatibilityMode
* {@code true} to generated html modules, {@code false} to *
* generate JavaScript modules
* @param themeName
* the theme defined using {@link Theme} or {@code null} if
* not defined
Expand All @@ -191,17 +184,16 @@ public static final class DirectoryWriter implements Serializable {
* share a class loader
* @throws java.lang.IllegalStateException
* if the received {@code writer} does not have method
* {@link #writeWebComponentsToDirectory(java.util.Set, java.io.File, boolean, java.lang.String)}
* {@link #writeWebComponentsToDirectory(java.util.Set, java.io.File, java.lang.String)}
* @throws java.lang.RuntimeException
* if reflective method invocation fails
* @see #writeWebComponentsToDirectory(java.util.Set, java.io.File,
* boolean, java.lang.String)
* java.lang.String)
*/
@SuppressWarnings("unchecked")
public static Set<File> generateWebComponentsToDirectory(
Class<?> writerClass, Set<Class<?>> exporterClasses,
File outputDirectory, boolean compatibilityMode,
String themeName) {
File outputDirectory, String themeName) {
Objects.requireNonNull(writerClass,
"Parameter 'writerClassSupplier' must not null");
Objects.requireNonNull(exporterClasses,
Expand Down Expand Up @@ -248,8 +240,7 @@ public static Set<File> generateWebComponentsToDirectory(
final boolean accessible = writeMethod.isAccessible();
writeMethod.setAccessible(true);
Set<File> files = ((Set<File>) writeMethod.invoke(null,
exporterClasses, outputDirectory, compatibilityMode,
themeName));
exporterClasses, outputDirectory, themeName));
writeMethod.setAccessible(accessible);
return files;
} catch (IllegalAccessException exception) {
Expand Down
Expand Up @@ -133,29 +133,12 @@ public void assertGeneratedReplacementMapContainsExpectedEntries(
containsString("this['_response'] = 'hello'"));
}

@Test
public void providesHTMLModuleInBowerMode() {
String module = WebComponentGenerator.generateModule(
new DefaultWebComponentExporterFactory<MyComponent>(
MyComponentExporter.class),
"", true, null);
// make sure that the test works on windows machines:
module = module.replace("\r", "");
MatcherAssert.assertThat(module, startsWith(
"\n" + "<script>\n" + " class Tag extends HTMLElement {\n"));
// this part is from the
// com.vaadin.flow.webcomponent-script-template
// .js to verify successful import
MatcherAssert.assertThat(module,
containsString("customElements.define('tag', Tag);\n"));
}

@Test
public void providesJSModulesInNpmMode() {
String module = WebComponentGenerator.generateModule(
new DefaultWebComponentExporterFactory<MyComponent>(
MyComponentExporter.class),
"", false, null);
"", null);
// make sure that the test works on windows machines:
module = module.replace("\r", "");
MatcherAssert.assertThat(module,
Expand All @@ -174,10 +157,8 @@ public void providesJSModulesInNpmMode() {
@Test
public void providedJSModuleContainsCorrectThemeReplacements() {
String module = WebComponentGenerator
.generateModule(
new DefaultWebComponentExporterFactory<>(
MyComponentExporter.class),
"", false, "my-theme");
.generateModule(new DefaultWebComponentExporterFactory<>(
MyComponentExporter.class), "", "my-theme");
// make sure that the test works on windows machines:
module = module.replace("\r", "");
MatcherAssert.assertThat(module,
Expand Down
Expand Up @@ -52,7 +52,7 @@ public void directoryWriter_generateWebComponentsToDirectory_canCallMethodReflec
.generateWebComponentsToDirectory(
WebComponentModulesWriter.class,
Collections.singleton(MyExporter.class),
outputDirectory, false, null);
outputDirectory, null);

Assert.assertEquals("One file was created", 1, files.size());
Assert.assertEquals("File is js module with correct name",
Expand All @@ -65,45 +65,19 @@ public void directoryWriter_generateWebComponentsToDirectoryUsingFactory_canCall
.generateWebComponentsToDirectory(
WebComponentModulesWriter.class,
Collections.singleton(ExporterFactory.class),
outputDirectory, false, null);
outputDirectory, null);

Assert.assertEquals("One file was created", 1, files.size());
Assert.assertEquals("File is js module with correct name", "foo-bar.js",
files.stream().findFirst().get().getName());
}

@Test
public void directoryWriter_generateWebComponentsToDirectory_canCallMethodReflectively_html() {
Set<File> files = WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(
WebComponentModulesWriter.class,
Collections.singleton(MyExporter.class),
outputDirectory, true, null);

Assert.assertEquals("One file was created", 1, files.size());
Assert.assertEquals("File is js module with correct name",
"real-tag.html", files.stream().findFirst().get().getName());
}

@Test
public void directoryWriter_generateWebComponentsToDirectoryUsingFactory_canCallMethodReflectively_html() {
Set<File> files = WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(
WebComponentModulesWriter.class,
Collections.singleton(ExporterFactory.class),
outputDirectory, true, null);

Assert.assertEquals("One file was created", 1, files.size());
Assert.assertEquals("File is js module with correct name",
"foo-bar.html", files.stream().findFirst().get().getName());
}

@Test
public void directoryWriter_generateWebComponentsToDirectory_zeroExportersCreatesZeroFiles() {
Set<File> files = WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(
WebComponentModulesWriter.class, new HashSet<>(),
outputDirectory, false, null);
outputDirectory, null);

Assert.assertEquals("No files were created", 0, files.size());
}
Expand All @@ -114,32 +88,32 @@ public void directoryWriter_generateWebComponentsToDirectory_nonWriterClassThrow
// end of the exception
exception.expectMessage(
"but it is '" + MyComponent.class.getName() + "'");
Set<File> files = WebComponentModulesWriter.DirectoryWriter
WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(MyComponent.class,
new HashSet<>(), outputDirectory, false, null);
new HashSet<>(), outputDirectory, null);
}

@Test(expected = NullPointerException.class)
public void directoryWriter_generateWebComponentsToDirectory_nullWriterThrows() {
Set<File> files = WebComponentModulesWriter.DirectoryWriter
WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(null, new HashSet<>(),
outputDirectory, false, null);
outputDirectory, null);
}

@Test(expected = NullPointerException.class)
public void directoryWriter_generateWebComponentsToDirectory_nullExporterSetThrows() {
Set<File> files = WebComponentModulesWriter.DirectoryWriter
WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(
WebComponentModulesWriter.class, null, outputDirectory,
false, null);
null);
}

@Test(expected = NullPointerException.class)
public void directoryWriter_generateWebComponentsToDirectory_nullOutputDirectoryThrows() {
Set<File> files = WebComponentModulesWriter.DirectoryWriter
WebComponentModulesWriter.DirectoryWriter
.generateWebComponentsToDirectory(
WebComponentModulesWriter.class, new HashSet<>(), null,
false, null);
null);
}

/*
Expand Down

0 comments on commit ddce81f

Please sign in to comment.