Anyone have experience calling Rake from MSBuild for code gen and other benefits? How did it go? Wha
Posted
by Charlie Flowers
on Stack Overflow
See other posts from Stack Overflow
or by Charlie Flowers
Published on 2009-03-24T19:58:03Z
Indexed on
2010/04/03
13:43 UTC
Read the original article
Hit count: 291
While programming in C# using Visual Studio 2008, I often wish for "automatic" code generation. If possible, I'd like to achieve it by making my MSBuild solution file call out to Rake, which would call Ruby code for the code generation, having the resulting generated files automatically appear in my solution.
Here's one business example (of many possible examples I could name) where this kind of automatic code generation would be helpful. In a recent project I had an interface with some properties that contained dollar amounts. I wanted a second interface and a third interface that had the same properties as the first interface, except they were "qualified" with a business unit name. Something like this:
public interface IQuarterlyResults
{
double TotalRevenue { get; set; }
double NetProfit { get; set; }
}
public interface IConsumerQuarterlyResults
{
double ConsumerTotalRevenue { get; set; }
double ConsumerNetProfit { get; set; }
}
public interface ICorporateQuarterResults
{
double CorporateTotalRevenue { get; set; }
double CorporateNetProfit { get; set; }
}
In this example, there is a "Consumer Business Unit" and a "Corporate Business Unit". Every property on IQuarterlyResults becomes a property called "Corporate" + [property name] on ICorporateQuarterlyResults, and likewise for IConsumerQuarterlyResults.
Why make interfaces for these, rather than merely having an instance of IQuarterlyResults for Consumer and another instance for Corporate? Because, when working with the calculator object I was building, the user had to deal with 100's of properties, and it is much less confusing if he can deal with "fully qualified" property names such as "ConsumerNetProfit".
But let's not get bogged down in this example. It is only an example and not the main question.
The main question is this: I love using Ruby and ERB for code generation, and I love using Rake to manage dependencies between tasks. To solve the problem above, what I'd like to do is have MSBuild call out to Rake, and have Rake / Ruby read the list of properties on the "core" interface and then generate the code to make all the dependent interfaces and their properties. This would get triggered every time I do a build, because I'd put it into the MSBuild file for the VS.NET solution.
Has anyone tried anything like this? How did it work out for you? What insights can you share about pros, cons, tips for success, etc.?
Thanks!
© Stack Overflow or respective owner