Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####################################################
- # 1: Save as DemoModule.psm1
- # 2: In Visual Studio, open package manager console
- # 2.1: Register: Import-Module "C:\pathtomodule\DemoModule.psm1"
- # 3: Usage (will list all classes in DemoProject that derives BaseEntity and print the names):
- # Get-DerivingTypes DemoProject BaseEntity| ForEach{$_.Name}
- # 4: Usage (Will look in all projects in the solution)
- # (Get-Project -All) | ForEach{Get-DerivingTypes $_.ProjectName PersistentEntity | ForEach{$_.Name}}
- #
- # Tips: Use tab to get projectnames automatically...
- #
- # To Remove: Remove-Module DemoModule
- ####################################################
- $global:files = $null
- function Get-AllFiles{
- param(
- [parameter(ValueFromPipelineByPropertyName = $true)][string[]]$ProjectName
- )
- # Set the global of files
- $global:files = @()
- Recurse-Project $ProjectName -Action {param($item)
- # ForEach file found in the project... add to list of files
- if($item.Type -eq 'File'){
- $global:files += $item
- }
- }
- return $global:files
- }
- function Get-DerivingTypes{
- param(
- [parameter(ValueFromPipelineByPropertyName = $true)][string[]]$ProjectName,
- [parameter(ValueFromPipelineByPropertyName = $true)][string[]]$TypeName
- )
- # Get files in the selected project
- $allfiles = Get-AllFiles $ProjectName
- $types = @()
- # Extract all namespaces
- $namespaces = $allfiles | ForEach{$_.ProjectItem.FileCodeModel.CodeElements | Where-Object{$_.Kind -eq 5}}
- # Extract all children in all namespaces
- $codeelements = $namespaces | ForEach{$_.Children}
- # Add types deriving your input type to the list of types.
- $codeelements | ForEach{
- $current = $_
- $_.Bases | ForEach{
- if($_.Name -eq $TypeName){
- $types += $current
- }
- }
- }
- return $types
- }
- function Recurse-Project {
- param(
- [parameter(ValueFromPipelineByPropertyName = $true)]
- [string[]]$ProjectName,
- [parameter(Mandatory = $true)]$Action
- )
- Process {
- # Convert project item guid into friendly name
- function Get-Type($kind) {
- switch($kind) {
- '{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}' { 'File' }
- '{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}' { 'Folder' }
- default { $kind }
- }
- }
- # Convert language guid to friendly name
- function Get-Language($item) {
- if(!$item.FileCodeModel) {
- return $null
- }
- $kind = $item.FileCodeModel.Language
- switch($kind) {
- '{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}' { 'C#' }
- '{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}' { 'VB' }
- default { $kind }
- }
- }
- # Walk over all project items running the action on each
- function Recurse-ProjectItems($projectItems, $action) {
- $projectItems | %{
- $obj = New-Object PSObject -Property @{
- ProjectItem = $_
- Type = Get-Type $_.Kind
- Language = Get-Language $_
- }
- & $action $obj
- if($_.ProjectItems) {
- Recurse-ProjectItems $_.ProjectItems $action
- }
- }
- }
- if($ProjectName) {
- $p = Get-Project $ProjectName
- }
- else {
- $p = Get-Project
- }
- $p | %{ Recurse-ProjectItems $_.ProjectItems $Action }
- }
- }
- Register-TabExpansion 'Get-DerivingTypes' @{
- ProjectName = { Get-Project -All | Select -ExpandProperty Name }
- TypeName = {"PersistentEntity"}
- }
- Export-ModuleMember Get-DerivingTypes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement