Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java-generator] Introduce an option to filter files #5287

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Improvements
* Fix #5166: Remove opinionated messages from Config's `errorMessages` and deprecate it
* Fix #5233: Generalized SchemaSwap to allow for cycle expansion
* Fix #5287: Add an option to filter the files processed by the java-generator, based on a suffix allowlist

#### Dependency Upgrade

Expand Down
6 changes: 6 additions & 0 deletions doc/java-generation-from-CRD.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Usage: java-gen [-hV] [-add-extra-annotations] [-enum-uppercase]
crds
-enum-uppercase, --enum-uppercase
Uppercase the enum values
-files-suffixes, --files-suffixes=<filesSuffixes>
Filter the source files with the specific suffixes
-h, --help Show this help message and exit.
-package-overrides, --package-overrides=<String=String>
Apply the overrides to the package names
Expand Down Expand Up @@ -118,6 +120,10 @@ And the corresponding configurations of the Maven plugin are (output of `mvn hel
User property: fabric8.java-generator.extra-annotations
Generate Extra annotation for lombok and sundrio integration

filesSuffixes
User property: fabric8.java-generator.files-suffixes
Files suffixes to be processed

generatedAnnotations
User property: fabric8.java-generator.generated-annotations
*advanced* Emit the @javax.annotation.processing.Generated annotation on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ public class GenerateJavaSources implements Runnable {
"--package-overrides" }, description = "Apply the overrides to the package names", required = false)
Map<String, String> packageOverrides = null;

@Option(names = { "-files-suffixes",
"--files-suffixes" }, description = "Filter the source files with the specific suffixes", required = false)
List<String> filesSuffixes = null;

@Override
public void run() {
final Boolean noGeneratedAnnotations = (skipGeneratedAnnotations != null) ? skipGeneratedAnnotations : false;
final Config config = new Config(
uppercaseEnum,
addExtraAnnotations,
!noGeneratedAnnotations,
packageOverrides);
packageOverrides,
filesSuffixes);

List<JavaGenerator> runners = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import lombok.Builder;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Builder(toBuilder = true)
Expand All @@ -28,11 +30,13 @@ public class Config {
private static final boolean DEFAULT_ADD_EXTRA_ANNOTATIONS = false;
private static final boolean DEFAULT_ADD_GENERATED_ANNOTATIONS = true;
private static final Map<String, String> DEFAULT_PACKAGE_OVERRIDES = new HashMap<>();
private static final List<String> DEFAULT_FILES_SUFFIXES = Arrays.asList(".yaml", ".yml", ".json");

private Boolean uppercaseEnums = DEFAULT_UPPERCASE_ENUM;
private Boolean objectExtraAnnotations = DEFAULT_ADD_EXTRA_ANNOTATIONS;
private Boolean generatedAnnotations = DEFAULT_ADD_GENERATED_ANNOTATIONS;
private Map<String, String> packageOverrides = DEFAULT_PACKAGE_OVERRIDES;
private List<String> filesSuffixes = DEFAULT_FILES_SUFFIXES;

public Config(
Boolean uppercaseEnums,
Expand All @@ -53,6 +57,29 @@ public Config(
}
}

public Config(
Boolean uppercaseEnums,
Boolean objectExtraAnnotations,
Boolean generatedAnnotations,
Map<String, String> packageOverrides,
List<String> filesSuffixes) {
if (uppercaseEnums != null) {
this.uppercaseEnums = uppercaseEnums;
}
if (objectExtraAnnotations != null) {
this.objectExtraAnnotations = objectExtraAnnotations;
}
if (generatedAnnotations != null) {
this.generatedAnnotations = generatedAnnotations;
}
if (packageOverrides != null) {
this.packageOverrides = packageOverrides;
}
if (filesSuffixes != null) {
this.filesSuffixes = filesSuffixes;
}
}

public boolean isUppercaseEnums() {
return (uppercaseEnums == null) ? DEFAULT_UPPERCASE_ENUM : uppercaseEnums;
}
Expand All @@ -70,8 +97,14 @@ public boolean isGeneratedAnnotations() {
}

public Map<String, String> getPackageOverrides() {
return (packageOverrides == null)
return (packageOverrides == null || packageOverrides.isEmpty())
? DEFAULT_PACKAGE_OVERRIDES
: packageOverrides;
}

public List<String> getFilesSuffixes() {
return (filesSuffixes == null || filesSuffixes.isEmpty())
? DEFAULT_FILES_SUFFIXES
: filesSuffixes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public class FileJavaGenerator implements JavaGenerator {

private static final Logger LOGGER = LoggerFactory.getLogger(FileJavaGenerator.class);

private final Config config;
private final File source;
private final CRGeneratorRunner crGeneratorRunner;

public FileJavaGenerator(Config config, File source) {
crGeneratorRunner = new CRGeneratorRunner(config);
this.config = config;
this.source = source;
}

Expand All @@ -64,6 +66,7 @@ public void run(File outputDirectory) {
walk
.map(Path::toFile)
.filter(f -> !f.getAbsolutePath().equals(source.getAbsolutePath()) && f.isFile())
.filter(f -> config.getFilesSuffixes().stream().anyMatch(suffix -> f.getName().endsWith(suffix)))
.forEach(f -> runOnSingleSource(f, outputDirectory));
} catch (IOException e) {
throw new JavaGeneratorException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public void setEnumUppercase(final Boolean isEnumUppercase) {
javaGeneratorConfig = new Config(isEnumUppercase,
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.getPackageOverrides());
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes());
}

/**
Expand All @@ -115,7 +116,8 @@ public void setExtraAnnotations(final Boolean isExtraAnnotations) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
isExtraAnnotations,
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.getPackageOverrides());
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes());
}

/**
Expand All @@ -130,7 +132,8 @@ public void setGeneratedAnnotations(final Boolean isGeneratedAnnotations) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
isGeneratedAnnotations,
javaGeneratorConfig.getPackageOverrides());
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes());
}

/**
Expand All @@ -145,6 +148,23 @@ public void setPackageOverrides(final Map<String, String> packageOverrides) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
packageOverrides);
packageOverrides,
javaGeneratorConfig.getFilesSuffixes());
}

/**
* Files suffixes to be processed
*
*/
public List<String> getFilesSuffixes() {
return javaGeneratorConfig.getFilesSuffixes();
}

public void setPackageOverrides(final List<String> filesSuffixes) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.getPackageOverrides(),
filesSuffixes);
}
}
17 changes: 17 additions & 0 deletions java-generator/it/src/it/skip-files-suffixes/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

invoker.goals.1=package
87 changes: 87 additions & 0 deletions java-generator/it/src/it/skip-files-suffixes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (C) 2015 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project>

<modelVersion>4.0.0</modelVersion>

<artifactId>cert-manager-extension</artifactId>
<groupId>io.fabric8.it</groupId>
<version>0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>@maven.compiler.source@</maven.compiler.source>
<maven.compiler.target>@maven.compiler.target@</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>sundr-adapter-reflect</artifactId>
<version>@sundrio.version@</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
<version>@sundrio.version@</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>@lombok.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>generator-annotations</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<source>src/main/resources/</source>
<extraAnnotations>true</extraAnnotations>
<generatedAnnotations>false</generatedAnnotations>
<filesSuffixes>.json</filesSuffixes>
</configuration>
</plugin>
</plugins>
</build>

</project>
Loading