├── src ├── CodeBuilder │ ├── Entities │ │ ├── Types │ │ │ ├── IType.cs │ │ │ ├── BaseType.cs │ │ │ ├── Type.cs │ │ │ ├── Modules.cs │ │ │ └── GenericType.cs │ │ ├── Instructions │ │ │ ├── IInstruction.cs │ │ │ ├── Statement.cs │ │ │ └── Block.cs │ │ ├── Modifiers │ │ │ ├── SyncModifier.cs │ │ │ ├── ImplementationModifier.cs │ │ │ ├── ScopeModifier.cs │ │ │ ├── OverrideModifier.cs │ │ │ └── AccessModifier.cs │ │ ├── Body.cs │ │ ├── Attribute.cs │ │ ├── Module.cs │ │ ├── Parameter.cs │ │ ├── Field.cs │ │ ├── Event.cs │ │ ├── Constructor.cs │ │ ├── Property.cs │ │ ├── Interface.cs │ │ ├── Class.cs │ │ └── Method.cs │ ├── Generators │ │ ├── ICodeGenerator.cs │ │ └── CsharpGenerator.cs │ ├── Extensions │ │ ├── TypeExtensions.cs │ │ ├── ModuleExtensions.cs │ │ ├── FieldExtensions.cs │ │ ├── ParameterExtensions.cs │ │ ├── EventExtensions.cs │ │ ├── ConstructorExtensions.cs │ │ ├── PropertyExtensions.cs │ │ ├── InterfaceExtension.cs │ │ ├── MethodExtensions.cs │ │ └── ClassExtensions.cs │ └── Code.Builders.csproj ├── CodeBuilder.Sample │ ├── Code.Builders.Sample.csproj │ └── Program.cs └── Code.Builders.sln ├── LICENSE ├── .gitignore └── README.md /src/CodeBuilder/Entities/Types/IType.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public interface IType 4 | { 5 | string Name { get; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Instructions/IInstruction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace CodeBuilder 3 | { 4 | public interface IInstruction 5 | { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Modifiers/SyncModifier.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public enum SyncModifier 4 | { 5 | Synchronous, 6 | Asynchronous, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/CodeBuilder/Generators/ICodeGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace CodeBuilder 3 | { 4 | public interface ICodeGenerator 5 | { 6 | string Generate(Module module); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Modifiers/ImplementationModifier.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public enum ImplementationModifier 4 | { 5 | SingleFile, 6 | MultiFiles, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Modifiers/ScopeModifier.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public enum ScopeModifier 4 | { 5 | Instance, 6 | Static, 7 | Extension, 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Modifiers/OverrideModifier.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public enum OverrideModifier 4 | { 5 | None, 6 | Virtual, 7 | Override, 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Modifiers/AccessModifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace CodeBuilder 3 | { 4 | public enum AccessModifier 5 | { 6 | Public, 7 | Private, 8 | Protected, 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Instructions/Statement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace CodeBuilder 3 | { 4 | public class Statement : IInstruction 5 | { 6 | public Statement(string content) 7 | { 8 | this.Content = content; 9 | } 10 | 11 | public string Content { get; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/TypeExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public static class TypeExtensions 4 | { 5 | public static IType ToBuilder(this System.Type clr) 6 | { 7 | var module = new Module(clr.Namespace); 8 | return new Type(module, clr.Name); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/CodeBuilder.Sample/Code.Builders.Sample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Exe 9 | netcoreapp3.0 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Instructions/Block.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace CodeBuilder 3 | { 4 | public class Block : IInstruction 5 | { 6 | public Block(params IInstruction[] instructions) 7 | { 8 | this.Instructions = instructions ?? new IInstruction[0]; 9 | } 10 | 11 | public IInstruction[] Instructions { get; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Body.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Body 4 | { 5 | public Body(IInstruction root = null) 6 | { 7 | this.Root = root; 8 | } 9 | 10 | public IInstruction Root { get; } 11 | 12 | public static Body Auto = new Body(); 13 | 14 | public static Body None = new Body(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Attribute.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Attribute 4 | { 5 | public Attribute(IType type, string[] arguments = null) 6 | { 7 | this.Type = type; 8 | this.Arguments = arguments ?? new string[0]; 9 | } 10 | 11 | public IType Type { get; } 12 | 13 | public string[] Arguments { get; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Types/BaseType.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace CodeBuilder 3 | { 4 | public class BaseType : IType 5 | { 6 | public BaseType(string name) 7 | { 8 | this.Name = name; 9 | } 10 | 11 | public string Name { get; } 12 | 13 | public override string ToString() 14 | { 15 | return this.Name; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Types/Type.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Type : BaseType 4 | { 5 | public Type(Module module, string name) : base(name) 6 | { 7 | this.Module = module; 8 | } 9 | 10 | public Module Module { get; } 11 | 12 | public string Fullname => $"{this.Module.Name}.{this.Name}"; 13 | 14 | public override string ToString() 15 | { 16 | return this.Fullname; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Module.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Module 4 | { 5 | public Module(string name, IType[] types = null, Module[] imports = null) 6 | { 7 | this.Name = name; 8 | this.Types = types ?? new IType[0]; 9 | this.Imports = imports ?? new Module[0]; 10 | } 11 | 12 | public string Name { get; } 13 | 14 | public IType[] Types { get; } 15 | 16 | public Module[] Imports { get; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Types/Modules.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace CodeBuilder 3 | { 4 | public static class Modules 5 | { 6 | public static readonly Module System = new Module("System"); 7 | 8 | public static readonly Module Threading = new Module("System.Threading"); 9 | 10 | public static readonly Module ThreadingTasks = new Module("System.Threading.Tasks"); 11 | 12 | public static readonly Module CollectionsGeneric = new Module("System.Collections.Generic"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Types/GenericType.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | using System.Linq; 4 | 5 | public class GenericType : Type 6 | { 7 | public GenericType(Module module, string name, params IType[] parameters) : base(module,name) 8 | { 9 | this.Parameters = parameters; 10 | } 11 | 12 | public IType[] Parameters { get; } 13 | 14 | public override string ToString() 15 | { 16 | return base.ToString() + "<" + string.Join(", ", this.Parameters.Select(p => p.ToString())) + ">"; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Parameter.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Parameter 4 | { 5 | public Parameter(string name, IType type, string documentation = null, Attribute[] attributes = null) 6 | { 7 | this.Type = type; 8 | this.Name = name; 9 | this.Documentation = documentation; 10 | this.Attributes = attributes ?? new Attribute[0]; 11 | } 12 | 13 | public Attribute[] Attributes { get; } 14 | 15 | public string Name { get; } 16 | 17 | public string Documentation { get; } 18 | 19 | public IType Type { get; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Field.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Field 4 | { 5 | public Field(string name, IType type, bool isReadonly = false, ScopeModifier scope = ScopeModifier.Instance, AccessModifier access = AccessModifier.Private) 6 | { 7 | this.Type = type; 8 | this.Name = name; 9 | this.IsReadonly = isReadonly; 10 | this.Scope = scope; 11 | this.Access = access; 12 | } 13 | 14 | public string Name { get; } 15 | 16 | public bool IsReadonly { get; } 17 | 18 | public IType Type { get; } 19 | 20 | public ScopeModifier Scope { get; } 21 | 22 | public AccessModifier Access { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Event.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Event 4 | { 5 | public Event(string name, string documentation = null, IType handlerType = null, ScopeModifier scope = ScopeModifier.Instance, AccessModifier access = AccessModifier.Public) 6 | { 7 | this.HandlerType = handlerType ?? new Type(Modules.System, "EventHandler"); 8 | this.Name = name; 9 | this.Documentation = documentation; 10 | this.Scope = scope; 11 | this.Access = access; 12 | } 13 | 14 | public string Name { get; } 15 | 16 | public string Documentation { get; } 17 | 18 | public IType HandlerType { get; } 19 | 20 | public ScopeModifier Scope { get; } 21 | 22 | public AccessModifier Access { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Constructor.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Constructor 4 | { 5 | public Constructor(string documentation = null, AccessModifier access = AccessModifier.Public, Parameter[] parameters = null, Body body = null, string[] baseInitializers = null) 6 | { 7 | this.Documentation = documentation; 8 | this.Parameters = parameters ?? new Parameter[0]; 9 | this.BaseInitializers = baseInitializers ?? new string[0]; 10 | this.Body = body; 11 | this.Access = access; 12 | } 13 | 14 | public AccessModifier Access { get; } 15 | 16 | public string Documentation { get; } 17 | 18 | public Parameter[] Parameters { get; } 19 | 20 | public string[] BaseInitializers { get; } 21 | 22 | public Body Body { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Property.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Property 4 | { 5 | public Property(string name, IType type, ScopeModifier scope = ScopeModifier.Instance, AccessModifier access = AccessModifier.Public, string documentation = null, Body getter = null, Body setter = null) 6 | { 7 | this.Type = type; 8 | this.Name = name; 9 | this.Scope = scope; 10 | this.Access = access; 11 | this.Documentation = documentation; 12 | this.Getter = getter; 13 | this.Setter = setter; 14 | } 15 | 16 | public string Name { get; } 17 | 18 | public ScopeModifier Scope { get; } 19 | 20 | public AccessModifier Access { get; } 21 | 22 | public string Documentation { get; } 23 | 24 | public IType Type { get; } 25 | 26 | public Body Getter { get; } 27 | 28 | public Body Setter { get; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Aloïs Deniel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/CodeBuilder/Code.Builders.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | netstandard2.0 6 | $(AssemblyName) ($(TargetFramework)) 7 | 1.0.0.0 8 | 1.0.0.0 9 | 0.5.9.0 10 | 0.5.9.0 11 | Aloïs Deniel 12 | Adeniel.Code.Builders 13 | true 14 | https://raw.githubusercontent.com/aloisdeniel/CodeBuilder/master/Documentation/Logo.png 15 | en 16 | https://github.com/aloisdeniel/CodeBuilder/blob/master/LICENSE 17 | Aloïs Deniel 18 | https://github.com/aloisdeniel/CodeBuilder 19 | A set of helper classes for generating code. 20 | code, build, csharp 21 | Code.Builders 22 | A set of helper classes for generating code. 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Interface.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Interface : IType 4 | { 5 | public Interface(string name, string documentation = null, AccessModifier access = AccessModifier.Public, Event[] events = null, Property[] properties = null, Method[] methods = null, IType[] interfaces = null, Attribute[] attributes = null) 6 | { 7 | this.Name = name; 8 | this.Access = access; 9 | this.Documentation = documentation; 10 | this.Interfaces = interfaces ?? new IType[0]; 11 | this.Properties = properties ?? new Property[0]; 12 | this.Attributes = attributes ?? new Attribute[0]; 13 | this.Methods = methods ?? new Method[0]; 14 | this.Events = events ?? new Event[0]; 15 | } 16 | 17 | public Attribute[] Attributes { get; } 18 | 19 | public string Name { get; } 20 | 21 | public string Documentation { get; } 22 | 23 | public AccessModifier Access { get; } 24 | 25 | public Event[] Events { get; } 26 | 27 | public IType[] Interfaces { get; } 28 | 29 | public Property[] Properties { get; } 30 | 31 | public Method[] Methods { get; } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Class.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Class : Interface 4 | { 5 | public Class(string name, string documentation = null, ScopeModifier scope = ScopeModifier.Instance, AccessModifier access = AccessModifier.Public, ImplementationModifier implementation = ImplementationModifier.SingleFile, IType parent = null, Constructor[] constructors = null, Field[] fields = null, Event[] events = null, Property[] properties = null, IType[] innerTypes = null, Method[] methods = null, IType[] interfaces = null, Attribute[] attributes = null) : base(name, documentation, access, events, properties, methods, interfaces, attributes) 6 | { 7 | this.Parent = parent; 8 | this.Fields = fields ?? new Field[0]; 9 | this.Scope = scope; 10 | this.Implementation = implementation; 11 | this.Constructors = constructors ?? new Constructor[0]; 12 | this.InnerTypes = innerTypes ?? new Class[0]; 13 | } 14 | 15 | public IType Parent { get; } 16 | 17 | public Constructor[] Constructors { get; } 18 | 19 | public Field[] Fields { get; } 20 | 21 | public IType[] InnerTypes { get; } 22 | 23 | public ScopeModifier Scope { get; } 24 | 25 | public ImplementationModifier Implementation { get; } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/ModuleExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace CodeBuilder 5 | { 6 | public static class ModuleExtensions 7 | { 8 | public static Module WithTypes(this Module @this, params IType[] types) => new Module(@this.Name, @this.Types.Concat(types).ToArray(), @this.Imports); 9 | 10 | public static Module WithClass(this Module @this, string name, Func initializer = null) 11 | { 12 | var newClass = new Class(@this.Name, name); 13 | initializer?.Invoke(newClass); 14 | return new Module(@this.Name, @this.Types.Concat(new [] { newClass }).ToArray(), @this.Imports); 15 | } 16 | 17 | public static Module WithImport(this Module @this, Module module) 18 | { 19 | return new Module(@this.Name, @this.Types, @this.Imports.Concat(new[] { module }).ToArray()); 20 | } 21 | 22 | public static Module WithImport(this Module @this, string module) 23 | { 24 | return new Module(@this.Name, @this.Types, @this.Imports.Concat(new[] { new Module(module) }).ToArray()); 25 | } 26 | 27 | public static Module WithImport(this Module @this) 28 | { 29 | return new Module(@this.Name, @this.Types, @this.Imports.Concat(new[] { new Module(typeof(T).Namespace) }).ToArray()); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Code.Builders.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Code.Builders", "CodeBuilder\Code.Builders.csproj", "{EBBB1CFC-CEB3-44F3-83CD-67BDC181E3A9}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Code.Builders.Sample", "CodeBuilder.Sample\Code.Builders.Sample.csproj", "{24ADEB7C-916A-48BC-86F0-4917BABAA57E}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {EBBB1CFC-CEB3-44F3-83CD-67BDC181E3A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {EBBB1CFC-CEB3-44F3-83CD-67BDC181E3A9}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {EBBB1CFC-CEB3-44F3-83CD-67BDC181E3A9}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {EBBB1CFC-CEB3-44F3-83CD-67BDC181E3A9}.Release|Any CPU.Build.0 = Release|Any CPU 18 | {24ADEB7C-916A-48BC-86F0-4917BABAA57E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {24ADEB7C-916A-48BC-86F0-4917BABAA57E}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {24ADEB7C-916A-48BC-86F0-4917BABAA57E}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {24ADEB7C-916A-48BC-86F0-4917BABAA57E}.Release|Any CPU.Build.0 = Release|Any CPU 22 | EndGlobalSection 23 | EndGlobal 24 | -------------------------------------------------------------------------------- /src/CodeBuilder/Entities/Method.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public class Method 4 | { 5 | public Method(string name, string documentation = null, ScopeModifier scope = ScopeModifier.Instance, OverrideModifier @override = OverrideModifier.None, AccessModifier access = AccessModifier.Public, ImplementationModifier implementation = ImplementationModifier.SingleFile, SyncModifier async = SyncModifier.Synchronous, IType returnType = null, Parameter[] parameters = null, Body body = null, Attribute[] attributes = null) 6 | { 7 | this.Name = name; 8 | this.Documentation = documentation; 9 | this.Parameters = parameters ?? new Parameter[0]; 10 | this.Attributes = attributes ?? new Attribute[0]; 11 | this.Body = body; 12 | this.ReturnType = returnType; 13 | this.Sync = async; 14 | this.Override = @override; 15 | this.Scope = scope; 16 | this.Access = access; 17 | this.Implementation = implementation; 18 | } 19 | 20 | public Attribute[] Attributes { get; } 21 | 22 | public IType ReturnType { get; } 23 | 24 | public ScopeModifier Scope { get; } 25 | 26 | public AccessModifier Access { get; } 27 | 28 | public SyncModifier Sync { get; } 29 | 30 | public OverrideModifier Override { get; } 31 | 32 | public ImplementationModifier Implementation { get; } 33 | 34 | public string Name { get; } 35 | 36 | public string Documentation { get; } 37 | 38 | public Parameter[] Parameters { get; } 39 | 40 | public Body Body { get; } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/FieldExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public static class FieldExtensions 4 | { 5 | public static Field WithScope(this Field @this, ScopeModifier value) => new Field(@this.Name, 6 | @this.Type, 7 | isReadonly: @this.IsReadonly, 8 | scope: value, 9 | access: @this.Access); 10 | 11 | public static Field WithAccess(this Field @this, AccessModifier value) => new Field(@this.Name, 12 | @this.Type, 13 | isReadonly: @this.IsReadonly, 14 | scope: @this.Scope, 15 | access: value); 16 | 17 | public static Field WithType(this Field @this, IType value) => new Field(@this.Name, 18 | value, 19 | isReadonly: @this.IsReadonly, 20 | scope: @this.Scope, 21 | access: @this.Access); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/ParameterExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace CodeBuilder 4 | { 5 | public static class ParameterExtensions 6 | { 7 | public static Parameter WithName(this Parameter @this, string value) => new Parameter(value, 8 | @this.Type, 9 | documentation: @this.Documentation, 10 | attributes: @this.Attributes); 11 | 12 | public static Parameter WithType(this Parameter @this, IType value) => new Parameter(@this.Name, 13 | value, 14 | documentation: @this.Documentation, 15 | attributes: @this.Attributes); 16 | 17 | public static Parameter WithDocumentation(this Parameter @this, string value) => new Parameter(@this.Name, 18 | @this.Type, 19 | documentation:value, 20 | attributes: @this.Attributes); 21 | 22 | public static Parameter WithAttribute(this Parameter @this, IType type, params string[] arguments) => new Parameter(@this.Name, 23 | @this.Type, 24 | documentation:@this.Documentation, 25 | attributes: @this.Attributes.Concat(new[] { new Attribute(type, arguments) }).ToArray()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/EventExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public static class EventExtensions 4 | { 5 | public static Event WithDocumentation(this Event @this, string value) => new Event(@this.Name, 6 | handlerType: @this.HandlerType, 7 | documentation: value, 8 | scope: @this.Scope, 9 | access: @this.Access); 10 | 11 | public static Event WithScope(this Event @this, ScopeModifier value) => new Event(@this.Name, 12 | handlerType: @this.HandlerType, 13 | documentation: @this.Documentation, 14 | scope: value, 15 | access: @this.Access); 16 | 17 | public static Event WithAccess(this Event @this, AccessModifier value) => new Event(@this.Name, 18 | handlerType: @this.HandlerType, 19 | documentation: @this.Documentation, 20 | scope: @this.Scope, 21 | access: value); 22 | 23 | public static Event WithHandlerType(this Event @this, IType value) => new Event(@this.Name, 24 | handlerType: value, 25 | documentation: @this.Documentation, 26 | scope: @this.Scope, 27 | access: @this.Access); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/CodeBuilder.Sample/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Threading.Tasks; 4 | using System.Threading; 5 | 6 | namespace CodeBuilder.Sample 7 | { 8 | class MainClass 9 | { 10 | public static void Main(string[] args) 11 | { 12 | var iViewModel = new Interface("IViewModel") 13 | .WithInterface() 14 | .WithAttribute(new Type(new Module("Test"), "Example")) 15 | .WithAttribute(new Type(new Module("Other"), "Sample"), "arg1", "arg2") 16 | .WithProperty("Test", x => x.WithDocumentation("A test property.")) 17 | .WithProperty("Test2", x => x.WithDocumentation("A test property.")) 18 | .WithEvent("Updated", initializer: x => x.WithDocumentation("When updated")) 19 | .WithEvent("Updated2") 20 | .WithMethod("UpdateAsync", x => 21 | { 22 | return x.WithParameter("date", p => p.WithDocumentation("The last updated date") 23 | .WithAttribute(new Type(new Module("Serializers"), "Body"))) 24 | .WithParameter("token") 25 | .WithAttribute(new Type(new Module("Test"), "Example")) 26 | .WithDocumentation("Updates the current state of the view model."); 27 | }); 28 | 29 | var sampleViewModel = new Class("SampleViewModel") 30 | .WithConstructor(x => x.WithBaseInitializers("test", "0").WithParameter("test").WithBody(new Statement("this.test = test;"))) 31 | .WithInterface(iViewModel) 32 | .WithField("test2") 33 | .WithField("test") 34 | .WithEvent("PropertyChanged") 35 | .WithEvent("Updated", initializer: x => x.WithDocumentation("When updated")) 36 | .WithAutoProperty("AutoProperty") 37 | .WithProperty("Test", x => 38 | { 39 | return x.WithFieldGetter("test") 40 | .WithSetter(new Block(new Statement("this.test = value;"))) 41 | .WithDocumentation("A test property."); 42 | }) 43 | .WithAsyncMethod("UpdateAsync", x => 44 | { 45 | return x.WithParameter("date") 46 | .WithParameter("token") 47 | .WithDocumentation("Updates the current state of the view model.") 48 | .WithBody(new Block(new Statement("await Task.Delay(3);"), new Statement("this.Updated?.Invoke(this, System.EventArgs.Empty);"))); 49 | }); 50 | 51 | var module = new Module("CodeBuilder.Sample").WithTypes(iViewModel, sampleViewModel) 52 | .WithImport(Modules.ThreadingTasks) 53 | .WithImport("System.ComponentModel"); 54 | 55 | var generator = new CsharpGenerator(); 56 | 57 | Console.WriteLine(generator.Generate(module)); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/ConstructorExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace CodeBuilder 5 | { 6 | public static class ConstructorExtensions 7 | { 8 | public static Constructor WithDocumentation(this Constructor @this, string value) => new Constructor( 9 | documentation: value, 10 | access: @this.Access, 11 | parameters: @this.Parameters, 12 | baseInitializers: @this.BaseInitializers, 13 | body: @this.Body); 14 | 15 | public static Constructor WithAccess(this Constructor @this, AccessModifier value) => new Constructor( 16 | documentation: @this.Documentation, 17 | access: value, 18 | parameters: @this.Parameters, 19 | baseInitializers: @this.BaseInitializers, 20 | body: @this.Body); 21 | 22 | public static Constructor WithParameter(this Constructor @this, Parameter value) => new Constructor( 23 | documentation: @this.Documentation, 24 | access: @this.Access, 25 | parameters: @this.Parameters.Concat(new[] { value }).ToArray(), 26 | baseInitializers: @this.BaseInitializers, 27 | body: @this.Body); 28 | 29 | public static Constructor WithBody(this Constructor @this, IInstruction value) => new Constructor( 30 | documentation: @this.Documentation, 31 | access: @this.Access, 32 | parameters: @this.Parameters, 33 | baseInitializers: @this.BaseInitializers, 34 | body: new Body(value)); 35 | 36 | public static Constructor WithBaseInitializers(this Constructor @this, params string[] value) => new Constructor( 37 | documentation: @this.Documentation, 38 | access: @this.Access, 39 | parameters: @this.Parameters, 40 | baseInitializers: @this.BaseInitializers.Concat(value).ToArray(), 41 | body: @this.Body); 42 | 43 | 44 | public static Constructor WithParameter(this Constructor @this, string name, IType type, Func initializer = null) 45 | { 46 | var parameter = new Parameter(name, type); 47 | if (initializer != null) 48 | { 49 | parameter = initializer(parameter); 50 | } 51 | return @this.WithParameter(parameter); 52 | } 53 | 54 | public static Constructor WithParameter(this Constructor @this, string name, Func initializer = null) 55 | { 56 | return @this.WithParameter(name, typeof(T).ToBuilder(), initializer); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/PropertyExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace CodeBuilder 2 | { 3 | public static class PropertyExtensions 4 | { 5 | public static Property WithDocumentation(this Property @this, string value) => new Property(@this.Name, 6 | @this.Type, 7 | documentation: value, 8 | getter: @this.Getter, 9 | setter: @this.Setter, 10 | scope: @this.Scope, 11 | access: @this.Access); 12 | 13 | public static Property WithScope(this Property @this, ScopeModifier value) => new Property(@this.Name, 14 | @this.Type, 15 | documentation: @this.Documentation, 16 | getter: @this.Getter, 17 | setter: @this.Setter, 18 | scope: value, 19 | access: @this.Access); 20 | 21 | public static Property WithAccess(this Property @this, AccessModifier value) => new Property(@this.Name, 22 | @this.Type, 23 | documentation: @this.Documentation, 24 | getter: @this.Getter, 25 | setter: @this.Setter, 26 | scope: @this.Scope, 27 | access: value); 28 | 29 | public static Property WithType(this Property @this, IType value) => new Property(@this.Name, 30 | value, 31 | documentation: @this.Documentation, 32 | getter: @this.Getter, 33 | setter: @this.Setter, 34 | scope: @this.Scope, 35 | access: @this.Access); 36 | 37 | public static Property WithGetter(this Property @this, Body getter) => new Property(@this.Name, 38 | @this.Type, 39 | documentation: @this.Documentation, 40 | getter: getter, 41 | setter: @this.Setter, 42 | scope: @this.Scope, 43 | access: @this.Access); 44 | 45 | public static Property WithSetter(this Property @this, Body setter) => new Property(@this.Name, 46 | @this.Type, 47 | documentation: @this.Documentation, 48 | getter: @this.Getter, 49 | setter: setter, 50 | scope: @this.Scope, 51 | access: @this.Access); 52 | 53 | public static Property WithAutoGetter(this Property @this) => @this.WithGetter(Body.Auto); 54 | 55 | public static Property WithAutoSetter(this Property @this) => @this.WithSetter(Body.Auto); 56 | 57 | public static Property WithGetter(this Property @this, IInstruction instruction) => @this.WithGetter(new Body(instruction)); 58 | 59 | public static Property WithSetter(this Property @this, IInstruction instruction) => @this.WithSetter(new Body(instruction)); 60 | 61 | public static Property WithFieldGetter(this Property @this, string fieldName) => @this.WithGetter(new Statement($"this.{fieldName};")); 62 | 63 | public static Property WithFieldSetter(this Property @this, string fieldName) => @this.WithSetter(new Statement($"this.{fieldName} = value;")); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code.Builders 2 | 3 | A set of helper classes for generating code. 4 | 5 | ## Install 6 | 7 | Available on NuGet 8 | 9 | [![NuGet](https://img.shields.io/nuget/v/Adeniel.Code.Builders?label=NuGet)](https://www.nuget.org/packages/Adeniel.Code.Builders/) 10 | 11 | ## Quickstart 12 | 13 | ```csharp 14 | var iViewModel = new Interface("IViewModel") 15 | .WithInterface() 16 | .WithProperty("Test", x => x.WithDocumentation("A test property.")) 17 | .WithProperty("Test2", x => x.WithDocumentation("A test property.")) 18 | .WithEvent("Updated", initializer: x => x.WithDocumentation("When updated")) 19 | .WithEvent("Updated2") 20 | .WithMethod("UpdateAsync", x => 21 | { 22 | return x.WithParameter("date", p => p.WithDocumentation("The last updated date")) 23 | .WithParameter("token") 24 | .WithDocumentation("Updates the current state of the view model."); 25 | }); 26 | 27 | var sampleViewModel = new Class("SampleViewModel") 28 | .WithConstructor(x => x.WithParameter("test").WithBody(new Statement("this.test = test;"))) 29 | .WithInterface(iViewModel) 30 | .WithField("test2") 31 | .WithField("test") 32 | .WithEvent("PropertyChanged") 33 | .WithEvent("Updated", initializer: x => x.WithDocumentation("When updated")) 34 | .WithAutoProperty("AutoProperty") 35 | .WithProperty("Test", x => 36 | { 37 | return x.WithFieldGetter("test") 38 | .WithSetter(new Block(new Statement("this.test = value;"))) 39 | .WithDocumentation("A test property."); 40 | }) 41 | .WithAsyncMethod("UpdateAsync", x => 42 | { 43 | return x.WithParameter("date") 44 | .WithParameter("token") 45 | .WithDocumentation("Updates the current state of the view model.") 46 | .WithBody(new Block(new Statement("await Task.Delay(3);"), new Statement("this.Updated?.Invoke(this, System.EventArgs.Empty);"))); 47 | }); 48 | 49 | var module = new Module("CodeBuilder.Sample").WithTypes(iViewModel,sampleViewModel) 50 | .WithImport(Modules.ThreadingTasks) 51 | .WithImport("System.ComponentModel"); 52 | 53 | var generator = new CsharpGenerator(); 54 | 55 | Console.WriteLine(generator.Generate(module)); 56 | ``` 57 | 58 | **Result** 59 | 60 | ```csharp 61 | using System.Threading.Tasks; 62 | using System.ComponentModel; 63 | 64 | namespace CodeBuilder.Sample 65 | { 66 | public interface IViewModel : System.ComponentModel.INotifyPropertyChanged 67 | { 68 | #region Properties 69 | 70 | /// 71 | ///A test property. 72 | /// 73 | System.String Test { get; set; } 74 | 75 | /// 76 | ///A test property. 77 | /// 78 | System.String Test2 { get; set; } 79 | 80 | #endregion 81 | 82 | #region Events 83 | 84 | /// 85 | ///When updated 86 | /// 87 | event System.EventHandler Updated; 88 | 89 | event System.EventHandler Updated2; 90 | 91 | #endregion 92 | 93 | #region Methods 94 | 95 | /// 96 | ///Updates the current state of the view model. 97 | /// 98 | ///The Task result. 99 | ///The last updated date 100 | /// 101 | System.Threading.Tasks.Task UpdateAsync(System.DateTime date, System.Threading.CancellationToken token); 102 | 103 | #endregion 104 | } 105 | 106 | public class SampleViewModel : IViewModel 107 | { 108 | #region Fields 109 | 110 | private System.Boolean test2; 111 | 112 | private System.String test; 113 | 114 | #endregion 115 | 116 | #region Constructors 117 | 118 | public SampleViewModel(System.String test) => this.test = test; 119 | 120 | #endregion 121 | 122 | #region Events 123 | 124 | public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 125 | 126 | /// 127 | ///When updated 128 | /// 129 | public event System.EventHandler Updated; 130 | 131 | #endregion 132 | 133 | #region Properties 134 | 135 | public System.Int32 AutoProperty { get; set; } 136 | 137 | /// 138 | ///A test property. 139 | /// 140 | public System.String Test 141 | { 142 | get => this.test; 143 | 144 | set 145 | { 146 | this.test = value; 147 | } 148 | } 149 | 150 | #endregion 151 | 152 | #region Methods 153 | 154 | /// 155 | ///Updates the current state of the view model. 156 | /// 157 | ///The Task result. 158 | /// 159 | /// 160 | public async System.Threading.Tasks.Task UpdateAsync(System.DateTime date, System.Threading.CancellationToken token) 161 | { 162 | await Task.Delay(3); 163 | this.Updated?.Invoke(this, System.EventArgs.Empty); 164 | } 165 | 166 | #endregion 167 | } 168 | } 169 | ``` 170 | 171 | ## Q&A 172 | 173 | #### Why not using Roslyn? 174 | 175 | > Roselyn is far too complex and heavy for several scenarios where you just want to have basic code generation. This should also allows us to have more target languages easily. 176 | 177 | 178 | ## Contributions 179 | 180 | Contributions are welcome! If you find a bug please report it and if you want a feature please report it. 181 | 182 | If you want to contribute code please file an issue and create a branch off of the current dev branch and file a pull request. 183 | 184 | ## License 185 | 186 | MIT © [Aloïs Deniel](http://aloisdeniel.github.io) 187 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/InterfaceExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace CodeBuilder 5 | { 6 | // TODO create a generator for creating this as soon as the generator is ready 7 | public static class InterfaceExtensions 8 | { 9 | 10 | public static Interface WithName(this Interface @this, string value) => new Interface(value, 11 | documentation: @this.Documentation, 12 | access: @this.Access, 13 | events: @this.Events, 14 | properties: @this.Properties, 15 | methods: @this.Methods, 16 | interfaces: @this.Interfaces, 17 | attributes: @this.Attributes); 18 | 19 | public static Interface WithAccess(this Interface @this, AccessModifier value) => new Interface(@this.Name, 20 | documentation: @this.Documentation, 21 | access: value, 22 | events: @this.Events, 23 | properties: @this.Properties, 24 | methods: @this.Methods, 25 | interfaces: @this.Interfaces, 26 | attributes: @this.Attributes); 27 | 28 | 29 | public static Interface WithProperty(this Interface @this, Property value) => new Interface(@this.Name, 30 | documentation: @this.Documentation, 31 | access: @this.Access, 32 | events: @this.Events, 33 | properties: @this.Properties.Concat(new[] { value }).ToArray(), 34 | methods: @this.Methods, 35 | interfaces: @this.Interfaces, 36 | attributes: @this.Attributes); 37 | 38 | 39 | public static Interface WithEvent(this Interface @this, Event value) => new Interface(@this.Name, 40 | documentation: @this.Documentation, 41 | access: @this.Access, 42 | events: @this.Events.Concat(new[] { value }).ToArray(), 43 | properties: @this.Properties, 44 | methods: @this.Methods, 45 | interfaces: @this.Interfaces, 46 | attributes: @this.Attributes); 47 | 48 | 49 | public static Interface WithMethod(this Interface @this, Method value) => new Interface(@this.Name, 50 | documentation: @this.Documentation, 51 | access: @this.Access, 52 | events: @this.Events, 53 | properties: @this.Properties, 54 | methods: @this.Methods.Concat(new[] { value }).ToArray(), 55 | interfaces: @this.Interfaces, 56 | attributes: @this.Attributes); 57 | 58 | 59 | public static Interface WithInterface(this Interface @this, IType value) => new Interface(@this.Name, 60 | documentation: @this.Documentation, 61 | access: @this.Access, 62 | events: @this.Events, 63 | properties: @this.Properties, 64 | methods: @this.Methods, 65 | interfaces: @this.Interfaces.Concat(new[] { value }).ToArray(), 66 | attributes: @this.Attributes); 67 | 68 | 69 | 70 | public static Interface WithAttribute(this Interface @this, IType type, params String[] arguments) => new Interface(@this.Name, 71 | documentation: @this.Documentation, 72 | access: @this.Access, 73 | events: @this.Events, 74 | properties: @this.Properties, 75 | methods: @this.Methods, 76 | interfaces: @this.Interfaces, 77 | attributes: @this.Attributes.Concat(new[] { new Attribute(type, arguments) }).ToArray()); 78 | 79 | 80 | public static Interface WithProperty(this Interface @this, string name, IType type, Func initializer = null) 81 | { 82 | var property = new Property(name, type, getter: Body.Auto, setter: Body.Auto); 83 | if (initializer != null) 84 | { 85 | property = initializer(property); 86 | } 87 | return @this.WithProperty(property); 88 | } 89 | 90 | public static Interface WithMethod(this Interface @this, string name, IType returnType, Func initializer = null) 91 | { 92 | var method = new Method(name, returnType: returnType); 93 | if (initializer != null) 94 | { 95 | method = initializer(method); 96 | } 97 | return @this.WithMethod(method); 98 | } 99 | 100 | public static Interface WithEvent(this Interface @this, string name, IType handlerType = null, Func initializer = null) 101 | { 102 | var ev = new Event(name, handlerType: handlerType); 103 | if (initializer != null) 104 | { 105 | ev = initializer(ev); 106 | } 107 | return @this.WithEvent(ev); 108 | } 109 | 110 | public static Interface WithInterface(this Interface @this) 111 | { 112 | return @this.WithInterface(typeof(T).ToBuilder()); 113 | } 114 | 115 | public static Interface WithMethod(this Interface @this, string name, Func initializer = null) 116 | { 117 | return @this.WithMethod(name, typeof(T).ToBuilder(), initializer); 118 | } 119 | 120 | public static Interface WithEvent(this Interface @this, string name, Func initializer = null) 121 | { 122 | return @this.WithEvent(name, typeof(T).ToBuilder(), initializer); 123 | } 124 | 125 | public static Interface WithProperty(this Interface @this, string name, Func initializer = null) 126 | { 127 | return @this.WithProperty(name, typeof(T).ToBuilder(), initializer); 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/MethodExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace CodeBuilder 5 | { 6 | public static class MethodExtensions 7 | { 8 | public static Method WithName(this Method @this, string name) => new Method(name, 9 | documentation: @this.Documentation, 10 | returnType: @this.ReturnType, 11 | @override: @this.Override, 12 | access: @this.Access, 13 | scope: @this.Scope, 14 | async: @this.Sync, 15 | implementation: @this.Implementation, 16 | parameters: @this.Parameters, 17 | body: @this.Body, 18 | attributes: @this.Attributes); 19 | 20 | public static Method WithDocumentation(this Method @this, string value) => new Method(@this.Name, 21 | documentation: value, 22 | returnType: @this.ReturnType, 23 | @override: @this.Override, 24 | access: @this.Access, 25 | scope: @this.Scope, 26 | async: @this.Sync, 27 | implementation: @this.Implementation, 28 | parameters: @this.Parameters, 29 | body: @this.Body, 30 | attributes: @this.Attributes); 31 | 32 | public static Method WithScope(this Method @this, ScopeModifier value) => new Method(@this.Name, 33 | documentation: @this.Documentation, 34 | returnType: @this.ReturnType, 35 | @override: @this.Override, 36 | access: @this.Access, 37 | scope: value, 38 | async: @this.Sync, 39 | implementation: @this.Implementation, 40 | parameters: @this.Parameters, 41 | body: @this.Body, 42 | attributes: @this.Attributes); 43 | 44 | public static Method WithSync(this Method @this, SyncModifier value = SyncModifier.Asynchronous) => new Method(@this.Name, 45 | documentation: @this.Documentation, 46 | returnType: @this.ReturnType, 47 | @override: @this.Override, 48 | access: @this.Access, 49 | scope: @this.Scope, 50 | async: value, 51 | implementation: @this.Implementation, 52 | parameters: @this.Parameters, 53 | body: @this.Body, 54 | attributes: @this.Attributes); 55 | 56 | 57 | public static Method WithAccess(this Method @this, AccessModifier value) => new Method(@this.Name, 58 | documentation: @this.Documentation, 59 | returnType: @this.ReturnType, 60 | @override: @this.Override, 61 | access: value, 62 | scope: @this.Scope, 63 | async: @this.Sync, 64 | implementation: @this.Implementation, 65 | parameters: @this.Parameters, 66 | body: @this.Body, 67 | attributes: @this.Attributes); 68 | 69 | 70 | public static Method WithOverride(this Method @this, OverrideModifier value) => new Method(@this.Name, 71 | documentation: @this.Documentation, 72 | returnType: @this.ReturnType, 73 | @override: value, 74 | access: @this.Access, 75 | scope: @this.Scope, 76 | async: @this.Sync, 77 | implementation: @this.Implementation, 78 | parameters: @this.Parameters, 79 | body: @this.Body, 80 | attributes: @this.Attributes); 81 | 82 | 83 | public static Method WithImplementation(this Method @this, ImplementationModifier value) => new Method(@this.Name, 84 | documentation: @this.Documentation, 85 | returnType: @this.ReturnType, 86 | @override: @this.Override, 87 | access: @this.Access, 88 | scope: @this.Scope, 89 | async: @this.Sync, 90 | implementation: value, 91 | parameters: @this.Parameters, 92 | body: @this.Body, 93 | attributes: @this.Attributes); 94 | 95 | 96 | public static Method WithParameter(this Method @this, Parameter value) => new Method(@this.Name, 97 | documentation: @this.Documentation, 98 | returnType: @this.ReturnType, 99 | @override: @this.Override, 100 | access: @this.Access, 101 | scope: @this.Scope, 102 | async: @this.Sync, 103 | implementation: @this.Implementation, 104 | parameters: @this.Parameters.Concat(new[] { value }).ToArray(), 105 | body: @this.Body, 106 | attributes: @this.Attributes); 107 | 108 | public static Method WithBody(this Method @this, IInstruction value) => new Method(@this.Name, 109 | documentation: @this.Documentation, 110 | returnType: @this.ReturnType, 111 | @override: @this.Override, 112 | access: @this.Access, 113 | scope: @this.Scope, 114 | async: @this.Sync, 115 | implementation: @this.Implementation, 116 | parameters: @this.Parameters, 117 | body: new Body(value), 118 | attributes: @this.Attributes); 119 | 120 | public static Method WithAttribute(this Method @this, IType type, params string[] arguments) => new Method(@this.Name, 121 | documentation: @this.Documentation, 122 | returnType: @this.ReturnType, 123 | @override: @this.Override, 124 | access: @this.Access, 125 | scope: @this.Scope, 126 | async: @this.Sync, 127 | implementation: @this.Implementation, 128 | parameters: @this.Parameters, 129 | body: @this.Body, 130 | attributes: @this.Attributes.Concat(new[] { new Attribute(type, arguments) }).ToArray()); 131 | 132 | 133 | public static Method WithParameter(this Method @this, string name, IType type, Func initializer = null) 134 | { 135 | var parameter = new Parameter(name, type); 136 | if (initializer != null) 137 | { 138 | parameter = initializer(parameter); 139 | } 140 | return @this.WithParameter(parameter); 141 | } 142 | 143 | public static Method WithParameter(this Method @this, string name, Func initializer = null) 144 | { 145 | return @this.WithParameter(name, typeof(T).ToBuilder(), initializer); 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/CodeBuilder/Extensions/ClassExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace CodeBuilder 5 | { 6 | // TODO create a generator for creating this as soon as the generator is ready 7 | public static class ClassExtensions 8 | { 9 | 10 | public static Class WithName(this Class @this, string value) => new Class(value, 11 | documentation: @this.Documentation, 12 | innerTypes: @this.InnerTypes, 13 | scope: @this.Scope, 14 | access: @this.Access, 15 | implementation: @this.Implementation, 16 | parent: @this.Parent, 17 | constructors: @this.Constructors, 18 | fields: @this.Fields, 19 | events: @this.Events, 20 | properties: @this.Properties, 21 | methods: @this.Methods, 22 | interfaces: @this.Interfaces); 23 | 24 | public static Class WithScope(this Class @this, ScopeModifier value) => new Class(@this.Name, 25 | documentation: @this.Documentation, 26 | innerTypes: @this.InnerTypes, 27 | scope: value, 28 | access: @this.Access, 29 | implementation: @this.Implementation, 30 | parent: @this.Parent, 31 | constructors: @this.Constructors, 32 | fields: @this.Fields, 33 | events: @this.Events, 34 | properties: @this.Properties, 35 | methods: @this.Methods, 36 | interfaces: @this.Interfaces); 37 | 38 | public static Class WithAccess(this Class @this, AccessModifier value) => new Class(@this.Name, 39 | documentation: @this.Documentation, 40 | innerTypes: @this.InnerTypes, 41 | scope: @this.Scope, 42 | access: value, 43 | implementation: @this.Implementation, 44 | parent: @this.Parent, 45 | constructors: @this.Constructors, 46 | fields: @this.Fields, 47 | events: @this.Events, 48 | properties: @this.Properties, 49 | methods: @this.Methods, 50 | interfaces: @this.Interfaces); 51 | 52 | public static Class WithImplementation(this Class @this, ImplementationModifier value) => new Class(@this.Name, 53 | documentation: @this.Documentation, 54 | innerTypes: @this.InnerTypes, 55 | scope: @this.Scope, 56 | access: @this.Access, 57 | implementation: value, 58 | parent: @this.Parent, 59 | constructors: @this.Constructors, 60 | fields: @this.Fields, 61 | events: @this.Events, 62 | properties: @this.Properties, 63 | methods: @this.Methods, 64 | interfaces: @this.Interfaces); 65 | 66 | public static Class WithParent(this Class @this, IType value) => new Class(@this.Name, 67 | documentation: @this.Documentation, 68 | innerTypes: @this.InnerTypes, 69 | scope: @this.Scope, 70 | access: @this.Access, 71 | implementation: @this.Implementation, 72 | parent: value, 73 | constructors: @this.Constructors, 74 | fields: @this.Fields, 75 | events: @this.Events, 76 | properties: @this.Properties, 77 | methods: @this.Methods, 78 | interfaces: @this.Interfaces); 79 | 80 | 81 | public static Class WithProperty(this Class @this, Property value) => new Class(@this.Name, 82 | documentation: @this.Documentation, 83 | innerTypes: @this.InnerTypes, 84 | scope: @this.Scope, 85 | access: @this.Access, 86 | implementation: @this.Implementation, 87 | parent: @this.Parent, 88 | constructors: @this.Constructors, 89 | fields: @this.Fields, 90 | events: @this.Events, 91 | properties: @this.Properties.Concat(new[] { value }).ToArray(), 92 | methods: @this.Methods, 93 | interfaces: @this.Interfaces); 94 | 95 | 96 | public static Class WithConstructor(this Class @this, Constructor value) => new Class(@this.Name, 97 | documentation: @this.Documentation, 98 | innerTypes: @this.InnerTypes, 99 | scope: @this.Scope, 100 | access: @this.Access, 101 | implementation: @this.Implementation, 102 | parent: @this.Parent, 103 | constructors: @this.Constructors.Concat(new[] { value }).ToArray(), 104 | fields: @this.Fields, 105 | events: @this.Events, 106 | properties: @this.Properties, 107 | methods: @this.Methods, 108 | interfaces: @this.Interfaces); 109 | 110 | 111 | public static Class WithField(this Class @this, Field value) => new Class(@this.Name, 112 | documentation: @this.Documentation, 113 | innerTypes: @this.InnerTypes, 114 | scope: @this.Scope, 115 | access: @this.Access, 116 | implementation: @this.Implementation, 117 | parent: @this.Parent, 118 | constructors: @this.Constructors, 119 | fields: @this.Fields.Concat(new[] { value }).ToArray(), 120 | events: @this.Events, 121 | properties: @this.Properties, 122 | methods: @this.Methods, 123 | interfaces: @this.Interfaces); 124 | 125 | 126 | public static Class WithInnerType(this Class @this, Class value) => new Class(@this.Name, 127 | documentation: @this.Documentation, 128 | innerTypes: @this.InnerTypes.Concat(new[] { value }).ToArray(), 129 | scope: @this.Scope, 130 | access: @this.Access, 131 | implementation: @this.Implementation, 132 | parent: @this.Parent, 133 | constructors: @this.Constructors, 134 | fields: @this.Fields, 135 | events: @this.Events, 136 | properties: @this.Properties, 137 | methods: @this.Methods, 138 | interfaces: @this.Interfaces); 139 | 140 | public static Class WithEvent(this Class @this, Event value) => new Class(@this.Name, 141 | documentation: @this.Documentation, 142 | innerTypes: @this.InnerTypes, 143 | scope: @this.Scope, 144 | access: @this.Access, 145 | implementation: @this.Implementation, 146 | parent: @this.Parent, 147 | constructors: @this.Constructors, 148 | fields: @this.Fields, 149 | events: @this.Events.Concat(new[] { value }).ToArray(), 150 | properties: @this.Properties, 151 | methods: @this.Methods, 152 | interfaces: @this.Interfaces); 153 | 154 | 155 | public static Class WithMethod(this Class @this, Method value) => new Class(@this.Name, 156 | documentation: @this.Documentation, 157 | innerTypes: @this.InnerTypes, 158 | scope: @this.Scope, 159 | access: @this.Access, 160 | implementation: @this.Implementation, 161 | parent: @this.Parent, 162 | constructors: @this.Constructors, 163 | fields: @this.Fields, 164 | events: @this.Events, 165 | properties: @this.Properties, 166 | methods: @this.Methods.Concat(new[] { value }).ToArray(), 167 | interfaces: @this.Interfaces); 168 | 169 | 170 | public static Class WithInterface(this Class @this, IType value) => new Class(@this.Name, 171 | documentation: @this.Documentation, 172 | innerTypes: @this.InnerTypes, 173 | scope: @this.Scope, 174 | access: @this.Access, 175 | implementation: @this.Implementation, 176 | parent: @this.Parent, 177 | constructors: @this.Constructors, 178 | fields: @this.Fields, 179 | events: @this.Events, 180 | properties: @this.Properties, 181 | methods: @this.Methods, 182 | interfaces: @this.Interfaces.Concat(new[] { value }).ToArray()); 183 | 184 | public static Class WithProperty(this Class @this, string name, IType type, Func initializer = null) 185 | { 186 | var property = new Property(name, type); 187 | if (initializer != null) 188 | { 189 | property = initializer(property); 190 | } 191 | return @this.WithProperty(property); 192 | } 193 | 194 | public static Class WithField(this Class @this, string name, IType type, Func initializer = null) 195 | { 196 | var field = new Field(name, type); 197 | if (initializer != null) 198 | { 199 | field = initializer(field); 200 | } 201 | return @this.WithField(field); 202 | } 203 | 204 | public static Class WithConstructor(this Class @this, Func initializer = null) 205 | { 206 | var constructor = new Constructor(); 207 | if (initializer != null) 208 | { 209 | constructor = initializer(constructor); 210 | } 211 | return @this.WithConstructor(constructor); 212 | } 213 | 214 | public static Class WithEvent(this Class @this, string name, IType type = null, Func initializer = null) 215 | { 216 | var field = new Event(name, handlerType: type); 217 | if (initializer != null) 218 | { 219 | field = initializer(field); 220 | } 221 | return @this.WithEvent(field); 222 | } 223 | 224 | public static Class WithMethod(this Class @this, string name, IType returnType, Func initializer = null) 225 | { 226 | var method = new Method(name, returnType: returnType); 227 | if (initializer != null) 228 | { 229 | method = initializer(method); 230 | } 231 | return @this.WithMethod(method); 232 | } 233 | 234 | public static Class WithAsyncMethod(this Class @this, string name, IType returnType, Func initializer = null) 235 | { 236 | var method = new Method(name, returnType: returnType, async: SyncModifier.Asynchronous); 237 | if (initializer != null) 238 | { 239 | method = initializer(method); 240 | } 241 | return @this.WithMethod(method); 242 | } 243 | 244 | public static Class WithMethod(this Class @this, string name, Func initializer = null) 245 | { 246 | return @this.WithMethod(name, typeof(T).ToBuilder(), initializer); 247 | } 248 | 249 | public static Class WithAsyncMethod(this Class @this, string name, Func initializer = null) 250 | { 251 | return @this.WithAsyncMethod(name, typeof(T).ToBuilder(), initializer); 252 | } 253 | 254 | public static Class WithEvent(this Class @this, string name, Func initializer = null) 255 | { 256 | return @this.WithEvent(name, typeof(T).ToBuilder(), initializer); 257 | } 258 | 259 | public static Class WithField(this Class @this, string name, Func initializer = null) 260 | { 261 | return @this.WithField(name, typeof(T).ToBuilder(), initializer); 262 | } 263 | 264 | public static Class WithProperty(this Class @this, string name, Func initializer = null) 265 | { 266 | return @this.WithProperty(name, typeof(T).ToBuilder(), initializer); 267 | } 268 | 269 | public static Class WithAutoProperty(this Class @this, string name, Func initializer = null) 270 | { 271 | return @this.WithProperty(name, typeof(T).ToBuilder(), (p) => initializer == null ? p.WithAutoGetter().WithAutoSetter() : initializer(p).WithAutoGetter().WithAutoSetter()); 272 | } 273 | 274 | public static Class WithInterface(this Class @this) 275 | { 276 | return @this.WithInterface(typeof(T).ToBuilder()); 277 | } 278 | 279 | public static Class WithParent(this Class @this) 280 | { 281 | return @this.WithParent(typeof(T).ToBuilder()); 282 | } 283 | } 284 | } 285 | -------------------------------------------------------------------------------- /src/CodeBuilder/Generators/CsharpGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using System.Linq; 4 | namespace CodeBuilder 5 | { 6 | public class CsharpGenerator : ICodeGenerator 7 | { 8 | public int Indentation { get; set; } = 4; 9 | 10 | public bool UsingInsideNamespace { get; set; } = false; 11 | 12 | private int scope; 13 | 14 | private StringBuilder builder; 15 | 16 | public string Generate(Module module) 17 | { 18 | scope = 0; 19 | builder = new StringBuilder(); 20 | 21 | if (!this.UsingInsideNamespace) 22 | { 23 | AppendImports(module.Imports); 24 | } 25 | 26 | this.AppendLine($"namespace {module.Name}"); 27 | this.AppendBlock(() => 28 | { 29 | if (this.UsingInsideNamespace) 30 | { 31 | AppendImports(module.Imports); 32 | } 33 | 34 | for (int i = 0; i < module.Types.Length; i++) 35 | { 36 | if (i > 0) this.NewLine(); 37 | var type = module.Types[i]; 38 | switch (type) 39 | { 40 | case Class c: 41 | this.Append(c); 42 | break; 43 | case Interface interf: 44 | this.Append(interf); 45 | break; 46 | } 47 | } 48 | }); 49 | 50 | return builder.ToString(); 51 | 52 | } 53 | 54 | private void AppendImports(Module[] imports) 55 | { 56 | if (imports.Any()) 57 | { 58 | foreach (var import in imports) 59 | { 60 | this.AppendLine("using " + import.Name + ";"); 61 | } 62 | this.NewLine(); 63 | } 64 | } 65 | 66 | private void Append(IType type) 67 | { 68 | switch (type) 69 | { 70 | case Class c: 71 | this.Append(c); 72 | break; 73 | case Interface i: 74 | this.Append(i); 75 | break; 76 | default: 77 | break; 78 | } 79 | } 80 | 81 | private void Append(Interface interf) 82 | { 83 | this.AppendDocumentationSummary(interf.Documentation); 84 | 85 | foreach (var attribute in interf.Attributes) 86 | { 87 | this.Scope().AppendAttribute(attribute).NewLine(); 88 | } 89 | 90 | this.Scope().Append($"public interface {interf.Name}"); 91 | 92 | if (interf.Interfaces.Any()) 93 | { 94 | this.Append(" : "); 95 | for (int i = 0; i < interf.Interfaces.Length; i++) 96 | { 97 | if (i > 0) this.Append(", "); 98 | var parent = this.GetFullname(interf.Interfaces[i]); 99 | this.Append(parent); 100 | } 101 | } 102 | 103 | this.NewLine(); 104 | 105 | this.AppendBlock(() => 106 | { 107 | var publicInstanceMethods = interf.Methods.Where(x => x.Scope == ScopeModifier.Instance && x.Access == AccessModifier.Public).ToArray(); 108 | 109 | var hasEvents = interf.Events.Any(); 110 | var hasProperties = interf.Properties.Any(); 111 | var hasMethods = publicInstanceMethods.Any(); 112 | 113 | if (hasProperties) 114 | { 115 | this.AppendLine("#region Properties"); 116 | 117 | for (int i = 0; i < interf.Properties.Length; i++) 118 | { 119 | var property = interf.Properties[i]; 120 | var type = this.GetFullname(property.Type); 121 | 122 | this.NewLine(); 123 | this.AppendDocumentationSummary(property.Documentation); 124 | this.Scope().Append(type).Append(" ").Append(property.Name).Append(" {"); 125 | 126 | if (property.Getter != null) 127 | { 128 | this.Append(" get;"); 129 | } 130 | 131 | if (property.Setter != null) 132 | { 133 | this.Append(" set;"); 134 | } 135 | 136 | this.Append(" }"); 137 | this.NewLine(); 138 | } 139 | 140 | this.NewLine(); 141 | 142 | this.AppendLine("#endregion"); 143 | } 144 | 145 | if (hasEvents) 146 | { 147 | if (hasProperties) this.NewLine(); 148 | 149 | this.AppendLine("#region Events"); 150 | 151 | for (int i = 0; i < interf.Events.Length; i++) 152 | { 153 | var ev = interf.Events[i]; 154 | var handlerType = this.GetFullname(ev.HandlerType); 155 | 156 | this.NewLine(); 157 | this.AppendDocumentationSummary(ev.Documentation); 158 | this.Scope().Append("event ").Append(handlerType).Append(" ").Append(ev.Name).Append(";"); 159 | this.NewLine(); 160 | } 161 | 162 | this.NewLine(); 163 | 164 | this.AppendLine("#endregion"); 165 | } 166 | 167 | if (hasMethods) 168 | { 169 | if (hasProperties || hasEvents) this.NewLine(); 170 | 171 | this.AppendLine("#region Methods"); 172 | 173 | for (int i = 0; i < publicInstanceMethods.Length; i++) 174 | { 175 | var method = publicInstanceMethods[i]; 176 | var returnType = this.GetFullname(method.ReturnType); 177 | 178 | this.NewLine(); 179 | 180 | if (!string.IsNullOrEmpty(method.Documentation)) 181 | { 182 | this.AppendDocumentationSummary(method.Documentation); 183 | 184 | if (method.ReturnType != null) 185 | { 186 | this.AppendCommentLine($"The {method.ReturnType.Name} result."); 187 | } 188 | 189 | foreach (var parameter in method.Parameters) 190 | { 191 | this.AppendCommentLine($"{parameter.Documentation}"); 192 | } 193 | } 194 | 195 | foreach (var attribute in method.Attributes) 196 | { 197 | this.Scope().AppendAttribute(attribute).NewLine(); 198 | } 199 | 200 | this.Scope().Append(returnType).Append(" ").Append(method.Name).Append("("); 201 | 202 | for (int pi = 0; pi < method.Parameters.Length; pi++) 203 | { 204 | var parameter = method.Parameters[pi]; 205 | 206 | if (pi > 0) 207 | { 208 | this.Append(", "); 209 | } 210 | else if (method.Scope == ScopeModifier.Extension) 211 | { 212 | this.Append("this "); 213 | } 214 | 215 | foreach (var attribute in parameter.Attributes) 216 | { 217 | this.Scope().AppendAttribute(attribute).NewLine(); 218 | } 219 | 220 | var paramType = this.GetFullname(parameter.Type); 221 | this.Append(paramType).Append(" ").Append(parameter.Name); 222 | } 223 | 224 | this.Append(");"); 225 | this.NewLine(); 226 | } 227 | 228 | this.NewLine(); 229 | 230 | this.AppendLine("#endregion"); 231 | } 232 | 233 | 234 | }); 235 | } 236 | 237 | private void Append(Class @class) 238 | { 239 | this.AppendDocumentationSummary(@class.Documentation); 240 | 241 | this.Scope().Append(this.Get(@class.Access)).Append(" ").Append(this.Get(@class.Implementation)).Append(this.Get(@class.Scope)); 242 | foreach (var attribute in @class.Attributes) 243 | { 244 | this.Scope().AppendAttribute(attribute).NewLine(); 245 | } 246 | 247 | this.Append($"class {@class.Name}"); 248 | 249 | if (@class.Parent != null) 250 | { 251 | this.Append(" : "); 252 | var parent = this.GetFullname(@class.Parent); 253 | this.Append(parent); 254 | } 255 | 256 | if (@class.Interfaces.Any()) 257 | { 258 | if (@class.Parent == null) this.Append(" : "); 259 | for (int i = 0; i < @class.Interfaces.Length; i++) 260 | { 261 | if (i > 0 || @class.Parent != null) this.Append(", "); 262 | var parent = this.GetFullname(@class.Interfaces[i]); 263 | this.Append(parent); 264 | } 265 | } 266 | 267 | this.NewLine(); 268 | 269 | this.AppendBlock(() => 270 | { 271 | var hasConstructors = @class.Constructors.Any(); 272 | var hasFields = @class.Fields.Any(); 273 | var hasEvents = @class.Events.Any(); 274 | var hasProperties = @class.Properties.Any(); 275 | var hasMethods = @class.Methods.Any(); 276 | 277 | if (hasFields) 278 | { 279 | this.AppendLine("#region Fields"); 280 | 281 | for (int i = 0; i < @class.Fields.Length; i++) 282 | { 283 | var field = @class.Fields[i]; 284 | var type = this.GetFullname(field.Type); 285 | 286 | this.NewLine().Scope().Append(this.Get(field.Access)).Append(field.IsReadonly ? " readonly" : "").Append(" ").Append(this.Get(field.Scope)); 287 | this.Append(type).Append(" ").Append(field.Name).Append(";"); 288 | this.NewLine(); 289 | } 290 | 291 | this.NewLine(); 292 | 293 | this.AppendLine("#endregion"); 294 | } 295 | 296 | if (hasConstructors) 297 | { 298 | if (hasFields) this.NewLine(); 299 | 300 | this.AppendLine("#region Constructors"); 301 | 302 | for (int i = 0; i < @class.Constructors.Length; i++) 303 | { 304 | var constructor = @class.Constructors[i]; 305 | 306 | this.AppendMethodComment(constructor.Documentation, null, constructor.Parameters); 307 | this.NewLine().Scope().Append(this.Get(constructor.Access)).Append(" ").Append(@class.Name); 308 | this.AppendParameters(constructor.Parameters); 309 | 310 | if(constructor.BaseInitializers.Any()) { 311 | this.Append($" : base({ string.Join(",", constructor.BaseInitializers)})"); 312 | } 313 | 314 | this.AppendBody(constructor.Body); 315 | } 316 | 317 | this.NewLine(); 318 | 319 | this.AppendLine("#endregion"); 320 | } 321 | 322 | if (hasEvents) 323 | { 324 | if (hasConstructors || hasFields) this.NewLine(); 325 | 326 | this.AppendLine("#region Events"); 327 | 328 | for (int i = 0; i < @class.Events.Length; i++) 329 | { 330 | var ev = @class.Events[i]; 331 | var handlerType = this.GetFullname(ev.HandlerType); 332 | 333 | this.NewLine(); 334 | this.AppendDocumentationSummary(ev.Documentation); 335 | this.Scope().Append(this.Get(ev.Access)).Append(" event ").Append(this.Get(ev.Scope)); 336 | this.Append(handlerType).Append(" ").Append(ev.Name).Append(";").NewLine(); 337 | } 338 | 339 | this.NewLine(); 340 | 341 | this.AppendLine("#endregion"); 342 | } 343 | 344 | if (hasProperties) 345 | { 346 | if (hasConstructors || hasEvents || hasFields) this.NewLine(); 347 | 348 | this.AppendLine("#region Properties"); 349 | 350 | for (int i = 0; i < @class.Properties.Length; i++) 351 | { 352 | var property = @class.Properties[i]; 353 | var type = this.GetFullname(property.Type); 354 | 355 | this.NewLine(); 356 | this.AppendDocumentationSummary(property.Documentation); 357 | this.Scope().Append(this.Get(property.Access)).Append(" ").Append(this.Get(property.Scope)); 358 | this.Append(type).Append(" ").Append(property.Name); 359 | 360 | 361 | var hasGetter = property.Getter != null && property.Getter != Body.None; 362 | var hasSetter = property.Setter != null && property.Setter != Body.None; 363 | 364 | if (property.Getter == Body.Auto || property.Setter == Body.Auto) 365 | { 366 | this.Append(" {"); 367 | if (hasGetter) this.Append(" get;"); 368 | if (hasSetter) this.Append(" set;"); 369 | this.Append(" }").NewLine(); 370 | } 371 | else 372 | { 373 | this.NewLine().AppendBlock(() => 374 | { 375 | if (hasGetter) 376 | { 377 | this.Scope().Append("get"); 378 | this.Append(property.Getter); 379 | } 380 | 381 | if (hasSetter) 382 | { 383 | if (hasGetter) 384 | this.NewLine(); 385 | 386 | this.Scope().Append("set"); 387 | this.Append(property.Setter); 388 | } 389 | }); 390 | } 391 | } 392 | 393 | this.NewLine().AppendLine("#endregion"); 394 | } 395 | 396 | 397 | if (hasMethods) 398 | { 399 | if (hasConstructors || hasFields || hasProperties) this.NewLine(); 400 | 401 | this.AppendLine("#region Methods"); 402 | 403 | for (int i = 0; i < @class.Methods.Length; i++) 404 | { 405 | var method = @class.Methods[i]; 406 | var returnType = this.GetFullname(method.ReturnType); 407 | 408 | this.NewLine(); 409 | 410 | this.AppendMethodComment(method.Documentation, method.ReturnType, method.Parameters); 411 | foreach (var attribute in method.Attributes) 412 | { 413 | this.Scope().AppendAttribute(attribute).NewLine(); 414 | } 415 | this.Scope().Append(this.Get(method.Access)).Append(this.Get(method.Implementation)).Append(this.Get(method.Override)).Append(" ").Append(this.Get(method.Scope)).Append(this.Get(method.Sync)); 416 | this.Append(returnType).Append(" ").Append(method.Name); 417 | this.AppendParameters(method.Parameters); 418 | this.AppendBody(method.Body); 419 | } 420 | 421 | this.NewLine().AppendLine("#endregion"); 422 | } 423 | 424 | @class.InnerTypes.ToList().ForEach((t) => this.Append(t)); 425 | }); 426 | } 427 | 428 | private void AppendMethodComment(string documentation, IType returnType, Parameter[] parameters) 429 | { 430 | if (!string.IsNullOrEmpty(documentation)) 431 | { 432 | this.AppendDocumentationSummary(documentation); 433 | 434 | if (returnType != null) 435 | { 436 | this.AppendCommentLine($"The {returnType.Name} result."); 437 | } 438 | 439 | foreach (var parameter in parameters) 440 | { 441 | this.AppendCommentLine($"{parameter.Documentation}"); 442 | } 443 | } 444 | } 445 | 446 | private void AppendBody(Body body) 447 | { 448 | if (body != null && body != Body.None) 449 | { 450 | this.Append(body); 451 | } 452 | else 453 | { 454 | this.Append(" {}"); 455 | } 456 | } 457 | 458 | private void AppendParameters(Parameter[] parameters) 459 | { 460 | this.Append("("); 461 | 462 | for (int pi = 0; pi < parameters.Length; pi++) 463 | { 464 | if (pi > 0) this.Append(", "); 465 | 466 | var parameter = parameters[pi]; 467 | var paramType = this.GetFullname(parameter.Type); 468 | foreach (var attribute in parameter.Attributes) 469 | { 470 | this.AppendAttribute(attribute).Append(" "); 471 | } 472 | this.Append(paramType).Append(" ").Append(parameter.Name); 473 | } 474 | 475 | this.Append(")"); 476 | } 477 | 478 | private CsharpGenerator AppendAttribute(Attribute attribute) 479 | { 480 | var paramType = this.GetFullname(attribute.Type); 481 | this.Append("["); 482 | this.Append(paramType); 483 | 484 | if (attribute.Arguments.Any()) 485 | { 486 | this.Append("("); 487 | this.Append(string.Join(",", attribute.Arguments)); 488 | this.Append(")"); 489 | } 490 | 491 | this.Append("]"); 492 | 493 | return this; 494 | } 495 | 496 | private void Append(Body body) 497 | { 498 | if (body != null && body != Body.None) 499 | { 500 | if (body.Root is Statement statement) 501 | { 502 | this.Append(" => "); 503 | } 504 | 505 | this.Append(body.Root); 506 | } 507 | else 508 | { 509 | this.Append(" {}"); 510 | } 511 | } 512 | 513 | private void Append(IInstruction instruction) 514 | { 515 | if (instruction is Block block) 516 | { 517 | this.NewLine().Append(block); 518 | } 519 | 520 | else if (instruction is Statement statement) 521 | { 522 | this.Append(statement).NewLine(); 523 | } 524 | } 525 | 526 | private CsharpGenerator Append(Block block) 527 | { 528 | this.AppendBlock(() => 529 | { 530 | foreach (var instruction in block.Instructions) 531 | { 532 | this.Scope().Append(instruction); 533 | } 534 | }); 535 | return this; 536 | } 537 | 538 | private CsharpGenerator Append(Statement statement) 539 | { 540 | return this.Append(statement.Content); 541 | } 542 | 543 | /// 544 | /// Gets the fullname. 545 | /// 546 | /// The fullname. 547 | /// Type. 548 | private string GetFullname(IType type) 549 | { 550 | switch (type) 551 | { 552 | case GenericType m: 553 | var parameterTypeNames = m.Parameters.Select(p => GetFullname(p)); 554 | return $"{m.Fullname}<{string.Join(", ", parameterTypeNames)}>"; 555 | case Type m: return m.Fullname; 556 | case BaseType b: return b.Name; 557 | default: return type.Name; 558 | } 559 | } 560 | 561 | #region Modifiers 562 | 563 | private string Get(AccessModifier access) 564 | { 565 | switch (access) 566 | { 567 | case AccessModifier.Private: 568 | return "private"; 569 | 570 | case AccessModifier.Protected: 571 | return "protected"; 572 | default: 573 | return "public"; 574 | } 575 | } 576 | 577 | private string Get(OverrideModifier o) 578 | { 579 | switch (o) 580 | { 581 | case OverrideModifier.Virtual: 582 | return " virtual"; 583 | 584 | case OverrideModifier.Override: 585 | return " override"; 586 | default: 587 | return ""; 588 | } 589 | } 590 | 591 | private string Get(ScopeModifier scope) 592 | { 593 | switch (scope) 594 | { 595 | case ScopeModifier.Extension: 596 | case ScopeModifier.Static: 597 | return "static "; 598 | default: 599 | return ""; 600 | } 601 | } 602 | 603 | private string Get(SyncModifier async) 604 | { 605 | switch (async) 606 | { 607 | case SyncModifier.Asynchronous: 608 | return "async "; 609 | default: 610 | return ""; 611 | } 612 | } 613 | 614 | 615 | private string Get(ImplementationModifier implementation) 616 | { 617 | switch (implementation) 618 | { 619 | case ImplementationModifier.MultiFiles: 620 | return "partial "; 621 | default: 622 | return ""; 623 | } 624 | } 625 | 626 | #endregion 627 | 628 | #region Helpers 629 | 630 | private CsharpGenerator Scope() 631 | { 632 | builder.Append(new String(' ', scope * this.Indentation)); 633 | return this; 634 | } 635 | 636 | private CsharpGenerator NewLine() 637 | { 638 | builder.AppendLine(); 639 | return this; 640 | } 641 | 642 | private CsharpGenerator Append(string line) 643 | { 644 | builder.Append(line); 645 | return this; 646 | } 647 | 648 | private CsharpGenerator AppendLine(string line, bool scoped = true) 649 | { 650 | if (scoped) this.Scope(); 651 | return this.Append(line).NewLine(); 652 | } 653 | 654 | private CsharpGenerator AppendCommentLine(string line, bool scoped = true) 655 | { 656 | return this.AppendLine("///" + line, scoped); 657 | } 658 | 659 | private CsharpGenerator AppendDocumentationSummary(string doc) 660 | { 661 | if (!string.IsNullOrEmpty(doc)) 662 | { 663 | this.AppendCommentLine(""); 664 | this.AppendCommentLine(doc); // TODO split by columns 665 | this.AppendCommentLine(""); 666 | } 667 | 668 | return this; 669 | } 670 | 671 | private void AppendBlock(Action action) 672 | { 673 | this.AppendLine("{"); 674 | this.scope++; 675 | action(); 676 | this.scope--; 677 | this.AppendLine("}"); 678 | 679 | } 680 | 681 | #endregion 682 | } 683 | } 684 | --------------------------------------------------------------------------------