Private/Class.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
function New-PSClassModel { param ( [Parameter(Mandatory=$true)] [string] $ClassPath, [Parameter(Mandatory=$true)] [string] $RootPath ) $class = (New-Object -TypeName PSObject -Property @{ Path = $ClassPath Name = [System.IO.Path]::GetFileNameWithoutExtension($ClassPath) Namespace = (Get-PSClassNamespace -ClassPath $ClassPath -RootPath $RootPath) Added = $false DependentOn = @() UsedBy = @() }) $class | Add-Member -MemberType ScriptMethod -Name 'Content' -Value { return @(Get-Content -Path $this.Path -Force -Encoding UTF8) } $class | Add-Member -MemberType ScriptMethod -Name 'Link' -Value { param ( [Parameter(Mandatory=$true)] $Class ) # add dependency $deps = @($this.DependentOn | Select-Object -ExpandProperty Namespace) if ($deps -inotcontains $Class.Namespace) { $this.DependentOn += $Class } # add used by $used = @($Class.UsedBy | Select-Object -ExpandProperty Namespace) if ($used -inotcontains $this.Namespace) { $Class.UsedBy += $this } } $class | Add-Member -MemberType ScriptMethod -Name 'AllDependencies' -Value { $dependencies = @() $visitor = @() # setup dependencies $this.DependentOn | ForEach-Object { $dependencies += @{ Class = $_ Links = @($this.Namespace) } } # find all dependencies and links for ($i = 0; $i -lt $dependencies.Length; $i++) { $class = $dependencies[$i].Class if ($visitor -inotcontains $class.Namespace) { foreach ($dep in $class.DependentOn) { $dependencies += @{ Class = $dep Links = ($dependencies[$i].Links + $class.Namespace) } } $visitor += $class.Namespace } } return $dependencies } return $class } function Get-PSClassNamespace { param ( [Parameter(Mandatory=$true)] [string] $ClassPath, [Parameter(Mandatory=$true)] [string] $RootPath ) $Path = ($ClassPath.Replace($RootPath, [string]::Empty)).Trim('\/') $Parent = (Split-Path -Parent -Path $Path) $File = [System.IO.Path]::GetFileNameWithoutExtension($Path) if ([string]::IsNullOrWhiteSpace($Parent)) { $namepsace = ($File -replace '(\\|/)', '.') } else { $namepsace = ((Join-Path $Parent $File) -replace '(\\|/)', '.') } return $namepsace } function Get-PSClassModels { param ( [Parameter(Mandatory=$true)] [string] $RootPath ) return @((Get-ChildItem -Path $RootPath -Filter '*.ps1' -Recurse -Force).FullName | ForEach-Object { New-PSClassModel -ClassPath $_ -RootPath $RootPath }) } |