added first deps implementation
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
plugins {
|
||||
id("gradle.java-library-conventions")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":prometeu-infra"))
|
||||
|
||||
implementation(project(":prometeu-compiler:prometeu-compiler-core"))
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package p.studio.compiler;
|
||||
|
||||
import p.studio.compiler.model.FrontendSpec;
|
||||
import p.studio.utilities.structures.ReadOnlySet;
|
||||
|
||||
public class PBSDefinitions {
|
||||
public static final FrontendSpec PBS = FrontendSpec
|
||||
.builder()
|
||||
.languageId("pbs")
|
||||
.allowedExtensions(ReadOnlySet.from("pbs"))
|
||||
.sourceRoots(ReadOnlySet.from("src"))
|
||||
.build();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id("gradle.java-library-conventions")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":prometeu-infra"))
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package p.studio.compiler.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import p.studio.utilities.structures.ReadOnlySet;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class FrontendSpec {
|
||||
private final String languageId;
|
||||
private final ReadOnlySet<String> allowedExtensions;
|
||||
private final ReadOnlySet<String> sourceRoots;
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package p.studio.compiler.source;
|
||||
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
@Getter
|
||||
public class Span {
|
||||
private final FileId fileId;
|
||||
private final long start;
|
||||
private final long end; // exclusive
|
||||
|
||||
public Span(FileId fileId, long start, long end) {
|
||||
this.fileId = fileId;
|
||||
this.start = Math.min(start, end);
|
||||
this.end = Math.max(start, end);
|
||||
}
|
||||
|
||||
public static Span none() {
|
||||
return new Span(FileId.none(), 0, 0);
|
||||
}
|
||||
|
||||
public boolean isNone() {
|
||||
return fileId.isNone();
|
||||
}
|
||||
|
||||
public long length() {
|
||||
return end - start;
|
||||
}
|
||||
|
||||
public boolean contains(long pos) {
|
||||
if (pos < 0L) return false;
|
||||
return start <= pos && end > pos;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public class Diagnostic {
|
||||
private final Severity severity;
|
||||
private final String code;
|
||||
private final String message;
|
||||
private final Span span;
|
||||
private final ReadOnlyList<RelatedSpan> related;
|
||||
|
||||
public Diagnostic(
|
||||
final Severity severity,
|
||||
final String code,
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
this.severity = severity;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.span = span;
|
||||
this.related = ReadOnlyList.wrap(related);
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
public class DiagnosticBundle {
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.source.Span;
|
||||
|
||||
@Getter
|
||||
public class RelatedSpan {
|
||||
private final String message;
|
||||
private final Span span;
|
||||
|
||||
public RelatedSpan(String message, Span span) {
|
||||
this.message = message;
|
||||
this.span = span;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
public enum Severity {
|
||||
Error,
|
||||
Warning,
|
||||
;
|
||||
|
||||
public boolean isError() {
|
||||
return this == Error;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||
abstract class AbstractSourceIdentifier {
|
||||
@EqualsAndHashCode.Include
|
||||
private final long id;
|
||||
|
||||
AbstractSourceIdentifier(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return (int) id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s(%d)", getClass().getSimpleName(), id);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class FileId extends AbstractSourceIdentifier {
|
||||
public static final FileId NONE = new FileId(-1L);
|
||||
|
||||
public FileId(long id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
public static FileId none() {
|
||||
return NONE;
|
||||
}
|
||||
|
||||
public boolean isNone() {
|
||||
return this == NONE;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class ModuleId extends AbstractSourceIdentifier {
|
||||
public ModuleId(long id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class NameId extends AbstractSourceIdentifier {
|
||||
public NameId(long id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class NodeId extends AbstractSourceIdentifier {
|
||||
public NodeId(long id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class ProjectId extends AbstractSourceIdentifier {
|
||||
public ProjectId(int id) {
|
||||
super(Integer.toUnsignedLong(id));
|
||||
}
|
||||
|
||||
public ProjectId(long id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class SymbolId extends AbstractSourceIdentifier {
|
||||
public SymbolId(int id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class TypeId extends AbstractSourceIdentifier {
|
||||
public TypeId(long id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
plugins {
|
||||
id("gradle.java-library-conventions")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":prometeu-infra"))
|
||||
implementation(project(":prometeu-compiler:prometeu-compiler-core"))
|
||||
implementation(project(":prometeu-compiler:prometeu-frontend-registry"))
|
||||
implementation(libs.jackson.databind)
|
||||
}
|
||||
+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);
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.compiler.exceptions.BuildException;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.model.ResolvedWorkspace;
|
||||
import p.studio.compiler.workspaces.phases.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DependencyPipelineServiceTest {
|
||||
|
||||
@Test
|
||||
void canonical_pipeline_e2e(@TempDir Path tempDir) throws IOException {
|
||||
final Path root = tempDir;
|
||||
final Path depA = root.resolve("depA");
|
||||
Files.createDirectories(depA);
|
||||
Files.createDirectories(root.resolve("src"));
|
||||
Files.createDirectories(depA.resolve("src"));
|
||||
|
||||
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
|
||||
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
|
||||
|
||||
final var cfg = new DependencyPipelineConfig(false, root, List.of());
|
||||
final var phases = List.of(
|
||||
new SeedPhase(),
|
||||
new DiscoverPhase(),
|
||||
new MaterializePhase(),
|
||||
new LocalizePhase(),
|
||||
new ValidatePhase(),
|
||||
new StackPhase(),
|
||||
new PolicyPhase()
|
||||
);
|
||||
|
||||
final var service = new DependencyPipelineService(phases);
|
||||
final ResolvedWorkspace ws = service.run(cfg);
|
||||
|
||||
assertNotNull(ws);
|
||||
assertNotNull(ws.graph());
|
||||
assertEquals(2, ws.graph().projects().size());
|
||||
|
||||
// Obter ids por nome
|
||||
var rootDesc = ws.graph().projects().stream().filter(p -> p.getName().equals("root-proj")).findFirst().orElseThrow();
|
||||
var depDesc = ws.graph().projects().stream().filter(p -> p.getName().equals("dep-a")).findFirst().orElseThrow();
|
||||
final var rootId = rootDesc.getProjectId();
|
||||
final var depId = depDesc.getProjectId();
|
||||
|
||||
// Edges: root -> dep
|
||||
assertTrue(ws.graph().edges().get(rootId.getIndex()).contains(depId));
|
||||
assertTrue(ws.graph().edges().get(depId.getIndex()).isEmpty());
|
||||
|
||||
// Stack: de acordo com a implementação atual (arestas root->dep), root vem antes
|
||||
assertEquals(rootId, ws.stack().projects().get(0));
|
||||
assertEquals(depId, ws.stack().projects().get(1));
|
||||
|
||||
// workspace.root é o projeto root
|
||||
assertEquals(rootId, ws.projectId());
|
||||
}
|
||||
|
||||
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
|
||||
final StringBuilder deps = new StringBuilder();
|
||||
deps.append("[");
|
||||
for (int i = 0; i < localDeps.length; i++) {
|
||||
if (i > 0) deps.append(",");
|
||||
deps.append("{\"path\":\"").append(localDeps[i]).append("\"}");
|
||||
}
|
||||
deps.append("]");
|
||||
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
|
||||
Files.writeString(dir.resolve("prometeu.json"), json);
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.workspaces.phases.DiscoverPhase;
|
||||
import p.studio.compiler.workspaces.phases.SeedPhase;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DiscoverPhaseTest {
|
||||
|
||||
@Test
|
||||
void discover_finds_root_and_local_dependency(@TempDir Path tempDir) throws IOException {
|
||||
// Estrutura: root/ (prometeu.json) -> deps: ["depA/"] e cria depA/(prometeu.json)
|
||||
final Path root = tempDir;
|
||||
final Path depA = root.resolve("depA");
|
||||
Files.createDirectories(depA);
|
||||
|
||||
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"}, null);
|
||||
writeManifest(depA, "dep-a", "0.1.0", new String[]{}, null);
|
||||
|
||||
final var cfg = new DependencyPipelineConfig(false, root, java.util.List.of());
|
||||
final var ctx = DependencyPipelineContext.seed(cfg);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
|
||||
|
||||
final var issues = new DiscoverPhase().run(ctx);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(issues), "Descoberta deve ocorrer sem issues");
|
||||
|
||||
assertEquals(2, ctx.projectInfos.size(), "Dois projetos devem ser descobertos");
|
||||
assertTrue(ctx.projectIndexByDirectory.containsKey(root.toRealPath()), "Projeto raiz indexado");
|
||||
assertTrue(ctx.projectIndexByDirectory.containsKey(depA.toRealPath()), "Projeto depA indexado");
|
||||
assertEquals(0L, ctx.projectIndexByDirectory.get(root.toRealPath()));
|
||||
assertEquals(1L, ctx.projectIndexByDirectory.get(depA.toRealPath()));
|
||||
}
|
||||
|
||||
private static void writeManifest(Path dir, String name, String version, String[] localDeps, String language) throws IOException {
|
||||
final StringBuilder deps = new StringBuilder();
|
||||
deps.append("[");
|
||||
for (int i = 0; i < localDeps.length; i++) {
|
||||
if (i > 0) deps.append(",");
|
||||
deps.append("{\"path\":\"").append(localDeps[i]).append("\"}");
|
||||
}
|
||||
deps.append("]");
|
||||
final String langField = language == null ? "" : ",\n \"language\": \"" + language + "\"";
|
||||
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + langField + "\n}";
|
||||
Files.writeString(dir.resolve("prometeu.json"), json);
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.workspaces.phases.*;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LocalizePhaseTest {
|
||||
|
||||
@Test
|
||||
void localize_resolves_dependency_references(@TempDir Path tempDir) throws IOException {
|
||||
final Path root = tempDir;
|
||||
final Path depA = root.resolve("depA");
|
||||
Files.createDirectories(depA);
|
||||
Files.createDirectories(root.resolve("src"));
|
||||
Files.createDirectories(depA.resolve("src"));
|
||||
|
||||
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
|
||||
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
|
||||
|
||||
final var cfg = new DependencyPipelineConfig(false, root, java.util.List.of());
|
||||
final var ctx = DependencyPipelineContext.seed(cfg);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new MaterializePhase().run(ctx)));
|
||||
|
||||
final var issues = new LocalizePhase().run(ctx);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(issues), "Localização deve ocorrer sem issues");
|
||||
|
||||
final var depId = ctx.projectIdByDirectoryRoot.get(depA.toRealPath());
|
||||
assertNotNull(depId);
|
||||
final var rootId = ctx.root;
|
||||
assertTrue(ctx.dependenciesByProject.get(rootId.getIndex()).contains(depId), "Root deve depender de depA");
|
||||
}
|
||||
|
||||
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
|
||||
final StringBuilder deps = new StringBuilder();
|
||||
deps.append("[");
|
||||
for (int i = 0; i < localDeps.length; i++) {
|
||||
if (i > 0) deps.append(",");
|
||||
deps.append("{\"path\":\"").append(localDeps[i]).append("\"}");
|
||||
}
|
||||
deps.append("]");
|
||||
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
|
||||
Files.writeString(dir.resolve("prometeu.json"), json);
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.workspaces.phases.DiscoverPhase;
|
||||
import p.studio.compiler.workspaces.phases.MaterializePhase;
|
||||
import p.studio.compiler.workspaces.phases.SeedPhase;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MaterializePhaseTest {
|
||||
|
||||
@Test
|
||||
void materialize_builds_nodes_and_sets_root(@TempDir Path tempDir) throws IOException {
|
||||
final Path root = tempDir;
|
||||
final Path depA = root.resolve("depA");
|
||||
Files.createDirectories(depA);
|
||||
Files.createDirectories(root.resolve("src"));
|
||||
Files.createDirectories(depA.resolve("src"));
|
||||
|
||||
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
|
||||
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
|
||||
|
||||
final var cfg = new DependencyPipelineConfig(false, root, java.util.List.of());
|
||||
final var ctx = DependencyPipelineContext.seed(cfg);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
|
||||
|
||||
final var issues = new MaterializePhase().run(ctx);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(issues), "Materialização sem issues esperadas");
|
||||
|
||||
assertNotNull(ctx.root, "Root ProjectId deve ser definido");
|
||||
assertEquals(2, ctx.projectNodes.size());
|
||||
assertEquals(2, ctx.dependenciesByProject.size());
|
||||
assertTrue(ctx.projectIdByDirectoryRoot.containsKey(root.toRealPath()));
|
||||
assertTrue(ctx.projectIdByDirectoryRoot.containsKey(depA.toRealPath()));
|
||||
|
||||
final var rootNode = ctx.projectNodes.get(ctx.root.getIndex());
|
||||
assertEquals("root-proj", rootNode.getName());
|
||||
assertEquals("0.1.0", rootNode.getVersion());
|
||||
assertEquals("pbs", rootNode.getLanguageId());
|
||||
assertFalse(rootNode.getSourceRoots().isEmpty(), "Deve haver pelo menos um source root (src)");
|
||||
}
|
||||
|
||||
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
|
||||
final StringBuilder deps = new StringBuilder();
|
||||
deps.append("[");
|
||||
for (int i = 0; i < localDeps.length; i++) {
|
||||
if (i > 0) deps.append(",");
|
||||
deps.append("{\"path\":\"").append(localDeps[i]).append("\"}");
|
||||
}
|
||||
deps.append("]");
|
||||
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
|
||||
Files.writeString(dir.resolve("prometeu.json"), json);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.workspaces.phases.SeedPhase;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SeedPhaseTest {
|
||||
|
||||
@Test
|
||||
void seed_initializes_root_and_pending(@TempDir Path tempDir) throws Exception {
|
||||
final var cfg = new DependencyPipelineConfig(false, tempDir, java.util.List.of());
|
||||
final var ctx = DependencyPipelineContext.seed(cfg);
|
||||
|
||||
final var issues = new SeedPhase().run(ctx);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(issues), "Nenhum issue esperado na SeedPhase");
|
||||
|
||||
assertNotNull(ctx.mainProjectRootPathCanon, "Caminho canônico do projeto raiz deve ser inicializado");
|
||||
assertEquals(1, ctx.pending.size(), "Uma entrada deve ser enfileirada para descoberta inicial");
|
||||
assertEquals(tempDir.toRealPath(), ctx.mainProjectRootPathCanon);
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.workspaces.phases.*;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StackPhaseTest {
|
||||
|
||||
@Test
|
||||
void stack_topologically_sorts_projects(@TempDir Path tempDir) throws IOException {
|
||||
final Path root = tempDir;
|
||||
final Path depA = root.resolve("depA");
|
||||
Files.createDirectories(depA);
|
||||
Files.createDirectories(root.resolve("src"));
|
||||
Files.createDirectories(depA.resolve("src"));
|
||||
|
||||
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
|
||||
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
|
||||
|
||||
final var cfg = new DependencyPipelineConfig(false, root, java.util.List.of());
|
||||
final var ctx = DependencyPipelineContext.seed(cfg);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new MaterializePhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new LocalizePhase().run(ctx)));
|
||||
|
||||
final var issues = new StackPhase().run(ctx);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(issues), "Ordenação sem issues");
|
||||
|
||||
assertNotNull(ctx.stack);
|
||||
assertEquals(2, ctx.stack.projects().size());
|
||||
final var depId = ctx.projectIdByDirectoryRoot.get(depA.toRealPath());
|
||||
final var rootId = ctx.root;
|
||||
assertEquals(rootId, ctx.stack.projects().get(0), "Root (sem entradas) vem primeiro pelo algoritmo atual");
|
||||
assertEquals(depId, ctx.stack.projects().get(1));
|
||||
}
|
||||
|
||||
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
|
||||
final StringBuilder deps = new StringBuilder();
|
||||
deps.append("[");
|
||||
for (int i = 0; i < localDeps.length; i++) {
|
||||
if (i > 0) deps.append(",");
|
||||
deps.append("{\"path\":\"").append(localDeps[i]).append("\"}");
|
||||
}
|
||||
deps.append("]");
|
||||
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
|
||||
Files.writeString(dir.resolve("prometeu.json"), json);
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.compiler.model.DependencyPipelineConfig;
|
||||
import p.studio.compiler.workspaces.phases.*;
|
||||
import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ValidateAndPolicyPhasesTest {
|
||||
|
||||
@Test
|
||||
void validate_checks_consistency_without_issues(@TempDir Path tempDir) throws IOException {
|
||||
final Path root = tempDir;
|
||||
final Path depA = root.resolve("depA");
|
||||
Files.createDirectories(depA);
|
||||
Files.createDirectories(root.resolve("src"));
|
||||
Files.createDirectories(depA.resolve("src"));
|
||||
|
||||
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
|
||||
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
|
||||
|
||||
final var cfg = new DependencyPipelineConfig(false, root, java.util.List.of());
|
||||
final var ctx = DependencyPipelineContext.seed(cfg);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new MaterializePhase().run(ctx)));
|
||||
assertTrue(ReadOnlyCollection.isEmpty(new LocalizePhase().run(ctx)));
|
||||
|
||||
final var issues = new ValidatePhase().run(ctx);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(issues), "Validação não deve reportar issues no cenário feliz");
|
||||
}
|
||||
|
||||
@Test
|
||||
void policy_is_noop(@TempDir Path tempDir) {
|
||||
final var cfg = new DependencyPipelineConfig(false, tempDir, java.util.List.of());
|
||||
final var ctx = DependencyPipelineContext.seed(cfg);
|
||||
final var issues = new PolicyPhase().run(ctx);
|
||||
assertTrue(ReadOnlyCollection.isEmpty(issues), "PolicyPhase atualmente é no-op");
|
||||
}
|
||||
|
||||
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
|
||||
final StringBuilder deps = new StringBuilder();
|
||||
deps.append("[");
|
||||
for (int i = 0; i < localDeps.length; i++) {
|
||||
if (i > 0) deps.append(",");
|
||||
deps.append("{\"path\":\"").append(localDeps[i]).append("\"}");
|
||||
}
|
||||
deps.append("]");
|
||||
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
|
||||
Files.writeString(dir.resolve("prometeu.json"), json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id("gradle.java-library-conventions")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":prometeu-infra"))
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package p.studio.compiler;
|
||||
|
||||
public class FrontendRegistryService {
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
plugins {
|
||||
id("gradle.java-library-conventions")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":prometeu-infra"))
|
||||
implementation(project(":prometeu-compiler:prometeu-compiler-core"))
|
||||
|
||||
implementation(project(":prometeu-compiler:frontends:prometeu-frontend-pbs"))
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package p.studio.compiler;
|
||||
|
||||
import p.studio.compiler.model.FrontendSpec;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class FrontendRegistryService {
|
||||
private static final FrontendSpec[] FRONTEND_SPECS = {
|
||||
PBSDefinitions.PBS,
|
||||
};
|
||||
|
||||
private static final Map<String, FrontendSpec> FRONTENDS = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (final var frontendSpec : FRONTEND_SPECS) {
|
||||
FRONTENDS.put(frontendSpec.getLanguageId(), frontendSpec);
|
||||
}
|
||||
if (FRONTENDS.size() != FRONTEND_SPECS.length) throw new IllegalStateException("Duplicate frontend specs found");
|
||||
}
|
||||
|
||||
public static FrontendSpec getDefaultFrontendSpec() {
|
||||
return FRONTENDS.get(PBSDefinitions.PBS.getLanguageId());
|
||||
}
|
||||
|
||||
public static Optional<FrontendSpec> getFrontendSpec(final String languageId) {
|
||||
return Optional.ofNullable(FRONTENDS.get(languageId));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user