aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java
blob: a086fe2be9c48196d64b21fc2a7da585c0e26f0d (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
package org.zwobble.couscous.backends.csharp;

import org.zwobble.couscous.Backend;
import org.zwobble.couscous.ast.TypeNode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static com.google.common.collect.Iterables.transform;

public class CsharpBackend implements Backend {
    private final Path outputFilePath;
    private final String namespace;

    public CsharpBackend(Path outputFilePath, String namespace) {
        this.outputFilePath = outputFilePath;
        this.namespace = namespace;
    }

    @Override
    public void compile(List<TypeNode> classes) throws IOException {
        outputFilePath.getParent().toFile().mkdirs();
        Files.write(
            outputFilePath,
            transform(
                classes,
                classNode -> compileClass(classNode)));
    }

    private String compileClass(TypeNode classNode) {
        return CsharpSerializer.serialize(CsharpCodeGenerator.generateCode(classNode, namespace));
    }
}