Skip to content

Commit

Permalink
Merge pull request #37081 from andreaTP/codegen-multiple-input-exts
Browse files Browse the repository at this point in the history
Add backward compatible support for multiple extensions in CodeGenProvider
  • Loading branch information
cescoffier authored Nov 19, 2023
2 parents ac3dc4c + 8af488d commit 8ca97f0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,27 @@ public interface CodeGenProvider {

/**
* File extension that CodeGenProvider will generate code from
* Deprecated: use inputExtensions instead
*
* @return file extension
*/
@Deprecated
default String inputExtension() {
return null;
}

/**
* File extensions that CodeGenProvider will generate code from
*
* @return file extensions
*/
@NotNull
String inputExtension();
default String[] inputExtensions() {
if (inputExtension() != null) {
return new String[] { inputExtension() };
}
return new String[] {};
}

/**
* Name of the directory containing input files for a given {@link CodeGenProvider} implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ class CodeGenWatcher {
final Config config = CodeGenerator.getConfig(curatedApplication.getApplicationModel(), LaunchMode.DEVELOPMENT,
properties, deploymentClassLoader);
for (CodeGenData codeGen : codeGens) {
watchers.add(new FSWatchUtil.Watcher(codeGen.sourceDir, codeGen.provider.inputExtension(),
modifiedPaths -> {
codeGenLock.lock();
try {
CodeGenerator.trigger(deploymentClassLoader,
codeGen,
curatedApplication.getApplicationModel(), config, false);
} catch (Exception any) {
log.warn("Code generation failed", any);
} finally {
codeGenLock.unlock();
}
}));
for (String ext : codeGen.provider.inputExtensions()) {
watchers.add(new FSWatchUtil.Watcher(codeGen.sourceDir, ext,
modifiedPaths -> {
codeGenLock.lock();
try {
CodeGenerator.trigger(deploymentClassLoader,
codeGen,
curatedApplication.getApplicationModel(), config, false);
} catch (Exception any) {
log.warn("Code generation failed", any);
} finally {
codeGenLock.unlock();
}
}));
}
}
fsWatchUtil = new FSWatchUtil();
fsWatchUtil.observe(watchers, 500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.avro.generic.GenericData;
import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -86,7 +88,8 @@ private Collection<Path> gatherAllFiles(Path importPath) throws CodeGenException
}
try {
return Files.find(importPath, 20,
(path, ignored) -> Files.isRegularFile(path) && path.toString().endsWith("." + inputExtension()))
(path, ignored) -> Files.isRegularFile(path)
&& Arrays.stream(inputExtensions()).anyMatch(ext -> path.toString().endsWith("." + ext)))
.map(Path::toAbsolutePath)
.collect(Collectors.toList());
} catch (IOException e) {
Expand Down Expand Up @@ -194,9 +197,11 @@ private boolean getBooleanProperty(String propName, boolean defaultValue) {
}

public String[] getImports(Config config) {
return config.getOptionalValue("avro.codegen." + inputExtension() + ".imports", String.class)
.map(i -> i.split(","))
.orElse(EMPTY);
return Arrays.stream(inputExtensions())
.flatMap(ext -> config.getOptionalValue("avro.codegen." + ext + ".imports", String.class)
.map(i -> Arrays.stream(i.split(","))).stream())
.reduce(Stream.empty(), Stream::concat)
.toArray(String[]::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "avdl";
public String[] inputExtensions() {
return new String[] { "avdl" };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "avpr";
public String[] inputExtensions() {
return new String[] { "avpr" };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "avsc";
public String[] inputExtensions() {
return new String[] { "avsc" };
}

void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "proto";
public String[] inputExtensions() {
return new String[] { "proto" };
}

@Override
Expand Down

0 comments on commit 8ca97f0

Please sign in to comment.