asset workspace progress
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package p.packer.models;
|
||||
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerAssetDeclaration(
|
||||
int schemaVersion,
|
||||
String assetUuid,
|
||||
String name,
|
||||
AssetFamilyCatalog assetFamily,
|
||||
Map<String, List<String>> inputsByRole,
|
||||
String outputFormat,
|
||||
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();
|
||||
assetFamily = Objects.requireNonNull(assetFamily, "assetFamily");
|
||||
inputsByRole = Map.copyOf(Objects.requireNonNull(inputsByRole, "inputsByRole"));
|
||||
outputFormat = Objects.requireNonNull(outputFormat, "outputFormat").trim();
|
||||
outputCodec = Objects.requireNonNull(outputCodec, "outputCodec");
|
||||
if (assetUuid.isBlank() || name.isBlank() || outputFormat.isBlank()) {
|
||||
throw new IllegalArgumentException("declaration fields must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package p.packer.models;
|
||||
|
||||
import p.packer.diagnostics.PackerDiagnostic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerAssetDeclarationParseResult(
|
||||
PackerAssetDeclaration declaration,
|
||||
List<PackerDiagnostic> diagnostics) {
|
||||
|
||||
public PackerAssetDeclarationParseResult {
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
}
|
||||
|
||||
public boolean valid() {
|
||||
return declaration != null && diagnostics.stream().noneMatch(PackerDiagnostic::blocking);
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package p.packer.models;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerRegistryEntry(
|
||||
int assetId,
|
||||
String assetUuid,
|
||||
String root,
|
||||
boolean includedInBuild) {
|
||||
|
||||
public PackerRegistryEntry(int assetId, String assetUuid, String root) {
|
||||
this(assetId, assetUuid, root, true);
|
||||
}
|
||||
|
||||
public PackerRegistryEntry {
|
||||
assetUuid = Objects.requireNonNull(assetUuid, "assetUuid").trim();
|
||||
root = Objects.requireNonNull(root, "root").trim();
|
||||
if (assetId <= 0) {
|
||||
throw new IllegalArgumentException("assetId must be positive");
|
||||
}
|
||||
if (assetUuid.isBlank() || root.isBlank()) {
|
||||
throw new IllegalArgumentException("assetUuid and root must not be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package p.packer.models;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record PackerRegistryState(
|
||||
int schemaVersion,
|
||||
int nextAssetId,
|
||||
List<PackerRegistryEntry> assets) {
|
||||
|
||||
public PackerRegistryState {
|
||||
if (schemaVersion <= 0) {
|
||||
throw new IllegalArgumentException("schemaVersion must be positive");
|
||||
}
|
||||
if (nextAssetId <= 0) {
|
||||
throw new IllegalArgumentException("nextAssetId must be positive");
|
||||
}
|
||||
assets = List.copyOf(Objects.requireNonNull(assets, "assets"));
|
||||
}
|
||||
|
||||
public PackerRegistryState withAssets(List<PackerRegistryEntry> entries, int nextId) {
|
||||
return new PackerRegistryState(
|
||||
schemaVersion,
|
||||
nextId,
|
||||
entries.stream()
|
||||
.sorted(Comparator.comparingInt(PackerRegistryEntry::assetId))
|
||||
.toList());
|
||||
}
|
||||
}
|
||||
+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"));
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
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.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
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.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public final class FileSystemPackerRegistryRepository implements PackerRegistryRepository {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final int REGISTRY_SCHEMA_VERSION = 1;
|
||||
|
||||
@Override
|
||||
public PackerRegistryState load(PackerProjectContext project) {
|
||||
final Path registryPath = PackerWorkspacePaths.registryPath(project);
|
||||
if (!Files.isRegularFile(registryPath)) {
|
||||
return emptyState();
|
||||
}
|
||||
|
||||
try {
|
||||
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 p.packer.exceptions.PackerRegistryException("Unsupported registry schema_version: " + schemaVersion);
|
||||
}
|
||||
final List<PackerRegistryEntry> entries = new ArrayList<>();
|
||||
if (document.assets != null) {
|
||||
for (RegistryAssetDocument asset : document.assets) {
|
||||
if (asset == null) {
|
||||
continue;
|
||||
}
|
||||
entries.add(new PackerRegistryEntry(
|
||||
asset.assetId,
|
||||
asset.assetUuid,
|
||||
normalizeRoot(asset.root),
|
||||
asset.includedInBuild == null || asset.includedInBuild));
|
||||
}
|
||||
}
|
||||
validateEntries(entries);
|
||||
final int nextAssetId = document.nextAssetId > 0
|
||||
? document.nextAssetId
|
||||
: entries.stream().mapToInt(PackerRegistryEntry::assetId).max().orElse(0) + 1;
|
||||
return new PackerRegistryState(
|
||||
schemaVersion,
|
||||
nextAssetId,
|
||||
entries.stream().sorted(Comparator.comparingInt(PackerRegistryEntry::assetId)).toList());
|
||||
} catch (IOException exception) {
|
||||
throw new p.packer.exceptions.PackerRegistryException("Unable to load registry: " + registryPath, exception);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(PackerProjectContext project, PackerRegistryState state) {
|
||||
final Path registryDirectory = PackerWorkspacePaths.registryDirectory(project);
|
||||
final Path registryPath = PackerWorkspacePaths.registryPath(project);
|
||||
validateEntries(state.assets());
|
||||
try {
|
||||
Files.createDirectories(registryDirectory);
|
||||
final RegistryDocument document = new RegistryDocument();
|
||||
document.schemaVersion = state.schemaVersion();
|
||||
document.nextAssetId = state.nextAssetId();
|
||||
document.assets = state.assets().stream()
|
||||
.map(entry -> new RegistryAssetDocument(
|
||||
entry.assetId(),
|
||||
entry.assetUuid(),
|
||||
entry.root(),
|
||||
entry.includedInBuild()))
|
||||
.toList();
|
||||
MAPPER.writerWithDefaultPrettyPrinter().writeValue(registryPath.toFile(), document);
|
||||
} catch (IOException exception) {
|
||||
throw new p.packer.exceptions.PackerRegistryException("Unable to save registry: " + registryPath, exception);
|
||||
}
|
||||
}
|
||||
|
||||
private PackerRegistryState emptyState() {
|
||||
return new PackerRegistryState(REGISTRY_SCHEMA_VERSION, 1, List.of());
|
||||
}
|
||||
|
||||
private void validateEntries(List<PackerRegistryEntry> entries) {
|
||||
final Set<Integer> assetIds = new HashSet<>();
|
||||
final Set<String> assetUuids = new HashSet<>();
|
||||
final Set<String> roots = new HashSet<>();
|
||||
for (PackerRegistryEntry entry : entries) {
|
||||
if (!assetIds.add(entry.assetId())) {
|
||||
throw new p.packer.exceptions.PackerRegistryException("Duplicate asset_id in registry: " + entry.assetId());
|
||||
}
|
||||
if (!assetUuids.add(entry.assetUuid())) {
|
||||
throw new p.packer.exceptions.PackerRegistryException("Duplicate asset_uuid in registry: " + entry.assetUuid());
|
||||
}
|
||||
if (!roots.add(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 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 p.packer.exceptions.PackerRegistryException("Registry asset root is outside the trusted assets boundary: " + normalized);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
private static final class RegistryDocument {
|
||||
@JsonProperty("schema_version")
|
||||
public int schemaVersion;
|
||||
|
||||
@JsonProperty("next_asset_id")
|
||||
public int nextAssetId;
|
||||
|
||||
@JsonProperty("assets")
|
||||
public List<RegistryAssetDocument> assets = List.of();
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
private record RegistryAssetDocument(
|
||||
@JsonProperty("asset_id") int assetId,
|
||||
@JsonProperty("asset_uuid") String assetUuid,
|
||||
@JsonProperty("root") String root,
|
||||
@JsonProperty("included_in_build") Boolean includedInBuild) {
|
||||
}
|
||||
}
|
||||
+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('\\', '/'));
|
||||
}
|
||||
}
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
package p.packer.services;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class PackerAssetDeclarationParser {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final int SUPPORTED_SCHEMA_VERSION = 1;
|
||||
|
||||
public PackerAssetDeclarationParseResult parse(Path assetManifestPath) {
|
||||
final Path manifestPath = Objects.requireNonNull(assetManifestPath, "assetManifestPath").toAbsolutePath().normalize();
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
|
||||
final JsonNode root;
|
||||
try {
|
||||
root = MAPPER.readTree(manifestPath.toFile());
|
||||
} catch (IOException exception) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Unable to parse asset.json: " + exception.getMessage(),
|
||||
manifestPath,
|
||||
true));
|
||||
return new PackerAssetDeclarationParseResult(null, diagnostics);
|
||||
}
|
||||
|
||||
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 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 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) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Unsupported asset.json schema_version: " + schemaVersion,
|
||||
manifestPath,
|
||||
true));
|
||||
}
|
||||
|
||||
if (diagnostics.stream().anyMatch(PackerDiagnostic::blocking)) {
|
||||
return new PackerAssetDeclarationParseResult(null, diagnostics);
|
||||
}
|
||||
|
||||
return new PackerAssetDeclarationParseResult(
|
||||
new PackerAssetDeclaration(
|
||||
schemaVersion,
|
||||
assetUuid,
|
||||
name,
|
||||
assetFamily,
|
||||
inputsByRole,
|
||||
outputFormat,
|
||||
outputCodec,
|
||||
preloadEnabled),
|
||||
diagnostics);
|
||||
}
|
||||
|
||||
private Integer requiredInt(JsonNode node, String fieldName, List<PackerDiagnostic> diagnostics, Path manifestPath) {
|
||||
final JsonNode field = node.path(fieldName);
|
||||
if (!field.isInt()) {
|
||||
diagnostics.add(missingOrInvalid(fieldName, "integer", manifestPath));
|
||||
return null;
|
||||
}
|
||||
return field.intValue();
|
||||
}
|
||||
|
||||
private String requiredText(JsonNode node, String fieldName, List<PackerDiagnostic> diagnostics, Path manifestPath) {
|
||||
final JsonNode field = node.path(fieldName);
|
||||
if (!field.isTextual() || field.asText().isBlank()) {
|
||||
diagnostics.add(missingOrInvalid(fieldName, "non-blank string", manifestPath));
|
||||
return null;
|
||||
}
|
||||
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()) {
|
||||
diagnostics.add(missingOrInvalid(fieldName, "boolean", manifestPath));
|
||||
return null;
|
||||
}
|
||||
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));
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
final Map<String, List<String>> result = new LinkedHashMap<>();
|
||||
inputsNode.fields().forEachRemaining(entry -> {
|
||||
if (!entry.getValue().isArray()) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Input role '" + entry.getKey() + "' must be an array of relative paths.",
|
||||
manifestPath,
|
||||
true));
|
||||
return;
|
||||
}
|
||||
final List<String> values = new ArrayList<>();
|
||||
entry.getValue().forEach(value -> {
|
||||
if (!value.isTextual() || value.asText().isBlank()) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Input role '" + entry.getKey() + "' must contain only non-blank path strings.",
|
||||
manifestPath,
|
||||
true));
|
||||
return;
|
||||
}
|
||||
final String relativePath = value.asText().trim();
|
||||
if (!isTrustedRelativePath(relativePath)) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Input role '" + entry.getKey() + "' contains an untrusted path outside the asset root.",
|
||||
manifestPath,
|
||||
true));
|
||||
return;
|
||||
}
|
||||
values.add(relativePath);
|
||||
});
|
||||
result.put(entry.getKey(), List.copyOf(values));
|
||||
});
|
||||
return Map.copyOf(result);
|
||||
}
|
||||
|
||||
private boolean isTrustedRelativePath(String value) {
|
||||
final Path path = Path.of(value).normalize();
|
||||
return !path.isAbsolute() && !path.startsWith("..");
|
||||
}
|
||||
|
||||
private PackerDiagnostic missingOrInvalid(String fieldName, String expected, Path manifestPath) {
|
||||
return new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Field '" + fieldName + "' must be a " + expected + ".",
|
||||
manifestPath,
|
||||
true);
|
||||
}
|
||||
}
|
||||
+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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class PackerIdentityAllocator {
|
||||
public PackerRegistryEntry allocate(
|
||||
PackerProjectContext project,
|
||||
PackerRegistryState state,
|
||||
Path assetRoot) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(state, "state");
|
||||
final Path normalizedAssetRoot = Objects.requireNonNull(assetRoot, "assetRoot").toAbsolutePath().normalize();
|
||||
final String relativeRoot = PackerWorkspacePaths.relativeAssetRoot(project, normalizedAssetRoot);
|
||||
final boolean alreadyRegistered = state.assets().stream().anyMatch(entry -> entry.root().equals(relativeRoot));
|
||||
if (alreadyRegistered) {
|
||||
throw new p.packer.exceptions.PackerRegistryException("Asset root is already registered: " + relativeRoot);
|
||||
}
|
||||
return new PackerRegistryEntry(state.nextAssetId(), UUID.randomUUID().toString(), relativeRoot);
|
||||
}
|
||||
|
||||
public PackerRegistryState append(PackerRegistryState state, PackerRegistryEntry entry) {
|
||||
Objects.requireNonNull(state, "state");
|
||||
Objects.requireNonNull(entry, "entry");
|
||||
final List<PackerRegistryEntry> updated = state.assets().stream()
|
||||
.collect(java.util.stream.Collectors.toCollection(java.util.ArrayList::new));
|
||||
updated.add(entry);
|
||||
updated.sort(Comparator.comparingInt(PackerRegistryEntry::assetId));
|
||||
return new PackerRegistryState(state.schemaVersion(), entry.assetId() + 1, updated);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package p.packer.services;
|
||||
|
||||
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;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class PackerOperationEventEmitter {
|
||||
private final PackerProjectContext project;
|
||||
private final PackerEventSink sink;
|
||||
private final String operationId;
|
||||
private long sequence;
|
||||
|
||||
public PackerOperationEventEmitter(PackerProjectContext project, PackerEventSink sink) {
|
||||
this(project, sink, UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
public PackerOperationEventEmitter(PackerProjectContext project, PackerEventSink sink, String operationId) {
|
||||
this.project = Objects.requireNonNull(project, "project");
|
||||
this.sink = Objects.requireNonNull(sink, "sink");
|
||||
this.operationId = Objects.requireNonNull(operationId, "operationId").trim();
|
||||
}
|
||||
|
||||
public String operationId() {
|
||||
return operationId;
|
||||
}
|
||||
|
||||
public void emit(PackerEventKind kind, String summary, List<String> affectedAssets) {
|
||||
emit(kind, summary, null, affectedAssets);
|
||||
}
|
||||
|
||||
public void emit(PackerEventKind kind, String summary, PackerProgress progress, List<String> affectedAssets) {
|
||||
sink.publish(new PackerEvent(
|
||||
project.projectId(),
|
||||
operationId,
|
||||
sequence++,
|
||||
Objects.requireNonNull(kind, "kind"),
|
||||
Instant.now(),
|
||||
Objects.requireNonNull(summary, "summary"),
|
||||
progress,
|
||||
affectedAssets == null ? List.of() : List.copyOf(affectedAssets)));
|
||||
}
|
||||
}
|
||||
+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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class PackerRegistryLookup {
|
||||
public Optional<PackerRegistryEntry> findByAssetId(PackerRegistryState state, int assetId) {
|
||||
Objects.requireNonNull(state, "state");
|
||||
return state.assets().stream().filter(entry -> entry.assetId() == assetId).findFirst();
|
||||
}
|
||||
|
||||
public Optional<PackerRegistryEntry> findByAssetUuid(PackerRegistryState state, String assetUuid) {
|
||||
Objects.requireNonNull(state, "state");
|
||||
final String normalized = Objects.requireNonNull(assetUuid, "assetUuid").trim();
|
||||
return state.assets().stream().filter(entry -> entry.assetUuid().equals(normalized)).findFirst();
|
||||
}
|
||||
|
||||
public Optional<PackerRegistryEntry> findByRoot(PackerProjectContext project, PackerRegistryState state, Path assetRoot) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(state, "state");
|
||||
final String relativeRoot = PackerWorkspacePaths.relativeAssetRoot(project, Objects.requireNonNull(assetRoot, "assetRoot"));
|
||||
return state.assets().stream().filter(entry -> entry.root().equals(relativeRoot)).findFirst();
|
||||
}
|
||||
|
||||
public Path resolveExistingRoot(PackerProjectContext project, PackerRegistryEntry entry) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(entry, "entry");
|
||||
final Path assetRoot = PackerWorkspacePaths.assetRoot(project, entry.root());
|
||||
if (!Files.isDirectory(assetRoot)) {
|
||||
throw new p.packer.exceptions.PackerRegistryException("Registered asset root does not exist: " + entry.root());
|
||||
}
|
||||
return assetRoot;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
public interface PackerRegistryRepository {
|
||||
PackerRegistryState load(PackerProjectContext project);
|
||||
|
||||
void save(PackerProjectContext project, PackerRegistryState state);
|
||||
}
|
||||
+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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package p.packer.services;
|
||||
|
||||
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;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class PackerWorkspaceFoundation {
|
||||
private final PackerRegistryRepository registryRepository;
|
||||
private final PackerIdentityAllocator identityAllocator;
|
||||
private final PackerRegistryLookup registryLookup;
|
||||
|
||||
public PackerWorkspaceFoundation() {
|
||||
this(new FileSystemPackerRegistryRepository(), new PackerIdentityAllocator(), new PackerRegistryLookup());
|
||||
}
|
||||
|
||||
public PackerWorkspaceFoundation(
|
||||
PackerRegistryRepository registryRepository,
|
||||
PackerIdentityAllocator identityAllocator,
|
||||
PackerRegistryLookup registryLookup) {
|
||||
this.registryRepository = Objects.requireNonNull(registryRepository, "registryRepository");
|
||||
this.identityAllocator = Objects.requireNonNull(identityAllocator, "identityAllocator");
|
||||
this.registryLookup = Objects.requireNonNull(registryLookup, "registryLookup");
|
||||
}
|
||||
|
||||
public InitWorkspaceResult initWorkspace(InitWorkspaceRequest request) {
|
||||
final PackerProjectContext project = Objects.requireNonNull(request, "request").project();
|
||||
try {
|
||||
Files.createDirectories(PackerWorkspacePaths.assetsRoot(project));
|
||||
Files.createDirectories(PackerWorkspacePaths.registryDirectory(project));
|
||||
final PackerRegistryState registryState = registryRepository.load(project);
|
||||
registryRepository.save(project, registryState);
|
||||
return new InitWorkspaceResult(
|
||||
PackerOperationStatus.SUCCESS,
|
||||
"Workspace initialized for packer control.",
|
||||
PackerWorkspacePaths.registryPath(project),
|
||||
List.of());
|
||||
} catch (Exception exception) {
|
||||
throw new p.packer.exceptions.PackerRegistryException("Unable to initialize workspace for " + project.projectId(), exception);
|
||||
}
|
||||
}
|
||||
|
||||
public PackerRegistryState loadRegistry(PackerProjectContext project) {
|
||||
return registryRepository.load(project);
|
||||
}
|
||||
|
||||
public void saveRegistry(PackerProjectContext project, PackerRegistryState state) {
|
||||
registryRepository.save(project, state);
|
||||
}
|
||||
|
||||
public PackerRegistryEntry allocateIdentity(PackerProjectContext project, PackerRegistryState state, java.nio.file.Path assetRoot) {
|
||||
return identityAllocator.allocate(project, state, assetRoot);
|
||||
}
|
||||
|
||||
public PackerRegistryState appendAllocatedEntry(PackerRegistryState state, PackerRegistryEntry entry) {
|
||||
return identityAllocator.append(state, entry);
|
||||
}
|
||||
|
||||
public PackerRegistryLookup lookup() {
|
||||
return registryLookup;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package p.packer.services;
|
||||
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class PackerWorkspacePaths {
|
||||
private static final String ASSETS_DIR = "assets";
|
||||
private static final String PROMETEU_DIR = ".prometeu";
|
||||
private static final String REGISTRY_FILE = "index.json";
|
||||
|
||||
private PackerWorkspacePaths() {
|
||||
}
|
||||
|
||||
public static Path assetsRoot(PackerProjectContext project) {
|
||||
return Objects.requireNonNull(project, "project").rootPath().resolve(ASSETS_DIR).toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
public static Path registryDirectory(PackerProjectContext project) {
|
||||
return assetsRoot(project).resolve(PROMETEU_DIR).toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
public static Path registryPath(PackerProjectContext project) {
|
||||
return registryDirectory(project).resolve(REGISTRY_FILE).toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
public static Path assetRoot(PackerProjectContext project, String relativeRoot) {
|
||||
return assetsRoot(project).resolve(relativeRoot).toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
public static String relativeAssetRoot(PackerProjectContext project, Path assetRoot) {
|
||||
return assetsRoot(project).relativize(assetRoot.toAbsolutePath().normalize()).toString().replace('\\', '/');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user