Exemplo n.º 1
0
        public void When_class_order_is_wrong_then_classes_are_correctly_reordered()
        {
            //// Arrange
            var classes = new List <CodeArtifact>
            {
                new CodeArtifact("Car", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
                new CodeArtifact("Apple", "List<Fruit>", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
                new CodeArtifact("Professor", "Teacher", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
                new CodeArtifact("Teacher", "Person[]", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
                new CodeArtifact("Fruit", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
                new CodeArtifact("Person", CodeArtifactType.Class, CodeArtifactLanguage.CSharp)
            };

            //// Act
            classes = CodeArtifactCollection.OrderByBaseDependency(classes).ToList();
            var order = string.Join(", ", classes.Select(c => c.TypeName));

            //// Assert
            Assert.Equal("Car, Fruit, Apple, Person, Teacher, Professor", order);
        }
        public async Task CreateClient(TextWriter writer)
        {
            var interfacesToWrite = new InterfaceManager();

            writer.WriteLine(
                $@"using Threax.AspNetCore.Halcyon.Client;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Net.Http;
using System.Linq;

namespace {options.Namespace} {{"
                );

            await WriteClient(interfacesToWrite, writer);

            writer.WriteLine("}");

            //Write interfaces, kind of weird, no good docs for this
            var settings = new CSharpGeneratorSettings()
            {
                Namespace = options.Namespace,
                GenerateDataAnnotations = false,
                ClassStyle = CSharpClassStyle.Poco,
                RequiredPropertiesMustBeDefined = false,
                EnumNameGenerator = new EnumValueEnumNameGenerator(),
                ArrayType         = "List" //This is imported in the using statements above (System.Collections.Generic.List)
            };

            //Gather up everything to write, skip duplicate instances of the same thing
            Dictionary <String, CodeArtifact> codeArtifacts = new Dictionary <String, CodeArtifact>();

            foreach (var item in interfacesToWrite.Interfaces)
            {
                //Remove any properties from item that are hal embeds
                var propertiesToRemove = item.Value.Properties.Where(i => i.Value.IsHalEmbedded()).ToList();
                foreach (var remove in propertiesToRemove)
                {
                    item.Value.Properties.Remove(remove.Key);
                }

                var resolver = new CSharpTypeResolver(settings);
                resolver.RegisterSchemaDefinitions(new Dictionary <String, JsonSchema4>()
                {
                    { item.Key, item.Value }
                });                                                                                                     //Add all discovered generators

                var generator = new CSharpGenerator(item.Value, settings, resolver);
                var artifacts = generator.GenerateTypes();
                foreach (var artifact in artifacts.Artifacts)
                {
                    if (!codeArtifacts.ContainsKey(artifact.TypeName))
                    {
                        codeArtifacts.Add(artifact.TypeName, artifact);
                    }
                }
            }

            //Write the classes officially
            //From TypeScriptGenerator.cs GenerateFile, (NJsonSchema 9.10.49)
            var model = new FileTemplateModel()
            {
                Namespace = settings.Namespace ?? string.Empty,
                TypesCode = ConversionUtilities.TrimWhiteSpaces(string.Join("\n\n", CodeArtifactCollection.OrderByBaseDependency(codeArtifacts.Values).Select(p => p.Code))),
            };

            var template = settings.TemplateFactory.CreateTemplate("CSharp", "File", model);
            var classes  = ConversionUtilities.TrimWhiteSpaces(template.Render());

            writer.WriteLine(classes);
            //End Write Interfaces
        }
        public async Task CreateClient(TextWriter writer)
        {
            var interfacesToWrite = new InterfaceManager();

            writer.WriteLine(
                @"import * as hal from 'htmlrapier.halcyon/src/EndpointClient';
import { Fetcher } from 'htmlrapier/src/fetcher';"
                );

            await WriteClient(interfacesToWrite, writer);

            //Write interfaces, kind of weird, no good docs for this
            var settings = new TypeScriptGeneratorSettings()
            {
                TypeStyle = TypeScriptTypeStyle.Interface,
                ForceAllPropertiesOptional = true,
                DateTimeType      = TypeScriptDateTimeType.String,
                EnumNameGenerator = new EnumValueEnumNameGenerator(),
            };

            //Gather up everything to write, skip duplicate instances of the same thing
            Dictionary <String, CodeArtifact> codeArtifacts = new Dictionary <String, CodeArtifact>();
            ExtensionCode lastExtensionCode = null;

            foreach (var item in interfacesToWrite.Interfaces)
            {
                //Remove any properties from item that are hal embeds
                var propertiesToRemove = item.Value.Properties.Where(i => i.Value.IsHalEmbedded()).ToList();
                foreach (var remove in propertiesToRemove)
                {
                    item.Value.Properties.Remove(remove.Key);
                }

                var resolver = new TypeScriptTypeResolver(settings);
                resolver.RegisterSchemaDefinitions(new Dictionary <String, JsonSchema4>()
                {
                    { item.Key, item.Value }
                });                                                                                                     //Add all discovered generators

                var generator = new TypeScriptGenerator(item.Value, settings, resolver);
                var artifacts = generator.GenerateTypes();
                foreach (var artifact in artifacts.Artifacts)
                {
                    if (!codeArtifacts.ContainsKey(artifact.TypeName))
                    {
                        codeArtifacts.Add(artifact.TypeName, artifact);
                    }
                }
                lastExtensionCode = artifacts.ExtensionCode;
            }

            //Write the classes officially
            //From TypeScriptGenerator.cs GenerateFile, (NJsonSchema 9.10.49)
            var model = new FileTemplateModel(settings)
            {
                Types         = ConversionUtilities.TrimWhiteSpaces(string.Join("\n\n", CodeArtifactCollection.OrderByBaseDependency(codeArtifacts.Values).Select(p => p.Code))),
                ExtensionCode = (TypeScriptExtensionCode)lastExtensionCode
            };

            var template = settings.TemplateFactory.CreateTemplate("TypeScript", "File", model);
            var classes  = ConversionUtilities.TrimWhiteSpaces(template.Render());

            writer.WriteLine(classes);

            //Write out common interfaces we reuse in all clients
            writer.WriteLine(@"
export interface HalEndpointDocQuery {
    includeRequest?: boolean;
    includeResponse?: boolean;
}");

            //End Write Interfaces
        }