mirror of
https://github.com/AlexMacocian/Sybil.git
synced 2026-07-15 15:19:59 +00:00
89 lines
3.7 KiB
C#
89 lines
3.7 KiB
C#
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace Sybil
|
|
{
|
|
public sealed class ConstructorBuilder : IBuilder<ConstructorDeclarationSyntax>
|
|
{
|
|
private BaseConstructorBuilder baseConstructorBuilder;
|
|
|
|
private ConstructorDeclarationSyntax ConstructorDeclarationSyntax { get; set; }
|
|
|
|
internal ConstructorBuilder(
|
|
string className)
|
|
{
|
|
_ = string.IsNullOrWhiteSpace(className) ? throw new ArgumentNullException(nameof(className)) : className;
|
|
|
|
this.ConstructorDeclarationSyntax = SyntaxFactory.ConstructorDeclaration(className);
|
|
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.WithBody(SyntaxFactory.Block());
|
|
}
|
|
|
|
public ConstructorBuilder WithBase(BaseConstructorBuilder baseConstructorBuilder)
|
|
{
|
|
_ = baseConstructorBuilder ?? throw new ArgumentNullException(nameof(baseConstructorBuilder));
|
|
|
|
this.baseConstructorBuilder = baseConstructorBuilder;
|
|
return this;
|
|
}
|
|
public ConstructorBuilder WithModifiers(string modifiers)
|
|
{
|
|
_ = string.IsNullOrWhiteSpace(modifiers) ? throw new ArgumentNullException(nameof(modifiers)) : modifiers;
|
|
|
|
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.AddModifiers(SyntaxFactory.ParseTokens(modifiers).ToArray());
|
|
|
|
return this;
|
|
}
|
|
public ConstructorBuilder WithModifier(string modifier)
|
|
{
|
|
_ = string.IsNullOrWhiteSpace(modifier) ? throw new ArgumentNullException(nameof(modifier)) : modifier;
|
|
|
|
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.AddModifiers(SyntaxFactory.ParseToken(modifier));
|
|
|
|
return this;
|
|
}
|
|
public ConstructorBuilder WithParameter(string parameterType, string parameterName, string defaultValue = null)
|
|
{
|
|
_ = string.IsNullOrWhiteSpace(parameterName) ? throw new ArgumentNullException(nameof(parameterName)) : parameterName;
|
|
_ = string.IsNullOrWhiteSpace(parameterType) ? throw new ArgumentNullException(nameof(parameterType)) : parameterType;
|
|
|
|
EqualsValueClauseSyntax equalsValueClauseSyntax = null;
|
|
if (string.IsNullOrWhiteSpace(defaultValue) is false)
|
|
{
|
|
equalsValueClauseSyntax = SyntaxFactory.EqualsValueClause(
|
|
SyntaxFactory.ParseExpression(defaultValue));
|
|
}
|
|
|
|
var parameter = SyntaxFactory.Parameter(
|
|
new SyntaxList<AttributeListSyntax>(),
|
|
new SyntaxTokenList(),
|
|
SyntaxFactory.IdentifierName(SyntaxFactory.Identifier(parameterType)),
|
|
SyntaxFactory.Identifier(parameterName),
|
|
equalsValueClauseSyntax);
|
|
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.AddParameterListParameters(parameter);
|
|
|
|
return this;
|
|
}
|
|
public ConstructorBuilder WithBody(string body)
|
|
{
|
|
_ = string.IsNullOrWhiteSpace(body) ? throw new ArgumentNullException(nameof(body)) : body;
|
|
|
|
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.WithBody(SyntaxFactory.Block(SyntaxFactory.ParseStatement(body)));
|
|
|
|
return this;
|
|
}
|
|
|
|
public ConstructorDeclarationSyntax Build()
|
|
{
|
|
if (this.baseConstructorBuilder is null is false)
|
|
{
|
|
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.WithInitializer(this.baseConstructorBuilder.Build());
|
|
}
|
|
|
|
return this.ConstructorDeclarationSyntax.NormalizeWhitespace();
|
|
}
|
|
}
|
|
}
|