Skip to content

Commit

Permalink
Change generated dir to be relative to workDir and not projectDir
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea committed Nov 9, 2023
1 parent 4e25fa5 commit 14cd1e1
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class CodeGenContext {
private final Path outDir;
private final Path workDir;
private final Path inputDir;
private final Path projectDir;
private final boolean redirectIO;
private final Config config;
private final boolean test;
Expand All @@ -26,18 +25,16 @@ public class CodeGenContext {
* @param outDir target directory for the generated output
* @param workDir working directory, typically the main build directory of the project
* @param inputDir directory containing input content for a code generator
* @param projectDir the base project directory
* @param redirectIO whether the code generating process should redirect its IO
* @param config application build time configuration
* @param test indicates whether the code generation is being triggered for tests
*/
public CodeGenContext(ApplicationModel model, Path outDir, Path workDir, Path inputDir, Path projectDir, boolean redirectIO,
public CodeGenContext(ApplicationModel model, Path outDir, Path workDir, Path inputDir, boolean redirectIO,
Config config, boolean test) {
this.model = model;
this.outDir = outDir;
this.workDir = workDir;
this.inputDir = inputDir;
this.projectDir = projectDir;
this.redirectIO = redirectIO;
this.config = config;
this.test = test;
Expand Down Expand Up @@ -65,15 +62,6 @@ public Path outDir() {
return outDir;
}

/**
* The base project directory
*
* @return The base project directory
*/
public Path projectDir() {
return projectDir;
}

/**
* Working directory, typically the main build directory of the project.
* For a typical Maven project it would be the {@code target} directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ public class CodeGenerator {
// used by Gradle and Maven
public static void initAndRun(QuarkusClassLoader classLoader,
PathCollection sourceParentDirs, Path generatedSourcesDir, Path buildDir,
Path projectDir, Consumer<Path> sourceRegistrar, ApplicationModel appModel, Properties properties,
Consumer<Path> sourceRegistrar, ApplicationModel appModel, Properties properties,
String launchMode, boolean test) throws CodeGenException {

Map<String, String> props = new HashMap<>();
properties.entrySet().stream().forEach(e -> props.put((String) e.getKey(), (String) e.getValue()));
final List<CodeGenData> generators = init(appModel, props, classLoader, sourceParentDirs, generatedSourcesDir, buildDir,
projectDir, sourceRegistrar);
sourceRegistrar);
if (generators.isEmpty()) {
return;
}
Expand All @@ -87,7 +86,6 @@ private static List<CodeGenData> init(
PathCollection sourceParentDirs,
Path generatedSourcesDir,
Path buildDir,
Path projectDir,
Consumer<Path> sourceRegistrar) throws CodeGenException {
return callWithClassloader(deploymentClassLoader, () -> {
final List<CodeGenProvider> codeGenProviders = loadCodeGenProviders(deploymentClassLoader);
Expand All @@ -104,7 +102,7 @@ private static List<CodeGenData> init(
in = sourceParentDir.resolve(provider.inputDirectory());
}
result.add(
new CodeGenData(provider, outputDir, in, buildDir, projectDir));
new CodeGenData(provider, outputDir, in, buildDir));
}
}
return result;
Expand Down Expand Up @@ -140,7 +138,7 @@ public static List<CodeGenData> init(ApplicationModel model, Map<String, String>
}
codeGens.add(
new CodeGenData(provider, outputDir, in,
Path.of(module.getTargetDir()), Path.of(module.getProjectDirectory())));
Path.of(module.getTargetDir())));
}

}
Expand Down Expand Up @@ -204,8 +202,7 @@ public static boolean trigger(ClassLoader deploymentClassLoader,
CodeGenProvider provider = data.provider;
return provider.shouldRun(data.sourceDir, config)
&& provider.trigger(
new CodeGenContext(appModel, data.outPath, data.buildDir, data.sourceDir, data.projectDir,
data.redirectIO, config,
new CodeGenContext(appModel, data.outPath, data.buildDir, data.sourceDir, data.redirectIO, config,
test));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,30 @@ public class CodeGenData {
public final Path outPath;
public final Path sourceDir;
public final Path buildDir;
public final Path projectDir;
public boolean redirectIO;

/**
* @param provider code gen provider
* @param outPath where the generated output should be stored
* @param sourceDir where the input sources are
* @param buildDir base project output directory
* @param projectDir project root directory
*/
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir, Path projectDir) {
this(provider, outPath, sourceDir, buildDir, projectDir, true);
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir) {
this(provider, outPath, sourceDir, buildDir, true);
}

/**
* @param provider code gen provider
* @param outPath where the generated output should be stored
* @param sourceDir where the input sources are
* @param buildDir base project output directory
* @param projectDir project root directory
* @param redirectIO whether to redirect IO, in case a provider is logging something
*/
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir, Path projectDir,
boolean redirectIO) {
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir, boolean redirectIO) {
this.provider = provider;
this.outPath = outPath;
this.sourceDir = sourceDir;
this.buildDir = buildDir.normalize();
this.projectDir = projectDir.normalize();
this.redirectIO = redirectIO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public void generateCode() {
params.getBuildSystemProperties().putAll(configMap);
params.getBaseName().set(extension().finalName());
params.getTargetDirectory().set(buildDir);
params.getProjectDirectory().set(projectDir);
params.getAppModel().set(appModel);
params
.getSourceDirectories()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void execute() {

ResolvedDependency appArtifact = params.getAppModel().get().getAppArtifact();
Path buildDir = params.getTargetDirectory().getAsFile().get().toPath();
Path projectDir = params.getProjectDirectory().getAsFile().get().toPath();
Path generatedSourceDir = params.getOutputPath().get().getAsFile().toPath();

String gav = appArtifact.getGroupId() + ":" + appArtifact.getArtifactId() + ":" + appArtifact.getVersion();
Expand All @@ -43,7 +42,6 @@ public void execute() {
LOGGER.info(" base name: {}", params.getBaseName().get());
LOGGER.info(" generated source directory: {}", generatedSourceDir);
LOGGER.info(" build directory: {}", buildDir);
LOGGER.info(" project directory: {}", projectDir);

try (CuratedApplication appCreationContext = createAppCreationContext()) {

Expand All @@ -53,7 +51,7 @@ public void execute() {
Method initAndRun;
try {
initAndRun = codeGenerator.getMethod(INIT_AND_RUN, QuarkusClassLoader.class, PathCollection.class,
Path.class, Path.class, Path.class,
Path.class, Path.class,
Consumer.class, ApplicationModel.class, Properties.class, String.class,
boolean.class);
} catch (Exception e) {
Expand All @@ -75,8 +73,6 @@ public void execute() {
generatedSourceDir,
// Path buildDir,
buildDir,
// Path projectDir
projectDir,
// Consumer<Path> sourceRegistrar,
sourceRegistrar,
// ApplicationModel appModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ public interface CodeGenWorkerParams extends QuarkusParams {
DirectoryProperty getOutputPath();

Property<LaunchMode> getLaunchMode();

DirectoryProperty getProjectDirectory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ void generateCode(PathCollection sourceParents,

final Class<?> codeGenerator = deploymentClassLoader.loadClass("io.quarkus.deployment.CodeGenerator");
final Method initAndRun = codeGenerator.getMethod("initAndRun", QuarkusClassLoader.class, PathCollection.class,
Path.class, Path.class, Path.class,
Path.class, Path.class,
Consumer.class, ApplicationModel.class, Properties.class, String.class,
boolean.class);
initAndRun.invoke(null, deploymentClassLoader, sourceParents,
generatedSourcesDir(test), buildDir().toPath(), baseDir().toPath(),
generatedSourcesDir(test), buildDir().toPath(),
sourceRegistrar, curatedApplication.getApplicationModel(), getBuildSystemProperties(false),
launchMode.name(),
test);
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/grpc-generation-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ By default, Quarkus does not generate these descriptors. Quarkus does provide se
* `quarkus.generate-code.grpc.descriptor-set.generate`
** Set to `true` to enable generation
* `quarkus.generate-code.grpc.descriptor-set.output-dir`
** Set this to a value relative to the project root directory
** Set this to a value relative to the project's build directory (i.e. `target` for Maven, `build` for Gradle)
** Maven default value: `target/generated-sources/grpc`
** Gradle default value: `$buildDir/classes/java/quarkus-generated-sources/grpc`
* `quarkus.generate-code.grpc.descriptor-set.name`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private boolean shouldGenerateDescriptorSet(Config config) {

private Path getDescriptorSetOutputFile(CodeGenContext context) throws IOException {
var dscOutputDir = context.config().getOptionalValue(DESCRIPTOR_SET_OUTPUT_DIR, String.class)
.map(context.projectDir()::resolve)
.map(context.workDir()::resolve)
.orElseGet(context::outDir);

if (Files.notExists(dscOutputDir)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
quarkus.generate-code.grpc.descriptor-set.generate=true
quarkus.generate-code.grpc.descriptor-set.name=hello.dsc
quarkus.generate-code.grpc.descriptor-set.output-dir=build/resources
quarkus.generate-code.grpc.descriptor-set.output-dir=proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void testGrpcDescriptorSetAlternateOutputDir() throws Exception {

var expectedOutputDir = projectDir.toPath()
.resolve("build")
.resolve("resources");
.resolve("proto");

assertThat(expectedOutputDir).exists();
assertThat(expectedOutputDir.resolve("hello.dsc"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
quarkus.generate-code.grpc.descriptor-set.generate=true
quarkus.generate-code.grpc.descriptor-set.name=hello.dsc
quarkus.generate-code.grpc.descriptor-set.output-dir=target/proto
quarkus.generate-code.grpc.descriptor-set.output-dir=proto

quarkus.grpc.server.port=9001

Expand Down

0 comments on commit 14cd1e1

Please sign in to comment.