Skip to content

Commit

Permalink
Replace Set of paths to PathsCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo committed May 19, 2021
1 parent e25af73 commit f7094fd
Show file tree
Hide file tree
Showing 29 changed files with 374 additions and 293 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,18 @@ 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,
this.main = new CompilationUnit(builder.sourcePaths, builder.classesPath,
builder.resourcePaths,
builder.resourcesOutputPath);
if (builder.testClassesPath != null) {
this.test = new CompilationUnit(new LinkedHashSet<>(builder.testSourcePaths),
this.test = new CompilationUnit(builder.testSourcePaths,
builder.testClassesPath, builder.testResourcePaths, builder.testResourcesOutputPath);
} else {
this.test = null;
Expand All @@ -264,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 @@ -300,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 Set<String> resourcePaths = Collections.emptySet();
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 Set<String> testResourcePaths = Collections.emptySet();
private PathsCollection testResourcePaths = PathsCollection.of();
private String testResourcesOutputPath;

public Builder setAppArtifactKey(AppArtifactKey appArtifactKey) {
Expand All @@ -329,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 @@ -339,7 +341,7 @@ public Builder setClassesPath(String classesPath) {
return this;
}

public Builder setResourcePaths(Set<String> resourcePaths) {
public Builder setResourcePaths(PathsCollection resourcePaths) {
this.resourcePaths = resourcePaths;
return this;
}
Expand All @@ -354,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 @@ -364,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 @@ -374,7 +376,7 @@ public Builder setTestClassesPath(String testClassesPath) {
return this;
}

public Builder setTestResourcePaths(Set<String> testResourcePaths) {
public Builder setTestResourcePaths(PathsCollection testResourcePaths) {
this.testResourcePaths = testResourcePaths;
return this;
}
Expand All @@ -391,28 +393,28 @@ public ModuleInfo build() {
}

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

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

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

public String getClassesPath() {
return classesPath;
}

public Set<String> getResourcePaths() {
public PathsCollection getResourcePaths() {
return resourcePaths;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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;
Expand All @@ -15,6 +14,7 @@
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 @@ -96,27 +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().getResourceDirectories().isEmpty()) {
// Peek the first one as we assume that it is the primary
resourceDirectory = module.getSourceSet().getResourceDirectories().iterator().next().getPath();
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())
.setResourcePaths(module.getSourceSourceSet().getResourceDirectories().stream().map(Object::toString)
.collect(Collectors.toSet()))
.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 @@ -127,14 +124,14 @@ 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())
.setResourcePaths(
project.getResourcesSourcesDirs().stream()
.map(resourcesSourcesDir -> resourcesSourcesDir.toAbsolutePath().toString())
.collect(Collectors.toSet()))
.setSourceParents(Collections.singleton(project.getSourcesDir().toString()))
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();
}
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.quarkus.bootstrap.app.CuratedApplication;
import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.bootstrap.model.AppDependency;
import io.quarkus.bootstrap.model.PathsCollection;

/**
* Class that handles compilation of source files
Expand Down Expand Up @@ -161,12 +162,12 @@ public void setupSourceCompilationContext(DevModeContext context, Set<File> clas
return;
}
compilationUnit.getSourcePaths().forEach(sourcePath -> {
this.compilationContexts.put(sourcePath,
this.compilationContexts.put(sourcePath.toString(),
new CompilationProvider.Context(
i.getName(),
classPathElements,
i.getProjectDirectory() == null ? null : new File(i.getProjectDirectory()),
new File(sourcePath),
sourcePath.toFile(),
new File(compilationUnit.getClassesPath()),
context.getSourceEncoding(),
context.getCompilerOptions(),
Expand Down Expand Up @@ -194,7 +195,7 @@ public void compile(String sourceDir, Map<String, Set<File>> extensionToChangedF
}
}

public Path findSourcePath(Path classFilePath, Set<String> sourcePaths, String classesPath) {
public Path findSourcePath(Path classFilePath, PathsCollection sourcePaths, String classesPath) {
for (CompilationProvider compilationProvider : compilationProviders) {
Path sourcePath = compilationProvider.getSourcePath(classFilePath, sourcePaths, classesPath);

Expand Down
Loading

0 comments on commit f7094fd

Please sign in to comment.