Created
March 2, 2011 06:41
-
-
Save davidfowl/850566 to your computer and use it in GitHub Desktop.
Script showing how to use Register-TabExpansion. The sample is adding intellisense for Scaffold-Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function global:Get-Types { | |
param ( | |
$ProjectName, | |
$BaseType, | |
[switch]$IgnoreProjectReferences | |
) | |
if($ProjectName) { | |
$project = Get-Project $ProjectName | |
} | |
else { | |
$project = Get-Project | |
} | |
return GetTypes $project $BaseType $IgnoreProjectReferences | |
} | |
function GetTypes($project, $BaseType, $IgnoreProjectReferences) { | |
$ns = $project.Properties.Item("RootNamespace").Value | |
$ns = $project.CodeModel.CodeElements | ?{ $_.Name -eq $ns } | |
GetTypesFromNamespace $ns | ?{ | |
if($BaseType) { | |
$shortNames = $_.Bases | Select -ExpandProperty Name | |
$shortNames -contains $BaseType | |
} | |
else { | |
$true | |
} | |
} | |
if(!$IgnoreProjectReferences) { | |
$project.Object.References | ?{ $_.SourceProject } | %{ GetTypes $_.SourceProject $BaseType $IgnoreProjectReferences } | |
} | |
} | |
function GetTypesFromNamespace($ns) { | |
$ns.Members | %{ | |
if($_.Kind -eq 5) { | |
GetTypesFromNamespace $_ | |
} | |
elseif($_.Kind -eq 1) { | |
$_ | |
} | |
} | |
} | |
function TypeHasID($type) { | |
$memberNames = $type.Members | Select -ExpandProperty Name | |
$convention = "$($type.Name)Id" | |
$hasKeyAttribute = $type.Members | ?{ | |
$attrs = $_.Attributes | Select -ExpandProperty FullName | |
$attrs -contains "System.ComponentModel.DataAnnotations.KeyAttribute" | |
} | |
$memberNames -contains "Id" -or $memberNames -contains $convention -or $hasKeyAttribute | |
} | |
function global:Scaffold-Controller($ModelType, $DbContextType, $ControllerName, [switch]$Force) { | |
if(!$ControllerName) { | |
$ControllerName = $ModelType | |
} | |
if($Force) { | |
Scaffold Controller -ModelType $ModelType -DbContextType $DbContextType -Repository -ControllerName $ControllerName -Force | |
} | |
else { | |
Scaffold Controller -ModelType $ModelType -DbContextType $DbContextType -Repository -ControllerName $ControllerName | |
} | |
} | |
Register-TabExpansion 'Scaffold-Controller' @{ | |
'DbContextType' = { Get-Types -BaseType DbContext | Select -ExpandProperty Name } | |
'ModelType' = { Get-Types | ?{ TypeHasID $_ } | Select -ExpandProperty Name } | |
'ControllerName' = { Get-Types -BaseType Controller | Select -ExpandProperty Name } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment