Exemple #1
0
        public static GenericTypeTemplate Generic(this Code _, string name, params TypeTemplate[] types)
        {
            GenericTypeTemplate generic = new GenericTypeTemplate(name);

            generic.Types.AddRange(types);
            return(generic);
        }
Exemple #2
0
        public static GenericTypeTemplate Generic(this Code _, string name, string nameSpace = null, bool nullable = false, bool fromSystem = false, params TypeTemplate[] types)
        {
            GenericTypeTemplate generic = new GenericTypeTemplate(name, nameSpace, nullable, fromSystem);

            generic.Types.AddRange(types);
            return(generic);
        }
 public void Write(GenericTypeTemplate template, IOutputCache output)
 {
     output.Add(template.Name)
     .Add("<")
     .Add(template.Types, ", ")
     .Add(">");
 }
Exemple #4
0
        public void Write(GenericTypeTemplate template, IOutputCache output)
        {
            TypeTemplate keyType   = template.Types.First();
            TypeTemplate valueType = template.Types.Second();

            if (keyType.Name is "string" or "number")
            {
                output.Add($"{{ [key: {keyType.Name}]: {valueType.Name}; }}");
            }
        public override void Write(ICodeFragment fragment, IOutputCache output)
        {
            GenericTypeTemplate template = (GenericTypeTemplate)fragment;

            if (template.Name == "Nullable")
            {
                output.Add(template.Types.Single().Name).Add("?");
            }
            else
            {
                base.Write(fragment, output);
            }
        }
Exemple #6
0
        public virtual void Write(ICodeFragment fragment, IOutputCache output)
        {
            GenericTypeTemplate template = (GenericTypeTemplate)fragment;

            if (this.Writers.ContainsKey(template.Name))
            {
                this.Writers[template.Name].Write(template, output);
            }
            else
            {
                this.Writers["Default"].Write(template, output);
            }
        }
        public void Write(GenericTypeTemplate template, IOutputCache output)
        {
            TypeTemplate keyType   = template.Types.First();
            TypeTemplate valueType = template.Types.Second();

            if (keyType.Name == "string" || keyType.Name == "number")
            {
                output.Add($"{{ [key: {keyType.Name}]: {valueType.Name}; }}");
            }
            else
            {
                output.Add($"{{ /* unsupported type for key. Expected string or number. Got '{keyType.Name}' */. }}");
            }
        }
Exemple #8
0
        public static TypeTemplate ToTemplate(this TypeTransferObject type)
        {
            if (type == null)
            {
                throw new NullReferenceException();
            }
            if (type.Generics.Count == 0)
            {
                return(new TypeTemplate(type.Name, type.Namespace, fromSystem: type.FromSystem, isNullable: type.IsNullable));
            }
            GenericTypeTemplate genericTypeTemplate = new GenericTypeTemplate(type.Name, type.Namespace, fromSystem: type.FromSystem, isNullable: type.IsNullable);

            type.Generics.Where(x => x.Type != null).ForEach(g => genericTypeTemplate.Types.Add(g.Type.ToTemplate()));
            return(genericTypeTemplate);
        }
        public override void Write(ICodeFragment fragment, IOutputCache output)
        {
            GenericTypeTemplate template = (GenericTypeTemplate)fragment;

            if (template.Name == "List" || template.Name == "IList" || template.Name == "IEnumerable")
            {
                output.Add(template.Types.Single().Name)
                .Add("[]");
            }
            else if (template.Name == "Nullable | undefined")
            {
                output.Add(template.Types.Single().Name + " | undefined");
            }
            else if (template.Name == "Nullable")
            {
                output.Add(template.Types.Single().Name);
            }
            else
            {
                base.Write(fragment, output);
            }
        }
 public void Write(GenericTypeTemplate template, IOutputCache output)
 {
     output.Add(template.Types.Single())
     .Add("[]");
 }
        public virtual void Write(AspDotNetWriteConfiguration configuration, List <FileTemplate> files)
        {
            Logger.Trace("Generate generator controller for ASP.net...");
            if (!configuration.Language.IsCsharp())
            {
                throw new InvalidOperationException($"Can not generate ASP.net Controller for language {configuration.Language?.Name ?? "Empty"}. Only Csharp is currently implemented");
            }

            if (configuration.Standalone)
            {
                throw new InvalidOperationException("Can not generate Generator.Controller with KY.Generator.CLI.Standalone use KY.Generator.CLI instead");
            }
            string        nameSpace     = (configuration.GeneratorController.Namespace ?? configuration.Namespace).AssertIsNotNull(nameof(configuration.Namespace), "asp writer requires a namespace");
            ClassTemplate classTemplate = files.AddFile(configuration.GeneratorController.RelativePath ?? configuration.RelativePath, configuration.AddHeader)
                                          .AddNamespace(nameSpace)
                                          .AddClass("GeneratorController", Code.Type(configuration.Template.ControllerBase))
                                          .WithUsing("System")
                                          .WithUsing("System.Linq")
                                          .WithUsing("KY.Generator")
                                          .WithUsing("KY.Generator.Output");

            classTemplate.Usings.AddRange(configuration.Template.Usings);

            if (configuration.Template.UseOwnCache)
            {
                GenericTypeTemplate type = Code.Generic("Dictionary", Code.Type("string"), Code.Type("MemoryOutput"));
                classTemplate.AddField("cache", type)
                .FormatName(configuration)
                .Static()
                .Readonly()
                .DefaultValue = Code.New(type);
            }

            MethodTemplate createMethod = classTemplate.AddMethod("Create", Code.Type("string"))
                                          .FormatName(configuration)
                                          .WithParameter(Code.Type("string"), "configuration");

            if (!configuration.Template.ValidateInput)
            {
                createMethod.WithAttribute("ValidateInput", Code.Local("false"));
            }

            MethodTemplate commandMethod = classTemplate.AddMethod("Command", Code.Type("string"))
                                           .FormatName(configuration)
                                           .WithParameter(Code.Type("string"), "command");

            MultilineCodeFragment createCode = createMethod.Code;

            createCode.AddLine(Code.Declare(Code.Type("string"), "id", Code.Local("Guid").Method("NewGuid").Method("ToString")))
            .AddLine(Code.Declare(Code.Type("MemoryOutput"), "output", Code.New(Code.Type("MemoryOutput"))))
            .AddLine(Code.Declare(Code.Type("Generator"), "generator", Code.New(Code.Type("Generator"))))
            .AddLine(Code.Local("generator").Method("SetOutput", Code.Local("output")).Close());
            MultilineCodeFragment commandCode = commandMethod.Code;

            commandCode.AddLine(Code.Declare(Code.Type("string"), "id", Code.Local("Guid").Method("NewGuid").Method("ToString")))
            .AddLine(Code.Declare(Code.Type("MemoryOutput"), "output", Code.New(Code.Type("MemoryOutput"))))
            .AddLine(Code.Declare(Code.Type("Generator"), "generator", Code.New(Code.Type("Generator"))))
            .AddLine(Code.Local("generator").Method("SetOutput", Code.Local("output")).Close());
            foreach (string ns in configuration.GeneratorController.Usings)
            {
                classTemplate.AddUsing(ns);
            }
            foreach (string moduleType in configuration.GeneratorController.PreloadModules)
            {
                createCode.AddLine(Code.Local("generator").GenericMethod("PreloadModule", Code.Type(moduleType)).Close());
                commandCode.AddLine(Code.Local("generator").GenericMethod("PreloadModule", Code.Type(moduleType)).Close());
            }
            foreach (AspDotNetControllerConfigureModule configure in configuration.GeneratorController.Configures)
            {
                createCode.AddLine(Code.Local("generator").Method(configure.Module, Code.Lambda("x", Code.Csharp("x." + configure.Action))));
                commandCode.AddLine(Code.Local("generator").Method(configure.Module, Code.Lambda("x", Code.Csharp("x." + configure.Action))));
            }
            createCode.AddLine(Code.Local("generator").Method("ParseConfiguration", Code.Local("configuration")).Close())
            .AddLine(Code.Local("generator").Method("Run").Close())
            .AddBlankLine();
            commandCode.AddLine(Code.Local("generator").Method("ParseCommand", Code.Local("command")).Close())
            .AddLine(Code.Local("generator").Method("Run").Close())
            .AddBlankLine();
            if (configuration.Template.UseOwnCache)
            {
                createCode.AddLine(Code.Local("cache").Index(Code.Local("id")).Assign(Code.Local("output")).Close());
                commandCode.AddLine(Code.Local("cache").Index(Code.Local("id")).Assign(Code.Local("output")).Close());
            }
            else
            {
                createCode.AddLine(Code.Static(Code.Type("HttpContext")).Property("Current").Property("Cache").Index(Code.Local("id")).Assign(Code.Local("output")).Close());
                commandCode.AddLine(Code.Static(Code.Type("HttpContext")).Property("Current").Property("Cache").Index(Code.Local("id")).Assign(Code.Local("output")).Close());
            }
            createCode.AddLine(Code.Return(Code.Local("id")));
            commandCode.AddLine(Code.Return(Code.Local("id")));

            ChainedCodeFragment getFromCacheForFilesFragment = configuration.Template.UseOwnCache
                                                                   ? (ChainedCodeFragment)Code.Local("cache")
                                                                   : Code.Static(Code.Type("HttpContext")).Property("Current").Property("Cache");
            MethodTemplate getFilesMethod = classTemplate.AddMethod("GetFiles", Code.Type("string"))
                                            .FormatName(configuration)
                                            .WithParameter(Code.Type("string"), "id");

            getFilesMethod.Code.AddLine(Code.If(Code.Local("id").Equals().Null(), x => x.Code.AddLine(Code.Return(Code.Null()))))
            .AddLine(Code.Declare(Code.Type("MemoryOutput"), "output", getFromCacheForFilesFragment.Index(Code.Local("id")).As(Code.Type("MemoryOutput"))))
            .AddLine(Code.Return(Code.InlineIf(Code.Local("output").Equals().Null(),
                                               Code.Null(),
                                               Code.Local("string").Method("Join", Code.Local("Environment").Property("NewLine"),
                                                                           Code.Local("output").Property("Files").Method("Select", Code.Lambda("x", Code.Local("x").Property("Key")))))));

            ChainedCodeFragment getFromCacheForFileFragment = configuration.Template.UseOwnCache
                                                                  ? (ChainedCodeFragment)Code.Local("cache")
                                                                  : Code.Static(Code.Type("HttpContext")).Property("Current").Property("Cache");
            MethodTemplate getFileMethod = classTemplate.AddMethod("GetFile", Code.Type("string"))
                                           .FormatName(configuration)
                                           .WithParameter(Code.Type("string"), "id")
                                           .WithParameter(Code.Type("string"), "path");

            getFileMethod.Code.AddLine(Code.If(Code.Local("id").Equals().Null(), x => x.Code.AddLine(Code.Return(Code.Null()))))
            .AddLine(Code.Declare(Code.Type("MemoryOutput"), "output", getFromCacheForFileFragment.Index(Code.Local("id")).As(Code.Type("MemoryOutput"))))
            .AddLine(Code.Return(Code.InlineIf(Code.Local("output").Equals().Null().Or().Not().Local("output").Property("Files").Method("ContainsKey", Code.Local("path")),
                                               Code.Null(),
                                               Code.Local("output").Property("Files").Index(Code.Local("path")))));
            MethodTemplate availableMethod = classTemplate.AddMethod("Available", Code.Type("bool"))
                                             .FormatName(configuration);

            availableMethod.Code.AddLine(Code.Return(Code.Local("true")));

            if (configuration.Template.UseAttributes)
            {
                classTemplate.WithUsing("Microsoft.AspNetCore.Mvc")
                .WithAttribute("Route", Code.String("[controller]"))
                .WithAttribute("Route", Code.String("api/v1/[controller]"));
                createMethod.WithAttribute("HttpPost", Code.String("[action]"));
                commandMethod.WithAttribute("HttpPost", Code.String("[action]"));
                getFilesMethod.WithAttribute("HttpPost", Code.String("[action]"));
                getFileMethod.WithAttribute("HttpPost", Code.String("[action]"));
                availableMethod.WithAttribute("HttpGet", Code.String("[action]"));
            }
        }