Continuous Learning and Sharing of Team Foundation Server and Application Lifecycle Management RSS 2.0
# Saturday, December 20, 2008

Doing builds and running unit tests after every check-in has become a standard practice in Agile and non-Agile development teams.  Continuous Integration is the term that describes this process and helps ensure that that nothing has been broken since the last check-in.    One area that is sometimes overlooked is the code generation.    Continuous Generation, the re-generating of code / stored procedures for every check-in and build, should be considered for your TFS builds.  I believe there is a lot of benefit to generating the code with every build.  There are several benefits:

  • Table schema and stored procedure parameters can change.  How do you know that your generated objects are up to date and match the database?
  • It keeps everyone honest so they don’t break the golden rule of never editing generated code.  If the build is going to re-generate the code there’s no way to sneak the semicolon or curly bracket that you manually fix each time after generation.
  • Sometimes we generate just the class we are working on and keep the rest checked in so it doesn’t overwrite the current object.  If any changes are made to the template only this new class will have them.  The other classes will be based on the older version.
  • It gives more credibility to the generation process.  There is sometimes a feeling of nervousness when generating all of the objects.  Since generated code is never edited, you can generate the code as often as you wish.

CodeSmith Tools is the code generation tool that we use.  It offers an easy way to accomplish this from your Team Foundation Builds.   With version 3.2 and newer there is a custom build task included with the Professional edition.  Here is the online help with some information about it.

There are a couple things to consider.  The generated classes will be checked-in to source control.  You will need to check them out before you call the task and then check them back in afterwards. Also, the user guide instructions do not work quite right in the link above.  The guide explains how to use it within your visual studio project.  However, the task needs to be called from Team Build.  Here’s the basic steps and changes for your CI tfsbuild.proj file.

Step 1: Install CodeSmith Professional on your TFS build server.  This will install the MSBuild task and Targets file.   I contacted CodeSmith Tool’s sales department and this does require an additional license for the server.

Step 2: Import the CodeSmith Targets file.  Add the following line just below the import element below the <!-- Do not edit this --> comment.

<Import Project="$(MSBuildExtensionsPath)\CodeSmith\CodeSmith.targets" />

Step 3:  Add TF property in the PropertyGroup element specifying the tf.exe location to be used throughout the process.

<PropertyGroup>
  <
TF>&quot;$(TeamBuildRefPath)\..\tf.exe&quot;</TF>
</
PropertyGroup>

Step 4:  Override the AfterGet target to check out the generated file(s).  This must be called before the CodeSmith task or it will return an access denied error.  This example demonstrates one file but you can use a subfolder or naming prefix for the generated files and recursively check out all of the necessary files.

<Target Name="AfterGet">

    <Exec WorkingDirectory="$(SolutionRoot)\Main\TestCodeSmithMSBuild\"

          Command="$(TF) checkout Measure.cs"  />

Step 5:  Call the CodeSmith task to execute the CodeSmith project(s) in your solution.  This will also be called in the AfterGet target.  Currently the example in the documentation incorrectly shows the CodeSmith task using the ProjectFile attribute.  As the usage description shows, the attribute is actually ProjectFiles. 

<CodeSmith ProjectFiles="$(SolutionRoot)\Main\TestCodeSmithMSBuild\Test.csp"  />

Step 6:  Override the AfterCompile target to check in the updated generated files.  Fortunately TFS will only check in the file if there is a change.  This is good because most of the time re-generating the code should generate the same thing each time.  However, tf.exe returns a code of 1 instead of 0 and results in a partial success of the build.  Use the IgnoreExitCode=”true” to ignore this.  You could additionally update this to create a work item for any other return code.  Here’s a post with a good example of this.

<Target Name="AfterCompile" Condition="'$(IsDesktopBuild)'!='true'">

    <Exec WorkingDirectory="$(SolutionRoot)\Main\TestCodeSmithMSBuild"

          IgnoreExitCode="true"

          Command="$(TF) checkin /comment:&quot;***NO_CI***Auto-Generate&quot; /noprompt /override:&quot;Auto Generate&quot; measure.cs "/>

  </Target>

