feat(studio): refine Play workspace and project preferences (WRK-001D)
This commit is contained in:
@@ -6,4 +6,7 @@ import p.studio.api.project.ProjectReference;
|
||||
/** The shipper and managed launch adapters consume the same qualified asset budget contract. */
|
||||
public interface AssetBuildPreflight {
|
||||
AssetBudgets.Snapshot inspect(ProjectReference project);
|
||||
default AssetBudgets.Snapshot inspect(ProjectReference project,p.studio.api.project.ProjectLocalStudioSetup effectiveSetup) {
|
||||
return inspect(project);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ final class InteractiveExecutionController implements ExecutionController {
|
||||
private ExecutionSnapshot start(Attempt a) {
|
||||
try {
|
||||
if(a.stopping)return session.snapshot();
|
||||
a.preparation=access.prepareExecution(project.rootPath(),()->shipper.prepare(project,session::appendShipperLog));
|
||||
a.preparation=access.prepareExecution(project.rootPath(),()->shipper.prepare(project,setup,session::appendShipperLog));
|
||||
if(a.stopping)a.preparation.cancel();
|
||||
ShipperPrepareResult prepared=a.preparation.completion().toCompletableFuture().join();
|
||||
a.preparation.termination().toCompletableFuture().join();
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package p.studio.core.project.preferences;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
|
||||
final class AtomicSettingsFile {
|
||||
private AtomicSettingsFile() { }
|
||||
static void write(Path file, ObjectMapper mapper, Object value) {
|
||||
Path temporary = null;
|
||||
try {
|
||||
Files.createDirectories(file.getParent());
|
||||
temporary = Files.createTempFile(file.getParent(), file.getFileName().toString(), ".tmp");
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(temporary.toFile(), value);
|
||||
try { Files.move(temporary, file, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); }
|
||||
catch (AtomicMoveNotSupportedException ignored) { Files.move(temporary, file, StandardCopyOption.REPLACE_EXISTING); }
|
||||
} catch (IOException failure) { throw new UncheckedIOException(failure); }
|
||||
finally { if (temporary != null) try { Files.deleteIfExists(temporary); } catch (IOException ignored) { } }
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package p.studio.core.project.preferences;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.studio.api.preferences.RuntimeSettingsStore;
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
|
||||
public final class JsonRuntimeSettingsStore implements RuntimeSettingsStore {
|
||||
private final Path file;
|
||||
private final ObjectMapper mapper;
|
||||
public JsonRuntimeSettingsStore(Path file, ObjectMapper mapper) {
|
||||
this.file = file.toAbsolutePath().normalize(); this.mapper = mapper.copy();
|
||||
}
|
||||
private record Settings(String runtimePath) { }
|
||||
@Override public synchronized String load() {
|
||||
if (!Files.exists(file)) return null;
|
||||
try { var value = mapper.readValue(file.toFile(), Settings.class); return normalize(value.runtimePath()); }
|
||||
catch (IOException failure) { throw new UncheckedIOException(failure); }
|
||||
}
|
||||
@Override public synchronized void save(String installation) {
|
||||
String value = normalize(installation);
|
||||
if (value != null) validate(value);
|
||||
AtomicSettingsFile.write(file, mapper, new Settings(value));
|
||||
}
|
||||
@Override public void validate(String installation) {
|
||||
String value = normalize(installation);
|
||||
if (value == null) throw new IllegalArgumentException("Runtime installation is missing");
|
||||
String name = System.getProperty("os.name").startsWith("Windows") ? "prometeu-runtime.exe" : "prometeu-runtime";
|
||||
Path executable = Path.of(value).resolve(name);
|
||||
if (!Files.isRegularFile(executable) || !Files.isExecutable(executable))
|
||||
throw new IllegalArgumentException("Interactive runtime not found: " + executable);
|
||||
}
|
||||
private static String normalize(String text) { return text == null || text.isBlank() ? null : text.trim(); }
|
||||
}
|
||||
+10
-9
@@ -25,20 +25,21 @@ public final class JsonWindowStateStore implements WindowStateStore {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<WindowGeometry> load(ProjectReference projectReference) {
|
||||
public synchronized Optional<WindowGeometry> load(ProjectReference projectReference) {
|
||||
return Optional.ofNullable(loadFile().projects().get(projectKey(projectReference)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(ProjectReference projectReference, WindowGeometry geometry) {
|
||||
public synchronized void save(ProjectReference projectReference, WindowGeometry geometry) {
|
||||
final WindowStateFile current = loadFile();
|
||||
current.projects().put(projectKey(projectReference), Objects.requireNonNull(geometry, "geometry"));
|
||||
try {
|
||||
Files.createDirectories(storageFile.getParent());
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(storageFile.toFile(), current.normalize());
|
||||
} catch (IOException exception) {
|
||||
throw new UncheckedIOException(exception);
|
||||
}
|
||||
AtomicSettingsFile.write(storageFile, mapper, current.normalize());
|
||||
}
|
||||
|
||||
@Override public synchronized Optional<WindowGeometry> loadLauncher() { return Optional.ofNullable(loadFile().launcher()); }
|
||||
@Override public synchronized void saveLauncher(WindowGeometry geometry) {
|
||||
var current = loadFile();
|
||||
AtomicSettingsFile.write(storageFile, mapper, new WindowStateFile(geometry, current.projects()));
|
||||
}
|
||||
|
||||
private WindowStateFile loadFile() {
|
||||
@@ -58,7 +59,7 @@ public final class JsonWindowStateStore implements WindowStateStore {
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
private record WindowStateFile(Object launcher, Map<String, WindowGeometry> projects) {
|
||||
private record WindowStateFile(WindowGeometry launcher, Map<String, WindowGeometry> projects) {
|
||||
private static WindowStateFile empty() {
|
||||
return new WindowStateFile(null, new LinkedHashMap<>());
|
||||
}
|
||||
|
||||
@@ -56,6 +56,16 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
|
||||
return access.submitManual(project.rootPath(), operations, "Build and pack " + project.name(), () -> prepareNow(project, sink));
|
||||
}
|
||||
|
||||
@Override public OperationHandle<ShipperPrepareResult> prepare(ProjectReference project,
|
||||
p.studio.api.project.ProjectLocalStudioSetup effectiveSetup,Consumer<ShipperLogEntry> sink) {
|
||||
Objects.requireNonNull(effectiveSetup);Objects.requireNonNull(sink);
|
||||
return access.submitManual(project.rootPath(),operations,"Build and pack "+project.name(),()->{
|
||||
var selection=p.studio.api.automation.BuildSelection.productionBuild();
|
||||
return assetPreflight==null?prepareInto(project,selection,project.rootPath().resolve("build"),sink,effectiveSetup)
|
||||
:prepareProduction(project,selection,sink,effectiveSetup);
|
||||
});
|
||||
}
|
||||
|
||||
@Override public ShipperPrepareResult prepareNow(ProjectReference project, Consumer<ShipperLogEntry> sink) {
|
||||
return prepareNow(project, p.studio.api.automation.BuildSelection.productionBuild(), sink);
|
||||
}
|
||||
@@ -113,6 +123,10 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
|
||||
}
|
||||
|
||||
private ShipperPrepareResult prepareProduction(ProjectReference project,p.studio.api.automation.BuildSelection selection,Consumer<ShipperLogEntry> sink) {
|
||||
return prepareProduction(project,selection,sink,null);
|
||||
}
|
||||
private ShipperPrepareResult prepareProduction(ProjectReference project,p.studio.api.automation.BuildSelection selection,Consumer<ShipperLogEntry> sink,
|
||||
p.studio.api.project.ProjectLocalStudioSetup effectiveSetup) {
|
||||
Path stage=null;
|
||||
try {
|
||||
var home=project.rootPath().resolve(".studio/build-staging");admitOutput(project.rootPath(),home);Files.createDirectories(home);
|
||||
@@ -120,7 +134,7 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
|
||||
packer.validatePackWorkspace(new ValidatePackWorkspaceRequest(new PackerProjectContext(project.name(),project.rootPath())));
|
||||
packer.initWorkspace(new p.packer.messages.InitWorkspaceRequest(new PackerProjectContext(project.name(),project.rootPath())));
|
||||
var inputs=inputHashes(project.rootPath());stage=Files.createTempDirectory(home,"production-");
|
||||
var result=prepareInto(project,selection,stage,sink);if(!result.success())return result;
|
||||
var result=prepareInto(project,selection,stage,sink,effectiveSetup);if(!result.success())return result;
|
||||
verifyConsumedSources(result.context());checkCancelled();
|
||||
final Path candidate=stage;return packer.withProject(new PackerProjectContext(project.name(),project.rootPath()),()->{
|
||||
if(!inputs.equals(inputHashes(project.rootPath())))throw new IOException("Project assets changed before production publication");
|
||||
@@ -141,6 +155,10 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
|
||||
|
||||
private ShipperPrepareResult prepareInto(ProjectReference project, p.studio.api.automation.BuildSelection selection,
|
||||
Path outputRoot, Consumer<ShipperLogEntry> sink) {
|
||||
return prepareInto(project,selection,outputRoot,sink,null);
|
||||
}
|
||||
private ShipperPrepareResult prepareInto(ProjectReference project,p.studio.api.automation.BuildSelection selection,
|
||||
Path outputRoot,Consumer<ShipperLogEntry> sink,p.studio.api.project.ProjectLocalStudioSetup effectiveSetup) {
|
||||
Objects.requireNonNull(project); Objects.requireNonNull(sink);
|
||||
var logs = new ArrayList<ShipperLogEntry>();
|
||||
var issues = new ArrayList<p.studio.api.diagnostic.StudioDiagnostic>();
|
||||
@@ -185,7 +203,7 @@ public final class StudioShipperService implements Shipper, SavedProjectPreparat
|
||||
return finish(ShipperPreparationStatus.FAILED, logs, null, List.of(), issues, stages, inputs, "rejected");
|
||||
}
|
||||
stages.put(current, "passed"); checkCancelled(); current = "pack"; stages.put(current, "running");
|
||||
var capacity=assetPreflight==null?null:assetPreflight.inspect(project);
|
||||
var capacity=assetPreflight==null?null:effectiveSetup==null?assetPreflight.inspect(project):assetPreflight.inspect(project,effectiveSetup);
|
||||
if(capacity!=null) {
|
||||
var expanded=new java.util.LinkedHashMap<String,Object>(inputs);expanded.put("assetCapacity",mapper.convertValue(capacity,java.util.Map.class));inputs=expanded;
|
||||
for(var diagnostic:capacity.diagnostics())issues.add(assetIssue(diagnostic,project));
|
||||
|
||||
+15
@@ -37,6 +37,21 @@ class InteractiveExecutionControllerTest {
|
||||
private RuntimeGateway gateway(Connection connection){return (p,s,l)->connection;}
|
||||
private ProjectReference project(){return new ProjectReference("Game","1","pbs",1,ProjectTarget.Game,root);}
|
||||
|
||||
@Test void buildAndLaunchReceiveTheSameFrozenEffectiveRuntime(){
|
||||
var ops=new ControlledOperations();var state=new DefaultExecutionSession();var connection=new Connection();
|
||||
var effective=new ProjectLocalStudioSetup("global-runtime",null);
|
||||
Shipper preparation=new Shipper(){
|
||||
public OperationHandle<ShipperPrepareResult> prepare(ProjectReference p,java.util.function.Consumer<ShipperLogEntry> logs){throw new AssertionError("Interactive build must receive effective setup");}
|
||||
public OperationHandle<ShipperPrepareResult> prepare(ProjectReference p,ProjectLocalStudioSetup setup,java.util.function.Consumer<ShipperLogEntry> logs){
|
||||
assertSame(effective,setup);return done(new ShipperPrepareResult(ShipperPreparationStatus.SUCCESS,List.of(),root.resolve("manifest.json"),List.of(),List.of()));
|
||||
}
|
||||
};
|
||||
RuntimeGateway runtime=(p,setup,logs)->{assertSame(effective,setup);return connection;};
|
||||
var controller=new InteractiveExecutionController(project(),effective,state,preparation,runtime,ops,new ProjectAccessRegistry(),PlayOptions.PERSISTENT);
|
||||
controller.play();ops.last.finish();assertEquals(ExecutionState.RUNNING,state.snapshot().state());
|
||||
var stop=controller.stopAsync();ops.last.finish();stop.toCompletableFuture().join();
|
||||
}
|
||||
|
||||
@Test void failedConnectionReleasesAuthorityAfterCompletionAndAllowsAnotherAttempt(){
|
||||
var ops=new ControlledOperations();var authority=new DefaultAutomationSession(root,ops);
|
||||
var access=new ProjectAccessRegistry();access.register(root,authority::enterManual,()->true,authority);
|
||||
|
||||
+18
@@ -16,6 +16,24 @@ final class JsonWindowStateStoreTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test void launcherAndTwoProjectsSurviveInterleavedWritesAndNewInstances() throws Exception {
|
||||
Path file=tempDir.resolve("windows.json");var mapper=new ObjectMapper();
|
||||
var a=new ProjectReference("A","1","pbs",1,ProjectTarget.Game,tempDir.resolve("A"));
|
||||
var b=new ProjectReference("B","1","pbs",1,ProjectTarget.Game,tempDir.resolve("B"));
|
||||
var ga=new WindowGeometry(-1000,30,900,700,true);
|
||||
var gb=new WindowGeometry(80,90,1000,720,false);
|
||||
var launcher=new WindowGeometry(40,50,960,720,true);
|
||||
var store=new JsonWindowStateStore(file,mapper);
|
||||
var pool=java.util.concurrent.Executors.newFixedThreadPool(3);
|
||||
try {
|
||||
var writes=java.util.List.of(pool.submit(()->store.save(a,ga)),pool.submit(()->store.save(b,gb)),pool.submit(()->store.saveLauncher(launcher)));
|
||||
for(var write:writes)write.get();
|
||||
} finally {pool.shutdown();}
|
||||
var reopened=new JsonWindowStateStore(file,mapper);
|
||||
assertEquals(ga,reopened.load(a).orElseThrow());assertEquals(gb,reopened.load(b).orElseThrow());
|
||||
assertEquals(launcher,reopened.loadLauncher().orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistsGeometryByNormalizedProjectRoot() {
|
||||
final Path storage = tempDir.resolve("window-state.json");
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package p.studio.core.project.preferences;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import java.nio.file.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class RuntimeSettingsStoreTest {
|
||||
@TempDir Path root;
|
||||
private Path installation(String name) throws Exception {
|
||||
Path path=Files.createDirectories(root.resolve(name));
|
||||
Path executable=Files.writeString(path.resolve(System.getProperty("os.name").startsWith("Windows")?"prometeu-runtime.exe":"prometeu-runtime"),"fixture");
|
||||
assertTrue(executable.toFile().setExecutable(true));return path;
|
||||
}
|
||||
@Test void defaultSurvivesRestartAndExplicitInvalidOverrideNeverFallsBack() throws Exception {
|
||||
Path path=root.resolve("settings.json"),global=installation("global"),specific=installation("specific");
|
||||
var store=new JsonRuntimeSettingsStore(path,new ObjectMapper());
|
||||
assertNull(store.load());assertThrows(IllegalArgumentException.class,()->store.resolve(null));
|
||||
store.save(global.toString());
|
||||
var reopened=new JsonRuntimeSettingsStore(path,new ObjectMapper());
|
||||
assertEquals(global.toString(),reopened.resolve(null));
|
||||
assertEquals(specific.toString(),reopened.resolve(specific.toString()));
|
||||
assertThrows(IllegalArgumentException.class,()->reopened.resolve(root.resolve("missing").toString()));
|
||||
assertEquals(global.toString(),reopened.load());
|
||||
reopened.save(null);assertNull(store.load());
|
||||
}
|
||||
@Test void failedUpdatePreservesPriorDefault() throws Exception {
|
||||
var store=new JsonRuntimeSettingsStore(root.resolve("settings.json"),new ObjectMapper());
|
||||
String path=installation("good").toString();store.save(path);
|
||||
assertThrows(IllegalArgumentException.class,()->store.save(root.resolve("invalid").toString()));
|
||||
assertEquals(path,store.load());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user