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

Propagate project directory to CodeGenContext #34489

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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.basedir>}.
*
* @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 {
Copy link
Member

@aloubyansky aloubyansky Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, you have an ApplicationModel here, you could do

var appModule = appModel.getApplicationModule();
Path projectDir = appModule == null ? Path.of("") : appModule.getModuleDir().toPath();

appModule might not be available in case we are re-augmenting from a JAR w/o sources.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aloubyansky not sure this will help with my original issue quarkiverse/quarkus-cxf#907 where maven is run with -f my-module/pom.xml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will be the project dir as Quarkus sees it.

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like basedir is passed, so perhaps only the javadoc above needs to get fixed?

generatedSourcesDir(test), buildDir().toPath(),
sourceRegistrar, curatedApplication.getApplicationModel(), mavenProject().getProperties(),
launchMode.name(),
Expand Down