Skip to content

Commit

Permalink
Added comments from the source class to the DTO class.
Browse files Browse the repository at this point in the history
Added "using"-s for vm.Aspects and Enterprise Library to the metadata class.
Fixed bugs in the interface tasks template where if any of the names in the attributes are constants, the generated class was incorrect.
  • Loading branch information
vmelamed committed Jan 23, 2016
1 parent f391b96 commit cdc6b5c
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013-2015 vm
Copyright (c) 2013-2016 vm

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[assembly: AssemblyCompany("vm")]
[assembly: AssemblyProduct("vm.Aspects")]
[assembly: AssemblyCopyright("Copyright © vm 2013-2015")]
[assembly: AssemblyCopyright("Copyright © vm 2013-2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
#if DEBUG
Expand All @@ -31,6 +31,6 @@
// associated with an assembly.
[assembly: AssemblyTitle("AddRelatedClasses")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyVersion("0.0.2")]
[assembly: AssemblyFileVersion("0.0.2")]
[assembly: AssemblyInformationalVersion("0.0.2")]
[assembly: AssemblyVersion("0.0.5")]
[assembly: AssemblyFileVersion("0.0.5")]
[assembly: AssemblyInformationalVersion("0.0.5")]
11 changes: 3 additions & 8 deletions Aspects/Visix/AddRelatedClasses/AddRelatedClasses/RootCommand.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//------------------------------------------------------------------------------
// <copyright file="RootCommand.cs" company="vm">
// Copyright (c) vm. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System;
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -175,7 +169,7 @@ void AddMetadataType(object sender, EventArgs e)
if (!vsProject.References.OfType<Reference>().Any(r => r.Name == "System.ComponentModel.DataAnnotations"))
vsProject.References.Add("System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

// 2. add the using System.ComponentModel.DataAnnotation
// 2. add using System.ComponentModel.DataAnnotation
if (!cached.SourceCodeModel.CodeElements.OfType<CodeImport>().Any(i => i.Namespace == "System.ComponentModel.DataAnnotations"))
cached.SourceCodeModel.AddImport("System.ComponentModel.DataAnnotations");

Expand All @@ -199,6 +193,7 @@ void AddMetadataType(object sender, EventArgs e)

// fire-up the T4 engine and generate the text
var t4 = ServiceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;

if (t4 == null)
return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<#@ template language="C#" debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Xml" #>
<#@ Assembly Name="System.Xml.Linq" #>
<#@ Assembly Name="EnvDTE" #>
<#@ Assembly Name="EnvDTE80" #>
<#@ import namespace="EnvDTE" #>
Expand All @@ -10,6 +12,8 @@
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ parameter name="sourcePathName" type="System.String" #>
Expand Down Expand Up @@ -46,20 +50,28 @@ using vm.Aspects.Wcf.DataContracts;

namespace <#= targetNameSpace.Name #>
{
[DataContract(Namespace="<Enter here the data contract namespace>")]
public class <#= sourceClass.Name #>Dto : DataTransferObject
{
<#
PushIndent(" ");
WriteDoc(sourceClass.DocComment);
#>
[DataContract(Namespace="<Enter here the data contract namespace>")]
public class <#= sourceClass.Name #>Dto : DataTransferObject
{
<#
PushIndent(" ");
foreach (var property in sourceProperties)
{
if (i > 0)
WriteLine("");
WriteDoc(property.DocComment);
#>
[DataMember]
public <#= property.Type.AsString #> <#= property.Name #> { get; set; }
[DataMember]
public <#= property.Type.AsString #> <#= property.Name #> { get; set; }
<#
i++;
}#>
}
PopIndent();
PopIndent();#>
}
}<#
}
Expand All @@ -69,7 +81,7 @@ namespace <#= targetNameSpace.Name #>
var targetProperties = targetClass.Members.OfType<CodeProperty2>();
var targetPropertyNames = new HashSet<string>(targetProperties.Select(p => p.Name));

// remove the metadata properties that were removed from the source's properties
// remove the properties that were removed from the source's properties
foreach (var propertyName in targetPropertyNames.Except(sourcePropertyNames))
targetClass.RemoveMember(targetProperties.First(p => p.Name == propertyName));

Expand Down Expand Up @@ -101,19 +113,25 @@ namespace <#= targetNameSpace.Name #>

var i = targetPropertyNames.Count();

PushIndent(" ");
PushIndent(" ");
// insert the new properties
foreach (var propertyName in sourcePropertyNames.Except(targetPropertyNames))
{
var property = sourceProperties.FirstOrDefault(p => p.Name == propertyName);

if (property == null)
continue;
WriteLine("");
WriteLine("");
WriteDoc(property.DocComment);
#>


[DataMember]
public <#= property.Type.AsString #> <#= property.Name #> { get; set; }<#
[DataMember]
public <#= property.Type.AsString #> <#= property.Name #> { get; set; }<#
}

PopIndent();
PopIndent();

// write the remaining of the text
Write(endText);
Expand All @@ -122,6 +140,19 @@ namespace <#= targetNameSpace.Name #>
}
#><#+

// copies the XML comment documentation from the source element to the target, commented with '/// '.
void WriteDoc(string doc)
{
if (string.IsNullOrWhiteSpace(doc))
return;

var docElement = XElement.Parse(doc);

PushIndent("/// ");
docElement.Elements().Select(e => { WriteLine(e.ToString()); return 1;}).Count();
PopIndent();
}

DTE2 GetEnvDte()
{
var hostServiceProvider = (IServiceProvider)Host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@

// generate the metadata class from scratch using plain T4
#>
using vm.Aspects.Diagnostics;
using System;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using vm.Aspects.Diagnostics;
using vm.Aspects.Validation;

namespace <#= targetNameSpace.Name #>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

var sourceMethods = sourceInterface.Children.OfType<EnvDTE.CodeFunction>().FirstOrDefault();

bool soapNamespaceIsQuoted;
bool soapNameIsQuoted;
string soapNamespace;
string soapName;

Expand All @@ -47,7 +49,7 @@

WriteDoc(sourceInterface.DocComment);

WriteInterfaceAttributes(sourceInterface, out soapNamespace, out soapName);
WriteInterfaceAttributes(sourceInterface, out soapNamespace, out soapNamespaceIsQuoted, out soapName, out soapNameIsQuoted);

WriteInterfaceDeclaration(sourceInterface);
WriteLine("{");
Expand All @@ -61,7 +63,7 @@
first = false;
else
WriteLine("");
WriteMethod(method, soapNamespace, soapName);
WriteMethod(method, soapNamespace, soapNamespaceIsQuoted, soapName, soapNameIsQuoted);
}

PopIndent();
Expand Down Expand Up @@ -223,7 +225,10 @@
if (quoted.StartsWith("\""))
return quoted.Substring(1, quoted.Length-2);
else
if (quoted.StartsWith("@\""))
return quoted.Substring(2, quoted.Length-3).Replace("\"\"", "\"");
else
return quoted;
}

