File: ContractWriter.cs

package info (click to toggle)
golang-github-microsoft-dev-tunnels 0.0.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,988 kB
  • sloc: cs: 9,969; java: 2,767; javascript: 328; xml: 186; makefile: 5
file content (120 lines) | stat: -rw-r--r-- 3,455 bytes parent folder | download
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
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
120
// <copyright file="ContractWriter.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
// </copyright>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;

namespace Microsoft.DevTunnels.Generator;

internal abstract class ContractWriter
{
    private static readonly Regex paragraphBreakRegex = new Regex(@" *\<para */\> *");

    protected readonly string repoRoot;
    protected readonly string csNamespace;

    public static string[] SupportedLanguages { get; } = new[]
    {
        "TypeScript",
        "Go",
        "Java",
        "Rust"
    };

    public static ContractWriter Create(string language, string repoRoot, string csNamespace)
    {
        return language switch
        {
            "TypeScript" => new TSContractWriter(repoRoot, csNamespace),
            "Go" => new GoContractWriter(repoRoot, csNamespace),
            "Java" => new JavaContractWriter(repoRoot, csNamespace),
            "Rust" => new RustContractWriter(repoRoot, csNamespace),
            _ => throw new NotSupportedException("Unsupported contract language: " + language),
        };
    }

    protected ContractWriter(string repoRoot, string csNamespace)
    {
        this.repoRoot = repoRoot;
        this.csNamespace = csNamespace;
    }

    public abstract void WriteContract(ITypeSymbol type, ICollection<ITypeSymbol> allTypes);

    public virtual void WriteCompleted()
    {
    }

    protected string GetAbsolutePath(string relativePath)
    {
        return Path.Combine(this.repoRoot, relativePath);
    }

    protected string GetRelativePath(string absolutePath)
    {
        if (absolutePath.StartsWith(this.repoRoot))
        {
            return absolutePath.Substring(this.repoRoot.Length + 1).Replace("\\", "/");
        }

        return absolutePath;
    }

    protected static IEnumerable<string> WrapComment(string comment, int wrapColumn)
    {
        var isFirst = true;
        foreach (var paragraph in paragraphBreakRegex.Split(comment))
        {
            if (isFirst)
            {
                isFirst = false;
            }
            else
            {
                // Insert a blank line between paragraphs.
                yield return string.Empty;
            }

            comment = paragraph;
            while (comment.Length > wrapColumn)
            {
                var i = wrapColumn;
                while (i > 0 && comment[i] != ' ')
                {
                    i--;
                }

                if (i == 0)
                {
                    i = comment.IndexOf(' ');
                }

                yield return comment.Substring(0, i).TrimEnd();
                comment = comment.Substring(i + 1);
            }

            yield return comment.TrimEnd();
        }
    }

    protected static AttributeData? GetAttribute(ISymbol symbol, string attributeName)
    {
        return symbol.GetAttributes().FirstOrDefault((a) => a.AttributeClass?.Name == attributeName);
    }

    protected static AttributeData? GetObsoleteAttribute(ISymbol symbol)
    {
        return GetAttribute(symbol, nameof(ObsoleteAttribute));
    }

    protected static string? GetObsoleteMessage(AttributeData? obsoleteAttribute)
    {
        return obsoleteAttribute?.ConstructorArguments.FirstOrDefault().Value?.ToString();
    }
}