CompilerOutput.razor 3.07 KB
Newer Older
1
@inject JSInterop Interop 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@using System.Text.RegularExpressions
@using Zapf
@using Zilf.Common
@using Zilf.Compiler

<h2>Compilation Results</h2>

@if (CompileResult == null)
{
    <p>No compilation requested.</p>
}
else
{
    @if (AssembleResult != null)
    {
        <h3>Story File</h3>

        if (!AssembleResult.Value.Success)
        {
            <p>Assembly failed. See below.</p>
        }
        else
        {
            <button @onclick="DownloadStoryFile">Download @StoryFileName</button>
        }
    }

    <h3>Details</h3>

    <table>
        <tr>
            <th>Success?</th>
            <td>@CompileResult.Success</td>
        </tr>
        <tr>
            <th>Errors</th>
            <td>@CompileResult.ErrorCount</td>
        </tr>
        <tr>
            <th>Warnings</th>
            <td>@CompileResult.WarningCount (@CompileResult.SuppressedWarningCount suppressed)</td>
        </tr>
    </table>

    @if (CompileResult.Diagnostics.Count > 0)
    {
        <h3>Diagnostics</h3>

        <table>
            <tr>
                <th>Severity</th>
                <th>Code</th>
                <th>Description</th>
                <th>Location</th>
            </tr>

            @foreach (var diag in CompileResult.Diagnostics)
            {
                <tr>
                    <td>@diag.Severity</td>
                    <td>@diag.Code</td>
                    <td>@diag.GetFormattedMessage()</td>
                    <td>@diag.Location.SourceInfo</td>
                </tr>
            }
        </table>
    }

    @if (CompileResult.Success)
    {
        <h3>Assembly Code</h3>

        <table>
            @foreach (var (path, text) in GetZapOutput())
            {
                <tr @key="@path">
                    <th style="vertical-align: top">@path</th>
                    <td><pre>@text</pre></td>
                </tr>
            }
        </table>
    }
}

@code {
    [Parameter]
    public FrontEndResult CompileResult { get; set; }

    [Parameter]
    public AssemblyResult? AssembleResult { get; set; }

    [Parameter]
    public InMemoryFileSystem FileSystem { get; set; }

    private static Regex StoryFileRegExp = new Regex(@"\.z\d$", RegexOptions.IgnoreCase);

    private string StoryFileName => FileSystem?.Paths.FirstOrDefault(StoryFileRegExp.IsMatch);

    private IEnumerable<(string path, string text)> GetZapOutput()
    {
        if (CompileResult == null)
            return Enumerable.Empty<(string, string)>();

        return from path in FileSystem.Paths
               where path.EndsWith(".zap")
               select (path, FileSystem.GetText(path));
    }

    private void DownloadStoryFile()
    {
        var path = StoryFileName;

        if (path != null)
        {
            var file = FileSystem.GetBytes(path);
            string filename = System.IO.Path.GetFileName(path);
            string contentType = "application/x-zmachine";

120
            Interop.DownloadBytesAsFile(file, filename, contentType);
121
122
123
        }
    }
}