// Enumerates an array of methods which return possible path names of a file. Returns the first file name that exists or null.
Expand Down Expand Up @@ -291,10 +296,19 @@
void WriteInterfaceAttributes(
CodeInterface sourceInterface,
out string soapNamespace,
out string soapName)
out bool soapNamespaceIsQuoted,
out string soapName,
out bool soapNameIsQuoted)
{
var ns = "http://tempuri.org";
var name = sourceInterface.Name;
soapNamespace = "http://tempuri.org";
soapNamespaceIsQuoted = true;
soapName = sourceInterface.Name;
soapNameIsQuoted = true;

var ns = "http://tempuri.org";
var nm = sourceInterface.Name;
var nsIsQuoted = true;
var nmIsQuoted = true;

foreach (CodeAttribute a in sourceInterface.Attributes)
{
Expand All @@ -312,9 +326,15 @@
if (a.Name == "ServiceContract")
{
if (arg.Name == "Namespace")
{
ns = GetQuotedValue(arg.Value);
nsIsQuoted = arg.Value.StartsWith("\"") || arg.Value.StartsWith("@\"");
}
if (arg.Name == "Name")
name = GetQuotedValue(arg.Value);
{
nm = GetQuotedValue(arg.Value);
nmIsQuoted = arg.Value.StartsWith("\"") || arg.Value.StartsWith("@\"");
}
}
}
},
Expand All @@ -325,8 +345,10 @@
WriteLine("]");
}

soapNamespace = ns;
soapName = name;
soapNamespace = ns;
soapNamespaceIsQuoted = nsIsQuoted;
soapName = nm;
soapNameIsQuoted = nmIsQuoted;
}

void WriteInterfaceDeclaration(CodeInterface sourceInterface)
Expand All @@ -342,10 +364,15 @@
WriteLine("");
}

void WriteMethod(CodeFunction method, string soapNamespace, string soapName)
void WriteMethod(
CodeFunction method,
string soapNamespace,
bool soapNamespaceIsQuoted,
string soapName,
bool soapNameIsQuoted)
{
WriteDoc(method.DocComment);
WriteMethodAttributes(method, soapNamespace, soapName);
WriteMethodAttributes(method, soapNamespace, soapNamespaceIsQuoted, soapName, soapNameIsQuoted);

var type = GetType(method.Type as CodeTypeRef2);

Expand All @@ -362,10 +389,13 @@

string WriteMethodAttributes(
CodeFunction method,
string soapNamespace,
string soapName)
string soapNamespace,
bool soapNamespaceIsQuoted,
string soapName,
bool soapNameIsQuoted)
{
var name = method.Name;
var nameIsQuoted = true;

foreach (CodeAttribute a in method.Attributes)
{
Expand All @@ -386,7 +416,10 @@
Write("{0}={1}", arg.Name, arg.Value);
if (a.Name == "OperationContract")
if (arg.Name == "Name")
{
name = GetQuotedValue(arg.Value);
nameIsQuoted = arg.Value.StartsWith("\"") || arg.Value.StartsWith("@\"");
}
else
{
hasAction = arg.Name == "Action";
Expand All @@ -410,7 +443,22 @@
}
else
WriteLine(",");
Write("Action=\"{0}/{1}/{2}\"", soapNamespace, soapName, name);

Write("Action=");
if (soapNamespaceIsQuoted)
Write("\"{0}", soapNamespace);
else
Write("{0}+\"", soapNamespace);
Write("/");
if (soapNameIsQuoted)
Write(soapName);
else
Write("\"+{0}+\"", soapName);

if (nameIsQuoted)
Write("/{0}\"", name);
else
Write("/\"+{0}", name);
}

if (!hasReply)
Expand All @@ -422,7 +470,22 @@
}
else
WriteLine(",");
Write("ReplyAction=\"{0}/{1}/{2}Response\"", soapNamespace, soapName, name);

Write("ReplyAction=");
if (soapNamespaceIsQuoted)
Write("\"{0}", soapNamespace);
else
Write("{0}+\"", soapNamespace);
Write("/");
if (soapNameIsQuoted)
Write(soapName);
else
Write("\"+{0}+\"", soapName);

if (nameIsQuoted)
Write("/{0}Response\"", name);
else
Write("/\"+{0}+\"Response\"", name);
}

PopIndent();
Expand Down
Loading

0 comments on commit cdc6b5c

Please sign in to comment.