added first deps implementation
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
package p.studio.compiler.exceptions;
|
||||
|
||||
public class BuildException extends RuntimeException {
|
||||
public BuildException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BuildException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record BuildStack(ReadOnlyList<ProjectId> projects) {
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class BuildingIssue {
|
||||
private final String message;
|
||||
private final Throwable exception;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public record DependencyPipelineConfig(
|
||||
boolean explain,
|
||||
Path cacheDir,
|
||||
List<Path> registryDirs) {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
public record LoadedFile(String uri, String text) {
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Sources already loaded by dependencies (IO happens in dependencies, not in pipeline).
|
||||
*/
|
||||
public record LoadedSources(
|
||||
/**
|
||||
* For each project in the stack, a list of files (uri + text).
|
||||
*/
|
||||
List<ProjectSources> perProject
|
||||
) {
|
||||
public LoadedSources {
|
||||
perProject = List.copyOf(perProject);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class ProjectDescriptor {
|
||||
private final ProjectId projectId;
|
||||
private final String name;
|
||||
private final String version;
|
||||
private final Path projectDir;
|
||||
private final ReadOnlyList<Path> sourceRoots;
|
||||
private final String languageId;
|
||||
private final SourcePolicy sourcePolicy;
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
public record ProjectSources(
|
||||
ProjectId projectId,
|
||||
ReadOnlyList<LoadedFile> files) {
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public record PrometeuLock(
|
||||
long schema,
|
||||
List<LockMapping> mappings) {
|
||||
|
||||
public PrometeuLock {
|
||||
mappings = mappings != null ? List.copyOf(mappings) : List.of();
|
||||
}
|
||||
|
||||
public static PrometeuLock blank() {
|
||||
return new PrometeuLock(0, List.of());
|
||||
}
|
||||
|
||||
public Optional<String> lookupGitLocalDir(
|
||||
final String url,
|
||||
final String rev) {
|
||||
return mappings
|
||||
.stream()
|
||||
.filter(m -> m instanceof LockMapping.Git g && g.url().equals(url) && g.rev().equals(rev))
|
||||
.map(m -> ((LockMapping.Git) m).localDir())
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@JsonTypeInfo(
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.PROPERTY,
|
||||
property = "kind"
|
||||
)
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = LockMapping.Git.class, name = "git")
|
||||
})
|
||||
public interface LockMapping {
|
||||
record Git(
|
||||
String url,
|
||||
String rev,
|
||||
@JsonProperty("local-dir") String localDir
|
||||
) implements LockMapping {}
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public record PrometeuManifest(
|
||||
String name,
|
||||
String version,
|
||||
String language,
|
||||
ReadOnlyList<DependencyDeclaration> dependencies) {
|
||||
public static PrometeuManifest extract(final Path path, final ObjectMapper mapper) {
|
||||
try {
|
||||
final var root = mapper.readTree(path.toFile());
|
||||
final var name = text(root, "name");
|
||||
final var version = text(root, "version");
|
||||
|
||||
final var language = Optional
|
||||
.ofNullable(root.get("language"))
|
||||
.map(JsonNode::asText)
|
||||
.orElseGet(() -> FrontendRegistryService.getDefaultFrontendSpec().getLanguageId());
|
||||
|
||||
final List<PrometeuManifest.DependencyDeclaration> dependencies = new ArrayList<>();
|
||||
final var dependencyNodes = root.get("dependencies");
|
||||
if (dependencyNodes != null && dependencyNodes.isArray()) {
|
||||
for (final var d : dependencyNodes) {
|
||||
if (d.has("path")) {
|
||||
dependencies.add(new PrometeuManifest.DependencyDeclaration.Local(d.get("path").asText()));
|
||||
} else if (d.has("url")) {
|
||||
final var url = d.get("url").asText();
|
||||
final var rev = d.has("rev") ? d.get("rev").asText(null) : null;
|
||||
dependencies.add(new PrometeuManifest.DependencyDeclaration.Git(url, rev));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new PrometeuManifest(name, version, language, ReadOnlyList.wrap(dependencies));
|
||||
} catch (IOException e) {
|
||||
throw new BuildException("dependencies: failed to read or parse prometeu manifest " + path, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String text(final JsonNode root, final String field) {
|
||||
JsonNode n = root.get(field);
|
||||
return n != null ? n.asText() : null;
|
||||
}
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = DependencyDeclaration.Local.class),
|
||||
@JsonSubTypes.Type(value = DependencyDeclaration.Git.class)
|
||||
})
|
||||
public interface DependencyDeclaration {
|
||||
@Getter
|
||||
class Local implements DependencyDeclaration {
|
||||
private final String path;
|
||||
|
||||
@JsonCreator
|
||||
public Local(@JsonProperty("path") final String path) {
|
||||
this.path = path;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@Getter
|
||||
class Git implements DependencyDeclaration {
|
||||
private final String git;
|
||||
private final String rev;
|
||||
|
||||
@JsonCreator
|
||||
public Git(@JsonProperty("url") final String git, @JsonProperty("rev") final String rev) {
|
||||
this.git = git;
|
||||
this.rev = rev;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record ResolvedGraph(
|
||||
ProjectId root,
|
||||
ReadOnlyList<ProjectDescriptor> projects,
|
||||
ReadOnlyList<ReadOnlyList<ProjectId>> edges) {
|
||||
public ProjectDescriptor project(ProjectId id) {
|
||||
return projects.get(id.getIndex());
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
|
||||
public record ResolvedWorkspace(
|
||||
ProjectId projectId,
|
||||
ResolvedGraph graph,
|
||||
BuildStack stack) {
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
public record SourcePolicy(ReadOnlyList<String> extensions, boolean caseSensitive) {
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import p.studio.compiler.model.*;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
public final class DependencyPipelineContext {
|
||||
private final DependencyPipelineConfig config;
|
||||
|
||||
// Internal state mirroring Rust ResolverState
|
||||
public Path mainProjectRootPathCanon;
|
||||
|
||||
// Phase 1 (Discover)
|
||||
public final List<ProjectInfo> projectInfos = new ArrayList<>();
|
||||
public final Map<Path, Long> projectIndexByDirectory = new HashMap<>();
|
||||
public final Deque<Path> pending = new ArrayDeque<>();
|
||||
|
||||
// Phase 2+
|
||||
public final List<ProjectNode> projectNodes = new ArrayList<>();
|
||||
public final Map<Path, ProjectId> projectIdByDirectoryRoot = new HashMap<>();
|
||||
public final List<List<ProjectId>> dependenciesByProject = new ArrayList<>();
|
||||
|
||||
public ProjectId root;
|
||||
public BuildStack stack;
|
||||
|
||||
private DependencyPipelineContext(DependencyPipelineConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public static DependencyPipelineContext seed(DependencyPipelineConfig config) {
|
||||
return new DependencyPipelineContext(config);
|
||||
}
|
||||
|
||||
public DependencyPipelineConfig config() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public ResolvedWorkspace toResolvedWorkspace() {
|
||||
if (root == null) {
|
||||
throw new BuildException("dependencies: internal error: root ProjectId not set");
|
||||
}
|
||||
final var projectDescriptors = ReadOnlyList.wrap(projectNodes
|
||||
.stream()
|
||||
.map(n -> {
|
||||
final var languageId = n.getLanguageId();
|
||||
final var sourcePolicy = new SourcePolicy(ReadOnlyList.empty(), true); // TODO: source policy should come from a frontend registry anginst language id
|
||||
return ProjectDescriptor
|
||||
.builder()
|
||||
.projectId(n.getProjectId())
|
||||
.name(n.getName())
|
||||
.version(n.getVersion())
|
||||
.projectDir(n.getProjectRootPath())
|
||||
.sourceRoots(n.getSourceRoots())
|
||||
.languageId(languageId)
|
||||
.sourcePolicy(sourcePolicy)
|
||||
.build();
|
||||
})
|
||||
.toList());
|
||||
final var edges = ReadOnlyList.wrap(this
|
||||
.dependenciesByProject
|
||||
.stream()
|
||||
.map(ReadOnlyList::wrap)
|
||||
.toList());
|
||||
final var graph = new ResolvedGraph(root, projectDescriptors, edges);
|
||||
return new ResolvedWorkspace(root, graph, stack);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
public interface DependencyPipelinePhase {
|
||||
ReadOnlyCollection<BuildingIssue> run(DependencyPipelineContext state);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.model.ResolvedWorkspace;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public final class DependencyPipelineService {
|
||||
private final List<DependencyPipelinePhase> phases;
|
||||
|
||||
public DependencyPipelineService(List<DependencyPipelinePhase> phases) {
|
||||
this.phases = phases;
|
||||
}
|
||||
|
||||
public ResolvedWorkspace run(DependencyPipelineConfig config) {
|
||||
final var ctx = DependencyPipelineContext.seed(config);
|
||||
|
||||
for (final var dependencyPipelinePhase : phases) {
|
||||
final var issues = dependencyPipelinePhase.run(ctx);
|
||||
if (ReadOnlyCollection.isNotEmpty(issues)) {
|
||||
for (final var issue : issues) {
|
||||
log.error(issue.getMessage(), issue.getException());
|
||||
}
|
||||
throw new BuildException("issues found during dependency pipeline phase: " + dependencyPipelinePhase.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
log.info("dependency pipeline completed successfully, going to resolve workspace");
|
||||
|
||||
return ctx.toResolvedWorkspace();
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public record DependencyReference(Path path) {
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.model.FrontendSpec;
|
||||
import p.studio.compiler.model.PrometeuManifest;
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public final class ProjectInfo {
|
||||
public final Path rootDirectory;
|
||||
public final Path manifestPath;
|
||||
public final PrometeuManifest manifest;
|
||||
public final FrontendSpec frontendSpec;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.model.FrontendSpec;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public final class ProjectNode {
|
||||
private final FrontendSpec frontendSpec;
|
||||
private final ProjectId projectId;
|
||||
private final Path projectRootPath;
|
||||
private final String name;
|
||||
private final String version;
|
||||
private final ReadOnlyList<Path> sourceRoots;
|
||||
private final String languageId;
|
||||
private final ReadOnlyList<DependencyReference> dependencies;
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package p.studio.compiler.workspaces.phases;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.compiler.model.PrometeuManifest;
|
||||
import p.studio.compiler.workspaces.DependencyPipelinePhase;
|
||||
import p.studio.compiler.workspaces.DependencyPipelineContext;
|
||||
import p.studio.compiler.workspaces.ProjectInfo;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class DiscoverPhase implements DependencyPipelinePhase {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public ReadOnlyCollection<BuildingIssue> run(final DependencyPipelineContext ctx) {
|
||||
final List<BuildingIssue> issues = new ArrayList<>();
|
||||
while (!ctx.pending.isEmpty()) {
|
||||
final var rootPathCanon = ctx.pending.pollFirst();
|
||||
|
||||
if (ctx.projectIndexByDirectory.containsKey(rootPathCanon)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final var manifestPath = rootPathCanon.resolve("prometeu.json");
|
||||
if (!Files.exists(manifestPath) || !Files.isRegularFile(manifestPath)) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: manifest not found: expected a file " + manifestPath + " (" + rootPathCanon + ")")
|
||||
.build();
|
||||
issues.add(issue);
|
||||
continue;
|
||||
}
|
||||
|
||||
final var manifest = PrometeuManifest.extract(manifestPath, mapper);
|
||||
|
||||
final var frontendSpec = FrontendRegistryService.getFrontendSpec(manifest.language());
|
||||
if (frontendSpec.isEmpty()) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: unknown language " + manifest.language() + " for project " + manifest.name())
|
||||
.build();
|
||||
issues.add(issue);
|
||||
continue;
|
||||
}
|
||||
if (ReadOnlyCollection.isEmpty(manifest.dependencies())) {
|
||||
log.warn("dependencies: no dependencies specified for project {}", manifest.name());
|
||||
}
|
||||
|
||||
final long projectIndex = ctx.projectInfos.size();
|
||||
final var projectInfo = ProjectInfo
|
||||
.builder()
|
||||
.rootDirectory(rootPathCanon)
|
||||
.manifestPath(manifestPath)
|
||||
.manifest(manifest)
|
||||
.frontendSpec(frontendSpec.get())
|
||||
.build();
|
||||
ctx.projectInfos.add(projectInfo);
|
||||
ctx.projectIndexByDirectory.put(rootPathCanon, projectIndex);
|
||||
|
||||
for (final var dependencyDeclaration : manifest.dependencies()) {
|
||||
if (dependencyDeclaration instanceof PrometeuManifest.DependencyDeclaration.Local local) {
|
||||
final var dependencyPath = rootPathCanon.resolve(local.getPath());
|
||||
try {
|
||||
final var dependencyPathCanon = dependencyPath.toRealPath();
|
||||
ctx.pending.add(dependencyPathCanon);
|
||||
} catch (IOException e) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: dep path does not exist: " + dependencyPath + " (" + rootPathCanon + ")")
|
||||
.exception(e)
|
||||
.build();
|
||||
issues.add(issue);
|
||||
}
|
||||
} else if (dependencyDeclaration instanceof PrometeuManifest.DependencyDeclaration.Git) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: url dependencies not yet supported " + manifest.name() + " (" + rootPathCanon + ")")
|
||||
.build();
|
||||
issues.add(issue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package p.studio.compiler.workspaces.phases;
|
||||
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.compiler.workspaces.*;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class LocalizePhase implements DependencyPipelinePhase {
|
||||
@Override
|
||||
public ReadOnlyCollection<BuildingIssue> run(final DependencyPipelineContext state) {
|
||||
final List<BuildingIssue> issues = new ArrayList<>();
|
||||
for (int i = 0; i < state.projectNodes.size(); i++) {
|
||||
final var fromProjectNode = state.projectNodes.get(i);
|
||||
for (final var dependencyReference : fromProjectNode.getDependencies()) {
|
||||
final var dependencyReferencePath = dependencyReference.path();
|
||||
final var projectId = state.projectIdByDirectoryRoot.get(dependencyReferencePath);
|
||||
if (Objects.isNull(projectId)) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: dependency not found: " + dependencyReferencePath + " (referenced from " + fromProjectNode.getProjectRootPath() + ")")
|
||||
.build();
|
||||
issues.add(issue);
|
||||
continue;
|
||||
}
|
||||
state.dependenciesByProject.get(fromProjectNode.getProjectId().getIndex()).add(projectId);
|
||||
}
|
||||
}
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package p.studio.compiler.workspaces.phases;
|
||||
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.compiler.model.PrometeuManifest;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.compiler.workspaces.*;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
public final class MaterializePhase implements DependencyPipelinePhase {
|
||||
@Override
|
||||
public ReadOnlyCollection<BuildingIssue> run(final DependencyPipelineContext ctx) {
|
||||
// start all over again, we will re-populate the project nodes and edges based on the project infos
|
||||
ctx.projectNodes.clear();
|
||||
ctx.projectIdByDirectoryRoot.clear();
|
||||
ctx.dependenciesByProject.clear();
|
||||
ctx.root = null;
|
||||
|
||||
final List<BuildingIssue> issues = new ArrayList<>();
|
||||
for (int index = 0; index < ctx.projectInfos.size(); index++) {
|
||||
final var projectId = new ProjectId(index);
|
||||
final var projectInfo = ctx.projectInfos.get(index);
|
||||
ctx.projectNodes.add(buildProjectNode(projectId, projectInfo, issues));
|
||||
ctx.projectIdByDirectoryRoot.put(projectInfo.rootDirectory, projectId);
|
||||
ctx.dependenciesByProject.add(new ArrayList<>());
|
||||
}
|
||||
|
||||
final var rootProjectId = ctx.projectIdByDirectoryRoot.get(ctx.mainProjectRootPathCanon);
|
||||
if (rootProjectId == null) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: root project dir " + ctx.mainProjectRootPathCanon + " was not discovered/materialized")
|
||||
.build();
|
||||
issues.add(issue);
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
ctx.root = rootProjectId;
|
||||
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
|
||||
private static ProjectNode buildProjectNode(
|
||||
final ProjectId projectId,
|
||||
final ProjectInfo projectInfo,
|
||||
final List<BuildingIssue> issues) {
|
||||
final List<BuildingIssue> sourceRootIssues = new ArrayList<>();
|
||||
final List<Path> sourceRoots = new ArrayList<>();
|
||||
for (final var sourceRoot : projectInfo.getFrontendSpec().getSourceRoots()) {
|
||||
final var sourceRootPath = projectInfo.rootDirectory.resolve(sourceRoot);
|
||||
try {
|
||||
final var sourceRootPathCanon = sourceRootPath.toRealPath();
|
||||
sourceRoots.add(sourceRootPathCanon);
|
||||
} catch (IOException e) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: source root path does not exist: " + sourceRootPath + " (from " + projectInfo.rootDirectory + ")")
|
||||
.exception(e)
|
||||
.build();
|
||||
sourceRootIssues.add(issue);
|
||||
}
|
||||
}
|
||||
if (sourceRootIssues.size() == projectInfo.getFrontendSpec().getSourceRoots().size()) {
|
||||
// no source roots were found at all
|
||||
issues.addAll(sourceRootIssues);
|
||||
}
|
||||
|
||||
final List<DependencyReference> dependencyReferencies = new ArrayList<>();
|
||||
for (PrometeuManifest.DependencyDeclaration d : projectInfo.manifest.dependencies()) {
|
||||
if (d instanceof PrometeuManifest.DependencyDeclaration.Local l) {
|
||||
final var dependencyPath = projectInfo.rootDirectory.resolve(l.getPath());
|
||||
try {
|
||||
final var dependencyPathCanon = dependencyPath.toRealPath();
|
||||
dependencyReferencies.add(new DependencyReference(dependencyPathCanon));
|
||||
} catch (IOException e) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: local dep path does not exist: " + dependencyPath + " (from " + projectInfo.rootDirectory + ")")
|
||||
.exception(e)
|
||||
.build();
|
||||
issues.add(issue);
|
||||
}
|
||||
} else if (d instanceof PrometeuManifest.DependencyDeclaration.Git g) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: url dependency '" + g.getGit() + "' requires an explicit 'rev' and lock mapping (not supported yet)")
|
||||
.build();
|
||||
issues.add(issue);
|
||||
}
|
||||
}
|
||||
|
||||
return ProjectNode
|
||||
.builder()
|
||||
.projectId(projectId)
|
||||
.projectRootPath(projectInfo.rootDirectory)
|
||||
.name(projectInfo.manifest.name())
|
||||
.version(projectInfo.manifest.version())
|
||||
.languageId(projectInfo.manifest.language())
|
||||
.sourceRoots(ReadOnlyList.wrap(sourceRoots))
|
||||
.dependencies(ReadOnlyList.wrap(dependencyReferencies))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package p.studio.compiler.workspaces.phases;
|
||||
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.compiler.workspaces.DependencyPipelinePhase;
|
||||
import p.studio.compiler.workspaces.DependencyPipelineContext;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class PolicyPhase implements DependencyPipelinePhase {
|
||||
@Override
|
||||
public ReadOnlyCollection<BuildingIssue> run(DependencyPipelineContext state) {
|
||||
final List<BuildingIssue> issues = new ArrayList<>();
|
||||
// No-op for now; in Rust this applies source policies
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package p.studio.compiler.workspaces.phases;
|
||||
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.compiler.workspaces.DependencyPipelinePhase;
|
||||
import p.studio.compiler.workspaces.DependencyPipelineContext;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class SeedPhase implements DependencyPipelinePhase {
|
||||
@Override
|
||||
public ReadOnlyCollection<BuildingIssue> run(final DependencyPipelineContext ctx) {
|
||||
final List<BuildingIssue> issues = new ArrayList<>();
|
||||
try {
|
||||
ctx.mainProjectRootPathCanon = ctx.config().cacheDir().toRealPath();
|
||||
} catch (IOException e) {
|
||||
final var issue = BuildingIssue
|
||||
.builder()
|
||||
.message("Failed to canonicalize root directory: " + ctx.config().cacheDir())
|
||||
.build();
|
||||
issues.add(issue);
|
||||
}
|
||||
ctx.pending.add(ctx.mainProjectRootPathCanon);
|
||||
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package p.studio.compiler.workspaces.phases;
|
||||
|
||||
import p.studio.compiler.model.BuildStack;
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.compiler.workspaces.DependencyPipelineContext;
|
||||
import p.studio.compiler.workspaces.DependencyPipelinePhase;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
|
||||
public final class StackPhase implements DependencyPipelinePhase {
|
||||
/**
|
||||
* Implements topological sort; detects dependency cycles; sets build stack
|
||||
*/
|
||||
@Override
|
||||
public ReadOnlyCollection<BuildingIssue> run(DependencyPipelineContext state) {
|
||||
final List<BuildingIssue> issues = new ArrayList<>();
|
||||
|
||||
final int n = state.projectNodes.size();
|
||||
final int[] indeg = new int[n];
|
||||
for (int from = 0; from < n; from++) {
|
||||
for (final ProjectId to : state.dependenciesByProject.get(from)) {
|
||||
indeg[to.getIndex()]++;
|
||||
}
|
||||
}
|
||||
|
||||
final Deque<Integer> q = new ArrayDeque<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (indeg[i] == 0) q.add(i);
|
||||
}
|
||||
|
||||
final List<ProjectId> travesalOrder = new ArrayList<>(n);
|
||||
// Performs topological sort using Kahn's algorithm
|
||||
while (!q.isEmpty()) {
|
||||
final int u = q.removeFirst();
|
||||
travesalOrder.add(state.projectNodes.get(u).getProjectId());
|
||||
for (final ProjectId v : state.dependenciesByProject.get(u)) {
|
||||
if (--indeg[(int) v.getId()] == 0) {
|
||||
q.addLast((int) v.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (travesalOrder.size() != n) {
|
||||
issues.add(BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: cycle detected in dependency graph")
|
||||
.build());
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
|
||||
state.stack = new BuildStack(ReadOnlyList.wrap(travesalOrder));
|
||||
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package p.studio.compiler.workspaces.phases;
|
||||
|
||||
import p.studio.compiler.model.BuildingIssue;
|
||||
import p.studio.compiler.workspaces.*;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class ValidatePhase implements DependencyPipelinePhase {
|
||||
@Override
|
||||
public ReadOnlyCollection<BuildingIssue> run(DependencyPipelineContext state) {
|
||||
final List<BuildingIssue> issues = new ArrayList<>();
|
||||
if (state.root == null) {
|
||||
issues.add(BuildingIssue
|
||||
.builder()
|
||||
.message("dependencies: root ProjectId not set")
|
||||
.build());
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
|
||||
// Ensure edges list matches number of nodes
|
||||
if (state.dependenciesByProject.size() != state.projectNodes.size()) {
|
||||
throw new BuildException("dependencies: internal error: edges list size mismatch");
|
||||
}
|
||||
|
||||
return ReadOnlyCollection.wrap(issues);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user