0

I am passing values to first two parameters of T4Scaffolding.Scaffolder. But it does not work what I excepted.

This is the powershell script file. Lets give it a name as Test.ps1

[T4Scaffolding.Scaffolder(Description = "Enter a description of Service here")][CmdletBinding()]
param(        
    [string]$ClassName,
    [string]$Project,
    [string]$CodeLanguage,
    [string[]]$TemplateFolders,
    [switch]$Force = $false
)

Write-Host "Class name is " $ClassName
Write-host "Project name is" $Project

Then I run it on package manager console as following

PM> Scaffold Test Member ConsoleApplication1.Domain

It prints me ClassName correctly but the project name is the one that is selected on nuget package manager console not the one that I give.

Class name is  Member
Project name is ConsoleApplication1.Scaffolder

But the excepted result is

Class name is  Member
Project name is ConsoleApplication1.Domain

Now the problem is how to pass ClassName and Project as parameter ? It just takes ClassName as the parameter but ignoring second one.

EDITED

Also tried this one but it did not work either.

Scaffold Service -ClassName Member -Project ConsoleApplication1.Domain
Barış Velioğlu
  • 5,709
  • 15
  • 59
  • 105
  • Interesting. The last 4 params of scaffolder method comes from default ($ProjectName, $CodeLanguage, $TemplateFolders and $Force). When I add a new parameter as $ProjectName it works as I excepted. But the default $Project parameter is always the one selected on the package manager console for me. – Barış Velioğlu May 18 '13 at 23:06

2 Answers2

0

Calling your script from the powershell console, after removing the T4Scaffolding attribute, called Scaffold.ps1 like this:

.\Scaffold Test Member ConsoleApplication1.Domain

Will return

Class name is  Test
Project name is Member

Which is as expected. I'm not sure why you say you get different results, but even your expected results are not to be expected.

Calling it like so:

Scaffold Service -ClassName Member -Project ConsoleApplication1.Domain

Does give your expected results

Class name is  Member
Project name is ConsoleApplication1.Domain

$CodeLanguage will be Service

So maybe it's the NuGet package manager console doing something different

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
0

The default $Project parameter is as you already have stated the project selected in the PMC.

To scaffold code into another project (or add a new project) you can do like this...

[T4Scaffolding.Scaffolder(Description = "Description here...")][CmdletBinding()]
param([parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$ProjectName = "",     
[string]$Project,
[string]$CodeLanguage,
[string[]]$TemplateFolders,
[switch]$Force = $false)

Note that the "ProjectName" will be retrieved from the pipeline by property name... So the following.

Scaffold Service -projectname SomeProjectName

Will set the value of $ProjectName to "SomeProjectName"..

After this you can do whatever you like with this name... Add code to the project if it already exists or add the project it self if it is not yet in the solution.

Example:

#
#Add new project if it does not exist
#
if(($DTE.Solution.Projects | Select-Object -ExpandProperty Name) -notcontains $ProjectName){
Write-Host "Adding new project"
$sln = [System.IO.Path]::GetFilename($dte.DTE.Solution.FullName)
$path = $dte.DTE.Solution.FullName.Replace($sln,'').Replace('\\','\')
$sln = Get-Interface $dte.Solution ([EnvDTE80.Solution2])
$templatePath = $sln.GetProjectTemplate("ClassLibrary.zip","CSharp")
$sln.AddFromTemplate($templatePath, $path+$ProjectName,$ProjectName)
$file = Get-ProjectItem "Class1.cs" -Project $ProjectName
$file.Remove()
Uffe
  • 2,275
  • 1
  • 13
  • 9