added Load into compiler pipeline
This commit is contained in:
+13
-3
@@ -1,18 +1,28 @@
|
||||
package p.studio.compiler.models;
|
||||
|
||||
import p.studio.compiler.messages.BuilderPipelineConfig;
|
||||
import p.studio.compiler.source.tables.FileTable;
|
||||
import p.studio.compiler.utilities.SourceProviderFactory;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class BuilderPipelineContext {
|
||||
public final BuilderPipelineConfig config;
|
||||
public final SourceProviderFactory sourceProviderFactory;
|
||||
|
||||
public Path rootProjectPathCanon;
|
||||
public ResolvedWorkspace resolvedWorkspace;
|
||||
|
||||
public final FileTable fileTable = new FileTable();
|
||||
|
||||
private BuilderPipelineContext(
|
||||
final BuilderPipelineConfig config) {
|
||||
final BuilderPipelineConfig config,
|
||||
final SourceProviderFactory factory) {
|
||||
this.config = config;
|
||||
this.sourceProviderFactory = factory;
|
||||
}
|
||||
|
||||
public static BuilderPipelineContext basedOn(BuilderPipelineConfig config) {
|
||||
return new BuilderPipelineContext(config);
|
||||
public static BuilderPipelineContext compilerContext(final BuilderPipelineConfig config) {
|
||||
return new BuilderPipelineContext(config, SourceProviderFactory.filesystem());
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.compiler.messages.BuilderPipelineConfig;
|
||||
import p.studio.compiler.models.BuilderPipelineContext;
|
||||
import p.studio.compiler.workspaces.stages.LoadPipelineStage;
|
||||
import p.studio.compiler.workspaces.stages.ResolvePipelineStage;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
@@ -16,7 +17,8 @@ public class BuilderPipelineService {
|
||||
|
||||
static {
|
||||
final var stages = List.<PipelineStage>of(
|
||||
new ResolvePipelineStage()
|
||||
new ResolvePipelineStage(),
|
||||
new LoadPipelineStage()
|
||||
);
|
||||
INSTANCE = new BuilderPipelineService(stages);
|
||||
}
|
||||
@@ -30,7 +32,7 @@ public class BuilderPipelineService {
|
||||
public void run(
|
||||
final BuilderPipelineConfig config,
|
||||
final LogAggregator logs) {
|
||||
final var ctx = BuilderPipelineContext.basedOn(config);
|
||||
final var ctx = BuilderPipelineContext.compilerContext(config);
|
||||
|
||||
for (final var builderPipelineStage : stages) {
|
||||
final var issues = builderPipelineStage.run(ctx, logs);
|
||||
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
package p.studio.compiler.workspaces.stages;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import p.studio.compiler.messages.BuildingIssues;
|
||||
import p.studio.compiler.models.BuilderPipelineContext;
|
||||
import p.studio.compiler.models.SourceHandle;
|
||||
import p.studio.compiler.workspaces.PipelineStage;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
import p.studio.utilities.structures.ReadOnlySet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class LoadPipelineStage implements PipelineStage {
|
||||
|
||||
@Override
|
||||
public BuildingIssues run(final BuilderPipelineContext ctx, final LogAggregator logs) {
|
||||
final var issues = BuildingIssues.empty();
|
||||
|
||||
// Iterates projects; "loads sources"; registers files
|
||||
ctx.resolvedWorkspace.topologicalOrder().forEach(pId -> {
|
||||
final var pd = ctx.resolvedWorkspace.graph().projectTable().get(pId);
|
||||
logs.using(log).info("Project [ " + pd.getName() + " ] source loading...");
|
||||
|
||||
final var allowedExtensions = normalize(pd.getFrontendSpec().getAllowedExtensions());
|
||||
for (final var sourceRootPath : pd.getSourceRoots()) {
|
||||
logs.using(log).debug("Walking source root [ " + sourceRootPath + " ]");
|
||||
|
||||
try {
|
||||
final List<Path> paths = new ArrayList<>();
|
||||
Files.walkFileTree(sourceRootPath, new SourceCrawler(allowedExtensions, paths));
|
||||
paths.sort(Path::compareTo);
|
||||
for (var path : paths) {
|
||||
logs.using(log).debug("file tabling [ " + path + " ]");
|
||||
final var attributes = Files.readAttributes(path, BasicFileAttributes.class);
|
||||
final var size = attributes.size();
|
||||
final var lastModified = attributes.lastModifiedTime().toMillis();
|
||||
final var rawFile = new SourceHandle(pId, path, size, lastModified, ctx.sourceProviderFactory);
|
||||
// register in dense tables
|
||||
var fileId = ctx.fileTable.register(rawFile);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
issues.add(builder -> builder
|
||||
.error(true)
|
||||
.message("Failed to load project [ " + pd.getName() + " ]")
|
||||
.exception(e));
|
||||
}
|
||||
}
|
||||
});
|
||||
return BuildingIssues.empty();
|
||||
}
|
||||
|
||||
private static ReadOnlySet<String> normalize(final ReadOnlySet<String> extensions) {
|
||||
return ReadOnlySet
|
||||
.wrap(extensions
|
||||
.map(String::toLowerCase)
|
||||
.map(s -> s.startsWith(".") ? s.substring(1) : s)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
private static final class SourceCrawler extends SimpleFileVisitor<Path> {
|
||||
|
||||
private static final Set<String> IGNORED_DIRS = Set.of(
|
||||
".git",
|
||||
".prometeu",
|
||||
".workspace",
|
||||
"target",
|
||||
"build",
|
||||
"out",
|
||||
"node_modules"
|
||||
);
|
||||
|
||||
private final ReadOnlySet<String> allowedExtensions;
|
||||
private final List<Path> paths;
|
||||
|
||||
public SourceCrawler(
|
||||
final ReadOnlySet<String> allowedExtensions,
|
||||
final List<Path> paths) {
|
||||
this.allowedExtensions = allowedExtensions;
|
||||
this.paths = paths;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attrs) {
|
||||
for (final var directory : path) {
|
||||
if (!isAllowedPath(directory)) {
|
||||
return FileVisitResult.SKIP_SUBTREE;
|
||||
}
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) {
|
||||
if (!attrs.isRegularFile()) {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
if (hasAllowedExt(path, allowedExtensions)) {
|
||||
paths.add(path);
|
||||
}
|
||||
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
private static boolean isAllowedPath(
|
||||
final Path path) {
|
||||
for (Path part : path) {
|
||||
if (IGNORED_DIRS.contains(part.toString())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean hasAllowedExt(
|
||||
final Path path,
|
||||
final ReadOnlySet<String> allowedExtensions) {
|
||||
|
||||
final var fileName = path.getFileName();
|
||||
if (Objects.isNull(fileName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final var extension = FilenameUtils.getExtension(fileName.toString()).toLowerCase();
|
||||
if (StringUtils.isBlank(extension)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return allowedExtensions.contains(extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -9,16 +9,14 @@ import p.studio.compiler.workspaces.PipelineStage;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Slf4j
|
||||
public class ResolvePipelineStage implements PipelineStage {
|
||||
@Override
|
||||
public BuildingIssues run(final BuilderPipelineContext ctx, LogAggregator logs) {
|
||||
final Path rootCanonPath;
|
||||
try {
|
||||
rootCanonPath = Paths.get(ctx.config.rootProjectPath()).toRealPath();
|
||||
ctx.rootProjectPathCanon = Paths.get(ctx.config.rootProjectPath()).toRealPath();
|
||||
} catch (IOException e) {
|
||||
return BuildingIssues.empty()
|
||||
.add(builder -> builder
|
||||
@@ -26,9 +24,8 @@ public class ResolvePipelineStage implements PipelineStage {
|
||||
.message("[BUILD]: root project directory no found: " + ctx.config.rootProjectPath())
|
||||
.exception(e));
|
||||
}
|
||||
final var dependencyConfig = new DependencyConfig(ctx.config.explain(), rootCanonPath);
|
||||
final var dependencyConfig = new DependencyConfig(ctx.config.explain(), ctx.rootProjectPathCanon);
|
||||
ctx.resolvedWorkspace = DependencyService.INSTANCE.run(dependencyConfig, logs);
|
||||
ctx.resolvedWorkspace.topologicalOrder().forEach(pd -> logs.using(log).info("Project [ " + pd.getName() + " ] read"));
|
||||
return BuildingIssues.empty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user