Functions/Get-Project.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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# =========================================================================== # Get-Projects.ps1 -------------------------------------------------------- # =========================================================================== # validation -------------------------------------------------------------- # --------------------------------------------------------------------------- Class ValidateProjectAlias: System.Management.Automation.IValidateSetValuesGenerator { [String[]] GetValidValues() { return [String[]] ((Get-ProjectList -Unformatted | Select-Object -ExpandProperty "Alias") + "") } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function Get-ConfigurationFile { <# .DESCRIPTION Returns the path of a single module project configuration file. .PARAMETER Type .OUTPUTS System.String. Path of project configuration file. #> [CmdletBinding(PositionalBinding=$True)] [OutputType([Void])] Param ( [Parameter(Position=1, HelpMessage="Existing project type.")] [System.String] $Type="Project" ) Process { return Join-Path -Path $SciProfile.ProjectDir -ChildPath "$($Type.ToLower()).json" } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function New-ConfigurationFile { <# .DESCRIPTION Returns an object containing projects of all types found in modules project directory. .OUTPUTS System.String. Path of project configuration file. #> [CmdletBinding(PositionalBinding=$True)] [OutputType([PSCustomObject])] Param () Process { $project_name = "Project" $project_file = Get-ConfigurationFile -Type $project_name if (Test-Path -Path $project_file) { Remove-Item -Path $project_file -Force } $project_list = @() Get-ChildItem -Path $SciProfile.ProjectDir -Filter "*.json" | ForEach-Object{ $fileData = Get-Content $_ | ConvertFrom-Json $project_list += $fileData | Select-Object -Property $SciProfile.Format } $project_json = $(ConvertTo-Json $project_list) Out-File -FilePath $project_file -InputObject $project_json return $project_file } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function Get-ProjectList { <# .DESCRIPTION Returns all entries of each user defined project type. .PARAMETER Type .PARAMETER Unformatted .OUTPUTS PSCustomObject. All project entries related to specified type. #> [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Position=1, HelpMessage="Existing project type.")] [System.String] $Type="Project", [Parameter(HelpMessage="Does not return information as readable table.")] [Switch] $Unformatted ) Process { $project_file = Get-ConfigurationFile -Type $Type if ($Type -eq "Project" -and -not $(Test-Path -Path $project_file)) { New-ConfigurationFile } $project_list = Get-Content $project_file | ConvertFrom-Json if ($Unformatted) { return $project_list } return Format-Project -Type $Type -Projects $project_list } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function Format-Project { <# .DESCRIPTION Returns all entries of each user defined project type as formatted table. .PARAMETER Type .PARAMETER Projects .OUTPUTS Format. All project entries as formatted table. #> [CmdletBinding(PositionalBinding=$True)] [OutputType([PSCustomObject])] Param ( [Parameter(Position=1, HelpMessage="Existing project type.")] [System.String] $Type="Project", [Parameter(Position=2, HelpMessage="Project list with all entries of specified project type")] [PSCustomObject] $Projects ) Process { return $Projects | Format-Table -Property $SciProfile.Format } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function Test-Project { <# .DESCRIPTION Check whether a list of projects contains a entry with specified identifier. .PARAMETER Name .PARAMETER Projects .OUTPUTS PSCustomObject. Project which contains the specified identifier. #> [CmdletBinding(PositionalBinding=$True)] [OutputType([PSCustomObject])] Param ( [Parameter(Position=1, HelpMessage="Name or alias of an project.")] [System.String] $Name, [Parameter(Position=2, HelpMessage="Project list with all entries of specified project type")] [PSCustomObject] $Projects ) Process { $project = $Projects | Where-Object {$_.Name -eq $Name -or $_.Alias -eq $Name} if ($project) { return $project } } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function Get-ProjectType { <# .DESCRIPTION Get project type of specified identifier. .PARAMETER Name .OUTPUTS System.String. Project type of specified identifier #> [CmdletBinding(PositionalBinding=$True)] [OutputType([PSCustomObject])] Param( [Parameter(Position=1, HelpMessage="Name or alias of an project.")] [System.String] $Name ) $project_list = Get-ProjectList -Unformatted $project = Test-Project -Name $Name -Projects $project_list if ($project) { return $project | Select-Object -ExpandProperty "Type" } else { Write-FormattedError -Message "No entry with user specification '$($Name)' was found in project type '$($Type)'." -Module $SciProfile.Name return } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function Select-Project { <# .DESCRIPTION Returns a project entry as dictionary for a given. .PARAMETER Name .PARAMETER Property .PARAMETER Project .OUTPUTS PSCustomObject. All project entries as formatted table. #> [CmdletBinding(PositionalBinding=$True)] [OutputType([PSCustomObject])] Param ( [Parameter(Position=1, HelpMessage="Name or alias of an project.")] [System.String] $Name, [Parameter(Position=2, HelpMessage="Property of project entry, which shall be returned.")] [System.String] $Property, [Parameter(Position=1, HelpMessage="Existing project type.")] [System.String] $Type="Project" ) Process{ $project_list = Get-ProjectList -Type $Type -Unformatted $project = Test-Project -Name $Name -Projects $project_list if ($project.($Property)) { return $project | Select-Object -ExpandProperty $Property } else { Write-FormattedError -Message "No property '$($Property)' for user specification '$($Name)' was found in project type '$($Type)'." -Module $SciProfile.Name return } } } |