Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions DashTools.CodeGenerator/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Security.Cryptography.Algorithms" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Thread" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
33 changes: 33 additions & 0 deletions DashTools.CodeGenerator/ClassWalker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace DashTools.CodeGenerator
{
public class ClassWalker : CSharpSyntaxWalker
{
public ClassWalker(SyntaxWalkerDepth depth = SyntaxWalkerDepth.Node)
: base(depth)
{
}

public override void VisitClassDeclaration(ClassDeclarationSyntax node)

{
var token = SyntaxFactory.Identifier(
node.Identifier.LeadingTrivia,
node.Identifier.Kind(),
node.Identifier.ValueText.Replace("type", ""),
node.Identifier.ValueText.Replace("type", ""),
node.Identifier.TrailingTrivia);
var changed = node.WithIdentifier(token);

base.VisitClassDeclaration(node);
}
}
}
243 changes: 243 additions & 0 deletions DashTools.CodeGenerator/DashTools.CodeGenerator.csproj

Large diffs are not rendered by default.

137 changes: 137 additions & 0 deletions DashTools.CodeGenerator/MoveNamespacesToUsingsRewriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace DashTools.CodeGenerator
{
public class MoveNamespacesToUsingsRewriter : CSharpSyntaxRewriter
{
private List<NameSyntax> namespacesToAdd = null;

public MoveNamespacesToUsingsRewriter(bool visitIntoStructuredTrivia = false)
: base(visitIntoStructuredTrivia)
{
}

public override SyntaxNode Visit(SyntaxNode node)
{
if (namespacesToAdd == null)
{
var finder = new UsingDirectivesFinder();
finder.Visit(node);
var existingUsingDirectives = finder.ExistingUsingDirectives.ToList();
namespacesToAdd = finder
.NamespacesInQualifiedNames
.Where(e => !existingUsingDirectives.Any(ee => ee.ToFullString() == e.ToFullString()))
.ToList();
}

node = base.Visit(node);

return node;
}

public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
{
var usingsToAdd = namespacesToAdd
.Select(e => e.WithLeadingTrivia(SyntaxFactory.Whitespace(" ")))
.Select(e => SyntaxFactory.UsingDirective(e).WithLeadingTrivia(SyntaxFactory.Tab))
.Select(e => e.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed));
var newUsings = node.Usings.AddRange(usingsToAdd);

return (base.VisitNamespaceDeclaration(node) as NamespaceDeclarationSyntax)
.WithUsings(new SyntaxList<UsingDirectiveSyntax>(newUsings));
}

public override SyntaxNode VisitAttribute(AttributeSyntax node)
{
node = base.VisitAttribute(node) as AttributeSyntax;

if (node.Name is QualifiedNameSyntax)
{
var qualifiedName = node.Name as QualifiedNameSyntax;
var simpleName = qualifiedName.Right;
node = node.WithName(simpleName);
}

return node;
}

public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
node = base.VisitPropertyDeclaration(node) as PropertyDeclarationSyntax;

var type = GetTypeWithoutNamespaces(node.Type).WithTriviaFrom(node.Type);
node = node
.WithType(type)
.WithTriviaFrom(node);

return node;
}

public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
{
node = base.VisitFieldDeclaration(node) as FieldDeclarationSyntax;

var type = GetTypeWithoutNamespaces(node.Declaration.Type)
.WithTriviaFrom(node.Declaration.Type);
var declaration = node.Declaration
.WithType(type)
.WithTriviaFrom(node.Declaration);
node = node.WithDeclaration(declaration);

return node;
}

public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
{
node = base.VisitMethodDeclaration(node) as MethodDeclarationSyntax;

node = node.WithReturnType(GetTypeWithoutNamespaces(node.ReturnType))
.WithLeadingTrivia(node.ReturnType.GetLeadingTrivia())
.WithTrailingTrivia(node.ReturnType.GetTrailingTrivia());


var parameters = node.ParameterList.Parameters;
for (int i = 0; i < parameters.Count; i++)
{
var arg = parameters[i];
parameters = parameters.Replace(
arg,
arg.WithType(GetTypeWithoutNamespaces(arg.Type))
.WithLeadingTrivia(arg.Type.GetLeadingTrivia())
.WithTrailingTrivia(arg.Type.GetTrailingTrivia())
);
}
node = node.WithParameterList(
node.ParameterList
.WithParameters(parameters)
.WithLeadingTrivia(node.ParameterList.GetLeadingTrivia())
.WithTrailingTrivia(node.ParameterList.GetTrailingTrivia())
);

return node;
}

private TypeSyntax GetTypeWithoutNamespaces<T>(T type)
where T : TypeSyntax
{
QualifiedNameSyntax qualifiedName = null;
if (type is QualifiedNameSyntax)
{
qualifiedName = type as QualifiedNameSyntax;
}
else if (type is ArrayTypeSyntax && (type as ArrayTypeSyntax).ElementType is QualifiedNameSyntax)
{
qualifiedName = (type as ArrayTypeSyntax).ElementType as QualifiedNameSyntax;
}

return qualifiedName?.Right as TypeSyntax ?? type;
}
}
}
87 changes: 87 additions & 0 deletions DashTools.CodeGenerator/MpdCodeRefactorer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Rename;

namespace DashTools.CodeGenerator
{
public class MpdCodeRefactorer : CSharpSyntaxRewriter
{
private readonly SemanticModel semanticModel;

public RenameSymbolInfo Refactoring { get; private set; }

public MpdCodeRefactorer(SemanticModel semanticModel, bool visitIntoStructuredTrivia = false)
: base(visitIntoStructuredTrivia)
{
this.semanticModel = semanticModel;
}

public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
{
string className = node.Identifier.ValueText;
if (className.EndsWith("type"))
{
className = className.Replace("type", "");

Refactoring = new RenameSymbolInfo
{
Symbol = semanticModel.GetDeclaredSymbol(node),
NewName = className,
Comment = "Remove \"type\" suffix from class name"
};
}

return Refactoring == null
? base.VisitClassDeclaration(node)
: node;
}

public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
string name = node.Identifier.ValueText;
if (name.Substring(0, 1) != name.Substring(0, 1).ToUpper())
{
name = name.Substring(0, 1).ToUpper() + name.Substring(1);

Refactoring = new RenameSymbolInfo
{
Symbol = semanticModel.GetDeclaredSymbol(node),
NewName = name,
Comment = "Capitalize property name's 1st letter"
};
}

return base.VisitPropertyDeclaration(node);
}

public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node)
{
foreach (var variable in node.Declaration.Variables)
{
string fieldName = variable.Identifier.ValueText;
if (fieldName.EndsWith("Field"))
{
fieldName = fieldName.Substring(0, fieldName.Length - "Field".Length);

Refactoring = new RenameSymbolInfo
{
Symbol = semanticModel.GetDeclaredSymbol(variable),
NewName = fieldName,
Comment = "Remove \"Field\" suffix from field name"
};
}
}

return Refactoring == null
? base.VisitFieldDeclaration(node)
: node;
}
}
}
Loading