implements PLN-0104
This commit is contained in:
@@ -5,5 +5,5 @@ plugins {
|
||||
dependencies {
|
||||
api(project(":prometeu-infra"))
|
||||
|
||||
implementation(project(":prometeu-compiler:prometeu-compiler-core"))
|
||||
}
|
||||
api(project(":prometeu-compiler:prometeu-compiler-core"))
|
||||
}
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package p.studio.compiler.services;
|
||||
|
||||
/**
|
||||
* Marker contract for optional frontend-owned editor services.
|
||||
*/
|
||||
public interface FrontendLanguageService {
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package p.studio.compiler.services;
|
||||
|
||||
import p.studio.compiler.models.FrontendSpec;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface FrontendProvider {
|
||||
FrontendSpec specification();
|
||||
|
||||
FrontendPhaseService compiler();
|
||||
|
||||
default Optional<FrontendLanguageService> languageService() {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
+41
-8
@@ -2,6 +2,7 @@ package p.studio.compiler;
|
||||
|
||||
import p.studio.compiler.models.FrontendSpec;
|
||||
import p.studio.compiler.services.FrontendPhaseService;
|
||||
import p.studio.compiler.services.FrontendProvider;
|
||||
import p.studio.compiler.services.PBSFrontendPhaseService;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -10,29 +11,61 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class FrontendRegistryService {
|
||||
private static final Map<String, FrontendSpec> FRONTEND_SPECS = new HashMap<>();
|
||||
private static final Map<String, FrontendPhaseService> FRONTEND_PHASE_SERVICES = new HashMap<>();
|
||||
private static final Map<String, FrontendProvider> FRONTEND_PROVIDERS = new HashMap<>();
|
||||
|
||||
static {
|
||||
FRONTEND_SPECS.put(PBSDefinitions.PBS.getLanguageId(), PBSDefinitions.PBS);
|
||||
FRONTEND_PHASE_SERVICES.put(PBSDefinitions.PBS.getLanguageId(), new PBSFrontendPhaseService());
|
||||
register(new StaticFrontendProvider(PBSDefinitions.PBS, new PBSFrontendPhaseService()));
|
||||
}
|
||||
|
||||
public static FrontendSpec getDefaultFrontendSpec() {
|
||||
return FRONTEND_SPECS.get(PBSDefinitions.PBS.getLanguageId());
|
||||
return require(PBSDefinitions.PBS.getLanguageId()).specification();
|
||||
}
|
||||
|
||||
public static Optional<FrontendSpec> getFrontendSpec(final String languageId) {
|
||||
return Optional.ofNullable(FRONTEND_SPECS.get(languageId));
|
||||
return getProvider(languageId).map(FrontendProvider::specification);
|
||||
}
|
||||
|
||||
public static Optional<FrontendPhaseService> getFrontendPhaseService(final String languageId) {
|
||||
return Optional.ofNullable(FRONTEND_PHASE_SERVICES.get(languageId));
|
||||
return getProvider(languageId).map(FrontendProvider::compiler);
|
||||
}
|
||||
|
||||
public static Optional<FrontendProvider> getProvider(final String languageId) {
|
||||
return Optional.ofNullable(FRONTEND_PROVIDERS.get(languageId));
|
||||
}
|
||||
|
||||
public static FrontendProvider require(final String languageId) {
|
||||
final var provider = FRONTEND_PROVIDERS.get(languageId);
|
||||
if (provider == null) {
|
||||
throw new IllegalArgumentException("Unknown frontend languageId: " + languageId);
|
||||
}
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static List<FrontendSpec> listFrontendSpecs() {
|
||||
return FRONTEND_SPECS.values().stream()
|
||||
return FRONTEND_PROVIDERS.values().stream()
|
||||
.map(FrontendProvider::specification)
|
||||
.sorted((left, right) -> left.getLanguageId().compareToIgnoreCase(right.getLanguageId()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static List<FrontendProvider> listProviders() {
|
||||
return FRONTEND_PROVIDERS.values().stream()
|
||||
.sorted((left, right) -> left.specification().getLanguageId().compareToIgnoreCase(right.specification().getLanguageId()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
static void register(final FrontendProvider provider) {
|
||||
final var languageId = provider.specification().getLanguageId();
|
||||
if (FRONTEND_PROVIDERS.containsKey(languageId)) {
|
||||
throw new IllegalArgumentException("Duplicate frontend languageId: " + languageId);
|
||||
}
|
||||
|
||||
FRONTEND_PROVIDERS.put(languageId, provider);
|
||||
}
|
||||
|
||||
private record StaticFrontendProvider(
|
||||
FrontendSpec specification,
|
||||
FrontendPhaseService compiler) implements FrontendProvider {
|
||||
}
|
||||
}
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package p.studio.compiler;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class FrontendRegistryServiceTest {
|
||||
|
||||
@Test
|
||||
void requireShouldResolveProviderByLanguageId() {
|
||||
final var provider = FrontendRegistryService.require(PBSDefinitions.PBS.getLanguageId());
|
||||
|
||||
assertEquals(PBSDefinitions.PBS.getLanguageId(), provider.specification().getLanguageId());
|
||||
assertSame(PBSDefinitions.PBS, provider.specification());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireShouldFailClearlyForUnknownLanguageId() {
|
||||
final var thrown = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> FrontendRegistryService.require("unknown-language"));
|
||||
|
||||
assertTrue(thrown.getMessage().contains("unknown-language"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void specListingShouldBeProviderDerived() {
|
||||
final var specs = FrontendRegistryService.listFrontendSpecs();
|
||||
|
||||
assertEquals(1, specs.size());
|
||||
assertEquals(PBSDefinitions.PBS.getLanguageId(), specs.getFirst().getLanguageId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user