asset workspace progress
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
plugins {
|
||||
id("gradle.java-library-conventions")
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api;
|
||||
package p.packer;
|
||||
|
||||
public enum PackerOperationStatus {
|
||||
SUCCESS,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api;
|
||||
package p.packer;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
package p.packer.api.workspace;
|
||||
package p.packer;
|
||||
|
||||
import p.packer.api.PackerOperationClass;
|
||||
import p.packer.messages.*;
|
||||
|
||||
public interface PackerWorkspaceService {
|
||||
PackerOperationClass operationClass();
|
||||
|
||||
InitWorkspaceResult initWorkspace(InitWorkspaceRequest request);
|
||||
|
||||
ListAssetsResult listAssets(ListAssetsRequest request);
|
||||
|
||||
GetAssetDetailsResult getAssetDetails(GetAssetDetailsRequest request);
|
||||
|
||||
CreateAssetResult createAsset(CreateAssetRequest request);
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package p.packer.assets;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public enum AssetFamilyCatalog {
|
||||
IMAGE_BANK("image_bank", "Image Bank"),
|
||||
PALETTE_BANK("palette_bank", "Palette Bank"),
|
||||
SOUND_BANK("sound_bank", "Sound Bank"),
|
||||
UNKNOWN("unknown", "Unknown");
|
||||
|
||||
private final String manifestType;
|
||||
private final String displayName;
|
||||
|
||||
AssetFamilyCatalog(String manifestType, String displayName) {
|
||||
this.manifestType = manifestType;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public String manifestType() {
|
||||
return manifestType;
|
||||
}
|
||||
|
||||
public String displayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public boolean matchesQuery(String normalizedQuery) {
|
||||
return manifestType.contains(normalizedQuery)
|
||||
|| displayName.toLowerCase(Locale.ROOT).contains(normalizedQuery);
|
||||
}
|
||||
|
||||
public static AssetFamilyCatalog fromManifestType(String manifestType) {
|
||||
if (manifestType == null) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
final String normalized = manifestType.trim().toLowerCase(Locale.ROOT);
|
||||
for (AssetFamilyCatalog candidate : values()) {
|
||||
if (candidate == UNKNOWN) {
|
||||
continue;
|
||||
}
|
||||
if (candidate.manifestType.equals(normalized)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package p.packer.assets;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record AssetReference(String value) {
|
||||
private static final String ASSET_ID_PREFIX = "asset:id:";
|
||||
private static final String ASSET_ROOT_PREFIX = "asset:root:";
|
||||
|
||||
public AssetReference {
|
||||
value = Objects.requireNonNull(value, "value").trim();
|
||||
if (value.isBlank()) {
|
||||
throw new IllegalArgumentException("value must not be blank");
|
||||
}
|
||||
}
|
||||
|
||||
public static AssetReference of(String value) {
|
||||
return new AssetReference(value);
|
||||
}
|
||||
|
||||
public static AssetReference forAssetId(int assetId) {
|
||||
if (assetId <= 0) {
|
||||
throw new IllegalArgumentException("assetId must be positive");
|
||||
}
|
||||
return new AssetReference(ASSET_ID_PREFIX + assetId);
|
||||
}
|
||||
|
||||
public static AssetReference forRelativeAssetRoot(String relativeAssetRoot) {
|
||||
final String normalized = Objects.requireNonNull(relativeAssetRoot, "relativeAssetRoot").trim();
|
||||
if (normalized.isBlank()) {
|
||||
throw new IllegalArgumentException("relativeAssetRoot must not be blank");
|
||||
}
|
||||
return new AssetReference(ASSET_ROOT_PREFIX + normalized);
|
||||
}
|
||||
|
||||
public boolean isAssetIdReference() {
|
||||
return value.startsWith(ASSET_ID_PREFIX);
|
||||
}
|
||||
|
||||
public boolean isAssetRootReference() {
|
||||
return value.startsWith(ASSET_ROOT_PREFIX);
|
||||
}
|
||||
|
||||
public String rawValue() {
|
||||
if (isAssetIdReference()) {
|
||||
return value.substring(ASSET_ID_PREFIX.length());
|
||||
}
|
||||
if (isAssetRootReference()) {
|
||||
return value.substring(ASSET_ROOT_PREFIX.length());
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package p.packer.assets;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public enum OutputCodecCatalog {
|
||||
NONE("NONE", "None"),
|
||||
UNKNOWN("UNKNOWN", "Unknown");
|
||||
|
||||
private final String manifestValue;
|
||||
private final String displayName;
|
||||
|
||||
OutputCodecCatalog(String manifestValue, String displayName) {
|
||||
this.manifestValue = manifestValue;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public String manifestValue() {
|
||||
return manifestValue;
|
||||
}
|
||||
|
||||
public String displayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public static OutputCodecCatalog fromManifestValue(String manifestValue) {
|
||||
if (manifestValue == null) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
final String normalized = manifestValue.trim().toUpperCase(Locale.ROOT);
|
||||
for (OutputCodecCatalog candidate : values()) {
|
||||
if (candidate.manifestValue.equals(normalized)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package p.packer.assets;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public enum OutputFormatCatalog {
|
||||
TILES_INDEXED_V1(AssetFamilyCatalog.IMAGE_BANK, "TILES/indexed_v1", "TILES/indexed_v1"),
|
||||
PALETTE_INDEXED_V1(AssetFamilyCatalog.PALETTE_BANK, "PALETTE/indexed_v1", "PALETTE/indexed_v1"),
|
||||
SOUND_BANK_V1(AssetFamilyCatalog.SOUND_BANK, "SOUND/bank_v1", "SOUND/bank_v1"),
|
||||
AUDIO_PCM_V1(AssetFamilyCatalog.SOUND_BANK, "AUDIO/pcm_v1", "AUDIO/pcm_v1"),
|
||||
UNKNOWN(AssetFamilyCatalog.UNKNOWN, "unknown", "Unknown");
|
||||
|
||||
private final AssetFamilyCatalog assetFamily;
|
||||
private final String manifestValue;
|
||||
private final String displayName;
|
||||
|
||||
OutputFormatCatalog(AssetFamilyCatalog assetFamily, String manifestValue, String displayName) {
|
||||
this.assetFamily = assetFamily;
|
||||
this.manifestValue = manifestValue;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public AssetFamilyCatalog assetFamily() {
|
||||
return assetFamily;
|
||||
}
|
||||
|
||||
public String manifestValue() {
|
||||
return manifestValue;
|
||||
}
|
||||
|
||||
public String displayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public List<OutputCodecCatalog> availableCodecs() {
|
||||
return this == UNKNOWN ? List.of() : List.of(OutputCodecCatalog.NONE);
|
||||
}
|
||||
|
||||
public boolean supports(OutputCodecCatalog codec) {
|
||||
return availableCodecs().contains(codec);
|
||||
}
|
||||
|
||||
public static List<OutputFormatCatalog> supportedFor(AssetFamilyCatalog assetFamily) {
|
||||
if (assetFamily == null || assetFamily == AssetFamilyCatalog.UNKNOWN) {
|
||||
return List.of();
|
||||
}
|
||||
return java.util.Arrays.stream(values())
|
||||
.filter(candidate -> candidate != UNKNOWN)
|
||||
.filter(candidate -> candidate.assetFamily == assetFamily)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static OutputFormatCatalog fromManifestValue(String manifestValue) {
|
||||
if (manifestValue == null) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
final String normalized = manifestValue.trim().toLowerCase(Locale.ROOT);
|
||||
for (OutputFormatCatalog candidate : values()) {
|
||||
if (candidate == UNKNOWN) {
|
||||
continue;
|
||||
}
|
||||
if (candidate.manifestValue.toLowerCase(Locale.ROOT).equals(normalized)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
+8
-4
@@ -1,6 +1,6 @@
|
||||
package p.packer.api.assets;
|
||||
package p.packer.assets;
|
||||
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
@@ -10,14 +10,18 @@ import java.util.Objects;
|
||||
public record PackerAssetDetails(
|
||||
PackerAssetSummary summary,
|
||||
String outputFormat,
|
||||
String outputCodec,
|
||||
OutputCodecCatalog outputCodec,
|
||||
List<OutputCodecCatalog> availableOutputCodecs,
|
||||
Map<OutputCodecCatalog, List<PackerCodecConfigurationField>> codecConfigurationFieldsByCodec,
|
||||
Map<String, List<Path>> inputsByRole,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
|
||||
public PackerAssetDetails {
|
||||
Objects.requireNonNull(summary, "summary");
|
||||
outputFormat = Objects.requireNonNullElse(outputFormat, "unknown").trim();
|
||||
outputCodec = Objects.requireNonNullElse(outputCodec, "unknown").trim();
|
||||
outputCodec = Objects.requireNonNullElse(outputCodec, OutputCodecCatalog.UNKNOWN);
|
||||
availableOutputCodecs = List.copyOf(Objects.requireNonNull(availableOutputCodecs, "availableOutputCodecs"));
|
||||
codecConfigurationFieldsByCodec = Map.copyOf(Objects.requireNonNull(codecConfigurationFieldsByCodec, "codecConfigurationFieldsByCodec"));
|
||||
inputsByRole = Map.copyOf(Objects.requireNonNull(inputsByRole, "inputsByRole"));
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.assets;
|
||||
package p.packer.assets;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.assets;
|
||||
package p.packer.assets;
|
||||
|
||||
public enum PackerAssetState {
|
||||
REGISTERED,
|
||||
+5
-6
@@ -1,23 +1,22 @@
|
||||
package p.packer.api.assets;
|
||||
package p.packer.assets;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerAssetSummary(
|
||||
AssetReference assetReference,
|
||||
PackerAssetIdentity identity,
|
||||
PackerAssetState state,
|
||||
PackerBuildParticipation buildParticipation,
|
||||
String assetFamily,
|
||||
AssetFamilyCatalog assetFamily,
|
||||
boolean preloadEnabled,
|
||||
boolean hasDiagnostics) {
|
||||
|
||||
public PackerAssetSummary {
|
||||
Objects.requireNonNull(assetReference, "assetReference");
|
||||
Objects.requireNonNull(identity, "identity");
|
||||
Objects.requireNonNull(state, "state");
|
||||
Objects.requireNonNull(buildParticipation, "buildParticipation");
|
||||
assetFamily = Objects.requireNonNullElse(assetFamily, "unknown").trim();
|
||||
if (assetFamily.isBlank()) {
|
||||
assetFamily = "unknown";
|
||||
}
|
||||
assetFamily = Objects.requireNonNullElse(assetFamily, AssetFamilyCatalog.UNKNOWN);
|
||||
if (state == PackerAssetState.REGISTERED && identity.assetId() == null) {
|
||||
throw new IllegalArgumentException("registered asset must expose assetId");
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.assets;
|
||||
package p.packer.assets;
|
||||
|
||||
public enum PackerBuildParticipation {
|
||||
INCLUDED,
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package p.packer.assets;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerCodecConfigurationField(
|
||||
String key,
|
||||
String label,
|
||||
PackerCodecConfigurationFieldType fieldType,
|
||||
String value,
|
||||
boolean required,
|
||||
List<String> options) {
|
||||
|
||||
public PackerCodecConfigurationField {
|
||||
key = Objects.requireNonNull(key, "key").trim();
|
||||
label = Objects.requireNonNull(label, "label").trim();
|
||||
Objects.requireNonNull(fieldType, "fieldType");
|
||||
value = Objects.requireNonNullElse(value, "").trim();
|
||||
options = List.copyOf(Objects.requireNonNull(options, "options"));
|
||||
if (key.isBlank() || label.isBlank()) {
|
||||
throw new IllegalArgumentException("codec configuration field key and label must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package p.packer.assets;
|
||||
|
||||
public enum PackerCodecConfigurationFieldType {
|
||||
TEXT,
|
||||
BOOLEAN,
|
||||
INTEGER,
|
||||
ENUM
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.diagnostics;
|
||||
package p.packer.diagnostics;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.diagnostics;
|
||||
package p.packer.diagnostics;
|
||||
|
||||
public enum PackerDiagnosticCategory {
|
||||
STRUCTURAL,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.diagnostics;
|
||||
package p.packer.diagnostics;
|
||||
|
||||
public enum PackerDiagnosticSeverity {
|
||||
INFO,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.events;
|
||||
package p.packer.events;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
@@ -0,0 +1,8 @@
|
||||
package p.packer.events;
|
||||
|
||||
public enum PackerEventKind {
|
||||
ASSET_DISCOVERED,
|
||||
DIAGNOSTICS_UPDATED,
|
||||
ACTION_APPLIED,
|
||||
ACTION_FAILED
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package p.packer.events;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PackerEventSink {
|
||||
void publish(PackerEvent event);
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.api.events;
|
||||
package p.packer.events;
|
||||
|
||||
public record PackerProgress(
|
||||
double value,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.exceptions;
|
||||
|
||||
public final class PackerRegistryException extends RuntimeException {
|
||||
public PackerRegistryException(String message) {
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.assets.OutputFormatCatalog;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record CreateAssetRequest(
|
||||
PackerProjectContext project,
|
||||
String assetRoot,
|
||||
String assetName,
|
||||
AssetFamilyCatalog assetFamily,
|
||||
OutputFormatCatalog outputFormat,
|
||||
OutputCodecCatalog outputCodec,
|
||||
boolean preloadEnabled) {
|
||||
|
||||
public CreateAssetRequest {
|
||||
Objects.requireNonNull(project, "project");
|
||||
assetRoot = Objects.requireNonNull(assetRoot, "assetRoot").trim();
|
||||
assetName = Objects.requireNonNull(assetName, "assetName").trim();
|
||||
assetFamily = Objects.requireNonNullElse(assetFamily, AssetFamilyCatalog.UNKNOWN);
|
||||
outputFormat = Objects.requireNonNullElse(outputFormat, OutputFormatCatalog.UNKNOWN);
|
||||
outputCodec = Objects.requireNonNullElse(outputCodec, OutputCodecCatalog.UNKNOWN);
|
||||
if (assetRoot.isBlank()) {
|
||||
throw new IllegalArgumentException("assetRoot must not be blank");
|
||||
}
|
||||
if (assetName.isBlank()) {
|
||||
throw new IllegalArgumentException("assetName must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.assets.AssetReference;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public record CreateAssetResult(
|
||||
PackerOperationStatus status,
|
||||
String summary,
|
||||
AssetReference assetReference,
|
||||
Path assetRoot,
|
||||
Path manifestPath) {
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetReference;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record GetAssetDetailsRequest(
|
||||
PackerProjectContext project,
|
||||
AssetReference assetReference) {
|
||||
|
||||
public GetAssetDetailsRequest {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(assetReference, "assetReference");
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
package p.packer.api.workspace;
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.assets.PackerAssetDetails;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.assets.PackerAssetDetails;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package p.packer.api.workspace;
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package p.packer.api.workspace;
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package p.packer.api.workspace;
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
package p.packer.api.workspace;
|
||||
package p.packer.messages;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.assets.PackerAssetSummary;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.assets.PackerAssetSummary;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -4,4 +4,5 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
implementation(project(":prometeu-infra"))
|
||||
implementation(project(":prometeu-packer:prometeu-packer-api"))
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package p.packer;
|
||||
|
||||
import p.packer.events.PackerEventSink;
|
||||
import p.packer.services.*;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Packer implements Closeable {
|
||||
private final PackerWorkspaceService workspaceService;
|
||||
private final PackerRuntimeRegistry runtimeRegistry;
|
||||
private final PackerProjectWriteCoordinator writeCoordinator;
|
||||
|
||||
private Packer(
|
||||
PackerWorkspaceService workspaceService,
|
||||
PackerRuntimeRegistry runtimeRegistry,
|
||||
PackerProjectWriteCoordinator writeCoordinator) {
|
||||
this.workspaceService = Objects.requireNonNull(workspaceService, "workspaceService");
|
||||
this.runtimeRegistry = Objects.requireNonNull(runtimeRegistry, "runtimeRegistry");
|
||||
this.writeCoordinator = Objects.requireNonNull(writeCoordinator, "writeCoordinator");
|
||||
}
|
||||
|
||||
public static Packer bootstrap(PackerEventSink eventSink) {
|
||||
final PackerEventSink resolvedEventSink = Objects.requireNonNull(eventSink, "eventSink");
|
||||
final PackerWorkspaceFoundation workspaceFoundation = new PackerWorkspaceFoundation();
|
||||
final PackerAssetDeclarationParser declarationParser = new PackerAssetDeclarationParser();
|
||||
final PackerRuntimeRegistry runtimeRegistry = new PackerRuntimeRegistry(
|
||||
new PackerRuntimeLoader(workspaceFoundation, declarationParser));
|
||||
final PackerAssetDetailsService assetDetailsService = new PackerAssetDetailsService(runtimeRegistry);
|
||||
final PackerProjectWriteCoordinator writeCoordinator = new PackerProjectWriteCoordinator();
|
||||
return new Packer(new FileSystemPackerWorkspaceService(
|
||||
workspaceFoundation,
|
||||
assetDetailsService,
|
||||
runtimeRegistry,
|
||||
writeCoordinator,
|
||||
resolvedEventSink), runtimeRegistry, writeCoordinator);
|
||||
}
|
||||
|
||||
public PackerWorkspaceService workspaceService() {
|
||||
return workspaceService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
writeCoordinator.close();
|
||||
runtimeRegistry.disposeAll();
|
||||
}
|
||||
}
|
||||
+11
-6
@@ -1,4 +1,7 @@
|
||||
package p.packer.declarations;
|
||||
package p.packer.models;
|
||||
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -6,23 +9,25 @@ import java.util.Objects;
|
||||
|
||||
public record PackerAssetDeclaration(
|
||||
int schemaVersion,
|
||||
String assetUuid,
|
||||
String name,
|
||||
String type,
|
||||
AssetFamilyCatalog assetFamily,
|
||||
Map<String, List<String>> inputsByRole,
|
||||
String outputFormat,
|
||||
String outputCodec,
|
||||
OutputCodecCatalog outputCodec,
|
||||
boolean preloadEnabled) {
|
||||
|
||||
public PackerAssetDeclaration {
|
||||
if (schemaVersion <= 0) {
|
||||
throw new IllegalArgumentException("schemaVersion must be positive");
|
||||
}
|
||||
assetUuid = Objects.requireNonNull(assetUuid, "assetUuid").trim();
|
||||
name = Objects.requireNonNull(name, "name").trim();
|
||||
type = Objects.requireNonNull(type, "type").trim();
|
||||
assetFamily = Objects.requireNonNull(assetFamily, "assetFamily");
|
||||
inputsByRole = Map.copyOf(Objects.requireNonNull(inputsByRole, "inputsByRole"));
|
||||
outputFormat = Objects.requireNonNull(outputFormat, "outputFormat").trim();
|
||||
outputCodec = Objects.requireNonNull(outputCodec, "outputCodec").trim();
|
||||
if (name.isBlank() || type.isBlank() || outputFormat.isBlank() || outputCodec.isBlank()) {
|
||||
outputCodec = Objects.requireNonNull(outputCodec, "outputCodec");
|
||||
if (assetUuid.isBlank() || name.isBlank() || outputFormat.isBlank()) {
|
||||
throw new IllegalArgumentException("declaration fields must not be blank");
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package p.packer.declarations;
|
||||
package p.packer.models;
|
||||
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.models;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.models;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package p.packer.models;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public record PackerRuntimeAsset(
|
||||
Path assetRoot,
|
||||
Path manifestPath,
|
||||
Optional<PackerRegistryEntry> registryEntry,
|
||||
PackerAssetDeclarationParseResult parsedDeclaration) {
|
||||
|
||||
public PackerRuntimeAsset {
|
||||
assetRoot = Objects.requireNonNull(assetRoot, "assetRoot").toAbsolutePath().normalize();
|
||||
manifestPath = Objects.requireNonNull(manifestPath, "manifestPath").toAbsolutePath().normalize();
|
||||
registryEntry = Objects.requireNonNull(registryEntry, "registryEntry");
|
||||
parsedDeclaration = Objects.requireNonNull(parsedDeclaration, "parsedDeclaration");
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package p.packer.models;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerRuntimeSnapshot(
|
||||
long generation,
|
||||
PackerRegistryState registry,
|
||||
List<PackerRuntimeAsset> assets) {
|
||||
|
||||
public PackerRuntimeSnapshot {
|
||||
if (generation <= 0L) {
|
||||
throw new IllegalArgumentException("generation must be positive");
|
||||
}
|
||||
registry = Objects.requireNonNull(registry, "registry");
|
||||
assets = List.copyOf(Objects.requireNonNull(assets, "assets"));
|
||||
}
|
||||
}
|
||||
+12
-10
@@ -1,9 +1,11 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.services;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -29,7 +31,7 @@ public final class FileSystemPackerRegistryRepository implements PackerRegistryR
|
||||
final RegistryDocument document = MAPPER.readValue(registryPath.toFile(), RegistryDocument.class);
|
||||
final int schemaVersion = document.schemaVersion <= 0 ? REGISTRY_SCHEMA_VERSION : document.schemaVersion;
|
||||
if (schemaVersion != REGISTRY_SCHEMA_VERSION) {
|
||||
throw new PackerRegistryException("Unsupported registry schema_version: " + schemaVersion);
|
||||
throw new p.packer.exceptions.PackerRegistryException("Unsupported registry schema_version: " + schemaVersion);
|
||||
}
|
||||
final List<PackerRegistryEntry> entries = new ArrayList<>();
|
||||
if (document.assets != null) {
|
||||
@@ -53,7 +55,7 @@ public final class FileSystemPackerRegistryRepository implements PackerRegistryR
|
||||
nextAssetId,
|
||||
entries.stream().sorted(Comparator.comparingInt(PackerRegistryEntry::assetId)).toList());
|
||||
} catch (IOException exception) {
|
||||
throw new PackerRegistryException("Unable to load registry: " + registryPath, exception);
|
||||
throw new p.packer.exceptions.PackerRegistryException("Unable to load registry: " + registryPath, exception);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +78,7 @@ public final class FileSystemPackerRegistryRepository implements PackerRegistryR
|
||||
.toList();
|
||||
MAPPER.writerWithDefaultPrettyPrinter().writeValue(registryPath.toFile(), document);
|
||||
} catch (IOException exception) {
|
||||
throw new PackerRegistryException("Unable to save registry: " + registryPath, exception);
|
||||
throw new p.packer.exceptions.PackerRegistryException("Unable to save registry: " + registryPath, exception);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,25 +92,25 @@ public final class FileSystemPackerRegistryRepository implements PackerRegistryR
|
||||
final Set<String> roots = new HashSet<>();
|
||||
for (PackerRegistryEntry entry : entries) {
|
||||
if (!assetIds.add(entry.assetId())) {
|
||||
throw new PackerRegistryException("Duplicate asset_id in registry: " + entry.assetId());
|
||||
throw new p.packer.exceptions.PackerRegistryException("Duplicate asset_id in registry: " + entry.assetId());
|
||||
}
|
||||
if (!assetUuids.add(entry.assetUuid())) {
|
||||
throw new PackerRegistryException("Duplicate asset_uuid in registry: " + entry.assetUuid());
|
||||
throw new p.packer.exceptions.PackerRegistryException("Duplicate asset_uuid in registry: " + entry.assetUuid());
|
||||
}
|
||||
if (!roots.add(entry.root())) {
|
||||
throw new PackerRegistryException("Duplicate asset root in registry: " + entry.root());
|
||||
throw new p.packer.exceptions.PackerRegistryException("Duplicate asset root in registry: " + entry.root());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeRoot(String root) {
|
||||
if (root == null || root.isBlank()) {
|
||||
throw new PackerRegistryException("Registry asset root must not be blank");
|
||||
throw new p.packer.exceptions.PackerRegistryException("Registry asset root must not be blank");
|
||||
}
|
||||
final String normalized = root.trim().replace('\\', '/');
|
||||
final Path normalizedPath = Path.of(normalized).normalize();
|
||||
if (normalizedPath.isAbsolute() || normalizedPath.startsWith("..")) {
|
||||
throw new PackerRegistryException("Registry asset root is outside the trusted assets boundary: " + normalized);
|
||||
throw new p.packer.exceptions.PackerRegistryException("Registry asset root is outside the trusted assets boundary: " + normalized);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
+324
@@ -0,0 +1,324 @@
|
||||
package p.packer.services;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.AssetReference;
|
||||
import p.packer.assets.OutputFormatCatalog;
|
||||
import p.packer.assets.PackerAssetIdentity;
|
||||
import p.packer.assets.PackerAssetState;
|
||||
import p.packer.assets.PackerAssetSummary;
|
||||
import p.packer.assets.PackerBuildParticipation;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
import p.packer.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.events.PackerEventKind;
|
||||
import p.packer.events.PackerEventSink;
|
||||
import p.packer.events.PackerProgress;
|
||||
import p.packer.messages.CreateAssetRequest;
|
||||
import p.packer.messages.CreateAssetResult;
|
||||
import p.packer.messages.GetAssetDetailsRequest;
|
||||
import p.packer.messages.GetAssetDetailsResult;
|
||||
import p.packer.messages.InitWorkspaceRequest;
|
||||
import p.packer.messages.InitWorkspaceResult;
|
||||
import p.packer.messages.ListAssetsRequest;
|
||||
import p.packer.messages.ListAssetsResult;
|
||||
import p.packer.PackerWorkspaceService;
|
||||
import p.packer.models.PackerAssetDeclarationParseResult;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
import p.packer.models.PackerRuntimeAsset;
|
||||
import p.packer.models.PackerRuntimeSnapshot;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class FileSystemPackerWorkspaceService implements PackerWorkspaceService {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private final PackerWorkspaceFoundation workspaceFoundation;
|
||||
private final PackerAssetDetailsService detailsService;
|
||||
private final PackerRuntimeRegistry runtimeRegistry;
|
||||
private final PackerProjectWriteCoordinator writeCoordinator;
|
||||
private final PackerEventSink eventSink;
|
||||
|
||||
public FileSystemPackerWorkspaceService(
|
||||
PackerWorkspaceFoundation workspaceFoundation,
|
||||
PackerAssetDetailsService detailsService,
|
||||
PackerRuntimeRegistry runtimeRegistry,
|
||||
PackerProjectWriteCoordinator writeCoordinator,
|
||||
PackerEventSink eventSink) {
|
||||
this.workspaceFoundation = Objects.requireNonNull(workspaceFoundation, "workspaceFoundation");
|
||||
this.detailsService = Objects.requireNonNull(detailsService, "detailsService");
|
||||
this.runtimeRegistry = Objects.requireNonNull(runtimeRegistry, "runtimeRegistry");
|
||||
this.writeCoordinator = Objects.requireNonNull(writeCoordinator, "writeCoordinator");
|
||||
this.eventSink = Objects.requireNonNull(eventSink, "eventSink");
|
||||
}
|
||||
|
||||
@Override
|
||||
public InitWorkspaceResult initWorkspace(InitWorkspaceRequest request) {
|
||||
final InitWorkspaceResult result = workspaceFoundation.initWorkspace(request);
|
||||
runtimeRegistry.refresh(Objects.requireNonNull(request, "request").project());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListAssetsResult listAssets(ListAssetsRequest request) {
|
||||
final PackerProjectContext project = Objects.requireNonNull(request, "request").project();
|
||||
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
|
||||
final PackerOperationEventEmitter events = new PackerOperationEventEmitter(project, eventSink);
|
||||
final PackerRegistryState registry = snapshot.registry();
|
||||
final Map<Path, PackerRegistryEntry> registryByRoot = new HashMap<>();
|
||||
for (PackerRegistryEntry entry : registry.assets()) {
|
||||
registryByRoot.put(PackerWorkspacePaths.assetRoot(project, entry.root()), entry);
|
||||
}
|
||||
|
||||
final List<PackerAssetSummary> assets = new ArrayList<>();
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
|
||||
final Set<Path> discoveredRoots = new HashSet<>();
|
||||
|
||||
final List<PackerRuntimeAsset> runtimeAssets = snapshot.assets();
|
||||
final int total = runtimeAssets.size();
|
||||
for (int index = 0; index < runtimeAssets.size(); index += 1) {
|
||||
final PackerRuntimeAsset runtimeAsset = runtimeAssets.get(index);
|
||||
final Path assetRoot = runtimeAsset.assetRoot();
|
||||
final Path assetManifestPath = runtimeAsset.manifestPath();
|
||||
discoveredRoots.add(assetRoot);
|
||||
final PackerRegistryEntry registryEntry = registryByRoot.get(assetRoot);
|
||||
final PackerAssetDeclarationParseResult parsed = runtimeAsset.parsedDeclaration();
|
||||
diagnostics.addAll(parsed.diagnostics());
|
||||
diagnostics.addAll(identityMismatchDiagnostics(registryEntry, parsed, assetManifestPath));
|
||||
final PackerAssetSummary summary = buildSummary(project, assetRoot, registryEntry, parsed);
|
||||
assets.add(summary);
|
||||
events.emit(
|
||||
PackerEventKind.ASSET_DISCOVERED,
|
||||
"Discovered asset: " + summary.identity().assetName(),
|
||||
new PackerProgress(total == 0 ? 1.0d : (index + 1) / (double) total, false),
|
||||
List.of(summary.identity().assetName()));
|
||||
}
|
||||
|
||||
for (PackerRegistryEntry entry : registry.assets()) {
|
||||
final Path registeredRoot = PackerWorkspacePaths.assetRoot(project, entry.root());
|
||||
if (!discoveredRoots.contains(registeredRoot)) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Registered asset root is missing asset.json: " + entry.root(),
|
||||
registeredRoot.resolve("asset.json"),
|
||||
true));
|
||||
}
|
||||
}
|
||||
|
||||
assets.sort(Comparator
|
||||
.comparing((PackerAssetSummary asset) -> asset.identity().assetRoot().toString(), String.CASE_INSENSITIVE_ORDER)
|
||||
.thenComparing(summary -> summary.identity().assetName(), String.CASE_INSENSITIVE_ORDER));
|
||||
final PackerOperationStatus status = diagnostics.stream().anyMatch(PackerDiagnostic::blocking)
|
||||
? PackerOperationStatus.PARTIAL
|
||||
: PackerOperationStatus.SUCCESS;
|
||||
if (!diagnostics.isEmpty()) {
|
||||
events.emit(PackerEventKind.DIAGNOSTICS_UPDATED, "Asset scan diagnostics updated.", List.of());
|
||||
}
|
||||
return new ListAssetsResult(
|
||||
status,
|
||||
"Packer asset snapshot ready.",
|
||||
assets,
|
||||
diagnostics);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GetAssetDetailsResult getAssetDetails(GetAssetDetailsRequest request) {
|
||||
return detailsService.getAssetDetails(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateAssetResult createAsset(CreateAssetRequest request) {
|
||||
final CreateAssetRequest safeRequest = Objects.requireNonNull(request, "request");
|
||||
final PackerProjectContext project = safeRequest.project();
|
||||
final PackerOperationEventEmitter events = new PackerOperationEventEmitter(project, eventSink);
|
||||
|
||||
return writeCoordinator.execute(project, () -> createAssetInWriteLane(safeRequest, events));
|
||||
}
|
||||
|
||||
private CreateAssetResult createAssetInWriteLane(
|
||||
CreateAssetRequest request,
|
||||
PackerOperationEventEmitter events) {
|
||||
final PackerProjectContext project = request.project();
|
||||
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(project));
|
||||
|
||||
final String relativeAssetRoot = normalizeRelativeAssetRoot(request.assetRoot());
|
||||
if (relativeAssetRoot == null) {
|
||||
return failureResult(events, "Asset root must stay inside assets/ and use a non-blank relative path.", null, null, List.of());
|
||||
}
|
||||
|
||||
if (request.assetFamily() == AssetFamilyCatalog.UNKNOWN) {
|
||||
return failureResult(events, "Asset type is required.", null, null, List.of());
|
||||
}
|
||||
if (request.outputFormat() == OutputFormatCatalog.UNKNOWN) {
|
||||
return failureResult(events, "Output format is required.", null, null, List.of());
|
||||
}
|
||||
if (request.outputFormat().assetFamily() != request.assetFamily()) {
|
||||
return failureResult(events, "Output format is not supported for the selected asset type.", null, null, List.of(request.assetName()));
|
||||
}
|
||||
if (!request.outputFormat().supports(request.outputCodec())) {
|
||||
return failureResult(events, "Output codec is not supported for the selected output format.", null, null, List.of(request.assetName()));
|
||||
}
|
||||
|
||||
final Path assetRoot = PackerWorkspacePaths.assetRoot(project, relativeAssetRoot);
|
||||
if (!assetRoot.startsWith(PackerWorkspacePaths.assetsRoot(project))) {
|
||||
return failureResult(events, "Asset root must stay inside assets/.", null, null, List.of());
|
||||
}
|
||||
|
||||
final Path manifestPath = assetRoot.resolve("asset.json");
|
||||
try {
|
||||
final PackerRegistryState registry = workspaceFoundation.loadRegistry(project);
|
||||
final boolean rootAlreadyRegistered = registry.assets().stream()
|
||||
.map(entry -> PackerWorkspacePaths.assetRoot(project, entry.root()))
|
||||
.anyMatch(assetRoot::equals);
|
||||
if (rootAlreadyRegistered) {
|
||||
return failureResult(events, "Asset root is already registered.", assetRoot, manifestPath, List.of(relativeAssetRoot));
|
||||
}
|
||||
|
||||
if (Files.isRegularFile(manifestPath)) {
|
||||
return failureResult(events, "Asset root already contains asset.json.", assetRoot, manifestPath, List.of(relativeAssetRoot));
|
||||
}
|
||||
if (Files.isDirectory(assetRoot)) {
|
||||
try (Stream<Path> children = Files.list(assetRoot)) {
|
||||
if (children.findAny().isPresent()) {
|
||||
return failureResult(events, "Asset root already exists and is not empty.", assetRoot, manifestPath, List.of(relativeAssetRoot));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final PackerRegistryEntry entry = workspaceFoundation.allocateIdentity(project, registry, assetRoot);
|
||||
Files.createDirectories(assetRoot);
|
||||
writeManifest(manifestPath, request, entry.assetUuid());
|
||||
final PackerRegistryState updated = workspaceFoundation.appendAllocatedEntry(registry, entry);
|
||||
workspaceFoundation.saveRegistry(project, updated);
|
||||
runtimeRegistry.refresh(project);
|
||||
final CreateAssetResult result = new CreateAssetResult(
|
||||
PackerOperationStatus.SUCCESS,
|
||||
"Asset created: " + relativeAssetRoot,
|
||||
AssetReference.forAssetId(entry.assetId()),
|
||||
assetRoot,
|
||||
manifestPath);
|
||||
events.emit(PackerEventKind.ACTION_APPLIED, result.summary(), List.of(relativeAssetRoot));
|
||||
return result;
|
||||
} catch (IOException exception) {
|
||||
return failureResult(
|
||||
events,
|
||||
"Unable to create asset: " + exception.getMessage(),
|
||||
assetRoot,
|
||||
manifestPath,
|
||||
List.of(relativeAssetRoot));
|
||||
}
|
||||
}
|
||||
|
||||
private CreateAssetResult failureResult(
|
||||
PackerOperationEventEmitter events,
|
||||
String summary,
|
||||
Path assetRoot,
|
||||
Path manifestPath,
|
||||
List<String> affectedAssets) {
|
||||
final CreateAssetResult result = new CreateAssetResult(
|
||||
PackerOperationStatus.FAILED,
|
||||
summary,
|
||||
null,
|
||||
assetRoot,
|
||||
manifestPath);
|
||||
events.emit(PackerEventKind.ACTION_FAILED, result.summary(), affectedAssets);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void writeManifest(Path manifestPath, CreateAssetRequest request, String assetUuid) throws IOException {
|
||||
final Map<String, Object> manifest = new LinkedHashMap<>();
|
||||
manifest.put("schema_version", 1);
|
||||
manifest.put("asset_uuid", assetUuid);
|
||||
manifest.put("name", request.assetName());
|
||||
manifest.put("type", request.assetFamily().manifestType());
|
||||
manifest.put("inputs", Map.of());
|
||||
manifest.put("output", Map.of(
|
||||
"format", request.outputFormat().manifestValue(),
|
||||
"codec", request.outputCodec().manifestValue()));
|
||||
manifest.put("preload", Map.of("enabled", request.preloadEnabled()));
|
||||
MAPPER.writerWithDefaultPrettyPrinter().writeValue(manifestPath.toFile(), manifest);
|
||||
}
|
||||
|
||||
private String normalizeRelativeAssetRoot(String candidate) {
|
||||
final String raw = Objects.requireNonNullElse(candidate, "").trim().replace('\\', '/');
|
||||
if (raw.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
final Path normalized = Path.of(raw).normalize();
|
||||
if (normalized.isAbsolute() || normalized.startsWith("..")) {
|
||||
return null;
|
||||
}
|
||||
final String value = normalized.toString().replace('\\', '/');
|
||||
return value.isBlank() ? null : value;
|
||||
}
|
||||
|
||||
private PackerAssetSummary buildSummary(
|
||||
PackerProjectContext project,
|
||||
Path assetRoot,
|
||||
PackerRegistryEntry registryEntry,
|
||||
PackerAssetDeclarationParseResult parsed) {
|
||||
final String assetName = parsed.declaration() != null
|
||||
? parsed.declaration().name()
|
||||
: assetRoot.getFileName().toString();
|
||||
final AssetFamilyCatalog assetFamily = parsed.declaration() != null
|
||||
? parsed.declaration().assetFamily()
|
||||
: AssetFamilyCatalog.UNKNOWN;
|
||||
final boolean preload = parsed.declaration() != null && parsed.declaration().preloadEnabled();
|
||||
final boolean hasDiagnostics = !parsed.diagnostics().isEmpty();
|
||||
final PackerAssetState state = registryEntry == null
|
||||
? PackerAssetState.UNREGISTERED
|
||||
: PackerAssetState.REGISTERED;
|
||||
final PackerBuildParticipation buildParticipation = state == PackerAssetState.REGISTERED
|
||||
? (registryEntry.includedInBuild() ? PackerBuildParticipation.INCLUDED : PackerBuildParticipation.EXCLUDED)
|
||||
: PackerBuildParticipation.EXCLUDED;
|
||||
return new PackerAssetSummary(
|
||||
assetReferenceFor(project, assetRoot, registryEntry),
|
||||
new PackerAssetIdentity(
|
||||
registryEntry == null ? null : registryEntry.assetId(),
|
||||
registryEntry == null
|
||||
? parsed.declaration() == null ? null : parsed.declaration().assetUuid()
|
||||
: registryEntry.assetUuid(),
|
||||
assetName,
|
||||
assetRoot),
|
||||
state,
|
||||
buildParticipation,
|
||||
assetFamily,
|
||||
preload,
|
||||
hasDiagnostics);
|
||||
}
|
||||
|
||||
private List<PackerDiagnostic> identityMismatchDiagnostics(
|
||||
PackerRegistryEntry registryEntry,
|
||||
PackerAssetDeclarationParseResult parsed,
|
||||
Path manifestPath) {
|
||||
if (registryEntry == null || parsed.declaration() == null || registryEntry.assetUuid().equals(parsed.declaration().assetUuid())) {
|
||||
return List.of();
|
||||
}
|
||||
return List.of(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Field 'asset_uuid' does not match the registered asset identity.",
|
||||
manifestPath,
|
||||
true));
|
||||
}
|
||||
|
||||
private AssetReference assetReferenceFor(
|
||||
PackerProjectContext project,
|
||||
Path assetRoot,
|
||||
PackerRegistryEntry registryEntry) {
|
||||
if (registryEntry != null) {
|
||||
return AssetReference.forAssetId(registryEntry.assetId());
|
||||
}
|
||||
return AssetReference.forRelativeAssetRoot(PackerWorkspacePaths.assetsRoot(project)
|
||||
.relativize(assetRoot.toAbsolutePath().normalize())
|
||||
.toString()
|
||||
.replace('\\', '/'));
|
||||
}
|
||||
}
|
||||
+49
-7
@@ -1,10 +1,14 @@
|
||||
package p.packer.declarations;
|
||||
package p.packer.services;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
import p.packer.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.models.PackerAssetDeclaration;
|
||||
import p.packer.models.PackerAssetDeclarationParseResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
@@ -35,11 +39,12 @@ public final class PackerAssetDeclarationParser {
|
||||
}
|
||||
|
||||
final Integer schemaVersion = requiredInt(root, "schema_version", diagnostics, manifestPath);
|
||||
final String assetUuid = requiredText(root, "asset_uuid", diagnostics, manifestPath);
|
||||
final String name = requiredText(root, "name", diagnostics, manifestPath);
|
||||
final String type = requiredText(root, "type", diagnostics, manifestPath);
|
||||
final AssetFamilyCatalog assetFamily = requiredAssetFamily(root, diagnostics, manifestPath);
|
||||
final Map<String, List<String>> inputsByRole = requiredInputs(root.path("inputs"), diagnostics, manifestPath);
|
||||
final String outputFormat = requiredText(root.path("output"), "format", diagnostics, manifestPath);
|
||||
final String outputCodec = requiredText(root.path("output"), "codec", diagnostics, manifestPath);
|
||||
final OutputCodecCatalog outputCodec = requiredOutputCodec(root.path("output"), diagnostics, manifestPath);
|
||||
final Boolean preloadEnabled = requiredBoolean(root.path("preload"), "enabled", diagnostics, manifestPath);
|
||||
|
||||
if (schemaVersion != null && schemaVersion != SUPPORTED_SCHEMA_VERSION) {
|
||||
@@ -58,8 +63,9 @@ public final class PackerAssetDeclarationParser {
|
||||
return new PackerAssetDeclarationParseResult(
|
||||
new PackerAssetDeclaration(
|
||||
schemaVersion,
|
||||
assetUuid,
|
||||
name,
|
||||
type,
|
||||
assetFamily,
|
||||
inputsByRole,
|
||||
outputFormat,
|
||||
outputCodec,
|
||||
@@ -85,6 +91,24 @@ public final class PackerAssetDeclarationParser {
|
||||
return field.asText().trim();
|
||||
}
|
||||
|
||||
private AssetFamilyCatalog requiredAssetFamily(JsonNode node, List<PackerDiagnostic> diagnostics, Path manifestPath) {
|
||||
final String manifestType = requiredText(node, "type", diagnostics, manifestPath);
|
||||
if (manifestType == null) {
|
||||
return null;
|
||||
}
|
||||
final AssetFamilyCatalog assetFamily = AssetFamilyCatalog.fromManifestType(manifestType);
|
||||
if (assetFamily == AssetFamilyCatalog.UNKNOWN) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Field 'type' must be one of: image_bank, palette_bank, sound_bank.",
|
||||
manifestPath,
|
||||
true));
|
||||
return null;
|
||||
}
|
||||
return assetFamily;
|
||||
}
|
||||
|
||||
private Boolean requiredBoolean(JsonNode node, String fieldName, List<PackerDiagnostic> diagnostics, Path manifestPath) {
|
||||
final JsonNode field = node.path(fieldName);
|
||||
if (!field.isBoolean()) {
|
||||
@@ -94,6 +118,24 @@ public final class PackerAssetDeclarationParser {
|
||||
return field.booleanValue();
|
||||
}
|
||||
|
||||
private OutputCodecCatalog requiredOutputCodec(JsonNode node, List<PackerDiagnostic> diagnostics, Path manifestPath) {
|
||||
final String codecValue = requiredText(node, "codec", diagnostics, manifestPath);
|
||||
if (codecValue == null) {
|
||||
return null;
|
||||
}
|
||||
final OutputCodecCatalog outputCodec = OutputCodecCatalog.fromManifestValue(codecValue);
|
||||
if (outputCodec == OutputCodecCatalog.UNKNOWN) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Field 'codec' must be one of: NONE.",
|
||||
manifestPath,
|
||||
true));
|
||||
return null;
|
||||
}
|
||||
return outputCodec;
|
||||
}
|
||||
|
||||
private Map<String, List<String>> requiredInputs(JsonNode inputsNode, List<PackerDiagnostic> diagnostics, Path manifestPath) {
|
||||
if (!inputsNode.isObject()) {
|
||||
diagnostics.add(missingOrInvalid("inputs", "object of input roles", manifestPath));
|
||||
+249
@@ -0,0 +1,249 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.AssetReference;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.assets.PackerAssetDetails;
|
||||
import p.packer.assets.PackerAssetIdentity;
|
||||
import p.packer.assets.PackerAssetState;
|
||||
import p.packer.assets.PackerAssetSummary;
|
||||
import p.packer.assets.PackerBuildParticipation;
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
import p.packer.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.messages.GetAssetDetailsRequest;
|
||||
import p.packer.messages.GetAssetDetailsResult;
|
||||
import p.packer.models.PackerAssetDeclaration;
|
||||
import p.packer.models.PackerAssetDeclarationParseResult;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRuntimeAsset;
|
||||
import p.packer.models.PackerRuntimeSnapshot;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class PackerAssetDetailsService {
|
||||
private final PackerRuntimeRegistry runtimeRegistry;
|
||||
|
||||
public PackerAssetDetailsService(PackerRuntimeRegistry runtimeRegistry) {
|
||||
this.runtimeRegistry = Objects.requireNonNull(runtimeRegistry, "runtimeRegistry");
|
||||
}
|
||||
|
||||
public GetAssetDetailsResult getAssetDetails(GetAssetDetailsRequest request) {
|
||||
final PackerProjectContext project = Objects.requireNonNull(request, "request").project();
|
||||
final PackerRuntimeSnapshot snapshot = runtimeRegistry.getOrLoad(project).snapshot();
|
||||
final ResolvedAssetReference resolved = resolveReference(project, snapshot, request.assetReference());
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>(resolved.diagnostics());
|
||||
|
||||
if (resolved.runtimeAsset().isEmpty()) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"asset.json was not found for the requested asset root.",
|
||||
resolved.assetRoot().resolve("asset.json"),
|
||||
true));
|
||||
return failureResult(project, request.assetReference(), resolved, diagnostics);
|
||||
}
|
||||
|
||||
final PackerRuntimeAsset runtimeAsset = resolved.runtimeAsset().get();
|
||||
final Path manifestPath = runtimeAsset.manifestPath();
|
||||
final PackerAssetDeclarationParseResult parsed = runtimeAsset.parsedDeclaration();
|
||||
diagnostics.addAll(parsed.diagnostics());
|
||||
if (!parsed.valid()) {
|
||||
return failureResult(project, request.assetReference(), resolved, diagnostics);
|
||||
}
|
||||
|
||||
final PackerAssetDeclaration declaration = parsed.declaration();
|
||||
diagnostics.addAll(identityMismatchDiagnostics(resolved.registryEntry(), declaration, manifestPath));
|
||||
final PackerOutputContractCatalog.OutputContractDefinition outputContract = PackerOutputContractCatalog.definitionFor(
|
||||
declaration.outputFormat(),
|
||||
declaration.outputCodec());
|
||||
final PackerAssetState state = resolved.registryEntry().isPresent()
|
||||
? PackerAssetState.REGISTERED
|
||||
: PackerAssetState.UNREGISTERED;
|
||||
final PackerAssetSummary summary = new PackerAssetSummary(
|
||||
canonicalReference(project, resolved.assetRoot(), resolved.registryEntry()),
|
||||
new PackerAssetIdentity(
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetId).orElse(null),
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetUuid).orElse(declaration.assetUuid()),
|
||||
declaration.name(),
|
||||
resolved.assetRoot()),
|
||||
state,
|
||||
resolved.registryEntry().map(entry -> entry.includedInBuild()
|
||||
? PackerBuildParticipation.INCLUDED
|
||||
: PackerBuildParticipation.EXCLUDED).orElse(PackerBuildParticipation.EXCLUDED),
|
||||
declaration.assetFamily(),
|
||||
declaration.preloadEnabled(),
|
||||
!diagnostics.isEmpty());
|
||||
final PackerAssetDetails details = new PackerAssetDetails(
|
||||
summary,
|
||||
declaration.outputFormat(),
|
||||
declaration.outputCodec(),
|
||||
outputContract.availableCodecs(),
|
||||
outputContract.codecConfigurationFieldsByCodec(),
|
||||
resolveInputs(resolved.assetRoot(), declaration.inputsByRole()),
|
||||
diagnostics);
|
||||
return new GetAssetDetailsResult(
|
||||
diagnostics.stream().anyMatch(PackerDiagnostic::blocking) ? PackerOperationStatus.PARTIAL : PackerOperationStatus.SUCCESS,
|
||||
"Asset details resolved from runtime snapshot.",
|
||||
details,
|
||||
diagnostics);
|
||||
}
|
||||
|
||||
private GetAssetDetailsResult failureResult(
|
||||
PackerProjectContext project,
|
||||
AssetReference requestedReference,
|
||||
ResolvedAssetReference resolved,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
final PackerAssetState state = resolved.registryEntry().isPresent()
|
||||
? PackerAssetState.REGISTERED
|
||||
: PackerAssetState.UNREGISTERED;
|
||||
final PackerAssetSummary summary = new PackerAssetSummary(
|
||||
canonicalReferenceOrRequested(project, requestedReference, resolved),
|
||||
new PackerAssetIdentity(
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetId).orElse(null),
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetUuid).orElse(null),
|
||||
resolved.assetRoot().getFileName().toString(),
|
||||
resolved.assetRoot()),
|
||||
state,
|
||||
resolved.registryEntry().map(entry -> entry.includedInBuild()
|
||||
? PackerBuildParticipation.INCLUDED
|
||||
: PackerBuildParticipation.EXCLUDED).orElse(PackerBuildParticipation.EXCLUDED),
|
||||
AssetFamilyCatalog.UNKNOWN,
|
||||
false,
|
||||
true);
|
||||
final PackerAssetDetails details = new PackerAssetDetails(
|
||||
summary,
|
||||
"unknown",
|
||||
OutputCodecCatalog.UNKNOWN,
|
||||
List.of(OutputCodecCatalog.NONE),
|
||||
Map.of(OutputCodecCatalog.NONE, List.of()),
|
||||
Map.of(),
|
||||
diagnostics);
|
||||
return new GetAssetDetailsResult(
|
||||
PackerOperationStatus.FAILED,
|
||||
"Asset declaration is invalid or unreadable.",
|
||||
details,
|
||||
diagnostics);
|
||||
}
|
||||
|
||||
private Map<String, List<Path>> resolveInputs(Path assetRoot, Map<String, List<String>> inputsByRole) {
|
||||
final Map<String, List<Path>> resolved = new LinkedHashMap<>();
|
||||
inputsByRole.forEach((role, inputs) -> resolved.put(
|
||||
role,
|
||||
inputs.stream().map(input -> assetRoot.resolve(input).toAbsolutePath().normalize()).toList()));
|
||||
return Map.copyOf(resolved);
|
||||
}
|
||||
|
||||
private List<PackerDiagnostic> identityMismatchDiagnostics(
|
||||
Optional<PackerRegistryEntry> registryEntry,
|
||||
PackerAssetDeclaration declaration,
|
||||
Path manifestPath) {
|
||||
if (registryEntry.isEmpty() || registryEntry.get().assetUuid().equals(declaration.assetUuid())) {
|
||||
return List.of();
|
||||
}
|
||||
return List.of(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Field 'asset_uuid' does not match the registered asset identity.",
|
||||
manifestPath,
|
||||
true));
|
||||
}
|
||||
|
||||
private ResolvedAssetReference resolveReference(PackerProjectContext project, PackerRuntimeSnapshot snapshot, AssetReference assetReference) {
|
||||
final PackerRegistryLookup lookup = new PackerRegistryLookup();
|
||||
final String reference = Objects.requireNonNull(assetReference, "assetReference").rawValue();
|
||||
final Optional<PackerRegistryEntry> byId = parseAssetId(reference).flatMap(assetId -> lookup.findByAssetId(snapshot.registry(), assetId));
|
||||
if (byId.isPresent()) {
|
||||
final Path assetRoot = PackerWorkspacePaths.assetRoot(project, byId.get().root());
|
||||
return new ResolvedAssetReference(assetRoot, byId, findRuntimeAsset(snapshot, assetRoot), List.of());
|
||||
}
|
||||
|
||||
final Optional<PackerRegistryEntry> byUuid = lookup.findByAssetUuid(snapshot.registry(), reference);
|
||||
if (byUuid.isPresent()) {
|
||||
final Path assetRoot = PackerWorkspacePaths.assetRoot(project, byUuid.get().root());
|
||||
return new ResolvedAssetReference(assetRoot, byUuid, findRuntimeAsset(snapshot, assetRoot), List.of());
|
||||
}
|
||||
|
||||
final Path candidateRoot = PackerWorkspacePaths.assetRoot(project, reference);
|
||||
final Optional<PackerRuntimeAsset> runtimeAsset = findRuntimeAsset(snapshot, candidateRoot);
|
||||
if (runtimeAsset.isPresent()) {
|
||||
return new ResolvedAssetReference(candidateRoot, lookup.findByRoot(project, snapshot.registry(), candidateRoot), runtimeAsset, List.of());
|
||||
}
|
||||
|
||||
final Optional<PackerRegistryEntry> registryEntry = lookup.findByRoot(project, snapshot.registry(), candidateRoot);
|
||||
if (registryEntry.isPresent()) {
|
||||
return new ResolvedAssetReference(candidateRoot, registryEntry, Optional.empty(), List.of());
|
||||
}
|
||||
|
||||
return new ResolvedAssetReference(
|
||||
candidateRoot,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Requested asset reference could not be resolved.",
|
||||
candidateRoot,
|
||||
true)));
|
||||
}
|
||||
|
||||
private AssetReference canonicalReference(
|
||||
PackerProjectContext project,
|
||||
Path assetRoot,
|
||||
Optional<PackerRegistryEntry> registryEntry) {
|
||||
if (registryEntry.isPresent()) {
|
||||
return AssetReference.forAssetId(registryEntry.get().assetId());
|
||||
}
|
||||
return AssetReference.forRelativeAssetRoot(PackerWorkspacePaths.assetsRoot(project)
|
||||
.relativize(assetRoot.toAbsolutePath().normalize())
|
||||
.toString()
|
||||
.replace('\\', '/'));
|
||||
}
|
||||
|
||||
private AssetReference canonicalReferenceOrRequested(
|
||||
PackerProjectContext project,
|
||||
AssetReference requestedReference,
|
||||
ResolvedAssetReference resolved) {
|
||||
if (resolved.registryEntry().isPresent() || resolved.runtimeAsset().isPresent()) {
|
||||
return canonicalReference(project, resolved.assetRoot(), resolved.registryEntry());
|
||||
}
|
||||
return requestedReference;
|
||||
}
|
||||
|
||||
private Optional<Integer> parseAssetId(String reference) {
|
||||
try {
|
||||
return Optional.of(Integer.parseInt(reference.trim()));
|
||||
} catch (NumberFormatException ignored) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<PackerRuntimeAsset> findRuntimeAsset(PackerRuntimeSnapshot snapshot, Path assetRoot) {
|
||||
final Path normalizedRoot = Objects.requireNonNull(assetRoot, "assetRoot").toAbsolutePath().normalize();
|
||||
return snapshot.assets().stream()
|
||||
.filter(candidate -> candidate.assetRoot().equals(normalizedRoot))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private record ResolvedAssetReference(
|
||||
Path assetRoot,
|
||||
Optional<PackerRegistryEntry> registryEntry,
|
||||
Optional<PackerRuntimeAsset> runtimeAsset,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
|
||||
private ResolvedAssetReference {
|
||||
assetRoot = Objects.requireNonNull(assetRoot, "assetRoot").toAbsolutePath().normalize();
|
||||
registryEntry = Objects.requireNonNull(registryEntry, "registryEntry");
|
||||
runtimeAsset = Objects.requireNonNull(runtimeAsset, "runtimeAsset");
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -1,6 +1,8 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
@@ -19,7 +21,7 @@ public final class PackerIdentityAllocator {
|
||||
final String relativeRoot = PackerWorkspacePaths.relativeAssetRoot(project, normalizedAssetRoot);
|
||||
final boolean alreadyRegistered = state.assets().stream().anyMatch(entry -> entry.root().equals(relativeRoot));
|
||||
if (alreadyRegistered) {
|
||||
throw new PackerRegistryException("Asset root is already registered: " + relativeRoot);
|
||||
throw new p.packer.exceptions.PackerRegistryException("Asset root is already registered: " + relativeRoot);
|
||||
}
|
||||
return new PackerRegistryEntry(state.nextAssetId(), UUID.randomUUID().toString(), relativeRoot);
|
||||
}
|
||||
+6
-6
@@ -1,10 +1,10 @@
|
||||
package p.packer.events;
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.api.events.PackerEvent;
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.packer.api.events.PackerEventSink;
|
||||
import p.packer.api.events.PackerProgress;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.events.PackerEvent;
|
||||
import p.packer.events.PackerEventKind;
|
||||
import p.packer.events.PackerEventSink;
|
||||
import p.packer.events.PackerProgress;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.assets.OutputFormatCatalog;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import p.packer.assets.PackerCodecConfigurationField;
|
||||
|
||||
final class PackerOutputContractCatalog {
|
||||
private PackerOutputContractCatalog() {
|
||||
}
|
||||
|
||||
static OutputContractDefinition definitionFor(String outputFormat, OutputCodecCatalog selectedCodec) {
|
||||
final List<OutputCodecCatalog> availableCodecs = availableCodecsFor(outputFormat, selectedCodec);
|
||||
return new OutputContractDefinition(
|
||||
availableCodecs,
|
||||
availableCodecs.stream().collect(java.util.stream.Collectors.toMap(
|
||||
codec -> codec,
|
||||
codec -> fieldsFor(outputFormat, codec),
|
||||
(left, right) -> left,
|
||||
java.util.LinkedHashMap::new)));
|
||||
}
|
||||
|
||||
static List<OutputCodecCatalog> availableCodecsFor(String outputFormat, OutputCodecCatalog selectedCodec) {
|
||||
final Set<OutputCodecCatalog> codecs = new LinkedHashSet<>();
|
||||
final OutputFormatCatalog formatCatalog = OutputFormatCatalog.fromManifestValue(outputFormat);
|
||||
if (formatCatalog != OutputFormatCatalog.UNKNOWN) {
|
||||
codecs.addAll(formatCatalog.availableCodecs());
|
||||
} else {
|
||||
final String normalizedFormat = Objects.requireNonNullElse(outputFormat, "").trim().toUpperCase(Locale.ROOT);
|
||||
if (normalizedFormat.startsWith("TILES/")
|
||||
|| normalizedFormat.startsWith("PALETTE/")
|
||||
|| normalizedFormat.startsWith("SOUND/")
|
||||
|| normalizedFormat.startsWith("AUDIO/")) {
|
||||
codecs.add(OutputCodecCatalog.NONE);
|
||||
}
|
||||
}
|
||||
|
||||
final OutputCodecCatalog normalizedSelectedCodec = Objects.requireNonNullElse(selectedCodec, OutputCodecCatalog.UNKNOWN);
|
||||
if (normalizedSelectedCodec != OutputCodecCatalog.UNKNOWN) {
|
||||
codecs.add(normalizedSelectedCodec);
|
||||
}
|
||||
|
||||
if (codecs.isEmpty()) {
|
||||
codecs.add(OutputCodecCatalog.NONE);
|
||||
}
|
||||
return List.copyOf(codecs);
|
||||
}
|
||||
|
||||
private static List<PackerCodecConfigurationField> fieldsFor(String outputFormat, OutputCodecCatalog codec) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
record OutputContractDefinition(
|
||||
List<OutputCodecCatalog> availableCodecs,
|
||||
Map<OutputCodecCatalog, List<PackerCodecConfigurationField>> codecConfigurationFieldsByCodec) {
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRuntimeSnapshot;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public final class PackerProjectRuntime {
|
||||
private final PackerProjectContext project;
|
||||
private final AtomicReference<PackerRuntimeSnapshot> snapshot;
|
||||
private final AtomicBoolean disposed = new AtomicBoolean(false);
|
||||
|
||||
public PackerProjectRuntime(PackerProjectContext project, PackerRuntimeSnapshot initialSnapshot) {
|
||||
this.project = Objects.requireNonNull(project, "project");
|
||||
this.snapshot = new AtomicReference<>(Objects.requireNonNull(initialSnapshot, "initialSnapshot"));
|
||||
}
|
||||
|
||||
public PackerProjectContext project() {
|
||||
return project;
|
||||
}
|
||||
|
||||
public PackerRuntimeSnapshot snapshot() {
|
||||
ensureActive();
|
||||
return snapshot.get();
|
||||
}
|
||||
|
||||
public void replaceSnapshot(PackerRuntimeSnapshot nextSnapshot) {
|
||||
ensureActive();
|
||||
snapshot.set(Objects.requireNonNull(nextSnapshot, "nextSnapshot"));
|
||||
}
|
||||
|
||||
public boolean disposed() {
|
||||
return disposed.get();
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
disposed.set(true);
|
||||
}
|
||||
|
||||
private void ensureActive() {
|
||||
if (disposed.get()) {
|
||||
throw new IllegalStateException("Runtime has been disposed for project: " + project.projectId());
|
||||
}
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public final class PackerProjectWriteCoordinator implements Closeable {
|
||||
private final ConcurrentMap<ProjectKey, ExecutorService> executors = new ConcurrentHashMap<>();
|
||||
private final AtomicInteger nextThreadId = new AtomicInteger(1);
|
||||
|
||||
public <T> Future<T> submit(PackerProjectContext project, Callable<T> task) {
|
||||
final PackerProjectContext safeProject = Objects.requireNonNull(project, "project");
|
||||
final Callable<T> safeTask = Objects.requireNonNull(task, "task");
|
||||
return executorFor(safeProject).submit(safeTask);
|
||||
}
|
||||
|
||||
public <T> T execute(PackerProjectContext project, Callable<T> task) {
|
||||
try {
|
||||
return submit(project, task).get();
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Write execution was interrupted for project: " + project.projectId(), exception);
|
||||
} catch (ExecutionException exception) {
|
||||
final Throwable cause = exception.getCause();
|
||||
if (cause instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
throw new IllegalStateException("Write execution failed for project: " + project.projectId(), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
executors.values().forEach(executor -> {
|
||||
executor.shutdownNow();
|
||||
try {
|
||||
executor.awaitTermination(5, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
executors.clear();
|
||||
}
|
||||
|
||||
private ExecutorService executorFor(PackerProjectContext project) {
|
||||
final ProjectKey key = ProjectKey.from(project);
|
||||
return executors.computeIfAbsent(key, ignored -> new ThreadPoolExecutor(
|
||||
1,
|
||||
1,
|
||||
0L,
|
||||
TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<>(),
|
||||
runnable -> {
|
||||
final Thread thread = new Thread(
|
||||
runnable,
|
||||
"packer-write-" + project.projectId() + "-" + nextThreadId.getAndIncrement());
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
}));
|
||||
}
|
||||
|
||||
private record ProjectKey(
|
||||
String projectId,
|
||||
Path rootPath) {
|
||||
|
||||
private static ProjectKey from(PackerProjectContext project) {
|
||||
return new ProjectKey(
|
||||
Objects.requireNonNull(project.projectId(), "projectId"),
|
||||
Objects.requireNonNull(project.rootPath(), "rootPath").toAbsolutePath().normalize());
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -1,6 +1,8 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -31,7 +33,7 @@ public final class PackerRegistryLookup {
|
||||
Objects.requireNonNull(entry, "entry");
|
||||
final Path assetRoot = PackerWorkspacePaths.assetRoot(project, entry.root());
|
||||
if (!Files.isDirectory(assetRoot)) {
|
||||
throw new PackerRegistryException("Registered asset root does not exist: " + entry.root());
|
||||
throw new p.packer.exceptions.PackerRegistryException("Registered asset root does not exist: " + entry.root());
|
||||
}
|
||||
return assetRoot;
|
||||
}
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
public interface PackerRegistryRepository {
|
||||
PackerRegistryState load(PackerProjectContext project);
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.messages.InitWorkspaceRequest;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
import p.packer.models.PackerRuntimeAsset;
|
||||
import p.packer.models.PackerRuntimeSnapshot;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class PackerRuntimeLoader {
|
||||
private final PackerWorkspaceFoundation workspaceFoundation;
|
||||
private final PackerAssetDeclarationParser parser;
|
||||
|
||||
public PackerRuntimeLoader(
|
||||
PackerWorkspaceFoundation workspaceFoundation,
|
||||
PackerAssetDeclarationParser parser) {
|
||||
this.workspaceFoundation = Objects.requireNonNull(workspaceFoundation, "workspaceFoundation");
|
||||
this.parser = Objects.requireNonNull(parser, "parser");
|
||||
}
|
||||
|
||||
public PackerRuntimeSnapshot load(PackerProjectContext project, long generation) {
|
||||
final PackerProjectContext safeProject = Objects.requireNonNull(project, "project");
|
||||
workspaceFoundation.initWorkspace(new InitWorkspaceRequest(safeProject));
|
||||
|
||||
final PackerRegistryState registry = workspaceFoundation.loadRegistry(safeProject);
|
||||
final Map<Path, PackerRegistryEntry> registryByRoot = registry.assets().stream()
|
||||
.collect(Collectors.toMap(
|
||||
entry -> PackerWorkspacePaths.assetRoot(safeProject, entry.root()),
|
||||
entry -> entry));
|
||||
|
||||
final List<PackerRuntimeAsset> assets = new ArrayList<>();
|
||||
final Path assetsRoot = PackerWorkspacePaths.assetsRoot(safeProject);
|
||||
if (Files.isDirectory(assetsRoot)) {
|
||||
try (Stream<Path> paths = Files.find(assetsRoot, Integer.MAX_VALUE, (path, attrs) ->
|
||||
attrs.isRegularFile() && path.getFileName().toString().equals("asset.json"))) {
|
||||
final List<Path> manifests = paths
|
||||
.map(path -> path.toAbsolutePath().normalize())
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.toList();
|
||||
for (Path manifestPath : manifests) {
|
||||
final Path assetRoot = manifestPath.getParent();
|
||||
final Optional<PackerRegistryEntry> registryEntry = Optional.ofNullable(registryByRoot.get(assetRoot));
|
||||
assets.add(new PackerRuntimeAsset(
|
||||
assetRoot,
|
||||
manifestPath,
|
||||
registryEntry,
|
||||
parser.parse(manifestPath)));
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
throw new p.packer.exceptions.PackerRegistryException(
|
||||
"Unable to build runtime snapshot for " + safeProject.projectId(),
|
||||
exception);
|
||||
}
|
||||
}
|
||||
|
||||
return new PackerRuntimeSnapshot(generation, registry, assets);
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public final class PackerRuntimeRegistry {
|
||||
private final PackerRuntimeLoader loader;
|
||||
private final ConcurrentMap<ProjectKey, PackerProjectRuntime> runtimes = new ConcurrentHashMap<>();
|
||||
private final AtomicLong nextGeneration = new AtomicLong(1L);
|
||||
|
||||
public PackerRuntimeRegistry(PackerRuntimeLoader loader) {
|
||||
this.loader = Objects.requireNonNull(loader, "loader");
|
||||
}
|
||||
|
||||
public PackerProjectRuntime getOrLoad(PackerProjectContext project) {
|
||||
final PackerProjectContext safeProject = Objects.requireNonNull(project, "project");
|
||||
final ProjectKey key = ProjectKey.from(safeProject);
|
||||
return runtimes.compute(key, (ignored, current) -> {
|
||||
if (current != null && !current.disposed()) {
|
||||
return current;
|
||||
}
|
||||
return new PackerProjectRuntime(safeProject, loader.load(safeProject, nextGeneration.getAndIncrement()));
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<PackerProjectRuntime> find(PackerProjectContext project) {
|
||||
return Optional.ofNullable(runtimes.get(ProjectKey.from(Objects.requireNonNull(project, "project"))));
|
||||
}
|
||||
|
||||
public PackerProjectRuntime refresh(PackerProjectContext project) {
|
||||
final PackerProjectContext safeProject = Objects.requireNonNull(project, "project");
|
||||
final ProjectKey key = ProjectKey.from(safeProject);
|
||||
return runtimes.compute(key, (ignored, current) -> {
|
||||
final var snapshot = loader.load(safeProject, nextGeneration.getAndIncrement());
|
||||
if (current == null || current.disposed()) {
|
||||
return new PackerProjectRuntime(safeProject, snapshot);
|
||||
}
|
||||
current.replaceSnapshot(snapshot);
|
||||
return current;
|
||||
});
|
||||
}
|
||||
|
||||
public void dispose(PackerProjectContext project) {
|
||||
final PackerProjectRuntime removed = runtimes.remove(ProjectKey.from(Objects.requireNonNull(project, "project")));
|
||||
if (removed != null) {
|
||||
removed.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void disposeAll() {
|
||||
runtimes.values().forEach(PackerProjectRuntime::dispose);
|
||||
runtimes.clear();
|
||||
}
|
||||
|
||||
private record ProjectKey(
|
||||
String projectId,
|
||||
Path rootPath) {
|
||||
|
||||
private static ProjectKey from(PackerProjectContext project) {
|
||||
return new ProjectKey(
|
||||
Objects.requireNonNull(project.projectId(), "projectId"),
|
||||
Objects.requireNonNull(project.rootPath(), "rootPath").toAbsolutePath().normalize());
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
-6
@@ -1,9 +1,11 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.api.workspace.InitWorkspaceRequest;
|
||||
import p.packer.api.workspace.InitWorkspaceResult;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.messages.InitWorkspaceRequest;
|
||||
import p.packer.messages.InitWorkspaceResult;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
@@ -40,7 +42,7 @@ public final class PackerWorkspaceFoundation {
|
||||
PackerWorkspacePaths.registryPath(project),
|
||||
List.of());
|
||||
} catch (Exception exception) {
|
||||
throw new PackerRegistryException("Unable to initialize workspace for " + project.projectId(), exception);
|
||||
throw new p.packer.exceptions.PackerRegistryException("Unable to initialize workspace for " + project.projectId(), exception);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+231
@@ -0,0 +1,231 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.assets.OutputFormatCatalog;
|
||||
import p.packer.assets.AssetReference;
|
||||
import p.packer.assets.PackerBuildParticipation;
|
||||
import p.packer.assets.PackerAssetState;
|
||||
import p.packer.events.PackerEvent;
|
||||
import p.packer.events.PackerEventKind;
|
||||
import p.packer.messages.CreateAssetRequest;
|
||||
import p.packer.messages.CreateAssetResult;
|
||||
import p.packer.messages.GetAssetDetailsRequest;
|
||||
import p.packer.messages.ListAssetsRequest;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class FileSystemPackerWorkspaceServiceTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void listsRegisteredAndUnregisteredAssetsFromWorkspaceScan() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-mixed", tempDir.resolve("mixed"));
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final var result = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertEquals(2, result.assets().size());
|
||||
assertTrue(result.assets().stream().anyMatch(asset -> asset.state() == PackerAssetState.REGISTERED));
|
||||
assertTrue(result.assets().stream().anyMatch(asset -> asset.state() == PackerAssetState.UNREGISTERED));
|
||||
assertTrue(result.assets().stream().allMatch(asset -> asset.identity().assetUuid() != null && !asset.identity().assetUuid().isBlank()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void surfacesMissingRegisteredRootAsStructuralDiagnostic() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-missing-root", tempDir.resolve("missing-root"));
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final var result = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(PackerOperationStatus.PARTIAL, result.status());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("missing asset.json")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void includesInvalidDeclarationsInSnapshotWithDiagnostics() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-invalid", tempDir.resolve("invalid"));
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final var result = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(1, result.assets().size());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.assets().getFirst().state());
|
||||
assertEquals(PackerBuildParticipation.EXCLUDED, result.assets().getFirst().buildParticipation());
|
||||
assertTrue(result.assets().getFirst().hasDiagnostics());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emitsDiscoveryAndDiagnosticsEventsDuringScan() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-invalid", tempDir.resolve("events"));
|
||||
final List<PackerEvent> events = new CopyOnWriteArrayList<>();
|
||||
final FileSystemPackerWorkspaceService service = service(events::add);
|
||||
|
||||
service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.ASSET_DISCOVERED));
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.DIAGNOSTICS_UPDATED));
|
||||
assertTrue(events.stream().allMatch(event -> event.sequence() >= 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsRegisteredAssetAndWritesManifest() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("created");
|
||||
final List<PackerEvent> events = new CopyOnWriteArrayList<>();
|
||||
final FileSystemPackerWorkspaceService service = service(events::add);
|
||||
|
||||
final var result = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/new-atlas",
|
||||
"new_atlas",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
true));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertNotNull(result.assetReference());
|
||||
assertTrue(Files.isRegularFile(projectRoot.resolve("assets/ui/new-atlas/asset.json")));
|
||||
final String manifestJson = Files.readString(projectRoot.resolve("assets/ui/new-atlas/asset.json"));
|
||||
assertTrue(manifestJson.contains("\"asset_uuid\""));
|
||||
|
||||
final var snapshot = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
assertEquals(1, snapshot.assets().size());
|
||||
assertEquals(PackerAssetState.REGISTERED, snapshot.assets().getFirst().state());
|
||||
assertEquals("new_atlas", snapshot.assets().getFirst().identity().assetName());
|
||||
assertNotNull(snapshot.assets().getFirst().identity().assetUuid());
|
||||
assertEquals(PackerBuildParticipation.INCLUDED, snapshot.assets().getFirst().buildParticipation());
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.ACTION_APPLIED));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsCreatedAssetThroughRuntimeBackedDetailsWithoutRescanMismatch() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("created-details");
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final CreateAssetResult createResult = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/new-atlas",
|
||||
"new_atlas",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
true));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, createResult.status());
|
||||
final AssetReference assetReference = createResult.assetReference();
|
||||
assertNotNull(assetReference);
|
||||
|
||||
final var detailsResult = service.getAssetDetails(new GetAssetDetailsRequest(
|
||||
project(projectRoot),
|
||||
assetReference));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, detailsResult.status());
|
||||
assertEquals(PackerAssetState.REGISTERED, detailsResult.details().summary().state());
|
||||
assertEquals("new_atlas", detailsResult.details().summary().identity().assetName());
|
||||
assertNotNull(detailsResult.details().summary().identity().assetUuid());
|
||||
assertTrue(detailsResult.diagnostics().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnsupportedFormatForSelectedFamily() {
|
||||
final Path projectRoot = tempDir.resolve("unsupported");
|
||||
final List<PackerEvent> events = new CopyOnWriteArrayList<>();
|
||||
final FileSystemPackerWorkspaceService service = service(events::add);
|
||||
|
||||
final var result = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"audio/bad",
|
||||
"bad_asset",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.SOUND_BANK_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
false));
|
||||
|
||||
assertEquals(PackerOperationStatus.FAILED, result.status());
|
||||
assertNull(result.assetReference());
|
||||
assertTrue(result.summary().contains("not supported"));
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.ACTION_FAILED));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesConcurrentCreateAssetRequestsPerProject() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("concurrent-create");
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
try (var executor = Executors.newFixedThreadPool(2)) {
|
||||
final Future<CreateAssetResult> first = executor.submit(() -> service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/atlas-a",
|
||||
"atlas_a",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
true)));
|
||||
final Future<CreateAssetResult> second = executor.submit(() -> service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/atlas-b",
|
||||
"atlas_b",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
false)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, first.get().status());
|
||||
assertEquals(PackerOperationStatus.SUCCESS, second.get().status());
|
||||
}
|
||||
|
||||
final var snapshot = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
assertEquals(2, snapshot.assets().size());
|
||||
assertEquals(2, new HashSet<>(snapshot.assets().stream().map(asset -> asset.identity().assetId()).toList()).size());
|
||||
}
|
||||
|
||||
private PackerProjectContext project(Path root) {
|
||||
return new PackerProjectContext("main", root);
|
||||
}
|
||||
|
||||
private FileSystemPackerWorkspaceService service() {
|
||||
return service(ignored -> {
|
||||
});
|
||||
}
|
||||
|
||||
private FileSystemPackerWorkspaceService service(p.packer.events.PackerEventSink eventSink) {
|
||||
final var foundation = new p.packer.services.PackerWorkspaceFoundation();
|
||||
final var parser = new p.packer.services.PackerAssetDeclarationParser();
|
||||
final var runtimeRegistry = new p.packer.services.PackerRuntimeRegistry(new p.packer.services.PackerRuntimeLoader(foundation, parser));
|
||||
final var detailsService = new p.packer.services.PackerAssetDetailsService(runtimeRegistry);
|
||||
final var writeCoordinator = new p.packer.services.PackerProjectWriteCoordinator();
|
||||
return new FileSystemPackerWorkspaceService(foundation, detailsService, runtimeRegistry, writeCoordinator, eventSink);
|
||||
}
|
||||
|
||||
private Path copyFixture(String relativePath, Path targetRoot) throws Exception {
|
||||
final Path sourceRoot = PackerFixtureLocator.fixtureRoot(relativePath);
|
||||
try (var stream = Files.walk(sourceRoot)) {
|
||||
for (Path source : stream.sorted(Comparator.naturalOrder()).toList()) {
|
||||
final Path target = targetRoot.resolve(sourceRoot.relativize(source).toString());
|
||||
if (Files.isDirectory(source)) {
|
||||
Files.createDirectories(target);
|
||||
} else {
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(source, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetRoot;
|
||||
}
|
||||
}
|
||||
+31
-5
@@ -1,8 +1,10 @@
|
||||
package p.packer.declarations;
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.nio.file.Files;
|
||||
@@ -23,10 +25,11 @@ final class PackerAssetDeclarationParserTest {
|
||||
assertTrue(result.valid());
|
||||
assertNotNull(result.declaration());
|
||||
assertEquals(1, result.declaration().schemaVersion());
|
||||
assertEquals("fixture-uuid-1", result.declaration().assetUuid());
|
||||
assertEquals("ui_atlas", result.declaration().name());
|
||||
assertEquals("image_bank", result.declaration().type());
|
||||
assertEquals(AssetFamilyCatalog.IMAGE_BANK, result.declaration().assetFamily());
|
||||
assertEquals("TILES/indexed_v1", result.declaration().outputFormat());
|
||||
assertEquals("RAW", result.declaration().outputCodec());
|
||||
assertEquals(OutputCodecCatalog.NONE, result.declaration().outputCodec());
|
||||
assertTrue(result.declaration().preloadEnabled());
|
||||
}
|
||||
|
||||
@@ -45,6 +48,7 @@ final class PackerAssetDeclarationParserTest {
|
||||
|
||||
assertFalse(result.valid());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("name")));
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("asset_uuid")));
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("format")));
|
||||
}
|
||||
|
||||
@@ -62,10 +66,11 @@ final class PackerAssetDeclarationParserTest {
|
||||
Files.writeString(manifest, """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "uuid-outside",
|
||||
"name": "bad_asset",
|
||||
"type": "image_bank",
|
||||
"inputs": { "sprites": ["../outside.png"] },
|
||||
"output": { "format": "TILES/indexed_v1", "codec": "RAW" },
|
||||
"output": { "format": "TILES/indexed_v1", "codec": "NONE" },
|
||||
"preload": { "enabled": true }
|
||||
}
|
||||
""");
|
||||
@@ -75,4 +80,25 @@ final class PackerAssetDeclarationParserTest {
|
||||
assertFalse(result.valid());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("untrusted path")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnknownAssetFamily() throws Exception {
|
||||
final Path manifest = tempDir.resolve("asset.json");
|
||||
Files.writeString(manifest, """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "uuid-video",
|
||||
"name": "bad_asset",
|
||||
"type": "video_bank",
|
||||
"inputs": { "sprites": ["atlas.png"] },
|
||||
"output": { "format": "TILES/indexed_v1", "codec": "NONE" },
|
||||
"preload": { "enabled": true }
|
||||
}
|
||||
""");
|
||||
|
||||
final var result = parser.parse(manifest);
|
||||
|
||||
assertFalse(result.valid());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("Field 'type' must be one of")));
|
||||
}
|
||||
}
|
||||
+29
-15
@@ -1,18 +1,20 @@
|
||||
package p.packer.declarations;
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.api.assets.PackerBuildParticipation;
|
||||
import p.packer.api.assets.PackerAssetState;
|
||||
import p.packer.api.workspace.GetAssetDetailsRequest;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetReference;
|
||||
import p.packer.assets.PackerBuildParticipation;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.assets.PackerAssetState;
|
||||
import p.packer.messages.GetAssetDetailsRequest;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -23,37 +25,43 @@ final class PackerAssetDetailsServiceTest {
|
||||
@Test
|
||||
void returnsRegisteredDetailsForRegisteredAssetReferenceById() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("managed"));
|
||||
final PackerAssetDetailsService service = new PackerAssetDetailsService();
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), "1"));
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), AssetReference.forAssetId(1)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertEquals(PackerAssetState.REGISTERED, result.details().summary().state());
|
||||
assertEquals(PackerBuildParticipation.INCLUDED, result.details().summary().buildParticipation());
|
||||
assertEquals("fixture-uuid-1", result.details().summary().identity().assetUuid());
|
||||
assertEquals("ui_atlas", result.details().summary().identity().assetName());
|
||||
assertEquals("TILES/indexed_v1", result.details().outputFormat());
|
||||
assertEquals(List.of(OutputCodecCatalog.NONE), result.details().availableOutputCodecs());
|
||||
assertEquals(List.of(), result.details().codecConfigurationFieldsByCodec().get(OutputCodecCatalog.NONE));
|
||||
assertTrue(result.diagnostics().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsUnregisteredDetailsForValidUnregisteredRootReference() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/orphan-valid", tempDir.resolve("orphan"));
|
||||
final PackerAssetDetailsService service = new PackerAssetDetailsService();
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), "orphans/ui_sounds"));
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), AssetReference.forRelativeAssetRoot("orphans/ui_sounds")));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.details().summary().state());
|
||||
assertEquals(PackerBuildParticipation.EXCLUDED, result.details().summary().buildParticipation());
|
||||
assertEquals("orphan-uuid-1", result.details().summary().identity().assetUuid());
|
||||
assertEquals("ui_sounds", result.details().summary().identity().assetName());
|
||||
assertEquals(List.of(OutputCodecCatalog.NONE), result.details().availableOutputCodecs());
|
||||
assertEquals(List.of(), result.details().codecConfigurationFieldsByCodec().get(OutputCodecCatalog.NONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsInvalidDetailsForInvalidDeclaration() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/invalid-missing-fields", tempDir.resolve("invalid"));
|
||||
final PackerAssetDetailsService service = new PackerAssetDetailsService();
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), "bad"));
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), AssetReference.forRelativeAssetRoot("bad")));
|
||||
|
||||
assertEquals(PackerOperationStatus.FAILED, result.status());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.details().summary().state());
|
||||
@@ -63,9 +71,9 @@ final class PackerAssetDetailsServiceTest {
|
||||
|
||||
@Test
|
||||
void returnsFailureWhenReferenceCannotBeResolved() {
|
||||
final PackerAssetDetailsService service = new PackerAssetDetailsService();
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(tempDir.resolve("empty")), "missing/root"));
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(tempDir.resolve("empty")), AssetReference.forRelativeAssetRoot("missing/root")));
|
||||
|
||||
assertEquals(PackerOperationStatus.FAILED, result.status());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.details().summary().state());
|
||||
@@ -77,6 +85,12 @@ final class PackerAssetDetailsServiceTest {
|
||||
return new PackerProjectContext("main", root);
|
||||
}
|
||||
|
||||
private PackerAssetDetailsService service() {
|
||||
final var foundation = new p.packer.services.PackerWorkspaceFoundation();
|
||||
final var parser = new PackerAssetDeclarationParser();
|
||||
return new PackerAssetDetailsService(new PackerRuntimeRegistry(new PackerRuntimeLoader(foundation, parser)));
|
||||
}
|
||||
|
||||
private Path copyFixture(String relativePath, Path targetRoot) throws Exception {
|
||||
final Path sourceRoot = PackerFixtureLocator.fixtureRoot(relativePath);
|
||||
try (var stream = Files.walk(sourceRoot)) {
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
final class PackerProjectWriteCoordinatorTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private final PackerProjectWriteCoordinator coordinator = new PackerProjectWriteCoordinator();
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
coordinator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesSameProjectWritesInSubmissionOrder() throws Exception {
|
||||
final PackerProjectContext project = new PackerProjectContext("main", tempDir.resolve("main"));
|
||||
final List<String> trace = new CopyOnWriteArrayList<>();
|
||||
final CountDownLatch firstStarted = new CountDownLatch(1);
|
||||
|
||||
final Future<String> first = coordinator.submit(project, () -> {
|
||||
trace.add("first-start");
|
||||
firstStarted.countDown();
|
||||
Thread.sleep(100L);
|
||||
trace.add("first-end");
|
||||
return "first";
|
||||
});
|
||||
|
||||
assertTrue(firstStarted.await(2, TimeUnit.SECONDS));
|
||||
final Future<String> second = coordinator.submit(project, () -> {
|
||||
trace.add("second");
|
||||
return "second";
|
||||
});
|
||||
|
||||
assertEquals("first", first.get(2, TimeUnit.SECONDS));
|
||||
assertEquals("second", second.get(2, TimeUnit.SECONDS));
|
||||
assertEquals(List.of("first-start", "first-end", "second"), trace);
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class PackerRuntimeRegistryTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void bootstrapsOneRuntimePerProjectAndReusesItUntilRefresh() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("managed"));
|
||||
final PackerRuntimeRegistry registry = runtimeRegistry();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
|
||||
final PackerProjectRuntime first = registry.getOrLoad(project);
|
||||
final PackerProjectRuntime second = registry.getOrLoad(project);
|
||||
|
||||
assertSame(first, second);
|
||||
assertEquals(1L, first.snapshot().generation());
|
||||
assertEquals(1, first.snapshot().registry().assets().size());
|
||||
assertEquals(1, first.snapshot().assets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refreshRebuildsSnapshotWithoutReplacingRuntimeInstance() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("refresh"));
|
||||
final PackerRuntimeRegistry registry = runtimeRegistry();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
final PackerProjectRuntime runtime = registry.getOrLoad(project);
|
||||
|
||||
Files.createDirectories(projectRoot.resolve("assets/audio/ui_sounds"));
|
||||
Files.writeString(projectRoot.resolve("assets/audio/ui_sounds/asset.json"), """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "runtime-refresh-uuid",
|
||||
"name": "ui_sounds",
|
||||
"type": "audio.bank",
|
||||
"inputs": {},
|
||||
"output": {
|
||||
"format": "SOUND/bank_v1",
|
||||
"codec": "none"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": false
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
final PackerProjectRuntime refreshed = registry.refresh(project);
|
||||
|
||||
assertSame(runtime, refreshed);
|
||||
assertTrue(refreshed.snapshot().generation() > 1L);
|
||||
assertEquals(2, refreshed.snapshot().assets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disposeMarksRuntimeInactiveAndRemovesItFromRegistry() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("dispose"));
|
||||
final PackerRuntimeRegistry registry = runtimeRegistry();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
final PackerProjectRuntime runtime = registry.getOrLoad(project);
|
||||
|
||||
registry.dispose(project);
|
||||
|
||||
assertTrue(runtime.disposed());
|
||||
assertTrue(registry.find(project).isEmpty());
|
||||
assertThrows(IllegalStateException.class, runtime::snapshot);
|
||||
}
|
||||
|
||||
private PackerRuntimeRegistry runtimeRegistry() {
|
||||
final var foundation = new PackerWorkspaceFoundation();
|
||||
final var parser = new PackerAssetDeclarationParser();
|
||||
return new PackerRuntimeRegistry(new PackerRuntimeLoader(foundation, parser));
|
||||
}
|
||||
|
||||
private PackerProjectContext project(Path root) {
|
||||
return new PackerProjectContext("main", root);
|
||||
}
|
||||
|
||||
private Path copyFixture(String relativePath, Path targetRoot) throws Exception {
|
||||
final Path sourceRoot = PackerFixtureLocator.fixtureRoot(relativePath);
|
||||
try (var stream = Files.walk(sourceRoot)) {
|
||||
for (Path source : stream.sorted(Comparator.naturalOrder()).toList()) {
|
||||
final Path target = targetRoot.resolve(sourceRoot.relativize(source).toString());
|
||||
if (Files.isDirectory(source)) {
|
||||
Files.createDirectories(target);
|
||||
} else {
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(source, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetRoot;
|
||||
}
|
||||
}
|
||||
+10
-8
@@ -1,9 +1,11 @@
|
||||
package p.packer.foundation;
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.api.workspace.InitWorkspaceRequest;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.messages.InitWorkspaceRequest;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -89,7 +91,7 @@ final class PackerWorkspaceFoundationTest {
|
||||
""");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final PackerRegistryException exception = assertThrows(PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("Duplicate asset root"));
|
||||
}
|
||||
@@ -101,7 +103,7 @@ final class PackerWorkspaceFoundationTest {
|
||||
Files.writeString(projectRoot.resolve("assets/.prometeu/index.json"), "{ nope ");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final PackerRegistryException exception = assertThrows(PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("Unable to load registry"));
|
||||
}
|
||||
@@ -119,7 +121,7 @@ final class PackerWorkspaceFoundationTest {
|
||||
""");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final PackerRegistryException exception = assertThrows(PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("Unsupported registry schema_version"));
|
||||
}
|
||||
@@ -139,7 +141,7 @@ final class PackerWorkspaceFoundationTest {
|
||||
""");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final PackerRegistryException exception = assertThrows(PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("trusted assets boundary"));
|
||||
}
|
||||
@@ -164,7 +166,7 @@ final class PackerWorkspaceFoundationTest {
|
||||
assertEquals(projectRoot.resolve("assets/ui/atlas").toAbsolutePath().normalize(), lookup.resolveExistingRoot(project, state.assets().getFirst()));
|
||||
|
||||
Files.delete(projectRoot.resolve("assets/ui/atlas"));
|
||||
final PackerRegistryException exception = assertThrows(PackerRegistryException.class, () -> lookup.resolveExistingRoot(project, state.assets().getFirst()));
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> lookup.resolveExistingRoot(project, state.assets().getFirst()));
|
||||
assertTrue(exception.getMessage().contains("does not exist"));
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"schema_version": 9,
|
||||
"asset_uuid": "future-uuid-1",
|
||||
"name": "future_asset",
|
||||
"type": "image_bank",
|
||||
"inputs": {
|
||||
@@ -7,7 +8,7 @@
|
||||
},
|
||||
"output": {
|
||||
"format": "TILES/indexed_v1",
|
||||
"codec": "RAW"
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "fixture-uuid-1",
|
||||
"name": "ui_atlas",
|
||||
"type": "image_bank",
|
||||
"inputs": {
|
||||
@@ -7,7 +8,7 @@
|
||||
},
|
||||
"output": {
|
||||
"format": "TILES/indexed_v1",
|
||||
"codec": "RAW"
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "orphan-uuid-1",
|
||||
"name": "ui_sounds",
|
||||
"type": "sound_bank",
|
||||
"inputs": {
|
||||
@@ -7,7 +8,7 @@
|
||||
},
|
||||
"output": {
|
||||
"format": "SOUND/bank_v1",
|
||||
"codec": "RAW"
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": false
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "orphan-uuid-2",
|
||||
"name": "ui_sounds",
|
||||
"type": "sound_bank",
|
||||
"inputs": {
|
||||
@@ -7,7 +8,7 @@
|
||||
},
|
||||
"output": {
|
||||
"format": "SOUND/bank_v1",
|
||||
"codec": "RAW"
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": false
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "uuid-1",
|
||||
"name": "ui_atlas",
|
||||
"type": "image_bank",
|
||||
"inputs": {
|
||||
@@ -7,7 +8,7 @@
|
||||
},
|
||||
"output": {
|
||||
"format": "TILES/indexed_v1",
|
||||
"codec": "RAW"
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
@@ -1,7 +0,0 @@
|
||||
package p.packer.api;
|
||||
|
||||
public enum PackerOperationClass {
|
||||
READ_ONLY,
|
||||
REGISTRY_MUTATION,
|
||||
WORKSPACE_MUTATION
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package p.packer.api.building;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerBuildRequest(
|
||||
PackerProjectContext project,
|
||||
boolean incremental) {
|
||||
|
||||
public PackerBuildRequest {
|
||||
Objects.requireNonNull(project, "project");
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package p.packer.api.building;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerBuildResult(
|
||||
PackerOperationStatus status,
|
||||
String summary,
|
||||
Path assetsArchive,
|
||||
Map<String, Path> companionArtifacts,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
|
||||
public PackerBuildResult {
|
||||
Objects.requireNonNull(status, "status");
|
||||
summary = Objects.requireNonNull(summary, "summary").trim();
|
||||
assetsArchive = Objects.requireNonNull(assetsArchive, "assetsArchive").toAbsolutePath().normalize();
|
||||
companionArtifacts = Map.copyOf(Objects.requireNonNull(companionArtifacts, "companionArtifacts"));
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
if (summary.isBlank()) {
|
||||
throw new IllegalArgumentException("summary must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package p.packer.api.building;
|
||||
|
||||
import p.packer.api.PackerOperationClass;
|
||||
|
||||
public interface PackerBuildService {
|
||||
PackerOperationClass operationClass();
|
||||
|
||||
PackerBuildResult build(PackerBuildRequest request);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package p.packer.api.doctor;
|
||||
|
||||
public enum PackerDoctorMode {
|
||||
MANAGED_WORLD,
|
||||
EXPANDED_WORKSPACE
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package p.packer.api.doctor;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerDoctorRequest(
|
||||
PackerProjectContext project,
|
||||
PackerDoctorMode mode,
|
||||
boolean includeSafeFixes) {
|
||||
|
||||
public PackerDoctorRequest {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(mode, "mode");
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package p.packer.api.doctor;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerDoctorResult(
|
||||
PackerOperationStatus status,
|
||||
String summary,
|
||||
List<PackerDiagnostic> diagnostics,
|
||||
List<String> safeFixes) {
|
||||
|
||||
public PackerDoctorResult {
|
||||
Objects.requireNonNull(status, "status");
|
||||
summary = Objects.requireNonNull(summary, "summary").trim();
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
safeFixes = List.copyOf(Objects.requireNonNull(safeFixes, "safeFixes"));
|
||||
if (summary.isBlank()) {
|
||||
throw new IllegalArgumentException("summary must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package p.packer.api.doctor;
|
||||
|
||||
import p.packer.api.PackerOperationClass;
|
||||
|
||||
public interface PackerDoctorService {
|
||||
PackerOperationClass operationClass();
|
||||
|
||||
PackerDoctorResult doctor(PackerDoctorRequest request);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package p.packer.api.events;
|
||||
|
||||
public enum PackerEventKind {
|
||||
ASSET_DISCOVERED,
|
||||
ASSET_CHANGED,
|
||||
DIAGNOSTICS_UPDATED,
|
||||
BUILD_STARTED,
|
||||
BUILD_FINISHED,
|
||||
CACHE_HIT,
|
||||
CACHE_MISS,
|
||||
PREVIEW_READY,
|
||||
ACTION_APPLIED,
|
||||
ACTION_FAILED,
|
||||
PROGRESS_UPDATED
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package p.packer.api.events;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PackerEventSink {
|
||||
void publish(PackerEvent event);
|
||||
|
||||
static PackerEventSink noop() {
|
||||
return ignored -> {
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package p.packer.api.mutations;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerMutationPreview(
|
||||
PackerOperationStatus status,
|
||||
String summary,
|
||||
String operationId,
|
||||
PackerMutationRequest request,
|
||||
List<PackerProposedAction> proposedActions,
|
||||
List<PackerDiagnostic> diagnostics,
|
||||
List<String> blockers,
|
||||
List<String> warnings,
|
||||
List<String> safeFixes,
|
||||
boolean highRisk,
|
||||
Path targetAssetRoot) {
|
||||
|
||||
public PackerMutationPreview {
|
||||
Objects.requireNonNull(status, "status");
|
||||
summary = Objects.requireNonNull(summary, "summary").trim();
|
||||
operationId = Objects.requireNonNull(operationId, "operationId").trim();
|
||||
Objects.requireNonNull(request, "request");
|
||||
proposedActions = List.copyOf(Objects.requireNonNull(proposedActions, "proposedActions"));
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
blockers = List.copyOf(Objects.requireNonNull(blockers, "blockers"));
|
||||
warnings = List.copyOf(Objects.requireNonNull(warnings, "warnings"));
|
||||
safeFixes = List.copyOf(Objects.requireNonNull(safeFixes, "safeFixes"));
|
||||
targetAssetRoot = targetAssetRoot == null ? null : targetAssetRoot.toAbsolutePath().normalize();
|
||||
if (summary.isBlank() || operationId.isBlank()) {
|
||||
throw new IllegalArgumentException("summary and operationId must not be blank");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canApply() {
|
||||
return blockers.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package p.packer.api.mutations;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerMutationRequest(
|
||||
PackerProjectContext project,
|
||||
PackerMutationType type,
|
||||
String assetReference,
|
||||
Path targetRoot) {
|
||||
|
||||
public PackerMutationRequest {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(type, "type");
|
||||
assetReference = Objects.requireNonNull(assetReference, "assetReference").trim();
|
||||
targetRoot = targetRoot == null ? null : targetRoot.toAbsolutePath().normalize();
|
||||
if (assetReference.isBlank()) {
|
||||
throw new IllegalArgumentException("assetReference must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package p.packer.api.mutations;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerMutationResult(
|
||||
PackerOperationStatus status,
|
||||
String summary,
|
||||
String operationId,
|
||||
List<PackerProposedAction> appliedActions,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
|
||||
public PackerMutationResult {
|
||||
Objects.requireNonNull(status, "status");
|
||||
summary = Objects.requireNonNull(summary, "summary").trim();
|
||||
operationId = Objects.requireNonNull(operationId, "operationId").trim();
|
||||
appliedActions = List.copyOf(Objects.requireNonNull(appliedActions, "appliedActions"));
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
if (summary.isBlank() || operationId.isBlank()) {
|
||||
throw new IllegalArgumentException("summary and operationId must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package p.packer.api.mutations;
|
||||
|
||||
import p.packer.api.PackerOperationClass;
|
||||
|
||||
public interface PackerMutationService {
|
||||
PackerOperationClass operationClass();
|
||||
|
||||
PackerMutationPreview preview(PackerMutationRequest request);
|
||||
|
||||
PackerMutationResult apply(PackerMutationPreview preview);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package p.packer.api.mutations;
|
||||
|
||||
public enum PackerMutationType {
|
||||
REGISTER_ASSET,
|
||||
INCLUDE_ASSET_IN_BUILD,
|
||||
EXCLUDE_ASSET_FROM_BUILD,
|
||||
REMOVE_ASSET,
|
||||
RELOCATE_ASSET
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package p.packer.api.mutations;
|
||||
|
||||
import p.packer.api.PackerOperationClass;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerProposedAction(
|
||||
PackerOperationClass operationClass,
|
||||
String verb,
|
||||
String target) {
|
||||
|
||||
public PackerProposedAction {
|
||||
Objects.requireNonNull(operationClass, "operationClass");
|
||||
verb = Objects.requireNonNull(verb, "verb").trim();
|
||||
target = Objects.requireNonNull(target, "target").trim();
|
||||
if (verb.isBlank() || target.isBlank()) {
|
||||
throw new IllegalArgumentException("verb and target must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package p.packer.api.workspace;
|
||||
|
||||
import p.packer.api.PackerProjectContext;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record GetAssetDetailsRequest(
|
||||
PackerProjectContext project,
|
||||
String assetReference) {
|
||||
|
||||
public GetAssetDetailsRequest {
|
||||
Objects.requireNonNull(project, "project");
|
||||
assetReference = Objects.requireNonNull(assetReference, "assetReference").trim();
|
||||
if (assetReference.isBlank()) {
|
||||
throw new IllegalArgumentException("assetReference must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
package p.packer.building;
|
||||
|
||||
import p.packer.api.PackerOperationClass;
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.building.PackerBuildRequest;
|
||||
import p.packer.api.building.PackerBuildResult;
|
||||
import p.packer.api.building.PackerBuildService;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.packer.api.events.PackerEventSink;
|
||||
import p.packer.api.events.PackerProgress;
|
||||
import p.packer.events.PackerOperationEventEmitter;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class FileSystemPackerBuildService implements PackerBuildService {
|
||||
private static final byte[] MAGIC = new byte[]{'P', 'P', 'A', '1'};
|
||||
private static final int SCHEMA_VERSION = 1;
|
||||
private static final int PRELUDE_SIZE = 24;
|
||||
|
||||
private final PackerBuildPlanner buildPlanner;
|
||||
private final PackerEventSink eventSink;
|
||||
|
||||
public FileSystemPackerBuildService() {
|
||||
this(new PackerBuildPlanner(), PackerEventSink.noop());
|
||||
}
|
||||
|
||||
public FileSystemPackerBuildService(PackerBuildPlanner buildPlanner) {
|
||||
this(buildPlanner, PackerEventSink.noop());
|
||||
}
|
||||
|
||||
public FileSystemPackerBuildService(PackerBuildPlanner buildPlanner, PackerEventSink eventSink) {
|
||||
this.buildPlanner = Objects.requireNonNull(buildPlanner, "buildPlanner");
|
||||
this.eventSink = Objects.requireNonNull(eventSink, "eventSink");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackerOperationClass operationClass() {
|
||||
return PackerOperationClass.WORKSPACE_MUTATION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackerBuildResult build(PackerBuildRequest request) {
|
||||
final PackerBuildRequest buildRequest = Objects.requireNonNull(request, "request");
|
||||
final PackerOperationEventEmitter events = new PackerOperationEventEmitter(buildRequest.project(), eventSink);
|
||||
final Path buildDirectory = buildRequest.project().rootPath().resolve("build");
|
||||
final Path assetsArchive = buildDirectory.resolve("assets.pa").toAbsolutePath().normalize();
|
||||
final Path assetTableJson = buildDirectory.resolve("asset_table.json").toAbsolutePath().normalize();
|
||||
final Path preloadJson = buildDirectory.resolve("preload.json").toAbsolutePath().normalize();
|
||||
final Path metadataJson = buildDirectory.resolve("asset_table_metadata.json").toAbsolutePath().normalize();
|
||||
final List<PackerDiagnostic> trustDiagnostics = new ArrayList<>(validateExistingOutputs(buildRequest, assetsArchive, metadataJson));
|
||||
if (!trustDiagnostics.isEmpty()) {
|
||||
final String summary = trustDiagnostics.getFirst().message();
|
||||
events.emit(PackerEventKind.BUILD_FINISHED, summary, new PackerProgress(1.0d, false), List.of());
|
||||
return new PackerBuildResult(
|
||||
PackerOperationStatus.FAILED,
|
||||
summary,
|
||||
assetsArchive,
|
||||
Map.of(),
|
||||
trustDiagnostics);
|
||||
}
|
||||
events.emit(PackerEventKind.BUILD_STARTED, "Build started.", new PackerProgress(0.0d, false), List.of());
|
||||
final PackerBuildPlanResult planResult = buildPlanner.plan(buildRequest.project());
|
||||
if (planResult.plan() == null) {
|
||||
events.emit(PackerEventKind.BUILD_FINISHED, planResult.summary(), new PackerProgress(1.0d, false), List.of());
|
||||
return new PackerBuildResult(
|
||||
PackerOperationStatus.FAILED,
|
||||
planResult.summary(),
|
||||
assetsArchive,
|
||||
Map.of(),
|
||||
planResult.diagnostics());
|
||||
}
|
||||
|
||||
try {
|
||||
Files.createDirectories(buildDirectory);
|
||||
final String previousCacheKey = loadPreviousCacheKey(metadataJson);
|
||||
events.emit(
|
||||
previousCacheKey != null && previousCacheKey.equals(planResult.plan().cacheKey()) ? PackerEventKind.CACHE_HIT : PackerEventKind.CACHE_MISS,
|
||||
previousCacheKey != null && previousCacheKey.equals(planResult.plan().cacheKey()) ? "Build cache hit." : "Build cache miss.",
|
||||
List.of());
|
||||
final EmittedArchive archive = emitArchive(planResult.plan());
|
||||
events.emit(PackerEventKind.PROGRESS_UPDATED, "Build archive prepared.", new PackerProgress(0.5d, false), List.of());
|
||||
Files.write(assetsArchive, archive.bytes());
|
||||
Files.writeString(assetTableJson, archive.assetTableJson(), StandardCharsets.UTF_8);
|
||||
Files.writeString(preloadJson, archive.preloadJson(), StandardCharsets.UTF_8);
|
||||
Files.writeString(metadataJson, archive.metadataJson(), StandardCharsets.UTF_8);
|
||||
events.emit(PackerEventKind.BUILD_FINISHED, "Build finished.", new PackerProgress(1.0d, false), List.of());
|
||||
return new PackerBuildResult(
|
||||
planResult.status(),
|
||||
"Build emitted " + planResult.plan().assets().size() + " assets.",
|
||||
assetsArchive,
|
||||
Map.of(
|
||||
"build/asset_table.json", assetTableJson,
|
||||
"build/preload.json", preloadJson,
|
||||
"build/asset_table_metadata.json", metadataJson),
|
||||
planResult.diagnostics());
|
||||
} catch (IOException exception) {
|
||||
throw new UncheckedIOException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private EmittedArchive emitArchive(PackerBuildPlan plan) throws IOException {
|
||||
final List<Map<String, Object>> assetTable = new ArrayList<>();
|
||||
final ByteArrayOutputStream payloadStream = new ByteArrayOutputStream();
|
||||
int payloadOffset = 0;
|
||||
for (PackerPlannedAsset asset : plan.assets()) {
|
||||
final ByteArrayOutputStream assetPayload = new ByteArrayOutputStream();
|
||||
for (PackerPlannedInput input : asset.inputs()) {
|
||||
assetPayload.write(Files.readAllBytes(input.absolutePath()));
|
||||
}
|
||||
final byte[] assetBytes = assetPayload.toByteArray();
|
||||
payloadStream.write(assetBytes);
|
||||
|
||||
final Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("asset_family", asset.assetFamily());
|
||||
row.put("asset_id", asset.assetId());
|
||||
row.put("asset_name", asset.assetName());
|
||||
row.put("asset_uuid", asset.assetUuid());
|
||||
row.put("output_codec", asset.outputCodec());
|
||||
row.put("output_format", asset.outputFormat());
|
||||
row.put("payload_length", assetBytes.length);
|
||||
row.put("payload_offset", payloadOffset);
|
||||
row.put("relative_root", asset.relativeRoot());
|
||||
assetTable.add(row);
|
||||
payloadOffset += assetBytes.length;
|
||||
}
|
||||
|
||||
final List<Map<String, Object>> preload = plan.assets().stream()
|
||||
.filter(PackerPlannedAsset::preload)
|
||||
.map(asset -> {
|
||||
final Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("asset_id", asset.assetId());
|
||||
row.put("asset_name", asset.assetName());
|
||||
row.put("asset_uuid", asset.assetUuid());
|
||||
return row;
|
||||
})
|
||||
.toList();
|
||||
|
||||
final String assetTableJson = PackerCanonicalJson.write(assetTable);
|
||||
final String preloadJson = PackerCanonicalJson.write(preload);
|
||||
final String headerJson = PackerCanonicalJson.write(Map.of(
|
||||
"asset_table", assetTable,
|
||||
"preload", preload));
|
||||
final byte[] headerBytes = headerJson.getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] payloadBytes = payloadStream.toByteArray();
|
||||
final byte[] prelude = ByteBuffer.allocate(PRELUDE_SIZE)
|
||||
.order(ByteOrder.BIG_ENDIAN)
|
||||
.put(MAGIC)
|
||||
.putInt(SCHEMA_VERSION)
|
||||
.putInt(headerBytes.length)
|
||||
.putInt(PRELUDE_SIZE + headerBytes.length)
|
||||
.putInt(0)
|
||||
.putInt(0)
|
||||
.array();
|
||||
|
||||
final byte[] archiveBytes = new byte[prelude.length + headerBytes.length + payloadBytes.length];
|
||||
System.arraycopy(prelude, 0, archiveBytes, 0, prelude.length);
|
||||
System.arraycopy(headerBytes, 0, archiveBytes, prelude.length, headerBytes.length);
|
||||
System.arraycopy(payloadBytes, 0, archiveBytes, prelude.length + headerBytes.length, payloadBytes.length);
|
||||
|
||||
final String metadataJson = PackerCanonicalJson.write(Map.of(
|
||||
"asset_count", plan.assets().size(),
|
||||
"cache_key", plan.cacheKey(),
|
||||
"header_length", headerBytes.length,
|
||||
"payload_bytes", payloadBytes.length,
|
||||
"payload_offset", PRELUDE_SIZE + headerBytes.length,
|
||||
"schema_version", SCHEMA_VERSION));
|
||||
return new EmittedArchive(archiveBytes, assetTableJson, preloadJson, metadataJson);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private String loadPreviousCacheKey(Path metadataJson) {
|
||||
if (!Files.isRegularFile(metadataJson)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
final Map<String, Object> metadata = new com.fasterxml.jackson.databind.ObjectMapper().readValue(Files.readString(metadataJson), Map.class);
|
||||
final Object schema = metadata.get("schema_version");
|
||||
if (!(schema instanceof Number) || ((Number) schema).intValue() != SCHEMA_VERSION) {
|
||||
throw new IllegalArgumentException("Unsupported build metadata schema_version: " + schema);
|
||||
}
|
||||
return metadata.getOrDefault("cache_key", "").toString();
|
||||
} catch (IOException exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<PackerDiagnostic> validateExistingOutputs(PackerBuildRequest request, Path assetsArchive, Path metadataJson) {
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
|
||||
if (Files.isRegularFile(metadataJson)) {
|
||||
try {
|
||||
final Map<?, ?> metadata = new com.fasterxml.jackson.databind.ObjectMapper().readValue(Files.readString(metadataJson), Map.class);
|
||||
final Object schemaVersion = metadata.get("schema_version");
|
||||
if (!(schemaVersion instanceof Number) || ((Number) schemaVersion).intValue() != SCHEMA_VERSION) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Unsupported build metadata schema_version: " + schemaVersion,
|
||||
metadataJson,
|
||||
true));
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Unable to read build metadata for version validation.",
|
||||
metadataJson,
|
||||
true));
|
||||
}
|
||||
}
|
||||
if (request.incremental() && Files.isRegularFile(assetsArchive)) {
|
||||
try {
|
||||
final byte[] prelude = Files.readAllBytes(assetsArchive);
|
||||
if (prelude.length < PRELUDE_SIZE) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Existing assets.pa prelude is truncated.",
|
||||
assetsArchive,
|
||||
true));
|
||||
} else {
|
||||
final ByteBuffer buffer = ByteBuffer.wrap(prelude, 0, PRELUDE_SIZE).order(ByteOrder.BIG_ENDIAN);
|
||||
final byte[] magic = new byte[4];
|
||||
buffer.get(magic);
|
||||
final int schemaVersion = buffer.getInt();
|
||||
if (!java.util.Arrays.equals(magic, MAGIC) || schemaVersion != SCHEMA_VERSION) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Existing assets.pa uses an unsupported writer-side schema surface.",
|
||||
assetsArchive,
|
||||
true));
|
||||
}
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Unable to inspect existing assets.pa for incremental trust validation.",
|
||||
assetsArchive,
|
||||
true));
|
||||
}
|
||||
}
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
private record EmittedArchive(
|
||||
byte[] bytes,
|
||||
String assetTableJson,
|
||||
String preloadJson,
|
||||
String metadataJson) {
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package p.packer.building;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerBuildPlan(
|
||||
String cacheKey,
|
||||
List<PackerPlannedAsset> assets,
|
||||
String assetTableJson,
|
||||
String preloadJson) {
|
||||
|
||||
public PackerBuildPlan {
|
||||
cacheKey = Objects.requireNonNull(cacheKey, "cacheKey").trim();
|
||||
assets = List.copyOf(Objects.requireNonNull(assets, "assets"));
|
||||
assetTableJson = Objects.requireNonNull(assetTableJson, "assetTableJson").trim();
|
||||
preloadJson = Objects.requireNonNull(preloadJson, "preloadJson").trim();
|
||||
if (cacheKey.isBlank() || assetTableJson.isBlank() || preloadJson.isBlank()) {
|
||||
throw new IllegalArgumentException("build plan fields must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package p.packer.building;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerBuildPlanResult(
|
||||
PackerOperationStatus status,
|
||||
String summary,
|
||||
PackerBuildPlan plan,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
|
||||
public PackerBuildPlanResult {
|
||||
Objects.requireNonNull(status, "status");
|
||||
summary = Objects.requireNonNull(summary, "summary").trim();
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
if (summary.isBlank()) {
|
||||
throw new IllegalArgumentException("summary must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,222 +0,0 @@
|
||||
package p.packer.building;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.api.assets.PackerBuildParticipation;
|
||||
import p.packer.api.assets.PackerAssetState;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.api.workspace.GetAssetDetailsRequest;
|
||||
import p.packer.api.workspace.ListAssetsRequest;
|
||||
import p.packer.declarations.PackerAssetDetailsService;
|
||||
import p.packer.workspace.FileSystemPackerWorkspaceService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class PackerBuildPlanner {
|
||||
private final FileSystemPackerWorkspaceService workspaceService;
|
||||
private final PackerAssetDetailsService detailsService;
|
||||
|
||||
public PackerBuildPlanner() {
|
||||
this(new FileSystemPackerWorkspaceService(), new PackerAssetDetailsService());
|
||||
}
|
||||
|
||||
public PackerBuildPlanner(
|
||||
FileSystemPackerWorkspaceService workspaceService,
|
||||
PackerAssetDetailsService detailsService) {
|
||||
this.workspaceService = Objects.requireNonNull(workspaceService, "workspaceService");
|
||||
this.detailsService = Objects.requireNonNull(detailsService, "detailsService");
|
||||
}
|
||||
|
||||
public PackerBuildPlanResult plan(PackerProjectContext project) {
|
||||
final var snapshot = workspaceService.listAssets(new ListAssetsRequest(Objects.requireNonNull(project, "project")));
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>(snapshot.diagnostics());
|
||||
final List<PackerPlannedAsset> plannedAssets = new ArrayList<>();
|
||||
|
||||
snapshot.assets().stream()
|
||||
.filter(asset -> asset.buildParticipation() == PackerBuildParticipation.INCLUDED)
|
||||
.sorted(Comparator.comparingInt(asset -> asset.identity().assetId()))
|
||||
.forEach(asset -> {
|
||||
final var detailsResult = detailsService.getAssetDetails(new GetAssetDetailsRequest(project, Integer.toString(asset.identity().assetId())));
|
||||
diagnostics.addAll(detailsResult.diagnostics());
|
||||
if (detailsResult.details().summary().state() != PackerAssetState.REGISTERED
|
||||
|| detailsResult.details().summary().buildParticipation() != PackerBuildParticipation.INCLUDED) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.BUILD,
|
||||
"Registered asset is not build-eligible: " + asset.identity().assetName(),
|
||||
asset.identity().assetRoot(),
|
||||
true));
|
||||
return;
|
||||
}
|
||||
|
||||
final List<PackerPlannedInput> plannedInputs = new ArrayList<>();
|
||||
detailsResult.details().inputsByRole().entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByKey())
|
||||
.forEach(entry -> entry.getValue().forEach(input -> {
|
||||
if (!Files.isRegularFile(input)) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.BUILD,
|
||||
"Build input is missing: " + relativePath(project, input),
|
||||
input,
|
||||
true));
|
||||
return;
|
||||
}
|
||||
plannedInputs.add(new PackerPlannedInput(
|
||||
entry.getKey(),
|
||||
asset.identity().assetRoot().relativize(input).toString().replace('\\', '/'),
|
||||
fileHash(input),
|
||||
input));
|
||||
}));
|
||||
|
||||
if (diagnostics.stream().anyMatch(diagnostic ->
|
||||
diagnostic.blocking() && asset.identity().assetRoot().equals(parentRoot(diagnostic.evidencePath(), asset.identity().assetRoot())))) {
|
||||
return;
|
||||
}
|
||||
|
||||
plannedAssets.add(new PackerPlannedAsset(
|
||||
asset.identity().assetId(),
|
||||
asset.identity().assetUuid(),
|
||||
asset.identity().assetName(),
|
||||
asset.assetFamily(),
|
||||
relativePath(project, asset.identity().assetRoot()),
|
||||
detailsResult.details().outputFormat(),
|
||||
detailsResult.details().outputCodec(),
|
||||
asset.preloadEnabled(),
|
||||
plannedInputs,
|
||||
asset.identity().assetRoot()));
|
||||
});
|
||||
|
||||
final boolean hasBlocking = diagnostics.stream().anyMatch(PackerDiagnostic::blocking);
|
||||
if (hasBlocking) {
|
||||
return new PackerBuildPlanResult(
|
||||
PackerOperationStatus.FAILED,
|
||||
"Build plan blocked by diagnostics.",
|
||||
null,
|
||||
List.copyOf(diagnostics));
|
||||
}
|
||||
|
||||
final String assetTableJson = buildAssetTableJson(plannedAssets);
|
||||
final String preloadJson = buildPreloadJson(plannedAssets);
|
||||
final String cacheKey = sha256(PackerCanonicalJson.write(Map.of(
|
||||
"asset_table", parseCanonical(assetTableJson),
|
||||
"preload", parseCanonical(preloadJson),
|
||||
"inputs", plannedAssets.stream().map(this::cacheKeyView).toList())));
|
||||
return new PackerBuildPlanResult(
|
||||
diagnostics.isEmpty() ? PackerOperationStatus.SUCCESS : PackerOperationStatus.PARTIAL,
|
||||
"Build plan ready for " + plannedAssets.size() + " included assets.",
|
||||
new PackerBuildPlan(cacheKey, plannedAssets, assetTableJson, preloadJson),
|
||||
List.copyOf(diagnostics));
|
||||
}
|
||||
|
||||
private Map<String, Object> cacheKeyView(PackerPlannedAsset asset) {
|
||||
return Map.of(
|
||||
"asset_family", asset.assetFamily(),
|
||||
"asset_id", asset.assetId(),
|
||||
"asset_name", asset.assetName(),
|
||||
"asset_uuid", asset.assetUuid(),
|
||||
"inputs", asset.inputs().stream().map(input -> Map.of(
|
||||
"content_hash", input.contentHash(),
|
||||
"path", input.relativePath(),
|
||||
"role", input.role())).toList(),
|
||||
"output_codec", asset.outputCodec(),
|
||||
"output_format", asset.outputFormat(),
|
||||
"preload", asset.preload(),
|
||||
"root", asset.relativeRoot());
|
||||
}
|
||||
|
||||
private String buildAssetTableJson(List<PackerPlannedAsset> assets) {
|
||||
final List<Map<String, Object>> rows = assets.stream()
|
||||
.map(asset -> {
|
||||
final Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("asset_family", asset.assetFamily());
|
||||
row.put("asset_id", asset.assetId());
|
||||
row.put("asset_name", asset.assetName());
|
||||
row.put("asset_uuid", asset.assetUuid());
|
||||
row.put("output_codec", asset.outputCodec());
|
||||
row.put("output_format", asset.outputFormat());
|
||||
row.put("relative_root", asset.relativeRoot());
|
||||
return row;
|
||||
})
|
||||
.toList();
|
||||
return PackerCanonicalJson.write(rows);
|
||||
}
|
||||
|
||||
private String buildPreloadJson(List<PackerPlannedAsset> assets) {
|
||||
final List<Map<String, Object>> rows = assets.stream()
|
||||
.filter(PackerPlannedAsset::preload)
|
||||
.map(asset -> {
|
||||
final Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("asset_id", asset.assetId());
|
||||
row.put("asset_name", asset.assetName());
|
||||
row.put("asset_uuid", asset.assetUuid());
|
||||
return row;
|
||||
})
|
||||
.toList();
|
||||
return PackerCanonicalJson.write(rows);
|
||||
}
|
||||
|
||||
private Object parseCanonical(String json) {
|
||||
try {
|
||||
return new com.fasterxml.jackson.databind.ObjectMapper().readValue(json, Object.class);
|
||||
} catch (IOException exception) {
|
||||
throw new IllegalArgumentException("Unable to parse canonical JSON", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private Path parentRoot(Path evidencePath, Path fallbackRoot) {
|
||||
if (evidencePath == null) {
|
||||
return fallbackRoot;
|
||||
}
|
||||
Path current = evidencePath.toAbsolutePath().normalize();
|
||||
while (current != null && !current.equals(fallbackRoot)) {
|
||||
if (current.getParent() != null && current.getParent().equals(fallbackRoot)) {
|
||||
return fallbackRoot;
|
||||
}
|
||||
current = current.getParent();
|
||||
}
|
||||
return fallbackRoot;
|
||||
}
|
||||
|
||||
private String relativePath(PackerProjectContext project, Path path) {
|
||||
final Path normalized = path.toAbsolutePath().normalize();
|
||||
final Path assetsRoot = project.rootPath().resolve("assets").toAbsolutePath().normalize();
|
||||
if (normalized.startsWith(assetsRoot)) {
|
||||
return assetsRoot.relativize(normalized).toString().replace('\\', '/');
|
||||
}
|
||||
return normalized.toString();
|
||||
}
|
||||
|
||||
private String fileHash(Path path) {
|
||||
try {
|
||||
return sha256(Files.readAllBytes(path));
|
||||
} catch (IOException exception) {
|
||||
throw new IllegalArgumentException("Unable to hash build input: " + path, exception);
|
||||
}
|
||||
}
|
||||
|
||||
private String sha256(String value) {
|
||||
return sha256(value.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private String sha256(byte[] value) {
|
||||
try {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value));
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException("SHA-256 is not available", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package p.packer.building;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
public final class PackerCanonicalJson {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper()
|
||||
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
|
||||
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
|
||||
|
||||
private PackerCanonicalJson() {
|
||||
}
|
||||
|
||||
public static String write(Object value) {
|
||||
try {
|
||||
return MAPPER.writeValueAsString(value);
|
||||
} catch (JsonProcessingException exception) {
|
||||
throw new IllegalArgumentException("Unable to serialize canonical JSON value", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package p.packer.building;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerPlannedAsset(
|
||||
int assetId,
|
||||
String assetUuid,
|
||||
String assetName,
|
||||
String assetFamily,
|
||||
String relativeRoot,
|
||||
String outputFormat,
|
||||
String outputCodec,
|
||||
boolean preload,
|
||||
List<PackerPlannedInput> inputs,
|
||||
Path assetRoot) {
|
||||
|
||||
public PackerPlannedAsset {
|
||||
assetUuid = Objects.requireNonNull(assetUuid, "assetUuid").trim();
|
||||
assetName = Objects.requireNonNull(assetName, "assetName").trim();
|
||||
assetFamily = Objects.requireNonNull(assetFamily, "assetFamily").trim();
|
||||
relativeRoot = Objects.requireNonNull(relativeRoot, "relativeRoot").trim();
|
||||
outputFormat = Objects.requireNonNull(outputFormat, "outputFormat").trim();
|
||||
outputCodec = Objects.requireNonNull(outputCodec, "outputCodec").trim();
|
||||
inputs = List.copyOf(Objects.requireNonNull(inputs, "inputs"));
|
||||
assetRoot = Objects.requireNonNull(assetRoot, "assetRoot").toAbsolutePath().normalize();
|
||||
if (assetId <= 0 || assetUuid.isBlank() || assetName.isBlank() || relativeRoot.isBlank() || outputFormat.isBlank() || outputCodec.isBlank()) {
|
||||
throw new IllegalArgumentException("planned asset fields must be valid");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package p.packer.building;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerPlannedInput(
|
||||
String role,
|
||||
String relativePath,
|
||||
String contentHash,
|
||||
Path absolutePath) {
|
||||
|
||||
public PackerPlannedInput {
|
||||
role = Objects.requireNonNull(role, "role").trim();
|
||||
relativePath = Objects.requireNonNull(relativePath, "relativePath").trim();
|
||||
contentHash = Objects.requireNonNull(contentHash, "contentHash").trim();
|
||||
absolutePath = Objects.requireNonNull(absolutePath, "absolutePath").toAbsolutePath().normalize();
|
||||
if (role.isBlank() || relativePath.isBlank() || contentHash.isBlank()) {
|
||||
throw new IllegalArgumentException("planned input fields must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
package p.packer.declarations;
|
||||
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.api.assets.PackerAssetDetails;
|
||||
import p.packer.api.assets.PackerAssetIdentity;
|
||||
import p.packer.api.assets.PackerAssetState;
|
||||
import p.packer.api.assets.PackerAssetSummary;
|
||||
import p.packer.api.assets.PackerBuildParticipation;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.api.workspace.GetAssetDetailsRequest;
|
||||
import p.packer.api.workspace.GetAssetDetailsResult;
|
||||
import p.packer.foundation.PackerRegistryEntry;
|
||||
import p.packer.foundation.PackerRegistryLookup;
|
||||
import p.packer.foundation.PackerRegistryState;
|
||||
import p.packer.foundation.PackerWorkspaceFoundation;
|
||||
import p.packer.foundation.PackerWorkspacePaths;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class PackerAssetDetailsService {
|
||||
private final PackerWorkspaceFoundation workspaceFoundation;
|
||||
private final PackerAssetDeclarationParser parser;
|
||||
|
||||
public PackerAssetDetailsService() {
|
||||
this(new PackerWorkspaceFoundation(), new PackerAssetDeclarationParser());
|
||||
}
|
||||
|
||||
public PackerAssetDetailsService(
|
||||
PackerWorkspaceFoundation workspaceFoundation,
|
||||
PackerAssetDeclarationParser parser) {
|
||||
this.workspaceFoundation = Objects.requireNonNull(workspaceFoundation, "workspaceFoundation");
|
||||
this.parser = Objects.requireNonNull(parser, "parser");
|
||||
}
|
||||
|
||||
public GetAssetDetailsResult getAssetDetails(GetAssetDetailsRequest request) {
|
||||
final PackerProjectContext project = Objects.requireNonNull(request, "request").project();
|
||||
final PackerRegistryState registry = workspaceFoundation.loadRegistry(project);
|
||||
final ResolvedAssetReference resolved = resolveReference(project, registry, request.assetReference());
|
||||
final Path manifestPath = resolved.assetRoot().resolve("asset.json");
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>(resolved.diagnostics());
|
||||
|
||||
if (!Files.isRegularFile(manifestPath)) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"asset.json was not found for the requested asset root.",
|
||||
manifestPath,
|
||||
true));
|
||||
return failureResult(resolved, diagnostics);
|
||||
}
|
||||
|
||||
final PackerAssetDeclarationParseResult parsed = parser.parse(manifestPath);
|
||||
diagnostics.addAll(parsed.diagnostics());
|
||||
if (!parsed.valid()) {
|
||||
return failureResult(resolved, diagnostics);
|
||||
}
|
||||
|
||||
final PackerAssetDeclaration declaration = parsed.declaration();
|
||||
final PackerAssetState state = resolved.registryEntry().isPresent()
|
||||
? PackerAssetState.REGISTERED
|
||||
: PackerAssetState.UNREGISTERED;
|
||||
final PackerAssetSummary summary = new PackerAssetSummary(
|
||||
new PackerAssetIdentity(
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetId).orElse(null),
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetUuid).orElse(null),
|
||||
declaration.name(),
|
||||
resolved.assetRoot()),
|
||||
state,
|
||||
resolved.registryEntry().map(entry -> entry.includedInBuild()
|
||||
? PackerBuildParticipation.INCLUDED
|
||||
: PackerBuildParticipation.EXCLUDED).orElse(PackerBuildParticipation.EXCLUDED),
|
||||
declaration.type(),
|
||||
declaration.preloadEnabled(),
|
||||
!diagnostics.isEmpty());
|
||||
final PackerAssetDetails details = new PackerAssetDetails(
|
||||
summary,
|
||||
declaration.outputFormat(),
|
||||
declaration.outputCodec(),
|
||||
resolveInputs(resolved.assetRoot(), declaration.inputsByRole()),
|
||||
diagnostics);
|
||||
return new GetAssetDetailsResult(
|
||||
diagnostics.isEmpty() ? PackerOperationStatus.SUCCESS : PackerOperationStatus.PARTIAL,
|
||||
"Asset details resolved from packer declaration state.",
|
||||
details,
|
||||
diagnostics);
|
||||
}
|
||||
|
||||
private GetAssetDetailsResult failureResult(ResolvedAssetReference resolved, List<PackerDiagnostic> diagnostics) {
|
||||
final PackerAssetState state = resolved.registryEntry().isPresent()
|
||||
? PackerAssetState.REGISTERED
|
||||
: PackerAssetState.UNREGISTERED;
|
||||
final PackerAssetSummary summary = new PackerAssetSummary(
|
||||
new PackerAssetIdentity(
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetId).orElse(null),
|
||||
resolved.registryEntry().map(PackerRegistryEntry::assetUuid).orElse(null),
|
||||
resolved.assetRoot().getFileName().toString(),
|
||||
resolved.assetRoot()),
|
||||
state,
|
||||
resolved.registryEntry().map(entry -> entry.includedInBuild()
|
||||
? PackerBuildParticipation.INCLUDED
|
||||
: PackerBuildParticipation.EXCLUDED).orElse(PackerBuildParticipation.EXCLUDED),
|
||||
"unknown",
|
||||
false,
|
||||
true);
|
||||
final PackerAssetDetails details = new PackerAssetDetails(summary, "unknown", "unknown", Map.of(), diagnostics);
|
||||
return new GetAssetDetailsResult(
|
||||
PackerOperationStatus.FAILED,
|
||||
"Asset declaration is invalid or unreadable.",
|
||||
details,
|
||||
diagnostics);
|
||||
}
|
||||
|
||||
private Map<String, List<Path>> resolveInputs(Path assetRoot, Map<String, List<String>> inputsByRole) {
|
||||
final Map<String, List<Path>> resolved = new LinkedHashMap<>();
|
||||
inputsByRole.forEach((role, inputs) -> resolved.put(
|
||||
role,
|
||||
inputs.stream().map(input -> assetRoot.resolve(input).toAbsolutePath().normalize()).toList()));
|
||||
return Map.copyOf(resolved);
|
||||
}
|
||||
|
||||
private ResolvedAssetReference resolveReference(PackerProjectContext project, PackerRegistryState registry, String reference) {
|
||||
final PackerRegistryLookup lookup = workspaceFoundation.lookup();
|
||||
final Optional<PackerRegistryEntry> byId = parseAssetId(reference).flatMap(assetId -> lookup.findByAssetId(registry, assetId));
|
||||
if (byId.isPresent()) {
|
||||
return new ResolvedAssetReference(
|
||||
PackerWorkspacePaths.assetRoot(project, byId.get().root()),
|
||||
byId,
|
||||
List.of());
|
||||
}
|
||||
|
||||
final Optional<PackerRegistryEntry> byUuid = lookup.findByAssetUuid(registry, reference);
|
||||
if (byUuid.isPresent()) {
|
||||
return new ResolvedAssetReference(
|
||||
PackerWorkspacePaths.assetRoot(project, byUuid.get().root()),
|
||||
byUuid,
|
||||
List.of());
|
||||
}
|
||||
|
||||
final Path candidateRoot = PackerWorkspacePaths.assetRoot(project, reference);
|
||||
if (Files.isDirectory(candidateRoot) || Files.isRegularFile(candidateRoot.resolve("asset.json"))) {
|
||||
return new ResolvedAssetReference(candidateRoot, lookup.findByRoot(project, registry, candidateRoot), List.of());
|
||||
}
|
||||
|
||||
final Path missingRoot = candidateRoot;
|
||||
return new ResolvedAssetReference(
|
||||
missingRoot,
|
||||
Optional.empty(),
|
||||
List.of(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Requested asset reference could not be resolved.",
|
||||
missingRoot,
|
||||
true)));
|
||||
}
|
||||
|
||||
private Optional<Integer> parseAssetId(String reference) {
|
||||
try {
|
||||
return Optional.of(Integer.parseInt(reference.trim()));
|
||||
} catch (NumberFormatException ignored) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private record ResolvedAssetReference(
|
||||
Path assetRoot,
|
||||
Optional<PackerRegistryEntry> registryEntry,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
|
||||
private ResolvedAssetReference {
|
||||
assetRoot = Objects.requireNonNull(assetRoot, "assetRoot").toAbsolutePath().normalize();
|
||||
registryEntry = Objects.requireNonNull(registryEntry, "registryEntry");
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
package p.packer.doctor;
|
||||
|
||||
import p.packer.api.PackerOperationClass;
|
||||
import p.packer.api.PackerOperationStatus;
|
||||
import p.packer.api.PackerProjectContext;
|
||||
import p.packer.api.assets.PackerAssetState;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.api.doctor.PackerDoctorMode;
|
||||
import p.packer.api.doctor.PackerDoctorRequest;
|
||||
import p.packer.api.doctor.PackerDoctorResult;
|
||||
import p.packer.api.doctor.PackerDoctorService;
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.packer.api.events.PackerEventSink;
|
||||
import p.packer.api.events.PackerProgress;
|
||||
import p.packer.api.workspace.GetAssetDetailsRequest;
|
||||
import p.packer.api.workspace.ListAssetsRequest;
|
||||
import p.packer.declarations.PackerAssetDetailsService;
|
||||
import p.packer.events.PackerOperationEventEmitter;
|
||||
import p.packer.workspace.FileSystemPackerWorkspaceService;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public final class FileSystemPackerDoctorService implements PackerDoctorService {
|
||||
private final FileSystemPackerWorkspaceService workspaceService;
|
||||
private final PackerAssetDetailsService detailsService;
|
||||
private final PackerEventSink eventSink;
|
||||
|
||||
public FileSystemPackerDoctorService() {
|
||||
this(new FileSystemPackerWorkspaceService(), new PackerAssetDetailsService(), PackerEventSink.noop());
|
||||
}
|
||||
|
||||
public FileSystemPackerDoctorService(
|
||||
FileSystemPackerWorkspaceService workspaceService,
|
||||
PackerAssetDetailsService detailsService) {
|
||||
this(workspaceService, detailsService, PackerEventSink.noop());
|
||||
}
|
||||
|
||||
public FileSystemPackerDoctorService(
|
||||
FileSystemPackerWorkspaceService workspaceService,
|
||||
PackerAssetDetailsService detailsService,
|
||||
PackerEventSink eventSink) {
|
||||
this.workspaceService = Objects.requireNonNull(workspaceService, "workspaceService");
|
||||
this.detailsService = Objects.requireNonNull(detailsService, "detailsService");
|
||||
this.eventSink = Objects.requireNonNull(eventSink, "eventSink");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackerOperationClass operationClass() {
|
||||
return PackerOperationClass.READ_ONLY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackerDoctorResult doctor(PackerDoctorRequest request) {
|
||||
final PackerDoctorRequest doctorRequest = Objects.requireNonNull(request, "request");
|
||||
final PackerProjectContext project = doctorRequest.project();
|
||||
final PackerOperationEventEmitter events = new PackerOperationEventEmitter(project, eventSink);
|
||||
final var snapshot = workspaceService.listAssets(new ListAssetsRequest(project));
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
|
||||
final Set<String> safeFixes = new LinkedHashSet<>();
|
||||
final Set<String> seenDiagnostics = new LinkedHashSet<>();
|
||||
|
||||
for (PackerDiagnostic diagnostic : snapshot.diagnostics()) {
|
||||
addDiagnostic(diagnostics, seenDiagnostics, diagnostic);
|
||||
}
|
||||
|
||||
final int totalAssets = snapshot.assets().size();
|
||||
int inspected = 0;
|
||||
for (var asset : snapshot.assets()) {
|
||||
if (!includeAsset(doctorRequest.mode(), asset.state())) {
|
||||
continue;
|
||||
}
|
||||
inspected += 1;
|
||||
final String assetReference = asset.identity().assetId() == null
|
||||
? relativeAssetRoot(project, asset.identity().assetRoot())
|
||||
: Integer.toString(asset.identity().assetId());
|
||||
final var details = detailsService.getAssetDetails(new GetAssetDetailsRequest(project, assetReference)).details();
|
||||
for (PackerDiagnostic diagnostic : details.diagnostics()) {
|
||||
addDiagnostic(diagnostics, seenDiagnostics, diagnostic);
|
||||
}
|
||||
|
||||
if (doctorRequest.mode() == PackerDoctorMode.EXPANDED_WORKSPACE && asset.state() == PackerAssetState.UNREGISTERED) {
|
||||
addDiagnostic(diagnostics, seenDiagnostics, new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.WARNING,
|
||||
PackerDiagnosticCategory.HYGIENE,
|
||||
"Asset is present in assets/ but not registered, so it stays excluded from builds: " + relativeAssetRoot(project, asset.identity().assetRoot()),
|
||||
asset.identity().assetRoot(),
|
||||
false));
|
||||
if (doctorRequest.includeSafeFixes() && details.diagnostics().stream().noneMatch(PackerDiagnostic::blocking)) {
|
||||
safeFixes.add("register_asset " + relativeAssetRoot(project, asset.identity().assetRoot()));
|
||||
}
|
||||
}
|
||||
|
||||
details.inputsByRole().forEach((role, inputs) -> inputs.forEach(input -> {
|
||||
if (Files.isRegularFile(input)) {
|
||||
return;
|
||||
}
|
||||
final boolean registered = asset.state() == PackerAssetState.REGISTERED;
|
||||
addDiagnostic(diagnostics, seenDiagnostics, new PackerDiagnostic(
|
||||
registered ? PackerDiagnosticSeverity.ERROR : PackerDiagnosticSeverity.WARNING,
|
||||
registered ? PackerDiagnosticCategory.STRUCTURAL : PackerDiagnosticCategory.HYGIENE,
|
||||
"Declared input is missing for role '" + role + "': " + relativeEvidence(project, input),
|
||||
input,
|
||||
registered));
|
||||
}));
|
||||
events.emit(
|
||||
PackerEventKind.PROGRESS_UPDATED,
|
||||
"Doctor inspected asset: " + asset.identity().assetName(),
|
||||
new PackerProgress(totalAssets == 0 ? 1.0d : inspected / (double) totalAssets, false),
|
||||
List.of(asset.identity().assetName()));
|
||||
}
|
||||
|
||||
final long blockingCount = diagnostics.stream().filter(PackerDiagnostic::blocking).count();
|
||||
final long warningCount = diagnostics.stream().filter(diagnostic -> diagnostic.severity() == PackerDiagnosticSeverity.WARNING).count();
|
||||
final PackerOperationStatus status = blockingCount > 0L ? PackerOperationStatus.FAILED
|
||||
: diagnostics.isEmpty() ? PackerOperationStatus.SUCCESS : PackerOperationStatus.PARTIAL;
|
||||
final String summary = blockingCount > 0L
|
||||
? "Doctor found " + blockingCount + " blocking diagnostics and " + warningCount + " warnings."
|
||||
: diagnostics.isEmpty()
|
||||
? "Doctor found no diagnostics."
|
||||
: "Doctor found " + diagnostics.size() + " diagnostics with no blockers.";
|
||||
events.emit(PackerEventKind.DIAGNOSTICS_UPDATED, summary, List.of());
|
||||
return new PackerDoctorResult(status, summary, diagnostics, List.copyOf(safeFixes));
|
||||
}
|
||||
|
||||
private boolean includeAsset(PackerDoctorMode mode, PackerAssetState state) {
|
||||
if (mode == PackerDoctorMode.EXPANDED_WORKSPACE) {
|
||||
return true;
|
||||
}
|
||||
return state == PackerAssetState.REGISTERED;
|
||||
}
|
||||
|
||||
private void addDiagnostic(List<PackerDiagnostic> diagnostics, Set<String> seenDiagnostics, PackerDiagnostic diagnostic) {
|
||||
final String key = diagnostic.category() + "|" + diagnostic.severity() + "|" + diagnostic.message() + "|" + diagnostic.evidencePath() + "|" + diagnostic.blocking();
|
||||
if (seenDiagnostics.add(key)) {
|
||||
diagnostics.add(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
private String relativeAssetRoot(PackerProjectContext project, Path assetRoot) {
|
||||
return project.rootPath().resolve("assets").toAbsolutePath().normalize()
|
||||
.relativize(assetRoot.toAbsolutePath().normalize())
|
||||
.toString()
|
||||
.replace('\\', '/');
|
||||
}
|
||||
|
||||
private String relativeEvidence(PackerProjectContext project, Path evidencePath) {
|
||||
final Path normalizedEvidence = evidencePath.toAbsolutePath().normalize();
|
||||
final Path projectRoot = project.rootPath().toAbsolutePath().normalize();
|
||||
if (normalizedEvidence.startsWith(projectRoot)) {
|
||||
return projectRoot.relativize(normalizedEvidence).toString().replace('\\', '/');
|
||||
}
|
||||
return normalizedEvidence.toString();
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user