private PropertyDefinition _CreatePropertyDefinition(PropertyDeclarationSyntax inputPropertyDeclaration, ClassDeclarationSyntax inputClassDeclaration) { var _propertyDefinition = PropertyDefinition.Create(inputPropertyDeclaration.Identifier.Text, inputPropertyDeclaration.Type.ToString()); var _propertyValidationMethod = inputClassDeclaration.Members .Where(_member => _member.Kind() == SyntaxKind.MethodDeclaration) .Cast <MethodDeclarationSyntax>() .Where(_method => _method.Modifiers.Any(SyntaxKind.StaticKeyword)) .Where(_method => _method.ParameterList.Parameters.Count == 1) .SingleOrDefault(_method => _method.Identifier.Text == NameHelper.TextToPublicMethodIdentifier("Is" + _propertyDefinition.Name + "Valid").Text); if (_propertyValidationMethod != null) { var _valueParameter = _propertyValidationMethod.ParameterList.Parameters[_propertyValidationMethod.ParameterList.Parameters.Count - 1]; Must.Assert(_valueParameter.Type.ToString() == _propertyDefinition.Type); _propertyDefinition = _propertyDefinition .WithValidateMethodName(SyntaxFactory.IdentifierName(_propertyValidationMethod.Identifier)); } var _defaultProperty = inputClassDeclaration.Members .Where(_member => _member.Kind() == SyntaxKind.PropertyDeclaration) .Cast <PropertyDeclarationSyntax>() .Where(_p => _p.Modifiers.Any(SyntaxKind.StaticKeyword)) .SingleOrDefault(_p => _p.Identifier.Text == NameHelper.TextToPublicPropertyIdentifier("Default" + _propertyDefinition.Name).Text); if (_defaultProperty != null) { Must.Assert(_defaultProperty.Type.ToString() == _propertyDefinition.Type); _propertyDefinition = _propertyDefinition.WithDefaultValuePropertyName(SyntaxFactory.IdentifierName(_defaultProperty.Identifier)); } return(_propertyDefinition); }
private MethodDeclarationSyntax _CreateOutputClassWithMethod(TypeDefinition typeDefinition, PropertyDefinition propertyDefinition) { var _validationStatement = _CreatePropertyValidationStatement(propertyDefinition, _property => NameHelper.TextToMethodParameterName(_property.Name)); var _typeCtorArguments = _CreatePropertyArgumentList( typeDefinition, _property => (_property.Name == propertyDefinition.Name ? NameHelper.TextToMethodParameterName(propertyDefinition.Name) : NameHelper.TextToPublicPropertyName(_property.Name))); var _createTypeInstanceStatement = _CreateDeclareVariableAndCreateTypeInstanceStatement(typeDefinition, _typeCtorArguments, SyntaxFactory.Identifier("_instance")); var _typeValidationStatement = _CreateTypeValidationStatement(typeDefinition, SyntaxFactory.IdentifierName("_instance")); var _returnTypeInstanceStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("_instance")); return(SyntaxFactory .MethodDeclaration( SyntaxFactory.IdentifierName(typeDefinition.Name), NameHelper.TextToPublicMethodIdentifier("With" + propertyDefinition.Name)) .AddParameterListParameters(SyntaxFactory .Parameter(NameHelper.TextToMethodParameterIdentifier(propertyDefinition.Name)) .WithType(SyntaxFactory.IdentifierName(propertyDefinition.Type))) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) .WithBody(SyntaxFactory.Block() .AddStatements(_validationStatement) .AddStatements(_createTypeInstanceStatement) .AddStatements(_typeValidationStatement) .AddStatements(_returnTypeInstanceStatement))); }
private MethodDeclarationSyntax _CreateOutputClassCreateMethod(TypeDefinition typeDefinition) { var _propertyValidationStatements = _CreatePropertyValidationStatements(typeDefinition, _property => NameHelper.TextToMethodParameterName(_property.Name)); var _typeCtorArguments = _CreatePropertyArgumentList(typeDefinition, _property => (_property.DefaultValuePropertyName ?? NameHelper.TextToMethodParameterName(_property.Name))); var _createTypeInstanceStatement = _CreateDeclareVariableAndCreateTypeInstanceStatement(typeDefinition, _typeCtorArguments, SyntaxFactory.Identifier("_instance")); var _typeValidationStatement = _CreateTypeValidationStatement(typeDefinition, SyntaxFactory.IdentifierName("_instance")); var _returnTypeInstanceStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("_instance")); return(SyntaxFactory .MethodDeclaration(SyntaxFactory.IdentifierName(typeDefinition.Name), NameHelper.TextToPublicMethodIdentifier("Create")) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)) .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(typeDefinition.Properties .Where(_property => _property.DefaultValuePropertyName == null) .Select(_property => SyntaxFactory .Parameter(NameHelper.TextToMethodParameterIdentifier(_property.Name)) .WithType(SyntaxFactory.IdentifierName(_property.Type)))))) .WithBody(SyntaxFactory.Block() .AddStatements(_propertyValidationStatements) .AddStatements(_createTypeInstanceStatement) .AddStatements(_typeValidationStatement) .AddStatements(_returnTypeInstanceStatement))); }
private ConstructorDeclarationSyntax _CreateOutputClassMainCtor(TypeDefinition typeDefinition) { return(SyntaxFactory .ConstructorDeclaration(SyntaxFactory.Identifier(typeDefinition.Name)) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)) .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(typeDefinition.Properties .Select(_property => SyntaxFactory .Parameter(NameHelper.TextToMethodParameterIdentifier(_property.Name)) .WithType(SyntaxFactory.IdentifierName(_property.Type)))))) .WithBody(SyntaxFactory .Block(typeDefinition.Properties .Select(_property => SyntaxFactory .ExpressionStatement(SyntaxFactory .AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(NameHelper.TextToPublicPropertyIdentifier(_property.Name)), SyntaxFactory.IdentifierName(NameHelper.TextToMethodParameterIdentifier(_property.Name)))))))); }
private TypeDefinition _CreateTypeDefinition(ClassDeclarationSyntax inputClassDeclaration, CompilationUnitSyntax rootNode) { var _typeDefinition = TypeDefinition.Create( inputClassDeclaration.Identifier.ToString(), ((NamespaceDeclarationSyntax)inputClassDeclaration.Parent).Name.ToString()); _typeDefinition = _typeDefinition .AddUsings(rootNode.Usings); var _typeValidationMethod = inputClassDeclaration.Members .Where(_member => _member.Kind() == SyntaxKind.MethodDeclaration) .Cast <MethodDeclarationSyntax>() .Where(_method => _method.Modifiers.Any(SyntaxKind.StaticKeyword)) .Where(_method => _method.ParameterList.Parameters.Count == 1) .SingleOrDefault(_method => _method.Identifier.Text == NameHelper.TextToPublicMethodIdentifier("IsValid").Text); if (_typeValidationMethod != null) { var _valueParameter = _typeValidationMethod.ParameterList.Parameters[_typeValidationMethod.ParameterList.Parameters.Count - 1]; Must.Assert(_valueParameter.Type.ToString() == _typeDefinition.Name); _typeDefinition = _typeDefinition .WithValidateMethodName(SyntaxFactory.IdentifierName(_typeValidationMethod.Identifier)); } _typeDefinition = _typeDefinition.AddProperties(_CreatePropertyDefinitions(inputClassDeclaration)); return(_typeDefinition); }