Ejemplo n.º 1
0
        private static void Generate(Generator generator, SpecData spec, string outputPath)
        {
            string filename = generator.GetOutputName();
            string data     = generator.Generate(spec);

            File.WriteAllText(Path.Join(outputPath, filename), data);
        }
Ejemplo n.º 2
0
 private static string GetCsTypeName(SpecData spec, string type, bool optional)
 {
     if (spec.Types.ContainsKey(type))
     {
         return(spec.Types[type].Real.ToPascalCase() + (optional ? "?" : ""));
     }
     return(type + (optional ? "?" : ""));
 }
Ejemplo n.º 3
0
        public override string Generate(SpecData spec)
        {
            _spec   = spec;
            _indent = 0;
            _sb     = new StringBuilder();

            _sb.AppendLine("/* This file was generated from GenerateSpec/GeneratorCS.cs */");
            _sb.AppendLine();
            _sb.AppendLine("using Newtonsoft.Json;");
            _sb.AppendLine("using System;");
            _sb.AppendLine();
            _sb.AppendLine("/// <summary>", _indent);
            _sb.AppendLine($"/// WhyB Version: {_spec.Version}");
            _sb.AppendLine("/// </summary>", _indent);
            _sb.AppendLine("namespace WhyB {");
            _indent++;

            _sb.AppendLine("public interface IWhyBBackend {", _indent);
            _indent++;

            foreach (var(name, request) in _spec.Requests)
            {
                GenerateRequestMethodInterface(name, request, false);
                _sb.AppendLine();
            }

            _sb.Replace(Environment.NewLine, string.Empty, _sb.Length - 2, 2);
            _indent--;
            _sb.AppendLine("}", _indent);

            _sb.AppendLine();

            _sb.AppendLine("public interface IWhyBFrontend {", _indent);
            _indent++;

            foreach (var(name, eventInfo) in _spec.Events)
            {
                GenerateEventMethodInterface(name, eventInfo, false);
                _sb.AppendLine();
            }

            _sb.Replace(Environment.NewLine, string.Empty, _sb.Length - 2, 2);
            _indent--;
            _sb.AppendLine("}", _indent);

            _sb.AppendLine();

            _sb.AppendLine("public interface IWhyBBackendSingleArgs {", _indent);
            _indent++;

            foreach (var(name, request) in _spec.Requests)
            {
                GenerateRequestMethodInterface(name, request, true);
                _sb.AppendLine();
            }

            _sb.Replace(Environment.NewLine, string.Empty, _sb.Length - 2, 2);
            _indent--;
            _sb.AppendLine("}", _indent);

            _sb.AppendLine();

            _sb.AppendLine("public interface IWhyBFrontendSingleArgs {", _indent);
            _indent++;

            foreach (var(name, eventInfo) in _spec.Events)
            {
                GenerateEventMethodInterface(name, eventInfo, true);
                _sb.AppendLine();
            }

            _sb.Replace(Environment.NewLine, string.Empty, _sb.Length - 2, 2);
            _indent--;
            _sb.AppendLine("}", _indent);

            _sb.AppendLine();

            _sb.AppendLine("#region Extensions");
            foreach (var(extensionName, extension) in _spec.Extensions)
            {
                GenerateExtension(extensionName, extension);
                _sb.AppendLine();
            }
            _sb.Replace(Environment.NewLine, string.Empty, _sb.Length - 2, 2);
            _sb.AppendLine("#endregion Extensions");

            _sb.AppendLine();

            _sb.AppendLine("#region RequestClasses");
            foreach (var(name, request) in _spec.Requests)
            {
                GenerateRequestArgsClass(name, request);
                _sb.AppendLine();
            }
            foreach (var(extensionName, extension) in _spec.Extensions)
            {
                foreach (var(name, request) in extension.Requests)
                {
                    GenerateRequestArgsClass(name, request);
                    _sb.AppendLine();
                }
            }
            _sb.Replace(Environment.NewLine, string.Empty, _sb.Length - 2, 2);
            _sb.AppendLine("#endregion RequestClasses");

            _sb.AppendLine();

            _sb.AppendLine("#region EventClasses");
            foreach (var(name, eventInfo) in _spec.Events)
            {
                GenerateEventClass(name, eventInfo);
                _sb.AppendLine();
            }
            foreach (var(extensionName, extension) in _spec.Extensions)
            {
                foreach (var(name, eventInfo) in extension.Events)
                {
                    GenerateEventClass(name, eventInfo);
                    _sb.AppendLine();
                }
            }
            _sb.Replace(Environment.NewLine, string.Empty, _sb.Length - 2, 2);
            _sb.AppendLine("#endregion EventClasses");

            _indent--;
            _sb.AppendLine("}", _indent);

            return(_sb.ToString());
        }
