asset addressable PBS surface
This commit is contained in:
+97
@@ -0,0 +1,97 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.studio.compiler.messages.Addressable;
|
||||
import p.studio.compiler.messages.FESurfaceContext;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class AssetSurfaceContextLoader {
|
||||
private static final Path REGISTRY_PATH = Path.of("assets", ".prometeu", "index.json");
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public AssetSurfaceContextLoader() {
|
||||
this(new ObjectMapper());
|
||||
}
|
||||
|
||||
AssetSurfaceContextLoader(final ObjectMapper mapper) {
|
||||
this.mapper = Objects.requireNonNull(mapper, "mapper");
|
||||
}
|
||||
|
||||
public FESurfaceContext load(final Path projectRoot) {
|
||||
final var registryPath = Objects.requireNonNull(projectRoot, "projectRoot")
|
||||
.toAbsolutePath()
|
||||
.normalize()
|
||||
.resolve(REGISTRY_PATH);
|
||||
if (!Files.isRegularFile(registryPath)) {
|
||||
return FESurfaceContext.empty();
|
||||
}
|
||||
try {
|
||||
final var document = mapper.readValue(registryPath.toFile(), RegistryDocument.class);
|
||||
final var addressables = new ArrayList<Addressable>();
|
||||
if (document.assets != null) {
|
||||
for (final var asset : document.assets) {
|
||||
if (asset == null || !isIncludedInBuild(asset)) {
|
||||
continue;
|
||||
}
|
||||
addressables.add(new Addressable(toAddress(asset.root()), asset.assetId()));
|
||||
}
|
||||
}
|
||||
return new FESurfaceContext(ReadOnlyList.wrap(addressables));
|
||||
} catch (IOException exception) {
|
||||
throw new IllegalStateException("Unable to load asset surface registry: " + registryPath, exception);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isIncludedInBuild(final RegistryAssetDocument asset) {
|
||||
return asset.includedInBuild() == null || asset.includedInBuild();
|
||||
}
|
||||
|
||||
private String toAddress(final String root) {
|
||||
final var normalizedRoot = normalizeRoot(root);
|
||||
if (normalizedRoot.isBlank()) {
|
||||
throw new IllegalStateException("Asset root must not be blank in surface registry");
|
||||
}
|
||||
return "assets." + normalizedRoot.replace('/', '.');
|
||||
}
|
||||
|
||||
private String normalizeRoot(final String root) {
|
||||
if (root == null) {
|
||||
return "";
|
||||
}
|
||||
final var trimmed = root.trim().replace('\\', '/');
|
||||
final List<String> segments = new ArrayList<>();
|
||||
for (final var rawSegment : trimmed.split("/")) {
|
||||
final var segment = rawSegment.trim();
|
||||
if (segment.isEmpty() || ".".equals(segment)) {
|
||||
continue;
|
||||
}
|
||||
if ("..".equals(segment)) {
|
||||
throw new IllegalStateException("Asset root escapes trusted boundary: " + root);
|
||||
}
|
||||
segments.add(segment);
|
||||
}
|
||||
return String.join("/", segments);
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
private record RegistryDocument(
|
||||
@JsonProperty("assets") List<RegistryAssetDocument> assets) {
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
private record RegistryAssetDocument(
|
||||
@JsonProperty("asset_id") int assetId,
|
||||
@JsonProperty("root") String root,
|
||||
@JsonProperty("included_in_build") Boolean includedInBuild) {
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -5,12 +5,15 @@ import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.messages.FrontendPhaseContext;
|
||||
import p.studio.compiler.models.BuilderPipelineContext;
|
||||
import p.studio.compiler.workspaces.AssetSurfaceContextLoader;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.workspaces.PipelineStage;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
|
||||
@Slf4j
|
||||
public class FrontendPhasePipelineStage implements PipelineStage {
|
||||
private final AssetSurfaceContextLoader assetSurfaceContextLoader = new AssetSurfaceContextLoader();
|
||||
|
||||
@Override
|
||||
public BuildingIssueSink run(final BuilderPipelineContext ctx, final LogAggregator logs) {
|
||||
final var frontedSpec = ctx.resolvedWorkspace.frontendSpec();
|
||||
@@ -27,7 +30,9 @@ public class FrontendPhasePipelineStage implements PipelineStage {
|
||||
projectTable,
|
||||
fileTable,
|
||||
ctx.resolvedWorkspace.stack(),
|
||||
ctx.resolvedWorkspace.stdlib());
|
||||
ctx.resolvedWorkspace.stdlib(),
|
||||
p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),
|
||||
assetSurfaceContextLoader.load(ctx.resolvedWorkspace.mainProject().getRootPath()));
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var issues = BuildingIssueSink.empty();
|
||||
ctx.irBackend = service.get().compile(frontendPhaseContext, diagnostics, logs, issues);
|
||||
|
||||
-12
@@ -16,8 +16,6 @@ class BackendClaimScopeSpecTest {
|
||||
"docs/specs/compiler/20. IRBackend to IRVM Lowering Specification.md";
|
||||
private static final String MATRIX_RELATIVE_PATH =
|
||||
"docs/specs/compiler/22. Backend Spec-to-Test Conformance Matrix.md";
|
||||
private static final String DECISION_RELATIVE_PATH =
|
||||
"docs/compiler/pbs/decisions/SPAWN-YIELD v1 Claim Rescope Decision.md";
|
||||
|
||||
@Test
|
||||
void loweringSpecMustDeclareSpawnYieldOutsideCoreV1ClaimScope() throws IOException {
|
||||
@@ -43,16 +41,6 @@ class BackendClaimScopeSpecTest {
|
||||
"G20-9.4.3 row must include explicit claim rescope note");
|
||||
}
|
||||
|
||||
@Test
|
||||
void decisionRecordMustExistForSpawnYieldClaimRescope() throws IOException {
|
||||
final var decisionPath = locateRepoRoot().resolve(DECISION_RELATIVE_PATH);
|
||||
assertTrue(Files.isRegularFile(decisionPath),
|
||||
"claim rescope decision is missing: " + decisionPath);
|
||||
final var content = Files.readString(decisionPath);
|
||||
assertTrue(content.contains("Trilha B"),
|
||||
"decision must record official Track B claim rescope choice");
|
||||
}
|
||||
|
||||
private Optional<String> findRequirementRow(
|
||||
final List<String> lines,
|
||||
final String requirementId) {
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package p.studio.compiler.workspaces;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AssetSurfaceContextLoaderTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void shouldLoadOnlyIncludedAssetsAsAddressables() throws IOException {
|
||||
final var registryDir = tempDir.resolve("assets").resolve(".prometeu");
|
||||
Files.createDirectories(registryDir);
|
||||
Files.writeString(registryDir.resolve("index.json"), """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"next_asset_id": 4,
|
||||
"assets": [
|
||||
{ "asset_id": 3, "root": "ui/atlas2", "included_in_build": true },
|
||||
{ "asset_id": 7, "root": "ui/one-more-atlas", "included_in_build": false },
|
||||
{ "asset_id": 8, "root": "ui/sound" }
|
||||
]
|
||||
}
|
||||
""");
|
||||
|
||||
final var surface = new AssetSurfaceContextLoader().load(tempDir);
|
||||
|
||||
assertEquals(2, surface.assets().size());
|
||||
assertEquals("assets.ui.atlas2", surface.assets().get(0).address());
|
||||
assertEquals(3, surface.assets().get(0).assetId());
|
||||
assertEquals("assets.ui.sound", surface.assets().get(1).address());
|
||||
assertEquals(8, surface.assets().get(1).assetId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptySurfaceWhenRegistryIsMissing() {
|
||||
final var surface = new AssetSurfaceContextLoader().load(tempDir);
|
||||
|
||||
assertTrue(surface.assets().isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user