aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/config/ProjectConfig.java
blob: 44a041f49545abb7d83d44917cf04173c57222e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<BinConfig> bins,
    List<DependencyConfig> dependencies,
    List<ProfileConfig> 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<BinConfig> binsFromJson5(Json5ObjectWithSource object) {
        return object.getArrayOfObjects("bins")
            .orElseGet(() -> List.of())
            .stream()
            .map(bin -> BinConfig.fromJson5(bin))
            .toList();
    }

    private static List<DependencyConfig> dependenciesFromJson5(
        Path projectRoot,
        Json5ObjectWithSource object
    ) {
        return object.getArrayOfObjects("dependencies")
            .orElseGet(() -> List.of())
            .stream()
            .map(dependency -> DependencyConfig.fromJson5(projectRoot, dependency))
            .toList();
    }

    public static List<ProfileConfig> 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();
    }
}