Scaffold into multiple projects

Feb 26, 2011 at 1:59 PM
Edited Feb 26, 2011 at 3:53 PM

Hi Steve,

Inspired by your great talk on Tuesday at DotNetDevNet in Bristol, I thought I'd have a look into your scaffolder to solve a particular nag of mine. We use a multi tier system with interfaces and testing all in different projects. Whenever we start a new module we have to create the same structure every time, controller + Unit test fixture, view,model, BLL + Interface + Unit tests fixture, DAL + Interface + Unit test fixture etc .....

What I like to do is scaffold all of this given a base name/tag used throughout. What I cant figure out is if it is possible to push the output files into different projects (into the csproj).

Ideally what I'd like to do is have all of the scaffold templates in a seperate project and when it is executed push the results into all of the differernt projects in the solution.

 

I suspect I need to do something with the -Project paramemter to Scaffold but am not sure how to use it or if it will write to any project

Any ideas on how to achieve this would be really helpful.

Thanks

Christian

Mar 22, 2011 at 2:02 AM

I second this request

Mar 22, 2011 at 12:22 PM

FYI - Here is what I ended up doing. Still work inprogress....

In a new file called StandardModule.ps1 add the following

# USAGE > scaffold StandardModule XYZ

[T4Scaffolding.Scaffolder(Description = "Write out all stub files for a module")][CmdletBinding()]
param(     
	[Parameter(Mandatory=$true)]$ClassTag,   
    [string]$Project,
	[string]$CodeLanguage,
	[string[]]$TemplateFolders,
	[switch]$Force = $false
)

. $TemplateFolders\Utilities.ps1
# ARGS 
# 1) TemplateName 
# 2) Projectin solution to insert result into 
# 3) Location and file name of result
ApplyTemplate "SearchController" "Website" Controllers\$ClassTag"Controller"
 
In a new file called Utilities.ps1 add the following:
$source = @"
public class MyHelper
{
    public static string ToLowerFirstChar(string input)
    {
        return char.ToLower(input[0]) + input.Substring(1);
    }
}
"@

Add-Type -TypeDefinition $source

$variableTag = [MyHelper]::ToLowerFirstChar($ClassTag)

# $TemplateFolders = $TemplateFolders\..\Templates

function ApplyTemplate([string]$templateFileName, [string]$outputProject, [string]$outputPath)
{
	$templateFile = Find-ScaffolderTemplate $templateFileName -TemplateFolders $TemplateFolders -CodeLanguage $CodeLanguage -ErrorIfNotFound
	if ($templateFile) {
		$wroteFile = Invoke-ScaffoldTemplate -Template $templateFile -Model @{ ClassTag = $ClassTag; VariableTag = $variableTag } -Project $outputProject -OutputPath $outputPath -Force:$Force 
		if($wroteFile) {
			Write-Host "Added $outputProject '$wroteFile'"
		}
	}
}
 
In a new file called SearchController.cs.t4 under a "Templates" folder add the following:
<#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #>
<#@ Output Extension="cs" #>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Blah.<#= Model.ClassTag #>
{
    public class <#= Model.ClassTag #>Controller
    {
        int <#= Model.VariableTag #>;
    }
}
Now run "scaffold StandardModule XYZ" in package manager
Apr 6, 2011 at 12:04 AM
Did you make further imporvements to this? Are you blogging about your experiences?
Anybody else doing the same?
Thanks,
Rad
Apr 7, 2011 at 7:26 PM

raca07, Sorry but nothing new to report. I have not changed the code from that shown above and no I am not blogging about it.
Did you have any luck with these code snippits?