|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Creates a new namespace object. |
| 4 | +.DESCRIPTION |
| 5 | + Creates a new namespace object. |
| 6 | + |
| 7 | + Namespaces can be created from anything. |
| 8 | +#> |
| 9 | +param() |
| 10 | + |
| 11 | +$myUnrolledArguments = @(@($args) | . { process { $_ }}) |
| 12 | + |
| 13 | +$primitiveNamespaceTypes = [regex], [IO.FileInfo], [IO.DirectoryInfo], [uri], [Management.Automation.PSModuleInfo] |
| 14 | + |
| 15 | + |
| 16 | +if ($myUnrolledArguments.Length -eq 0) { |
| 17 | + # Default behavior, returning nothing |
| 18 | +} else { |
| 19 | + |
| 20 | + # Make sure the namespace name is blank by default |
| 21 | + $namespaceName = '' |
| 22 | + for ($argumentIndex = 0; $argumentIndex -lt $myUnrolledArguments.Length; $argumentIndex++) { |
| 23 | + $argument = $myUnrolledArguments[$argumentIndex] |
| 24 | + $nextArgument = $myUnrolledArguments[$argumentIndex + 1] |
| 25 | + |
| 26 | + # If we have a string and the next argument is not a string, we have a namespace name |
| 27 | + if ($argument -is [string] -and $nextArgument -and $nextArgument -isnot [string]) { |
| 28 | + $namespaceName = $argument |
| 29 | + $argumentIndex++ |
| 30 | + # and we can move to the next argument |
| 31 | + $argument = $nextArgument |
| 32 | + } |
| 33 | + |
| 34 | + if ($null -eq $argument) { continue } |
| 35 | + |
| 36 | + # If the argument is a string (not followed by a non-string), treat it as a pattern |
| 37 | + if ($argument -is [string]) { |
| 38 | + $newNamespace = [PSCustomObject]@{PSTypeName='Namespace'} |
| 39 | + $newNamespace.Pattern = $argument |
| 40 | + $newNamespace |
| 41 | + } |
| 42 | + elseif ($argument.GetType() -in $primitiveNamespaceTypes) { |
| 43 | + $argumentShallowCopy = [PSObject]::new($argument) |
| 44 | + $argumentShallowCopy.pstypenames.insert(0, 'Namespace') |
| 45 | + if ($namespaceName) { |
| 46 | + $newNamespace.psobject.Properties.Add([PSNoteProperty]::new('Name', $namespaceName),$true) |
| 47 | + } |
| 48 | + $argumentShallowCopy |
| 49 | + } |
| 50 | + else { |
| 51 | + |
| 52 | + $argumentDeepCopy = |
| 53 | + [Management.Automation.LanguagePrimitives]::ConvertPSObjectToType($argument, [PSObject], $true, $null, $true) |
| 54 | + $argumentDeepCopy.pstypenames.insert(0, 'Namespace') |
| 55 | + |
| 56 | + if ($argumentDeepCopy.Pattern -and $argumentDeepCopy.Pattern -isnot [regex]) { |
| 57 | + $argumentDeepCopy.Pattern = [regex]::new($argumentDeepCopy.Pattern,'IgnoreCase,IgnorePatternWhitespace','00:00:00.01') |
| 58 | + } elseif (-not $argumentDeepCopy.Pattern) { |
| 59 | + # If we don't have a pattern, let's figure out a good default. |
| 60 | + if ($argumentDeepCopy.CommandPattern) { |
| 61 | + $argumentDeepCopy.Pattern = $argumentDeepCopy.CommandPattern |
| 62 | + } elseif ($argumentDeepCopy.FilePattern) { |
| 63 | + $argumentDeepCopy.Pattern = $argumentDeepCopy.FilePattern |
| 64 | + } |
| 65 | + } |
| 66 | + if ($namespaceName) { |
| 67 | + $newNamespace.psobject.Properties.Add([PSNoteProperty]::new('Name', $namespaceName),$true) |
| 68 | + } |
| 69 | + $argumentDeepCopy |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments