package org.shedlang.compiler.config; import org.shedlang.compiler.backends.Backend; import org.shedlang.compiler.backends.paths.TargetDir; import org.shedlang.compiler.config.json.Json5ObjectWithSource; import org.zwobble.sourcetext.SourceText; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public record ProjectConfig( Path root, List bins, List dependencies, List profiles ) { public static ProjectConfig read(Path projectPath) throws IOException { var configPath = projectPath.resolve("shed.json5"); var contents = Files.readString(configPath); var sourceText = SourceText.fromString(configPath.toString(), contents); return fromSourceText(projectPath, sourceText); } public static ProjectConfig fromSourceText(Path projectPath, SourceText sourceText) { return fromJson5( projectPath, Json5ObjectWithSource.parse(sourceText) ); } public static ProjectConfig fromJson5(Path root, Json5ObjectWithSource object) { return new ProjectConfig( root, binsFromJson5(object), dependenciesFromJson5(root, object), profilesFromJson5(object) ); } private static List binsFromJson5(Json5ObjectWithSource object) { return object.getArrayOfObjects("bins") .orElseGet(() -> List.of()) .stream() .map(bin -> BinConfig.fromJson5(bin)) .toList(); } private static List dependenciesFromJson5( Path projectRoot, Json5ObjectWithSource object ) { return object.getArrayOfObjects("dependencies") .orElseGet(() -> List.of()) .stream() .map(dependency -> DependencyConfig.fromJson5(projectRoot, dependency)) .toList(); } public static List profilesFromJson5(Json5ObjectWithSource object) { return object.getArrayOfObjects("profiles") .orElseGet(() -> List.of()) .stream() .map(profile -> ProfileConfig.fromJson5(profile)) .toList(); } public TargetDir targetDir(Backend backend, ProfileConfig profileConfig) { var path = root.resolve("target") .resolve(backend.targetName()) .resolve(profileConfig.name()); return new TargetDir(path); } public ProfileConfig profile(String profileName) { return this.profiles.stream() .filter(profile -> profile.name().equals(profileName)) .findFirst() // TODO: better error .orElseThrow(); } }