Skip to content

Commit

Permalink
Propagate project directory to CodeGenContext
Browse files Browse the repository at this point in the history
Fixes #34422
  • Loading branch information
gsmet committed Jul 3, 2023
1 parent 8bcd0a9 commit bb1d0c4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
public class CodeGenContext {
private final ApplicationModel model;
private final Path projectDir;
private final Path outDir;
private final Path workDir;
private final Path inputDir;
Expand All @@ -22,16 +23,18 @@ public class CodeGenContext {
* Creates a code generation context
*
* @param model application model
* @param projectDir project directory
* @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 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, boolean redirectIO,
Config config, boolean test) {
public CodeGenContext(ApplicationModel model, Path projectDir, Path outDir, Path workDir, Path inputDir,
boolean redirectIO, Config config, boolean test) {
this.model = model;
this.projectDir = projectDir;
this.outDir = outDir;
this.workDir = workDir;
this.inputDir = inputDir;
Expand All @@ -49,6 +52,16 @@ public ApplicationModel applicationModel() {
return model;
}

/**
* Current project directory.
* The directory would typically be resolved as {@code <project.build.directory>}.
*
* @return project directory
*/
public Path projectDir() {
return projectDir;
}

/**
* Target directory for the generated output.
* The directory would typically be resolved as {@code <project.build.directory>/generated-sources/<codegen-provider-id>},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public class CodeGenerator {
"META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceProvider");

// used by Gradle and Maven
public static void initAndRun(QuarkusClassLoader classLoader,
public static void initAndRun(QuarkusClassLoader classLoader, Path projectDir,
PathCollection sourceParentDirs, Path generatedSourcesDir, Path buildDir,
Consumer<Path> sourceRegistrar, ApplicationModel appModel, Properties properties,
String launchMode, boolean test) throws CodeGenException {
final List<CodeGenData> generators = init(classLoader, sourceParentDirs, generatedSourcesDir, buildDir,
final List<CodeGenData> generators = init(classLoader, projectDir, sourceParentDirs, generatedSourcesDir, buildDir,
sourceRegistrar);
if (generators.isEmpty()) {
return;
Expand All @@ -65,6 +65,7 @@ public static void initAndRun(QuarkusClassLoader classLoader,
}

private static List<CodeGenData> init(ClassLoader deploymentClassLoader,
Path projectDir,
PathCollection sourceParentDirs,
Path generatedSourcesDir,
Path buildDir,
Expand All @@ -79,7 +80,8 @@ private static List<CodeGenData> init(ClassLoader deploymentClassLoader,
Path outputDir = codeGenOutDir(generatedSourcesDir, provider, sourceRegistrar);
for (Path sourceParentDir : sourceParentDirs) {
result.add(
new CodeGenData(provider, outputDir, sourceParentDir.resolve(provider.inputDirectory()), buildDir));
new CodeGenData(provider, projectDir, outputDir, sourceParentDir.resolve(provider.inputDirectory()),
buildDir));
}
}
return result;
Expand Down Expand Up @@ -109,7 +111,8 @@ public static List<CodeGenData> init(ClassLoader deploymentClassLoader, Collecti
codeGens = new ArrayList<>();
}
codeGens.add(
new CodeGenData(provider, outputDir, sourceParentDir.resolve(provider.inputDirectory()),
new CodeGenData(provider, Path.of(module.getProjectDirectory()), outputDir,
sourceParentDir.resolve(provider.inputDirectory()),
Path.of(module.getTargetDir())));
}

Expand Down Expand Up @@ -174,8 +177,8 @@ 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.redirectIO, config,
test));
new CodeGenContext(appModel, data.projectDir, data.outPath, data.buildDir, data.sourceDir,
data.redirectIO, config, test));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,35 @@
*/
public class CodeGenData {
public final CodeGenProvider provider;
public final Path projectDir;
public final Path outPath;
public final Path sourceDir;
public final Path buildDir;
public boolean redirectIO;

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

/**
* @param provider code gen provider
* @param projectDir the project directory
* @param outPath where the generated output should be stored
* @param sourceDir where the input sources are
* @param buildDir base project output directory
* @param redirectIO whether to redirect IO, in case a provider is logging something
*/
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir, boolean redirectIO) {
public CodeGenData(CodeGenProvider provider, Path projectDir, Path outPath, Path sourceDir, Path buildDir,
boolean redirectIO) {
this.provider = provider;
this.projectDir = projectDir;
this.outPath = outPath;
this.sourceDir = sourceDir;
this.buildDir = buildDir.normalize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void generateCode() {
params.getBaseName().set(extension().finalName());
params.getTargetDirectory().set(buildDir);
params.getAppModel().set(appModel);
params.getProjectDirectory().set(projectDir);
params
.getSourceDirectories()
.setFrom(sourcesDirectories.stream().map(Path::toFile).collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public void execute() {
Properties props = buildSystemProperties();

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

String gav = appArtifact.getGroupId() + ":" + appArtifact.getArtifactId() + ":" + appArtifact.getVersion();
LOGGER.info("Generating Quarkus code for {}", gav);
LOGGER.info(" launch mode: {}", params.getLaunchMode().get());
LOGGER.info(" base name: {}", params.getBaseName().get());
LOGGER.info(" project directory: {}", projectDir);
LOGGER.info(" generated source directory: {}", generatedSourceDir);
LOGGER.info(" build directory: {}", buildDir);

Expand All @@ -50,8 +52,8 @@ public void execute() {

Method initAndRun;
try {
initAndRun = codeGenerator.getMethod(INIT_AND_RUN, QuarkusClassLoader.class, PathCollection.class,
Path.class, Path.class,
initAndRun = codeGenerator.getMethod(INIT_AND_RUN, QuarkusClassLoader.class,
Path.class, PathCollection.class, Path.class, Path.class,
Consumer.class, ApplicationModel.class, Properties.class, String.class,
boolean.class);
} catch (Exception e) {
Expand All @@ -66,6 +68,8 @@ public void execute() {
initAndRun.invoke(null,
// QuarkusClassLoader classLoader,
deploymentClassLoader,
// Path projectDir
projectDir,
// PathCollection sourceParentDirs,
PathList.from(
params.getSourceDirectories().getFiles().stream().map(File::toPath).collect(Collectors.toList())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public interface CodeGenWorkerParams extends QuarkusParams {

DirectoryProperty getProjectDirectory();

ConfigurableFileCollection getSourceDirectories();

DirectoryProperty getOutputPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ void generateCode(PathCollection sourceParents,
Thread.currentThread().setContextClassLoader(deploymentClassLoader);

final Class<?> codeGenerator = deploymentClassLoader.loadClass("io.quarkus.deployment.CodeGenerator");
final Method initAndRun = codeGenerator.getMethod("initAndRun", QuarkusClassLoader.class, PathCollection.class,
Path.class, Path.class,
final Method initAndRun = codeGenerator.getMethod("initAndRun", QuarkusClassLoader.class,
Path.class, PathCollection.class, Path.class, Path.class,
Consumer.class, ApplicationModel.class, Properties.class, String.class,
boolean.class);
initAndRun.invoke(null, deploymentClassLoader, sourceParents,
initAndRun.invoke(null, deploymentClassLoader, baseDir().toPath(), sourceParents,
generatedSourcesDir(test), buildDir().toPath(),
sourceRegistrar, curatedApplication.getApplicationModel(), mavenProject().getProperties(),
launchMode.name(),
Expand Down

0 comments on commit bb1d0c4

Please sign in to comment.