Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created March 2, 2011 06:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidfowl/850566 to your computer and use it in GitHub Desktop.
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
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