Skip to content

Commit

Permalink
Merge pull request #17184 from essobedo/multiple-resources-dirs
Browse files Browse the repository at this point in the history
Add support of several resource and test resource folders in dev mode
  • Loading branch information
aloubyansky authored May 20, 2021
2 parents 6fd054d + 15145b7 commit bce6a9e
Show file tree
Hide file tree
Showing 39 changed files with 1,126 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Consumer;

import io.quarkus.bootstrap.model.AppModel;
import io.quarkus.bootstrap.model.PathsCollection;
import io.quarkus.bootstrap.prebuild.CodeGenException;
import io.quarkus.deployment.codegen.CodeGenData;

Expand All @@ -22,7 +22,7 @@ public class CodeGenerator {
// used by Gradle
@SuppressWarnings("unused")
public static void initAndRun(ClassLoader classLoader,
Set<Path> sourceParentDirs, Path generatedSourcesDir, Path buildDir,
PathsCollection sourceParentDirs, Path generatedSourcesDir, Path buildDir,
Consumer<Path> sourceRegistrar,
AppModel appModel, Map<String, String> properties) throws CodeGenException {
List<CodeGenData> generators = init(classLoader, sourceParentDirs, generatedSourcesDir, buildDir, sourceRegistrar);
Expand All @@ -33,7 +33,7 @@ public static void initAndRun(ClassLoader classLoader,
}

public static List<CodeGenData> init(ClassLoader deploymentClassLoader,
Set<Path> sourceParentDirs,
PathsCollection sourceParentDirs,
Path generatedSourcesDir,
Path buildDir,
Consumer<Path> sourceRegistrar) throws CodeGenException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.List;
import java.util.Set;

import io.quarkus.bootstrap.model.PathsCollection;

public interface CompilationProvider extends Closeable {

Set<String> handledExtensions();
Expand All @@ -21,7 +23,7 @@ default Set<String> handledSourcePaths() {

void compile(Set<File> files, Context context);

Path getSourcePath(Path classFilePath, Set<String> sourcePaths, String classesPath);
Path getSourcePath(Path classFilePath, PathsCollection sourcePaths, String classesPath);

@Override
default void close() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.bootstrap.model.PathsCollection;

/**
* Object that is used to pass context data from the plugin doing the invocation
Expand All @@ -26,7 +26,7 @@
*/
public class DevModeContext implements Serializable {

public static final CompilationUnit EMPTY_COMPILATION_UNIT = new CompilationUnit(Collections.emptySet(), null, null, null);
public static final CompilationUnit EMPTY_COMPILATION_UNIT = new CompilationUnit(PathsCollection.of(), null, null, null);

public static final String ENABLE_PREVIEW_FLAG = "--enable-preview";

Expand Down Expand Up @@ -235,18 +235,19 @@ public static class ModuleInfo implements Serializable {
private final CompilationUnit test;

private final String preBuildOutputDir;
private final Set<String> sourceParents;
private final PathsCollection sourceParents;
private final String targetDir;

ModuleInfo(Builder builder) {
this.appArtifactKey = builder.appArtifactKey;
this.name = builder.name;
this.projectDirectory = builder.projectDirectory;
this.main = new CompilationUnit(new LinkedHashSet<>(builder.sourcePaths), builder.classesPath, builder.resourcePath,
this.main = new CompilationUnit(builder.sourcePaths, builder.classesPath,
builder.resourcePaths,
builder.resourcesOutputPath);
if (builder.testClassesPath != null) {
this.test = new CompilationUnit(new LinkedHashSet<>(builder.testSourcePaths),
builder.testClassesPath, builder.testResourcePath, builder.testResourcesOutputPath);
this.test = new CompilationUnit(builder.testSourcePaths,
builder.testClassesPath, builder.testResourcePaths, builder.testResourcesOutputPath);
} else {
this.test = null;
}
Expand All @@ -263,15 +264,17 @@ public String getProjectDirectory() {
return projectDirectory;
}

public Set<String> getSourceParents() {
public PathsCollection getSourceParents() {
return sourceParents;
}

//TODO: why isn't this immutable?
public void addSourcePaths(Collection<String> additionalPaths) {
additionalPaths.stream()
.map(p -> Paths.get(p).isAbsolute() ? p : (projectDirectory + File.separator + p))
.forEach(main.sourcePaths::add);
this.main.sourcePaths = this.main.sourcePaths.add(
additionalPaths.stream()
.map(p -> Paths.get(p).isAbsolute() ? p : (projectDirectory + File.separator + p))
.map(Paths::get)
.toArray(Path[]::new));
}

public String getPreBuildOutputDir() {
Expand Down Expand Up @@ -299,18 +302,18 @@ public static class Builder {
private AppArtifactKey appArtifactKey;
private String name;
private String projectDirectory;
private Set<String> sourcePaths = Collections.emptySet();
private PathsCollection sourcePaths = PathsCollection.of();
private String classesPath;
private String resourcePath;
private PathsCollection resourcePaths = PathsCollection.of();
private String resourcesOutputPath;

private String preBuildOutputDir;
private Set<String> sourceParents = Collections.emptySet();
private PathsCollection sourceParents = PathsCollection.of();
private String targetDir;

private Set<String> testSourcePaths = Collections.emptySet();
private PathsCollection testSourcePaths = PathsCollection.of();
private String testClassesPath;
private String testResourcePath;
private PathsCollection testResourcePaths = PathsCollection.of();
private String testResourcesOutputPath;

public Builder setAppArtifactKey(AppArtifactKey appArtifactKey) {
Expand All @@ -328,7 +331,7 @@ public Builder setProjectDirectory(String projectDirectory) {
return this;
}

public Builder setSourcePaths(Set<String> sourcePaths) {
public Builder setSourcePaths(PathsCollection sourcePaths) {
this.sourcePaths = sourcePaths;
return this;
}
Expand All @@ -338,8 +341,8 @@ public Builder setClassesPath(String classesPath) {
return this;
}

public Builder setResourcePath(String resourcePath) {
this.resourcePath = resourcePath;
public Builder setResourcePaths(PathsCollection resourcePaths) {
this.resourcePaths = resourcePaths;
return this;
}

Expand All @@ -353,7 +356,7 @@ public Builder setPreBuildOutputDir(String preBuildOutputDir) {
return this;
}

public Builder setSourceParents(Set<String> sourceParents) {
public Builder setSourceParents(PathsCollection sourceParents) {
this.sourceParents = sourceParents;
return this;
}
Expand All @@ -363,7 +366,7 @@ public Builder setTargetDir(String targetDir) {
return this;
}

public Builder setTestSourcePaths(Set<String> testSourcePaths) {
public Builder setTestSourcePaths(PathsCollection testSourcePaths) {
this.testSourcePaths = testSourcePaths;
return this;
}
Expand All @@ -373,8 +376,8 @@ public Builder setTestClassesPath(String testClassesPath) {
return this;
}

public Builder setTestResourcePath(String testResourcePath) {
this.testResourcePath = testResourcePath;
public Builder setTestResourcePaths(PathsCollection testResourcePaths) {
this.testResourcePaths = testResourcePaths;
return this;
}

Expand All @@ -390,28 +393,29 @@ public ModuleInfo build() {
}

public static class CompilationUnit implements Serializable {
private final Set<String> sourcePaths;
private PathsCollection sourcePaths;
private final String classesPath;
private final String resourcePath;
private final PathsCollection resourcePaths;
private final String resourcesOutputPath;

public CompilationUnit(Set<String> sourcePaths, String classesPath, String resourcePath, String resourcesOutputPath) {
public CompilationUnit(PathsCollection sourcePaths, String classesPath, PathsCollection resourcePaths,
String resourcesOutputPath) {
this.sourcePaths = sourcePaths;
this.classesPath = classesPath;
this.resourcePath = resourcePath;
this.resourcePaths = resourcePaths;
this.resourcesOutputPath = resourcesOutputPath;
}

public Set<String> getSourcePaths() {
public PathsCollection getSourcePaths() {
return sourcePaths;
}

public String getClassesPath() {
return classesPath;
}

public String getResourcePath() {
return resourcePath;
public PathsCollection getResourcePaths() {
return resourcePaths;
}

public String getResourcesOutputPath() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package io.quarkus.deployment.dev;

import java.io.Closeable;
import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;

import io.quarkus.bootstrap.BootstrapGradleException;
import io.quarkus.bootstrap.app.CuratedApplication;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.bootstrap.model.PathsCollection;
import io.quarkus.bootstrap.model.gradle.QuarkusModel;
import io.quarkus.bootstrap.model.gradle.WorkspaceModule;
import io.quarkus.bootstrap.resolver.AppModelResolverException;
Expand Down Expand Up @@ -95,25 +96,24 @@ private DevModeContext.ModuleInfo toModule(WorkspaceModule module) throws Bootst
AppArtifactKey key = new AppArtifactKey(module.getArtifactCoords().getGroupId(),
module.getArtifactCoords().getArtifactId(), module.getArtifactCoords().getClassifier());

Set<String> sourceDirectories = new HashSet<>();
Set<String> sourceParents = new HashSet<>();
for (File srcDir : module.getSourceSourceSet().getSourceDirectories()) {
sourceDirectories.add(srcDir.getPath());
final Set<Path> sourceParents = new LinkedHashSet<>();
for (Path srcDir : module.getSourceSourceSet().getSourceDirectories()) {
sourceParents.add(srcDir.getParent());
}
String resourceDirectory = null;
if (module.getSourceSet().getResourceDirectory() != null) {
resourceDirectory = module.getSourceSet().getResourceDirectory().getPath();
if (!module.getSourceSet().getResourceDirectories().isEmpty()) {
// Peek the first one as we assume that it is the primary
resourceDirectory = module.getSourceSet().getResourceDirectories().iterator().next().toString();
}
return new DevModeContext.ModuleInfo.Builder()
.setAppArtifactKey(key)
.setName(module.getArtifactCoords().getArtifactId())
.setProjectDirectory(module.getProjectRoot().getPath())
.setSourcePaths(sourceDirectories)
.setSourcePaths(module.getSourceSourceSet().getSourceDirectories())
.setClassesPath(QuarkusModelHelper.getClassPath(module).toAbsolutePath().toString())
.setResourcePath(module.getSourceSourceSet().getResourceDirectory().toString())
.setResourcePaths(module.getSourceSourceSet().getResourceDirectories())
.setResourcesOutputPath(resourceDirectory)
.setSourceParents(sourceParents)
.setSourceParents(PathsCollection.from(sourceParents))
.setPreBuildOutputDir(module.getBuildDir().toPath().resolve("generated-sources").toAbsolutePath().toString())
.setTargetDir(module.getBuildDir().toString()).build();
}
Expand All @@ -124,12 +124,15 @@ private DevModeContext.ModuleInfo toModule(LocalProject project) {
.setAppArtifactKey(project.getKey())
.setName(project.getArtifactId())
.setProjectDirectory(project.getDir().toAbsolutePath().toString())
.setSourcePaths(Collections.singleton(project.getSourcesSourcesDir().toAbsolutePath().toString()))
.setSourcePaths(PathsCollection.of(project.getSourcesSourcesDir().toAbsolutePath()))
.setClassesPath(project.getClassesDir().toAbsolutePath().toString())
.setResourcesOutputPath(project.getClassesDir().toAbsolutePath().toString())
.setResourcePath(project.getResourcesSourcesDir().toAbsolutePath().toString())
.setSourceParents(Collections.singleton(project.getSourcesDir().toString()))
.setResourcePaths(
PathsCollection.from(project.getResourcesSourcesDirs().toList().stream()
.map(Path::toAbsolutePath)
.collect(Collectors.toCollection(LinkedHashSet::new))))
.setSourceParents(PathsCollection.of(project.getSourcesDir()))
.setPreBuildOutputDir(project.getCodeGenOutputDir().toString())
.setTargetDir(project.getOutputDir().toString()).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -395,7 +394,7 @@ public boolean test(String s) {
codeGens.addAll(
CodeGenerator.init(
deploymentClassLoader,
module.getSourceParents().stream().map(Paths::get).collect(Collectors.toSet()),
module.getSourceParents(),
Paths.get(module.getPreBuildOutputDir()),
Paths.get(module.getTargetDir()),
sourcePath -> module.addSourcePaths(singleton(sourcePath.toAbsolutePath().toString()))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;

import io.quarkus.bootstrap.model.PathsCollection;
import io.quarkus.gizmo.Gizmo;

public class JavaCompilationProvider implements CompilationProvider {
Expand Down Expand Up @@ -94,7 +95,7 @@ public void compile(Set<File> filesToCompile, Context context) {
}

@Override
public Path getSourcePath(Path classFilePath, Set<String> sourcePaths, String classesPath) {
public Path getSourcePath(Path classFilePath, PathsCollection sourcePaths, String classesPath) {
Path sourceFilePath = null;
final RuntimeUpdatesClassVisitor visitor = new RuntimeUpdatesClassVisitor(sourcePaths, classesPath);
try (final InputStream inputStream = Files.newInputStream(classFilePath)) {
Expand All @@ -117,11 +118,11 @@ public void close() throws IOException {
}

static class RuntimeUpdatesClassVisitor extends ClassVisitor {
private Set<String> sourcePaths;
private String classesPath;
private final PathsCollection sourcePaths;
private final String classesPath;
private String sourceFile;

public RuntimeUpdatesClassVisitor(Set<String> sourcePaths, String classesPath) {
public RuntimeUpdatesClassVisitor(PathsCollection sourcePaths, String classesPath) {
super(Gizmo.ASM_API_VERSION);
this.sourcePaths = sourcePaths;
this.classesPath = classesPath;
Expand All @@ -133,8 +134,7 @@ public void visitSource(String source, String debug) {
}

public Path getSourceFileForClass(final Path classFilePath) {
for (String moduleSourcePath : sourcePaths) {
final Path sourcesDir = Paths.get(moduleSourcePath);
for (Path sourcesDir : sourcePaths) {
final Path classesDir = Paths.get(classesPath);
final StringBuilder sourceRelativeDir = new StringBuilder();
sourceRelativeDir.append(classesDir.relativize(classFilePath.getParent()));
Expand Down
Loading

0 comments on commit bce6a9e

Please sign in to comment.