Advertisement
codeplanner

Get Deriving Types Visual Studio

Mar 12th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ####################################################
  2. # 1: Save as DemoModule.psm1
  3. # 2: In Visual Studio, open package manager console
  4. #   2.1: Register: Import-Module "C:\pathtomodule\DemoModule.psm1"
  5. # 3: Usage (will list all classes in DemoProject that derives BaseEntity and print the names):
  6. #   Get-DerivingTypes DemoProject BaseEntity| ForEach{$_.Name}
  7. # 4: Usage (Will look in all projects in the solution)
  8. #   (Get-Project -All) | ForEach{Get-DerivingTypes $_.ProjectName PersistentEntity | ForEach{$_.Name}}
  9. #
  10. # Tips: Use tab to get projectnames automatically...
  11. #
  12. # To Remove: Remove-Module DemoModule
  13. ####################################################
  14. $global:files = $null
  15. function Get-AllFiles{
  16.     param(
  17.         [parameter(ValueFromPipelineByPropertyName = $true)][string[]]$ProjectName
  18.     )
  19.     # Set the global of files
  20.     $global:files = @()
  21.     Recurse-Project $ProjectName -Action {param($item)
  22.         # ForEach file found in the project... add to list of files
  23.         if($item.Type -eq 'File'){
  24.             $global:files += $item
  25.         }
  26.     }
  27.  
  28.     return $global:files
  29. }
  30.  
  31. function Get-DerivingTypes{
  32.     param(
  33.         [parameter(ValueFromPipelineByPropertyName = $true)][string[]]$ProjectName,
  34.         [parameter(ValueFromPipelineByPropertyName = $true)][string[]]$TypeName
  35.     )
  36.  
  37.     # Get files in the selected project
  38.     $allfiles = Get-AllFiles $ProjectName
  39.  
  40.     $types = @()
  41.     # Extract all namespaces
  42.     $namespaces = $allfiles | ForEach{$_.ProjectItem.FileCodeModel.CodeElements | Where-Object{$_.Kind -eq 5}}
  43.    
  44.     # Extract all children in all namespaces
  45.     $codeelements = $namespaces | ForEach{$_.Children}
  46.  
  47.     # Add types deriving your input type to the list of types.
  48.     $codeelements | ForEach{       
  49.         $current = $_
  50.         $_.Bases | ForEach{
  51.             if($_.Name -eq $TypeName){                         
  52.                 $types += $current     
  53.             }
  54.         }      
  55.     }
  56.     return $types
  57. }
  58.  
  59. function Recurse-Project {
  60.     param(
  61.         [parameter(ValueFromPipelineByPropertyName = $true)]
  62.         [string[]]$ProjectName,
  63.         [parameter(Mandatory = $true)]$Action
  64.     )
  65.     Process {
  66.         # Convert project item guid into friendly name
  67.         function Get-Type($kind) {
  68.             switch($kind) {
  69.                 '{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}' { 'File' }
  70.                 '{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}' { 'Folder' }
  71.                 default { $kind }
  72.             }
  73.         }
  74.        
  75.         # Convert language guid to friendly name
  76.         function Get-Language($item) {
  77.             if(!$item.FileCodeModel) {
  78.                 return $null
  79.             }
  80.            
  81.             $kind = $item.FileCodeModel.Language
  82.             switch($kind) {
  83.                 '{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}' { 'C#' }
  84.                 '{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}' { 'VB' }
  85.                 default { $kind }
  86.             }
  87.         }
  88.        
  89.         # Walk over all project items running the action on each
  90.         function Recurse-ProjectItems($projectItems, $action) {
  91.             $projectItems | %{
  92.                 $obj = New-Object PSObject -Property @{
  93.                     ProjectItem = $_
  94.                     Type = Get-Type $_.Kind
  95.                     Language = Get-Language $_
  96.                 }
  97.                
  98.                 & $action $obj
  99.                
  100.                 if($_.ProjectItems) {
  101.                     Recurse-ProjectItems $_.ProjectItems $action
  102.                 }
  103.             }
  104.         }
  105.        
  106.         if($ProjectName) {
  107.             $p = Get-Project $ProjectName
  108.         }
  109.         else {
  110.             $p = Get-Project
  111.         }
  112.        
  113.         $p | %{ Recurse-ProjectItems $_.ProjectItems $Action }
  114.     }
  115. }
  116.  
  117. Register-TabExpansion 'Get-DerivingTypes' @{
  118.     ProjectName = { Get-Project -All | Select -ExpandProperty Name }
  119.     TypeName = {"PersistentEntity"}
  120. }
  121.  
  122. Export-ModuleMember Get-DerivingTypes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement