complete Studio core and JavaFX boundary migration
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 64.01% (20482/31999)
* Branch Coverage: 54.22% (8240/15196)
* Lines of Code: 31999
* Cyclomatic Complexity: 13164
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 715
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 64.01% (20482/31999)
* Branch Coverage: 54.22% (8240/15196)
* Lines of Code: 31999
* Cyclomatic Complexity: 13164
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 715
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
package p.studio;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
import p.studio.window.StudioWindowCoordinator;
|
||||
|
||||
public class App extends Application {
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
super.init();
|
||||
Container.install(new AppContainer());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) {
|
||||
new StudioWindowCoordinator(stage).showLauncher();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
Container.shutdown();
|
||||
super.stop();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package p.studio;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.packer.Packer;
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.lsp.api.LspServerLifecycle;
|
||||
import p.studio.lsp.events.StudioEventBus;
|
||||
import p.studio.lsp.events.StudioPackerEventAdapter;
|
||||
import p.studio.lsp.services.LspServerLifecycleImpl;
|
||||
import p.studio.utilities.ThemeService;
|
||||
import p.studio.utilities.i18n.I18nService;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public final class AppContainer implements Container {
|
||||
private final I18nService i18nService;
|
||||
private final ThemeService themeService;
|
||||
private final StudioEventBus studioEventBus;
|
||||
private final ObjectMapper mapper;
|
||||
private final EmbeddedPacker embeddedPacker;
|
||||
private final StudioBackgroundTasks backgroundTasks;
|
||||
private final LspServerLifecycle lspServerLifecycle;
|
||||
|
||||
public AppContainer() {
|
||||
FrontendRegistryService.bootstrapDefaults();
|
||||
this.i18nService = new I18nService();
|
||||
this.themeService = new ThemeService();
|
||||
this.studioEventBus = new StudioEventBus();
|
||||
this.mapper = new ObjectMapper();
|
||||
final ExecutorService backgroundExecutor = Executors.newFixedThreadPool(2, new StudioWorkerThreadFactory());
|
||||
this.backgroundTasks = new StudioBackgroundTasks(backgroundExecutor);
|
||||
final Packer packer = Packer.bootstrap(this.mapper, new StudioPackerEventAdapter(studioEventBus));
|
||||
this.embeddedPacker = new EmbeddedPacker(packer.workspaceService(), packer::close);
|
||||
this.lspServerLifecycle = new LspServerLifecycleImpl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public I18nService getI18n() {
|
||||
return i18nService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ThemeService getTheme() {
|
||||
return themeService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StudioEventBus getEventBus() {
|
||||
return studioEventBus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectMapper getMapper() {
|
||||
return mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EmbeddedPacker getPacker() {
|
||||
return embeddedPacker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StudioBackgroundTasks getBackgroundTasks() {
|
||||
return backgroundTasks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public p.studio.lsp.api.LspServerLifecycle getLspServerLifecycle() {
|
||||
return lspServerLifecycle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdownContainer() {
|
||||
embeddedPacker.shutdown();
|
||||
backgroundTasks.shutdown();
|
||||
}
|
||||
|
||||
private static final class StudioWorkerThreadFactory implements ThreadFactory {
|
||||
private final AtomicInteger nextId = new AtomicInteger(1);
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable runnable) {
|
||||
final Thread thread = new Thread(runnable, "studio-worker-" + nextId.getAndIncrement());
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package p.studio.app;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import p.studio.ui.javafx.window.StudioWindowCoordinator;
|
||||
|
||||
public class App extends Application {
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
private StudioWindowCoordinator studioWindowCoordinator;
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
super.init();
|
||||
applicationContext = new SpringApplicationBuilder(StudioApplicationConfiguration.class)
|
||||
.web(WebApplicationType.NONE)
|
||||
.headless(false)
|
||||
.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) {
|
||||
studioWindowCoordinator = applicationContext.getBean(StudioUiFactory.class).create(stage);
|
||||
studioWindowCoordinator.showLauncher();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
Exception failure = null;
|
||||
try {
|
||||
if (studioWindowCoordinator != null) {
|
||||
studioWindowCoordinator.close();
|
||||
}
|
||||
} catch (RuntimeException exception) {
|
||||
failure = exception;
|
||||
}
|
||||
try {
|
||||
if (applicationContext != null) {
|
||||
applicationContext.close();
|
||||
}
|
||||
} catch (RuntimeException exception) {
|
||||
failure = append(failure, exception);
|
||||
}
|
||||
try {
|
||||
super.stop();
|
||||
} catch (Exception exception) {
|
||||
failure = append(failure, exception);
|
||||
}
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private static Exception append(Exception prior, Exception failure) {
|
||||
if (prior == null) {
|
||||
return failure;
|
||||
}
|
||||
prior.addSuppressed(failure);
|
||||
return prior;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
package p.studio.app;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import p.packer.PackerWorkspaceService;
|
||||
import p.studio.api.project.KnownProjects;
|
||||
import p.studio.api.assets.AssetCatalog;
|
||||
import p.studio.api.assets.AssetCommands;
|
||||
import p.studio.api.assets.AssetPacking;
|
||||
import p.studio.api.assets.AssetDetails;
|
||||
import p.studio.api.assets.AssetCreationCatalog;
|
||||
import p.studio.api.assets.AssetWorkspaceStorage;
|
||||
import p.studio.api.assets.AssetCapacityPolicy;
|
||||
import p.studio.api.assets.schema.AssetColorSchemaFactory;
|
||||
import p.studio.api.assets.bank.AssetBankCompositionSessionFactory;
|
||||
import p.studio.api.assets.metadata.AssetMetadataService;
|
||||
import p.studio.core.assets.PackerAssetCatalog;
|
||||
import p.studio.core.assets.PackerAssetCommands;
|
||||
import p.studio.core.assets.PackerAssetPacking;
|
||||
import p.studio.core.assets.PackerAssetDetails;
|
||||
import p.studio.core.assets.PackerAssetCreationCatalog;
|
||||
import p.studio.core.assets.FileSystemAssetWorkspaceStorage;
|
||||
import p.studio.core.assets.DefaultAssetCapacityPolicy;
|
||||
import p.studio.core.assets.schema.DefaultAssetColorSchemaFactory;
|
||||
import p.studio.core.assets.bank.DefaultAssetBankCompositionSessionFactory;
|
||||
import p.studio.core.assets.metadata.AssetStudioMetadataService;
|
||||
import p.studio.api.assets.scene.SceneBankWorkflow;
|
||||
import p.studio.api.assets.tiled.TiledAssetService;
|
||||
import p.studio.core.assets.scene.SceneBankWorkflowService;
|
||||
import p.studio.core.assets.tiled.TiledAssetGenerationService;
|
||||
import p.studio.api.project.ProjectCatalog;
|
||||
import p.studio.api.project.ProjectLanguageCatalog;
|
||||
import p.studio.app.compiler.CompilerProjectLanguageCatalog;
|
||||
import p.studio.app.compiler.CompilerBuildGatewayAdapter;
|
||||
import p.studio.api.shipper.Shipper;
|
||||
import p.studio.core.shipper.CompilerBuildGateway;
|
||||
import p.studio.core.shipper.StudioShipperService;
|
||||
import p.studio.core.project.KnownProjectsService;
|
||||
import p.studio.core.project.ProjectCatalogService;
|
||||
import p.studio.core.project.ProjectLocalStudioSetupStore;
|
||||
import p.studio.core.project.ProjectLocalStudioStateStore;
|
||||
import p.studio.api.project.ProjectWorkspaceInitializer;
|
||||
import p.studio.core.project.PackerProjectWorkspaceInitializer;
|
||||
import p.studio.api.session.ProjectSessionFactory;
|
||||
import p.studio.app.lsp.LspLifecycleAdapter;
|
||||
import p.studio.core.session.DefaultProjectSessionFactory;
|
||||
import p.studio.core.session.LspLifecyclePort;
|
||||
import p.studio.lsp.api.LspServerLifecycle;
|
||||
import p.studio.lsp.services.LspServerLifecycleImpl;
|
||||
import p.studio.ui.javafx.events.StudioEventBus;
|
||||
import p.studio.ui.javafx.utilities.ThemeService;
|
||||
import p.studio.ui.javafx.utilities.i18n.I18nService;
|
||||
import p.packer.Packer;
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.api.execution.ExecutionControllerFactory;
|
||||
import p.studio.app.runtime.ExternalRuntimeGateway;
|
||||
import p.studio.core.execution.DefaultExecutionControllerFactory;
|
||||
import p.studio.core.runtime.RuntimeGateway;
|
||||
import p.studio.api.preferences.WindowStateStore;
|
||||
import p.studio.core.project.preferences.JsonWindowStateStore;
|
||||
import p.studio.api.preferences.ProjectActivityHistoryStore;
|
||||
import p.studio.core.project.preferences.JsonProjectActivityHistoryStore;
|
||||
import p.studio.api.operation.OperationExecutor;
|
||||
import p.studio.app.operation.ExecutorOperationExecutor;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class StudioApplicationConfiguration {
|
||||
@Bean
|
||||
ObjectMapper objectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
|
||||
@Bean
|
||||
I18nService i18nService() {
|
||||
return new I18nService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ThemeService themeService() {
|
||||
return new ThemeService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
StudioEventBus studioEventBus() {
|
||||
return new StudioEventBus();
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
Packer packer(ObjectMapper mapper, StudioEventBus events) {
|
||||
return Packer.bootstrap(mapper, new StudioPackerEventAdapter(events));
|
||||
}
|
||||
|
||||
@Bean
|
||||
PackerWorkspaceService packerWorkspaceService(Packer packer) {
|
||||
return packer.workspaceService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetCatalog assetCatalog(PackerWorkspaceService packer) {
|
||||
return new PackerAssetCatalog(packer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetCommands assetCommands(PackerWorkspaceService packer) {
|
||||
return new PackerAssetCommands(packer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetPacking assetPacking(PackerWorkspaceService packer, OperationExecutor operations) {
|
||||
return new PackerAssetPacking(packer, operations);
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetDetails assetDetails(PackerWorkspaceService packer) {
|
||||
return new PackerAssetDetails(packer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetCreationCatalog assetCreationCatalog() {
|
||||
return new PackerAssetCreationCatalog();
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetWorkspaceStorage assetWorkspaceStorage() {
|
||||
return new FileSystemAssetWorkspaceStorage();
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetCapacityPolicy assetCapacityPolicy() {
|
||||
return new DefaultAssetCapacityPolicy();
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetColorSchemaFactory assetColorSchemaFactory() {
|
||||
return new DefaultAssetColorSchemaFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetBankCompositionSessionFactory assetBankCompositionSessionFactory(AssetCapacityPolicy capacityPolicy) {
|
||||
return new DefaultAssetBankCompositionSessionFactory(capacityPolicy);
|
||||
}
|
||||
|
||||
@Bean
|
||||
AssetMetadataService assetMetadataService(ObjectMapper mapper) {
|
||||
return new AssetStudioMetadataService(mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
SceneBankWorkflow sceneBankWorkflow(AssetCatalog assetCatalog, ObjectMapper mapper) {
|
||||
return new SceneBankWorkflowService(assetCatalog, mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
TiledAssetService tiledAssetService(AssetMetadataService metadataService) {
|
||||
return new TiledAssetGenerationService(metadataService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
WindowStateStore windowStateStore(ObjectMapper mapper) {
|
||||
return new JsonWindowStateStore(resolveWindowStateStorage(), mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProjectActivityHistoryStore projectActivityHistoryStore(ObjectMapper mapper) {
|
||||
return new JsonProjectActivityHistoryStore(mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
CompilerBuildGateway compilerBuildGateway() {
|
||||
return new CompilerBuildGatewayAdapter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
Shipper shipper(CompilerBuildGateway compiler, PackerWorkspaceService packer, ObjectMapper mapper,
|
||||
OperationExecutor operations) {
|
||||
return new StudioShipperService(compiler, packer, mapper, operations);
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "shutdown")
|
||||
ExecutorService studioExecutor() {
|
||||
return Executors.newFixedThreadPool(4, Thread.ofPlatform().daemon().name("studio-worker-", 1).factory());
|
||||
}
|
||||
|
||||
@Bean
|
||||
OperationExecutor operationExecutor(ExecutorService studioExecutor) {
|
||||
return new ExecutorOperationExecutor(studioExecutor);
|
||||
}
|
||||
|
||||
@Bean
|
||||
RuntimeGateway runtimeGateway(ObjectMapper mapper, ExecutorService studioExecutor) {
|
||||
return new ExternalRuntimeGateway(mapper, studioExecutor);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExecutionControllerFactory executionControllerFactory(Shipper shipper, RuntimeGateway runtime,
|
||||
OperationExecutor operations) {
|
||||
return new DefaultExecutionControllerFactory(shipper, runtime, operations);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProjectLocalStudioSetupStore projectLocalStudioSetupStore(ObjectMapper mapper) {
|
||||
return new ProjectLocalStudioSetupStore(mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProjectLocalStudioStateStore projectLocalStudioStateStore(ObjectMapper mapper) {
|
||||
return new ProjectLocalStudioStateStore(mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
LspServerLifecycle lspServerLifecycle() {
|
||||
return new LspServerLifecycleImpl();
|
||||
}
|
||||
|
||||
@Bean
|
||||
LspLifecyclePort lspLifecyclePort(LspServerLifecycle lifecycle) {
|
||||
return new LspLifecycleAdapter(lifecycle);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProjectSessionFactory projectSessionFactory(ProjectLocalStudioStateStore stateStore,
|
||||
ProjectLocalStudioSetupStore setupStore, LspLifecyclePort lsp) {
|
||||
return new DefaultProjectSessionFactory(stateStore, setupStore, lsp);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProjectCatalog projectCatalog(ObjectMapper mapper, ProjectLocalStudioSetupStore setupStore) {
|
||||
return new ProjectCatalogService(resolveDefaultProjectsRoot(), mapper, setupStore);
|
||||
}
|
||||
|
||||
@Bean
|
||||
KnownProjects knownProjects(ProjectCatalog projectCatalog) {
|
||||
return new KnownProjectsService(resolveKnownProjectsStorage(), projectCatalog);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProjectWorkspaceInitializer projectWorkspaceInitializer(PackerWorkspaceService packer) {
|
||||
return new PackerProjectWorkspaceInitializer(packer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProjectLanguageCatalog projectLanguageCatalog() {
|
||||
FrontendRegistryService.bootstrapDefaults();
|
||||
return new CompilerProjectLanguageCatalog();
|
||||
}
|
||||
|
||||
@Bean
|
||||
StudioUiFactory studioUiFactory(
|
||||
ProjectCatalog projectCatalog,
|
||||
KnownProjects knownProjects,
|
||||
ProjectLanguageCatalog languageCatalog,
|
||||
ProjectSessionFactory projectSessionFactory,
|
||||
ExecutionControllerFactory executionControllerFactory,
|
||||
ProjectWorkspaceInitializer projectWorkspaceInitializer,
|
||||
ExecutorService studioExecutor,
|
||||
I18nService i18n,
|
||||
ThemeService theme,
|
||||
StudioEventBus eventBus,
|
||||
Shipper shipper,
|
||||
AssetCatalog assetCatalog,
|
||||
AssetCommands assetCommands,
|
||||
AssetPacking assetPacking,
|
||||
AssetDetails assetDetails,
|
||||
AssetCreationCatalog assetCreationCatalog,
|
||||
AssetMetadataService assetMetadataService,
|
||||
AssetWorkspaceStorage assetWorkspaceStorage,
|
||||
AssetCapacityPolicy assetCapacityPolicy,
|
||||
AssetBankCompositionSessionFactory bankCompositionSessionFactory,
|
||||
AssetColorSchemaFactory assetColorSchemaFactory,
|
||||
TiledAssetService tiledAssetService,
|
||||
SceneBankWorkflow sceneBankWorkflow,
|
||||
WindowStateStore windowStateStore,
|
||||
ProjectActivityHistoryStore activityHistoryStore) {
|
||||
return new StudioUiFactory(projectCatalog, knownProjects, languageCatalog, projectSessionFactory,
|
||||
executionControllerFactory, projectWorkspaceInitializer, studioExecutor, i18n, theme, eventBus, shipper,
|
||||
assetCatalog, assetCommands, assetPacking, assetDetails, assetCreationCatalog, assetMetadataService,
|
||||
assetWorkspaceStorage, assetCapacityPolicy, bankCompositionSessionFactory, assetColorSchemaFactory,
|
||||
tiledAssetService, sceneBankWorkflow, windowStateStore,
|
||||
activityHistoryStore);
|
||||
}
|
||||
|
||||
private static Path resolveDefaultProjectsRoot() {
|
||||
Path cursor = Path.of(System.getProperty("user.dir")).toAbsolutePath().normalize();
|
||||
while (cursor != null) {
|
||||
if (cursor.resolve("settings.gradle.kts").toFile().exists()
|
||||
&& cursor.resolve("test-projects").toFile().exists()) {
|
||||
return cursor.resolve("test-projects");
|
||||
}
|
||||
cursor = cursor.getParent();
|
||||
}
|
||||
return Path.of("test-projects").toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
private static Path resolveKnownProjectsStorage() {
|
||||
return Path.of(System.getProperty("user.home"), ".prometeu-studio", "known-projects.txt")
|
||||
.toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
private static Path resolveWindowStateStorage() {
|
||||
return Path.of(System.getProperty("user.home"), ".prometeu-studio", "window-state.json")
|
||||
.toAbsolutePath().normalize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package p.studio.app;
|
||||
|
||||
import javafx.application.Application;
|
||||
|
||||
public final class StudioLauncher {
|
||||
private StudioLauncher() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Application.launch(App.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package p.studio.app;
|
||||
|
||||
import p.packer.events.PackerEvent;
|
||||
import p.packer.events.PackerEventSink;
|
||||
import p.studio.ui.javafx.events.StudioEventBus;
|
||||
import p.studio.ui.javafx.events.StudioPackerOperationEvent;
|
||||
import p.studio.ui.javafx.events.StudioPackerOperationKind;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
final class StudioPackerEventAdapter implements PackerEventSink {
|
||||
private final StudioEventBus eventBus;
|
||||
|
||||
StudioPackerEventAdapter(StudioEventBus eventBus) {
|
||||
this.eventBus = Objects.requireNonNull(eventBus, "eventBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(PackerEvent event) {
|
||||
final PackerEvent packerEvent = Objects.requireNonNull(event, "event");
|
||||
eventBus.publish(new StudioPackerOperationEvent(
|
||||
packerEvent.operationId(),
|
||||
StudioPackerOperationKind.valueOf(packerEvent.kind().name()),
|
||||
packerEvent.summary(),
|
||||
packerEvent.progress() == null ? null : packerEvent.progress().value(),
|
||||
packerEvent.progress() == null || packerEvent.progress().indeterminate()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package p.studio.app;
|
||||
|
||||
import javafx.stage.Stage;
|
||||
import p.studio.api.assets.AssetCatalog;
|
||||
import p.studio.api.assets.AssetCommands;
|
||||
import p.studio.api.assets.AssetPacking;
|
||||
import p.studio.api.assets.AssetDetails;
|
||||
import p.studio.api.assets.AssetCreationCatalog;
|
||||
import p.studio.api.assets.AssetWorkspaceStorage;
|
||||
import p.studio.api.assets.AssetCapacityPolicy;
|
||||
import p.studio.api.assets.schema.AssetColorSchemaFactory;
|
||||
import p.studio.api.assets.bank.AssetBankCompositionSessionFactory;
|
||||
import p.studio.api.assets.metadata.AssetMetadataService;
|
||||
import p.studio.api.assets.scene.SceneBankWorkflow;
|
||||
import p.studio.api.assets.tiled.TiledAssetService;
|
||||
import p.studio.api.project.KnownProjects;
|
||||
import p.studio.api.project.ProjectCatalog;
|
||||
import p.studio.api.project.ProjectLanguageCatalog;
|
||||
import p.studio.api.shipper.Shipper;
|
||||
import p.studio.api.session.ProjectSessionFactory;
|
||||
import p.studio.api.execution.ExecutionControllerFactory;
|
||||
import p.studio.api.project.ProjectWorkspaceInitializer;
|
||||
import p.studio.api.preferences.WindowStateStore;
|
||||
import p.studio.api.preferences.ProjectActivityHistoryStore;
|
||||
import p.studio.ui.javafx.window.StudioWindowCoordinator;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executor;
|
||||
import p.studio.ui.javafx.utilities.ThemeService;
|
||||
import p.studio.ui.javafx.utilities.i18n.I18nService;
|
||||
import p.studio.ui.javafx.events.StudioEventBus;
|
||||
|
||||
public final class StudioUiFactory {
|
||||
private final ProjectCatalog projectCatalog;
|
||||
private final KnownProjects knownProjects;
|
||||
private final ProjectLanguageCatalog languageCatalog;
|
||||
private final ProjectSessionFactory projectSessionFactory;
|
||||
private final ExecutionControllerFactory executionControllerFactory;
|
||||
private final ProjectWorkspaceInitializer projectWorkspaceInitializer;
|
||||
private final Executor executor;
|
||||
private final I18nService i18n;
|
||||
private final ThemeService theme;
|
||||
private final StudioEventBus eventBus;
|
||||
private final Shipper shipper;
|
||||
private final AssetCatalog assetCatalog;
|
||||
private final AssetCommands assetCommands;
|
||||
private final AssetPacking assetPacking;
|
||||
private final AssetDetails assetDetails;
|
||||
private final AssetCreationCatalog assetCreationCatalog;
|
||||
private final AssetMetadataService assetMetadataService;
|
||||
private final AssetWorkspaceStorage assetWorkspaceStorage;
|
||||
private final AssetCapacityPolicy assetCapacityPolicy;
|
||||
private final AssetBankCompositionSessionFactory bankCompositionSessionFactory;
|
||||
private final AssetColorSchemaFactory assetColorSchemaFactory;
|
||||
private final TiledAssetService tiledAssetService;
|
||||
private final SceneBankWorkflow sceneBankWorkflow;
|
||||
private final WindowStateStore windowStateStore;
|
||||
private final ProjectActivityHistoryStore activityHistoryStore;
|
||||
|
||||
public StudioUiFactory(
|
||||
ProjectCatalog projectCatalog,
|
||||
KnownProjects knownProjects,
|
||||
ProjectLanguageCatalog languageCatalog,
|
||||
ProjectSessionFactory projectSessionFactory,
|
||||
ExecutionControllerFactory executionControllerFactory,
|
||||
ProjectWorkspaceInitializer projectWorkspaceInitializer,
|
||||
Executor executor,
|
||||
I18nService i18n,
|
||||
ThemeService theme,
|
||||
StudioEventBus eventBus,
|
||||
Shipper shipper,
|
||||
AssetCatalog assetCatalog,
|
||||
AssetCommands assetCommands,
|
||||
AssetPacking assetPacking,
|
||||
AssetDetails assetDetails,
|
||||
AssetCreationCatalog assetCreationCatalog,
|
||||
AssetMetadataService assetMetadataService,
|
||||
AssetWorkspaceStorage assetWorkspaceStorage,
|
||||
AssetCapacityPolicy assetCapacityPolicy,
|
||||
AssetBankCompositionSessionFactory bankCompositionSessionFactory,
|
||||
AssetColorSchemaFactory assetColorSchemaFactory,
|
||||
TiledAssetService tiledAssetService,
|
||||
SceneBankWorkflow sceneBankWorkflow,
|
||||
WindowStateStore windowStateStore,
|
||||
ProjectActivityHistoryStore activityHistoryStore) {
|
||||
this.projectCatalog = Objects.requireNonNull(projectCatalog, "projectCatalog");
|
||||
this.knownProjects = Objects.requireNonNull(knownProjects, "knownProjects");
|
||||
this.languageCatalog = Objects.requireNonNull(languageCatalog, "languageCatalog");
|
||||
this.projectSessionFactory = Objects.requireNonNull(projectSessionFactory, "projectSessionFactory");
|
||||
this.executionControllerFactory = Objects.requireNonNull(executionControllerFactory, "executionControllerFactory");
|
||||
this.projectWorkspaceInitializer = Objects.requireNonNull(projectWorkspaceInitializer, "projectWorkspaceInitializer");
|
||||
this.executor = Objects.requireNonNull(executor, "executor");
|
||||
this.i18n = Objects.requireNonNull(i18n, "i18n");
|
||||
this.theme = Objects.requireNonNull(theme, "theme");
|
||||
this.eventBus = Objects.requireNonNull(eventBus, "eventBus");
|
||||
this.shipper = Objects.requireNonNull(shipper, "shipper");
|
||||
this.assetCatalog = Objects.requireNonNull(assetCatalog, "assetCatalog");
|
||||
this.assetCommands = Objects.requireNonNull(assetCommands, "assetCommands");
|
||||
this.assetPacking = Objects.requireNonNull(assetPacking, "assetPacking");
|
||||
this.assetDetails = Objects.requireNonNull(assetDetails, "assetDetails");
|
||||
this.assetCreationCatalog = Objects.requireNonNull(assetCreationCatalog, "assetCreationCatalog");
|
||||
this.assetMetadataService = Objects.requireNonNull(assetMetadataService, "assetMetadataService");
|
||||
this.assetWorkspaceStorage = Objects.requireNonNull(assetWorkspaceStorage, "assetWorkspaceStorage");
|
||||
this.assetCapacityPolicy = Objects.requireNonNull(assetCapacityPolicy, "assetCapacityPolicy");
|
||||
this.bankCompositionSessionFactory = Objects.requireNonNull(bankCompositionSessionFactory, "bankCompositionSessionFactory");
|
||||
this.assetColorSchemaFactory = Objects.requireNonNull(assetColorSchemaFactory, "assetColorSchemaFactory");
|
||||
this.tiledAssetService = Objects.requireNonNull(tiledAssetService, "tiledAssetService");
|
||||
this.sceneBankWorkflow = Objects.requireNonNull(sceneBankWorkflow, "sceneBankWorkflow");
|
||||
this.windowStateStore = Objects.requireNonNull(windowStateStore, "windowStateStore");
|
||||
this.activityHistoryStore = Objects.requireNonNull(activityHistoryStore, "activityHistoryStore");
|
||||
}
|
||||
|
||||
public StudioWindowCoordinator create(Stage stage) {
|
||||
return new StudioWindowCoordinator(stage, projectCatalog, knownProjects, languageCatalog, projectSessionFactory,
|
||||
executionControllerFactory, projectWorkspaceInitializer, executor, i18n, theme, eventBus, shipper,
|
||||
assetCatalog, assetCommands, assetPacking, assetDetails, assetCreationCatalog, assetMetadataService,
|
||||
assetWorkspaceStorage, assetCapacityPolicy, bankCompositionSessionFactory, assetColorSchemaFactory,
|
||||
tiledAssetService, sceneBankWorkflow, windowStateStore,
|
||||
activityHistoryStore);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package p.studio.app.compiler;
|
||||
|
||||
import p.studio.api.project.ProjectReference;
|
||||
import p.studio.compiler.messages.BuilderPipelineConfig;
|
||||
import p.studio.compiler.models.BuilderPipelineContext;
|
||||
import p.studio.compiler.workspaces.BuilderPipelineService;
|
||||
import p.studio.core.shipper.CompilerBuildGateway;
|
||||
import p.studio.utilities.logs.LogAggregator;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class CompilerBuildGatewayAdapter implements CompilerBuildGateway {
|
||||
@Override
|
||||
public CompilerBuildOutput compile(ProjectReference project, Consumer<String> logSink) {
|
||||
final var config = new BuilderPipelineConfig(false, project.rootPath().toString());
|
||||
final var result = BuilderPipelineService.INSTANCE.build(
|
||||
BuilderPipelineContext.fromConfig(config),
|
||||
LogAggregator.with(logSink));
|
||||
final var capabilities = new LinkedHashSet<>(
|
||||
result.compileResult().analysisSnapshot().irBackend()
|
||||
.getReservedMetadata().requiredCapabilities().asList());
|
||||
return new CompilerBuildOutput(result.bytecodeArtifactPath(), capabilities.stream().toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package p.studio.app.compiler;
|
||||
|
||||
import p.studio.api.project.ProjectLanguageCatalog;
|
||||
import p.studio.api.project.ProjectLanguageTemplate;
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.models.FrontendSpec;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public final class CompilerProjectLanguageCatalog implements ProjectLanguageCatalog {
|
||||
@Override
|
||||
public List<ProjectLanguageTemplate> listLanguageTemplates() {
|
||||
return FrontendRegistryService.listProviders().stream()
|
||||
.map(provider -> toTemplate(provider.specification()))
|
||||
.sorted(Comparator.comparing(ProjectLanguageTemplate::languageId, String.CASE_INSENSITIVE_ORDER))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private ProjectLanguageTemplate toTemplate(FrontendSpec spec) {
|
||||
final List<String> roots = spec.getSourceRoots().stream().sorted(String.CASE_INSENSITIVE_ORDER).toList();
|
||||
final List<ProjectLanguageTemplate.StdlibOption> options = spec.getStdlibVersions().stream()
|
||||
.map(value -> new ProjectLanguageTemplate.StdlibOption(
|
||||
value.version(), ProjectLanguageTemplate.Stability.valueOf(value.type().name())))
|
||||
.sorted(Comparator.comparingInt(ProjectLanguageTemplate.StdlibOption::version))
|
||||
.toList();
|
||||
final ProjectLanguageTemplate.StdlibOption defaultOption = options.stream()
|
||||
.filter(value -> value.stability() == ProjectLanguageTemplate.Stability.DEFAULT)
|
||||
.findFirst().orElseGet(() -> options.isEmpty()
|
||||
? new ProjectLanguageTemplate.StdlibOption(1, ProjectLanguageTemplate.Stability.DEFAULT)
|
||||
: options.getFirst());
|
||||
return new ProjectLanguageTemplate(
|
||||
spec.getLanguageId(), options, defaultOption, roots,
|
||||
roots.isEmpty() ? "src" : roots.getFirst());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package p.studio.app.lsp;
|
||||
|
||||
import p.studio.api.project.ProjectReference;
|
||||
import p.studio.core.session.LspLifecyclePort;
|
||||
import p.studio.lsp.api.LspServerLifecycle;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
import p.studio.lsp.messages.LspServerConfiguration;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class LspLifecycleAdapter implements LspLifecyclePort {
|
||||
private final LspServerLifecycle lifecycle;
|
||||
|
||||
public LspLifecycleAdapter(LspServerLifecycle lifecycle) {
|
||||
this.lifecycle = Objects.requireNonNull(lifecycle, "lifecycle");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void boot(ProjectReference project) {
|
||||
lifecycle.bootServer(LspServerConfiguration.defaults(), context(project));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown(ProjectReference project) {
|
||||
lifecycle.shutdownServer(context(project));
|
||||
}
|
||||
|
||||
private static LspProjectContext context(ProjectReference project) {
|
||||
final var safeProject = Objects.requireNonNull(project, "project");
|
||||
return new LspProjectContext(
|
||||
safeProject.rootPath().toAbsolutePath().normalize().toString(),
|
||||
safeProject.languageId(),
|
||||
safeProject.rootPath());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package p.studio.app.operation;
|
||||
|
||||
import p.studio.api.operation.OperationExecutor;
|
||||
import p.studio.api.operation.OperationHandle;
|
||||
import p.studio.api.operation.OperationId;
|
||||
import p.studio.api.operation.OperationSnapshot;
|
||||
import p.studio.api.operation.OperationState;
|
||||
import p.studio.api.operation.OperationSubscription;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class ExecutorOperationExecutor implements OperationExecutor {
|
||||
private final ExecutorService executor;
|
||||
|
||||
public ExecutorOperationExecutor(ExecutorService executor) {
|
||||
this.executor = Objects.requireNonNull(executor, "executor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> OperationHandle<R> submit(String summary, Callable<R> operation) {
|
||||
final ExecutorOperationHandle<R> handle = new ExecutorOperationHandle<>(summary, operation);
|
||||
handle.start(executor);
|
||||
return handle;
|
||||
}
|
||||
|
||||
private static final class ExecutorOperationHandle<R> implements OperationHandle<R> {
|
||||
private final OperationId id = new OperationId(UUID.randomUUID().toString());
|
||||
private final String summary;
|
||||
private final Callable<R> operation;
|
||||
private final CompletableFuture<R> completion = new CompletableFuture<>();
|
||||
private final CopyOnWriteArrayList<Consumer<OperationSnapshot>> subscribers = new CopyOnWriteArrayList<>();
|
||||
private final AtomicReference<OperationSnapshot> snapshot;
|
||||
private volatile Future<?> task;
|
||||
|
||||
private ExecutorOperationHandle(String summary, Callable<R> operation) {
|
||||
this.summary = Objects.requireNonNullElse(summary, "");
|
||||
this.operation = Objects.requireNonNull(operation, "operation");
|
||||
this.snapshot = new AtomicReference<>(new OperationSnapshot(id, OperationState.PENDING, Instant.now(), this.summary));
|
||||
}
|
||||
|
||||
private void start(ExecutorService executor) {
|
||||
final FutureTask<Void> submitted = new FutureTask<>(() -> {
|
||||
try {
|
||||
final R result = operation.call();
|
||||
if (transitionTerminal(OperationState.SUCCEEDED, summary)) {
|
||||
completion.complete(result);
|
||||
}
|
||||
} catch (Throwable failure) {
|
||||
if (transitionTerminal(OperationState.FAILED, failureSummary(failure))) {
|
||||
completion.completeExceptionally(failure);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
task = submitted;
|
||||
try {
|
||||
transition(OperationState.RUNNING, summary);
|
||||
executor.execute(submitted);
|
||||
} catch (RuntimeException failure) {
|
||||
if (transitionTerminal(OperationState.FAILED, failureSummary(failure))) {
|
||||
completion.completeExceptionally(failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OperationId id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OperationSnapshot snapshot() {
|
||||
return snapshot.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<R> completion() {
|
||||
return completion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
while (true) {
|
||||
final OperationSnapshot current = snapshot.get();
|
||||
if (terminal(current.state())) {
|
||||
return false;
|
||||
}
|
||||
final OperationSnapshot cancelled = new OperationSnapshot(id, OperationState.CANCELLED, Instant.now(), summary);
|
||||
if (snapshot.compareAndSet(current, cancelled)) {
|
||||
final Future<?> submitted = task;
|
||||
if (submitted != null) {
|
||||
submitted.cancel(true);
|
||||
}
|
||||
completion.cancel(false);
|
||||
publish(cancelled);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OperationSubscription subscribe(Consumer<OperationSnapshot> subscriber) {
|
||||
final Consumer<OperationSnapshot> listener = Objects.requireNonNull(subscriber, "subscriber");
|
||||
subscribers.add(listener);
|
||||
notifySubscriber(listener, snapshot.get());
|
||||
return () -> subscribers.remove(listener);
|
||||
}
|
||||
|
||||
private void transition(OperationState state, String message) {
|
||||
final OperationSnapshot next = new OperationSnapshot(id, state, Instant.now(), message);
|
||||
snapshot.set(next);
|
||||
publish(next);
|
||||
}
|
||||
|
||||
private boolean transitionTerminal(OperationState state, String message) {
|
||||
while (true) {
|
||||
final OperationSnapshot current = snapshot.get();
|
||||
if (terminal(current.state())) {
|
||||
return false;
|
||||
}
|
||||
final OperationSnapshot next = new OperationSnapshot(id, state, Instant.now(), message);
|
||||
if (snapshot.compareAndSet(current, next)) {
|
||||
publish(next);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void publish(OperationSnapshot value) {
|
||||
subscribers.forEach(subscriber -> notifySubscriber(subscriber, value));
|
||||
}
|
||||
|
||||
private static void notifySubscriber(Consumer<OperationSnapshot> subscriber, OperationSnapshot value) {
|
||||
try {
|
||||
subscriber.accept(value);
|
||||
} catch (RuntimeException ignored) {
|
||||
// Observation must not change operation completion.
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean terminal(OperationState state) {
|
||||
return state == OperationState.SUCCEEDED || state == OperationState.FAILED || state == OperationState.CANCELLED;
|
||||
}
|
||||
|
||||
private String failureSummary(Throwable failure) {
|
||||
final String message = failure.getMessage();
|
||||
return message == null || message.isBlank() ? summary : message;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package p.studio.app.runtime;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.studio.api.project.ProjectLocalStudioSetup;
|
||||
import p.studio.api.project.ProjectReference;
|
||||
import p.studio.api.runtime.RuntimeLogEntry;
|
||||
import p.studio.core.runtime.RuntimeConnection;
|
||||
import p.studio.core.runtime.RuntimeGateway;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class ExternalRuntimeGateway implements RuntimeGateway {
|
||||
private static final int DEBUG_PORT = 7777;
|
||||
private final ObjectMapper mapper;
|
||||
private final Executor executor;
|
||||
|
||||
public ExternalRuntimeGateway(ObjectMapper mapper, Executor executor) {
|
||||
this.mapper = Objects.requireNonNull(mapper, "mapper");
|
||||
this.executor = Objects.requireNonNull(executor, "executor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public RuntimeConnection start(ProjectReference project, ProjectLocalStudioSetup setup,
|
||||
Consumer<RuntimeLogEntry> logs) throws Exception {
|
||||
final Consumer<RuntimeLogEntry> sink = Objects.requireNonNull(logs, "logs");
|
||||
final Path executable = resolveExecutable(setup);
|
||||
final Process process = new ProcessBuilder(executable.toString(), "debug", "build")
|
||||
.directory(project.rootPath().toFile()).start();
|
||||
stream(process.getInputStream(), false, sink);
|
||||
stream(process.getErrorStream(), true, sink);
|
||||
try {
|
||||
final Socket socket = connectWithRetry();
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
|
||||
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));
|
||||
final JsonNode handshake = mapper.readTree(requireLine(reader, "connection closed during handshake"));
|
||||
if (handshake == null || !"handshake".equals(handshake.path("type").asText())) {
|
||||
throw new IOException("invalid runtime handshake");
|
||||
}
|
||||
writer.write("{\"type\":\"start\"}");
|
||||
writer.newLine();
|
||||
writer.flush();
|
||||
sink.accept(RuntimeLogEntry.info("Runtime protocol %d (%s)".formatted(
|
||||
handshake.path("protocol_version").asInt(), handshake.path("runtime_version").asText("unknown"))));
|
||||
final var connection = new ProcessRuntimeConnection(process, socket, reader, writer);
|
||||
executor.execute(() -> receive(reader, sink, connection));
|
||||
return connection;
|
||||
} catch (Exception failure) {
|
||||
process.descendants().forEach(ProcessHandle::destroyForcibly);
|
||||
process.destroyForcibly();
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private static Path resolveExecutable(ProjectLocalStudioSetup setup) throws IOException {
|
||||
if (setup.prometeuRuntimePath() == null) throw new IOException("prometeuRuntimePath is missing");
|
||||
final Path executable = Path.of(setup.prometeuRuntimePath()).resolve("prometeu").toAbsolutePath().normalize();
|
||||
if (!Files.isRegularFile(executable)) throw new IOException("runtime not found at " + executable);
|
||||
if (!Files.isExecutable(executable)) throw new IOException("runtime is not executable at " + executable);
|
||||
return executable;
|
||||
}
|
||||
|
||||
private Socket connectWithRetry() throws IOException {
|
||||
IOException failure = null;
|
||||
for (int attempt = 0; attempt < 20; attempt++) {
|
||||
final Socket socket = new Socket();
|
||||
try {
|
||||
socket.connect(new InetSocketAddress("127.0.0.1", DEBUG_PORT), 500);
|
||||
socket.setSoTimeout(5_000);
|
||||
return socket;
|
||||
} catch (IOException exception) {
|
||||
failure = exception;
|
||||
closeQuietly(socket);
|
||||
try { Thread.sleep(100L); } catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("runtime connection interrupted", interrupted);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw failure == null ? new IOException("runtime connection failed") : failure;
|
||||
}
|
||||
|
||||
private void stream(InputStream stream, boolean error, Consumer<RuntimeLogEntry> sink) {
|
||||
executor.execute(() -> {
|
||||
try (var reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) sink.accept(error ? RuntimeLogEntry.error(line) : RuntimeLogEntry.info(line));
|
||||
} catch (IOException ignored) { }
|
||||
});
|
||||
}
|
||||
|
||||
private void receive(BufferedReader reader, Consumer<RuntimeLogEntry> sink, ProcessRuntimeConnection connection) {
|
||||
try {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
final JsonNode event = mapper.readTree(line);
|
||||
if (event != null && "log".equals(event.path("event").asText())) {
|
||||
final String source = event.path("source").asText("");
|
||||
final String message = event.path("msg").asText("");
|
||||
final String rendered = source.isBlank() ? message : "[" + source + "] " + message;
|
||||
final boolean error = switch (event.path("level").asText("").toUpperCase()) {
|
||||
case "ERROR", "FATAL" -> true;
|
||||
default -> false;
|
||||
};
|
||||
sink.accept(error ? RuntimeLogEntry.error(rendered) : RuntimeLogEntry.info(rendered));
|
||||
}
|
||||
}
|
||||
} catch (IOException failure) {
|
||||
if (!connection.closed) sink.accept(RuntimeLogEntry.error("Runtime debug connection lost: " + failure.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private static String requireLine(BufferedReader reader, String message) throws IOException {
|
||||
final String line = reader.readLine();
|
||||
if (line == null) throw new IOException(message);
|
||||
return line;
|
||||
}
|
||||
|
||||
private static void closeQuietly(Closeable closeable) {
|
||||
if (closeable == null) return;
|
||||
try { closeable.close(); } catch (IOException ignored) { }
|
||||
}
|
||||
|
||||
private static final class ProcessRuntimeConnection implements RuntimeConnection {
|
||||
private final Process process;
|
||||
private final Socket socket;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
private volatile boolean closed;
|
||||
|
||||
private ProcessRuntimeConnection(Process process, Socket socket, Reader reader, Writer writer) {
|
||||
this.process = process;
|
||||
this.socket = socket;
|
||||
this.reader = reader;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
@Override public CompletableFuture<Integer> completion() { return process.onExit().thenApply(Process::exitValue); }
|
||||
|
||||
@Override public void close() {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
closeQuietly(reader);
|
||||
closeQuietly(writer);
|
||||
closeQuietly(socket);
|
||||
process.descendants().forEach(ProcessHandle::destroyForcibly);
|
||||
process.destroyForcibly();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user