implements packer PR-09 event lane and studio adapter
This commit is contained in:
@@ -30,7 +30,18 @@ public final class StudioActivityEventMapper {
|
||||
Optional.of(new StudioActivityEntry("Assets", "Action applied: " + applied.action().name().toLowerCase(), StudioActivityEntrySeverity.SUCCESS, false));
|
||||
case StudioAssetsMutationFailedEvent failed ->
|
||||
Optional.of(new StudioActivityEntry("Assets", failed.message(), StudioActivityEntrySeverity.ERROR, true));
|
||||
case StudioPackerOperationEvent packerEvent ->
|
||||
Optional.of(new StudioActivityEntry("Assets", packerEvent.summary(), severity(packerEvent), packerEvent.kind() == p.packer.api.events.PackerEventKind.ACTION_FAILED));
|
||||
default -> Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
private static StudioActivityEntrySeverity severity(StudioPackerOperationEvent event) {
|
||||
return switch (event.kind()) {
|
||||
case BUILD_FINISHED, CACHE_HIT -> StudioActivityEntrySeverity.SUCCESS;
|
||||
case DIAGNOSTICS_UPDATED, CACHE_MISS, BUILD_STARTED, ASSET_DISCOVERED, ASSET_CHANGED, PROGRESS_UPDATED -> StudioActivityEntrySeverity.INFO;
|
||||
case ACTION_FAILED -> StudioActivityEntrySeverity.ERROR;
|
||||
default -> StudioActivityEntrySeverity.INFO;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ public final class StudioActivityFeedControl extends VBox implements StudioContr
|
||||
subscriptions.add(eventBus.subscribe(StudioAssetsMutationPreviewReadyEvent.class, this::onEvent));
|
||||
subscriptions.add(eventBus.subscribe(StudioAssetsMutationAppliedEvent.class, this::onEvent));
|
||||
subscriptions.add(eventBus.subscribe(StudioAssetsMutationFailedEvent.class, this::onEvent));
|
||||
subscriptions.add(eventBus.subscribe(StudioPackerOperationEvent.class, this::onPackerOperation));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -114,6 +115,23 @@ public final class StudioActivityFeedControl extends VBox implements StudioContr
|
||||
clearProgress();
|
||||
}
|
||||
|
||||
private void onPackerOperation(StudioPackerOperationEvent event) {
|
||||
onEvent(event);
|
||||
if (event.progress() != null) {
|
||||
Platform.runLater(() -> {
|
||||
progressLabel.setText(event.summary());
|
||||
progressBar.setVisible(true);
|
||||
progressBar.setManaged(true);
|
||||
progressBar.setProgress(event.indeterminate() ? ProgressBar.INDETERMINATE_PROGRESS : event.progress());
|
||||
});
|
||||
if (event.kind() == p.packer.api.events.PackerEventKind.BUILD_FINISHED) {
|
||||
clearProgress();
|
||||
}
|
||||
} else if (event.kind() == p.packer.api.events.PackerEventKind.BUILD_FINISHED) {
|
||||
clearProgress();
|
||||
}
|
||||
}
|
||||
|
||||
private void clearProgress() {
|
||||
Platform.runLater(() -> {
|
||||
progressLabel.setText(Container.i18n().text(I18n.ACTIVITY_PROGRESS_IDLE));
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package p.studio.events;
|
||||
|
||||
import p.packer.api.events.PackerEvent;
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.packer.api.events.PackerEventSink;
|
||||
import p.studio.projects.ProjectReference;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class StudioPackerEventAdapter implements PackerEventSink {
|
||||
private final StudioWorkspaceEventBus eventBus;
|
||||
private final ProjectReference projectReference;
|
||||
|
||||
public StudioPackerEventAdapter(StudioWorkspaceEventBus eventBus, ProjectReference projectReference) {
|
||||
this.eventBus = Objects.requireNonNull(eventBus, "eventBus");
|
||||
this.projectReference = Objects.requireNonNull(projectReference, "projectReference");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(PackerEvent event) {
|
||||
final PackerEvent packerEvent = Objects.requireNonNull(event, "event");
|
||||
if (ignore(packerEvent.kind())) {
|
||||
return;
|
||||
}
|
||||
eventBus.publish(new StudioPackerOperationEvent(
|
||||
projectReference,
|
||||
packerEvent.operationId(),
|
||||
packerEvent.kind(),
|
||||
packerEvent.summary(),
|
||||
packerEvent.progress() == null ? null : packerEvent.progress().value(),
|
||||
packerEvent.progress() == null || packerEvent.progress().indeterminate()));
|
||||
}
|
||||
|
||||
private boolean ignore(PackerEventKind kind) {
|
||||
return kind == PackerEventKind.PREVIEW_READY
|
||||
|| kind == PackerEventKind.ACTION_APPLIED
|
||||
|| kind == PackerEventKind.ACTION_FAILED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package p.studio.events;
|
||||
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.studio.projects.ProjectReference;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record StudioPackerOperationEvent(
|
||||
ProjectReference project,
|
||||
String operationId,
|
||||
PackerEventKind kind,
|
||||
String summary,
|
||||
Double progress,
|
||||
boolean indeterminate) implements StudioEvent {
|
||||
|
||||
public StudioPackerOperationEvent {
|
||||
Objects.requireNonNull(project, "project");
|
||||
operationId = Objects.requireNonNull(operationId, "operationId").trim();
|
||||
Objects.requireNonNull(kind, "kind");
|
||||
summary = Objects.requireNonNull(summary, "summary").trim();
|
||||
if (operationId.isBlank() || summary.isBlank()) {
|
||||
throw new IllegalArgumentException("operationId and summary must not be blank");
|
||||
}
|
||||
if (progress != null && !indeterminate && (progress < 0.0d || progress > 1.0d)) {
|
||||
throw new IllegalArgumentException("progress must be between 0.0 and 1.0 when determinate");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,9 @@ import p.studio.projects.ProjectReference;
|
||||
import p.studio.utilities.i18n.I18n;
|
||||
import p.studio.workspaces.Workspace;
|
||||
import p.studio.workspaces.WorkspaceId;
|
||||
import p.packer.declarations.PackerAssetDeclarationParser;
|
||||
import p.packer.foundation.PackerWorkspaceFoundation;
|
||||
import p.packer.workspace.FileSystemPackerWorkspaceService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -58,7 +61,7 @@ public final class AssetWorkspace implements Workspace {
|
||||
public AssetWorkspace(ProjectReference projectReference) {
|
||||
this(
|
||||
projectReference,
|
||||
new PackerBackedAssetWorkspaceService(),
|
||||
null,
|
||||
defaultWorkspaceBus(),
|
||||
null);
|
||||
}
|
||||
@@ -76,8 +79,13 @@ public final class AssetWorkspace implements Workspace {
|
||||
StudioWorkspaceEventBus workspaceBus,
|
||||
AssetWorkspaceMutationService mutationService) {
|
||||
this.projectReference = Objects.requireNonNull(projectReference, "projectReference");
|
||||
this.assetWorkspaceService = Objects.requireNonNull(assetWorkspaceService, "assetWorkspaceService");
|
||||
this.workspaceBus = Objects.requireNonNull(workspaceBus, "workspaceBus");
|
||||
this.assetWorkspaceService = assetWorkspaceService == null
|
||||
? new PackerBackedAssetWorkspaceService(new FileSystemPackerWorkspaceService(
|
||||
new PackerWorkspaceFoundation(),
|
||||
new PackerAssetDeclarationParser(),
|
||||
new StudioPackerEventAdapter(this.workspaceBus, this.projectReference)))
|
||||
: Objects.requireNonNull(assetWorkspaceService, "assetWorkspaceService");
|
||||
this.mutationService = mutationService == null
|
||||
? new PackerBackedAssetWorkspaceMutationService(this.workspaceBus)
|
||||
: Objects.requireNonNull(mutationService, "mutationService");
|
||||
|
||||
+13
@@ -1,9 +1,11 @@
|
||||
package p.studio.controls.shell;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.studio.events.StudioAssetsMutationAppliedEvent;
|
||||
import p.studio.events.StudioAssetsMutationFailedEvent;
|
||||
import p.studio.events.StudioAssetsMutationPreviewReadyEvent;
|
||||
import p.studio.events.StudioPackerOperationEvent;
|
||||
import p.studio.events.StudioAssetsWorkspaceRefreshFailedEvent;
|
||||
import p.studio.events.StudioAssetsWorkspaceRefreshedEvent;
|
||||
import p.studio.events.StudioProjectOpenedEvent;
|
||||
@@ -83,6 +85,17 @@ final class StudioActivityEventMapperTest {
|
||||
assertEquals("Apply failed", entry.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapsPackerBuildEventToActivityEntry() {
|
||||
final StudioActivityEntry entry = StudioActivityEventMapper
|
||||
.map(new StudioPackerOperationEvent(project(), "op-1", PackerEventKind.BUILD_FINISHED, "Build finished.", 1.0d, false))
|
||||
.orElseThrow();
|
||||
|
||||
assertEquals("Assets", entry.source());
|
||||
assertEquals(StudioActivityEntrySeverity.SUCCESS, entry.severity());
|
||||
assertEquals("Build finished.", entry.message());
|
||||
}
|
||||
|
||||
private ProjectReference project() {
|
||||
return new ProjectReference("Main", "1.0.0", "pbs", 1, Path.of("/tmp/main"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package p.studio.events;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.packer.api.events.PackerEvent;
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.packer.api.events.PackerProgress;
|
||||
import p.studio.projects.ProjectReference;
|
||||
import p.studio.workspaces.WorkspaceId;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
final class StudioPackerEventAdapterTest {
|
||||
@Test
|
||||
void forwardsBuildEventsIntoStudioOperationEvents() {
|
||||
final StudioEventBus globalBus = new StudioEventBus();
|
||||
final List<StudioPackerOperationEvent> events = new ArrayList<>();
|
||||
globalBus.subscribe(StudioPackerOperationEvent.class, events::add);
|
||||
final StudioPackerEventAdapter adapter = new StudioPackerEventAdapter(
|
||||
new StudioWorkspaceEventBus(WorkspaceId.ASSETS, globalBus),
|
||||
project());
|
||||
|
||||
adapter.publish(new PackerEvent(
|
||||
"main",
|
||||
"op-1",
|
||||
0L,
|
||||
PackerEventKind.BUILD_STARTED,
|
||||
Instant.now(),
|
||||
"Build started.",
|
||||
new PackerProgress(0.25d, false),
|
||||
List.of("ui_atlas")));
|
||||
|
||||
assertEquals(1, events.size());
|
||||
assertEquals(PackerEventKind.BUILD_STARTED, events.getFirst().kind());
|
||||
assertEquals(0.25d, events.getFirst().progress());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresMutationLifecycleEventsAlreadyHandledByTypedStudioEvents() {
|
||||
final StudioEventBus globalBus = new StudioEventBus();
|
||||
final List<StudioPackerOperationEvent> events = new ArrayList<>();
|
||||
globalBus.subscribe(StudioPackerOperationEvent.class, events::add);
|
||||
final StudioPackerEventAdapter adapter = new StudioPackerEventAdapter(
|
||||
new StudioWorkspaceEventBus(WorkspaceId.ASSETS, globalBus),
|
||||
project());
|
||||
|
||||
adapter.publish(new PackerEvent(
|
||||
"main",
|
||||
"op-1",
|
||||
0L,
|
||||
PackerEventKind.PREVIEW_READY,
|
||||
Instant.now(),
|
||||
"Preview ready.",
|
||||
null,
|
||||
List.of("ui_atlas")));
|
||||
|
||||
assertTrue(events.isEmpty());
|
||||
}
|
||||
|
||||
private ProjectReference project() {
|
||||
return new ProjectReference("Main", "1.0.0", "pbs", 1, Path.of("/tmp/main"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user