/// <summary>Generates all types from the resolver with the given extension code.</summary>
        /// <returns>The code.</returns>
        public CodeArtifactCollection GenerateTypes(ExtensionCode extensionCode)
        {
            var collection = base.GenerateTypes();

            foreach (var artifact in collection.Artifacts)
            {
                if (extensionCode?.ExtensionClasses.ContainsKey(artifact.TypeName) == true)
                {
                    var classCode = artifact.Code;

                    var index = classCode.IndexOf("constructor(", StringComparison.Ordinal);
                    if (index != -1)
                    {
                        artifact.Code = classCode.Insert(index, extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n\n    ");
                    }
                    else
                    {
                        index = classCode.IndexOf("class", StringComparison.Ordinal);
                        index = classCode.IndexOf("{", index, StringComparison.Ordinal) + 1;

                        artifact.Code = classCode.Insert(index, "\n    " + extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n");
                    }
                }
            }

            return(new CodeArtifactCollection(collection.Artifacts, extensionCode));
        }
Пример #2
0
        /// <summary>Generates all types from the resolver with the given extension code.</summary>
        /// <returns>The code.</returns>
        public CodeArtifactCollection GenerateTypes(ExtensionCode extensionCode)
        {
            var collection = base.GenerateTypes();
            var artifacts  = collection.Artifacts.ToList();

            foreach (var artifact in collection.Artifacts)
            {
                if (extensionCode?.ExtensionClasses.ContainsKey(artifact.TypeName) == true)
                {
                    var classCode = artifact.Code;

                    var index = classCode.IndexOf("constructor(", StringComparison.Ordinal);
                    if (index != -1)
                    {
                        artifact.Code = classCode.Insert(index, extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n\n    ");
                    }
                    else
                    {
                        index = classCode.IndexOf("class", StringComparison.Ordinal);
                        index = classCode.IndexOf("{", index, StringComparison.Ordinal) + 1;

                        artifact.Code = classCode.Insert(index, "\n    " + extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n");
                    }
                }
            }

            if (artifacts.Any(r => r.Code.Contains("formatDate(")))
            {
                var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File.FormatDate", null);
                artifacts.Add(new CodeArtifact("formatDate", CodeArtifactType.Function, CodeArtifactLanguage.CSharp, template));
            }

            if (Settings.HandleReferences)
            {
                var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File.ReferenceHandling", null);
                artifacts.Add(new CodeArtifact("jsonParse", CodeArtifactType.Function, CodeArtifactLanguage.CSharp, template));
            }

            return(new CodeArtifactCollection(artifacts, extensionCode));
        }
        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
        }