2

I'm using T4Scaffolding to do some do some C# code generation. Consider the following project structure:

Project structure
I've created a custrom scaffolder in the Templates project. Now I would like to run it for the Dummy project, but I get this exception:

enter image description here

How can I use Custom Scaffolding over projects?

The thing is: I've got 20 projects in TFS and I would like to manage the code of the t4 templates in 1 central location.

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203

2 Answers2

1

I think the trick is to do the scaffolding from the Templates project and pass in the name of the project you want to generate into e.g.

T4Scaffolding.Scaffolder(Description = "Creates a model entity")][CmdletBinding()]
param(
    [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$EntityName,
    [string]$ProjectName,
    [string]$CodeLanguage,
    [string[]]$TemplateFolders,
    [switch]$Force = $false
)

# Find the code project, or abort
$project = Get-Project ( $ProjectName) -ErrorAction SilentlyContinue
if (!$project) { throw "Cannot find code project corresponding to solution '$ProjectName'" }

$entityFile = $EntityName
Add-ProjectItemViaTemplate $entityFile -Template EntityTemplate `
    -Model @{ Namespace = $namespace; EntityName = $EntityName; MappingName = $MappingName } `
    -SuccessMessage "Added Model non-gen output at {0}" `
    -TemplateFolders $TemplateFolders -Project $ProjectName -CodeLanguage $CodeLanguage -Force:$false

You must pass a string to the -Project argument in Add-ProjectItemViaTemplates not a project object

If you already know the projects that the various scaffolders need to apply to, then you can set a default value for ProjectName in the scaffolder.

There's some examples of this in the MvcScaffolding package.

Paul Hatcher
  • 7,342
  • 1
  • 54
  • 51
1

I think it is just be a matter of copying the files and Directories located in CodeTemplates/Scaffolders/BaseSuperModule into the Dummy Project and then calling the Scaffold command.

Edit: That makes perfect sense. I am in the middle of learning how to move custom t4Scaffolding code into a NuGet package. I think that would be the route that would work best for you. I will update my answer as soon as I figure it out.

Edit 2: I found this article and I went through it and have a working example. Give it a try and let me know if I might be able to help further.

http://geekswithblogs.net/michelotti/archive/2011/07/14/leverage-t4scaffolding-for-wcf-web-api.aspx

bytebender
  • 7,371
  • 2
  • 31
  • 54
  • Well, I've got 20 projects in TFS and I would like to manage the code of the t4 templates in 1 central location. Copying the data to all the project wouldn't work. – Kees C. Bakker Feb 23 '12 at 08:49
  • I've read the Michelotti article and it shows how powerful T4 can be (that's why I love to learn some t4-fu!). Wrapping them in a package sounds like a cool idea, but I have (yet) no idea how to do something like that. Also, I can't publish this package on a public server. If I get a break-trough on it, I'll let you know. Let me know if you get any closer! :-) – Kees C. Bakker Feb 23 '12 at 19:47
  • I actually have a working prototype. I am just building on it as we speak. Do you have experience with creating Nuget packages? We currently have a Nuget server hosted in our Domain as I work for a bank. It was really easy to set up. – bytebender Feb 23 '12 at 20:36
  • I've never created packages before. I'm now focusing on building the templates right and doing interaction with them, using pure T4Scaffolding (with some `cs.t4` files and a `ps1` file). After I'm done I hope I can find out how to package and distribute them. – Kees C. Bakker Feb 24 '12 at 13:22
  • 2
    Well let me know when you actually get to building your NuGet package :0) – bytebender Feb 27 '12 at 22:03