Step 7:  Undo the check out if the build fails by overriding the BeforeOnBuildBreak.

<Target Name="BeforeOnBuildBreak" Condition="'$(IsDesktopBuild)'!='true'">

    <Exec WorkingDirectory="$(SolutionRoot)\Main\TestCodeSmithMSBuild"

          Command="$(TF) undo /noprompt measure.cs"/>

  </Target>

That is it.  Enjoy!

Mike

Saturday, December 20, 2008 10:49:44 PM (Central Standard Time, UTC-06:00)  #    Comments [0] -
Code Generation | Team Build | Team Foundation Server

# Sunday, December 07, 2008

I admit that while I have been keeping up with Csla information, I haven't had the opportunity to use the newer versions of Csla.  We have an opportunity to use Csla with a client so I'm using this chance to catch up on it and get some practical experience with 3.6.  I downloaded the framework and samples downloads. You can download the latest framework at http://www.lhotka.net/cslanet/Download.aspx

I was excited by how much is included in both.  The framework includes snippets, visual studio templates, and a deployment package.  Here's some highlights.

Snippets
There are several snippets included. With the new way to do properties in Csla, the editable and readonly property snippets will come in handy.  Here's the results of the "cslaprop" snippet.

private static PropertyInfo<int> NameProperty = RegisterProperty(new PropertyInfo<int>("Name", "Name"));

public int Name
{
   get { return GetProperty(NameProperty); }
   set { SetProperty(NameProperty, value); }
}

Item Templates
When you install the VSI, it installs 14 templates for almost all common Csla usages and objects.  Like the rest of the framework, the full source for the templates are included.  So customizing the templates to fit your projects needs is easy.

 

Csla Samples
I downloaded the samples project expecting to just see the Project Tracker example.  The Project Tracker is still the main example but it has been expanded to include examples for every configuration that Csla supports including WCF, Web Services, WPF, winforms, Webforms, Silverlight (requires additional Csla for Silverlight download), LINQ, DTO, Entity Framework, Workflow, etc.

In addition to the Project Tracker, there are examples for doing WPF design time support, Deep loading and saving of data, and establishing parent-child-grandchild relationships in Csla. 

I am looking forward to working through all of these examples to see Rocky's recommendations in the 3.6 version of his framework.

Mike
 

Sunday, December 07, 2008 6:17:28 PM (Central Standard Time, UTC-06:00)  #    Comments [0] -
CSLA.NET


A couple weeks ago I started a new and exciting chapter of my career.  After 8 great years at Farm Credit Services of America as a developer and lead developer, I took a new job as a solution consultant with a small, yet proven and experienced consultanting company called Deliveron Consulting Services.   I have no complaints about FCSAmerica and appreciate all of the oportunities that I received while I was there.  It was a great place to work and leaving was one of the hardest decisions I have had to make. I'm going to miss working with all of the great people I have gotten to know.  I felt this was the right opporturnity at the right time for me.  I'm excited to leverage all of my experiences in Team Foundation Server, CSLA.NET, code generation, configuration management, Agile, Biztalk, etc along with the leadership skills in a new environment.  In fact, I have already begun helping one of our clients convert a projects to TFS.  I'm also utilizing Team Deploy to automate their build and deploy process.  I am excited to work with more companies on implementing or improving their implementation of Team Foundation Server.

So this begins a new chapter and I am excited!

Mike

Sunday, December 07, 2008 8:29:11 AM (Central Standard Time, UTC-06:00)  #    Comments [1] -
General | Team Foundation Server

Visual Studio ALM MVP
Microsoft Visual Studio ALM MVP
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Mike Douglas
Sign In
Statistics
Total Posts: 76
This Year: 0
This Month: 0
This Week: 0
Comments: 52
All Content © 2012, Mike Douglas
DasBlog theme 'Business' created by Christoph De Baene (delarou)