Ejemplo n.º 4
0
        public override string Generate(SpecData spec)
        {
            _spec = spec;

            _head = new StringBuilder();
            _toc  = new List <TocElement>();
            _head.AppendLine("[comment]: # \"This file was generated from GenerateSpec/GeneratorMD.cs\"  ");
            _head.AppendLine("# **Specification**  ");
            _toc.Add(new TocElement("Specification"));
            _head.AppendLine($"Version: {_spec.Version}  ");
            _head.AppendLine($"{_spec.Desc}  ");

            _body = new StringBuilder();

            _body.AppendLine("# **Mandates**  ");
            _toc.Add(new TocElement("Mandates"));
            foreach (string mandate in _spec.Mandates)
            {
                _body.AppendLine($"{mandate}  ");
            }

            _body.AppendLine();
            _body.AppendLine();

            _body.AppendLine("# **Codes**  ");
            _toc.Add(new TocElement("Codes"));
            int longestDesc = _spec.Codes.Values.Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur).Length;

            _body.AppendLine("|  Code   |  Meaning  ");
            _body.AppendLine($"|:-------:|:-{new string('-', longestDesc - 1)}  ");
            foreach (var(code, desc) in _spec.Codes)
            {
                _body.AppendLine($"| {code}  | {desc}  ");
            }

            _body.AppendLine();
            _body.AppendLine();

            _body.AppendLine("# **Requests**  ");
            _toc.Add(new TocElement("Requests"));
            foreach (var(name, request) in _spec.Requests)
            {
                _toc.Add(new TocElement(name, $"**`{name}`**", 1));
                GenerateRequest(name, request);
            }

            _body.AppendLine("# **Events**  ");
            _toc.Add(new TocElement("Events"));
            foreach (var(name, eventInfo) in _spec.Events)
            {
                _toc.Add(new TocElement(name, $"**`{name}`**", 1));
                GenerateEvent(name, eventInfo);
            }

            _toc.Add(new TocElement("Extensions", $"**Extensions**"));
            _body.AppendLine("# **Extensions**  ");
            _body.AppendLine($"{_spec.ExtensionsDesc}  ");
            _body.AppendLine();
            foreach (var(extensionName, extension) in _spec.Extensions)
            {
                _toc.Add(new TocElement(extensionName, $"**{extensionName}**", 1));
                _body.AppendLine($"# {extensionName}  ");
                _body.AppendLine($"{extension.Desc}  ");
                _body.AppendLine();

                if (extension.Requests.Count > 0)
                {
                    _body.AppendLine($"# Requests ({extensionName})  ");
                    _toc.Add(new TocElement($"Requests ({extensionName})", "Requests", 2));
                    foreach (var(name, request) in extension.Requests)
                    {
                        _toc.Add(new TocElement(name, $"**`{name}`**", 3));
                        GenerateRequest(name, request);
                    }
                }

                if (extension.Events.Count > 0)
                {
                    _body.AppendLine($"# Events ({extensionName})  ");
                    _toc.Add(new TocElement($"Events ({extensionName})", "Events", 2));
                    foreach (var(name, eventInfo) in extension.Events)
                    {
                        _toc.Add(new TocElement(name, $"**`{name}`**", 3));
                        GenerateEvent(name, eventInfo);
                    }
                }

                _body.AppendLine();
            }

            // Finish up head
            _head.AppendLine();
            foreach (TocElement elem in _toc)
            {
                _head.AppendLine($"{new string('\t', elem.Depth)}- [{elem.Name}](#{HttpUtility.UrlPathEncode(elem.Target)})  ");
            }
            _head.AppendLine();
            _head.AppendLine();

            _body.Insert(0, _head);
            return(_body.ToString());
        }