|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Expands the aliases in either the file or text passed to the function. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Any scripts in 'corporate' or 'formal' use should have any aliases expanded. |
| 7 | + This this removes ambiguity and any potential clashes or errors. |
| 8 | +
|
| 9 | +.PARAMETER Text |
| 10 | + The script text that should parsed for aliases to expand. |
| 11 | +
|
| 12 | +.PARAMETER Directory |
| 13 | + The directory to use for parsing files. |
| 14 | +
|
| 15 | +.PARAMETER Filter |
| 16 | + The filter to use when traversing a directory for files to parse. |
| 17 | +
|
| 18 | +.PARAMETER Files |
| 19 | + The files to expand aliases in. |
| 20 | +
|
| 21 | +.EXAMPLE |
| 22 | + Expand-Aliases -Text "rm file.txt; gi file.exe" |
| 23 | +
|
| 24 | + Should be expanded to "Remove-Item file.txt; Get-Item file.exe" |
| 25 | +
|
| 26 | +.EXAMPLE |
| 27 | + Expand-Aliases -Directory .\tools -Filter "*.ps1" |
| 28 | +
|
| 29 | + Should traverse the tools directory relative to the current |
| 30 | + directory, and expand all aliases in files matching the specified filter. |
| 31 | +
|
| 32 | +.EXAMPLE |
| 33 | + Expand-Aliases -Files ".\file1.ps1","text-File.txt" |
| 34 | +
|
| 35 | + Should expand all aliases in the two specified files. |
| 36 | +
|
| 37 | +.LINK |
| 38 | + https://wormiecorp.github.io/Wormies-AU-Helpers/docs/functions/expand-aliases |
| 39 | +#> |
| 40 | + |
| 41 | +function Expand-Aliases () { |
| 42 | + param( |
| 43 | + [Parameter(Mandatory = $true, ParameterSetName = 'text')] |
| 44 | + [string]$Text, |
| 45 | + [Parameter(Mandatory = $true, ParameterSetName = 'directory')] |
| 46 | + [string]$Directory, |
| 47 | + [Parameter(Mandatory = $false, ParameterSetName = 'directory')] |
| 48 | + [string]$Filter = "*.ps1", |
| 49 | + [Parameter(Mandatory = $true, ParameterSetName = 'files')] |
| 50 | + [string[]]$Files |
| 51 | + ) |
| 52 | + |
| 53 | + BEGIN { |
| 54 | + $moduleExists = Get-Module chocolateyInstaller -All -ErrorAction SilentlyContinue |
| 55 | + if (!$moduleExists) { |
| 56 | + Import-Module "$env:ChocolateyInstall\helpers\chocolateyInstaller.psm1" -ErrorAction SilentlyContinue -Force -WarningAction SilentlyContinue |
| 57 | + } |
| 58 | + $aliases = Get-Alias | Group-Object -AsHashTable -Property Name |
| 59 | + $ParserErrors = $null |
| 60 | + } |
| 61 | + |
| 62 | + PROCESS { |
| 63 | + if ($directory -or $files) { |
| 64 | + $allFiles = if (!$files) { |
| 65 | + Get-ChildItem -LiteralPath $directory -Filter $filter -Recurse |
| 66 | + } |
| 67 | + else { |
| 68 | + $files | ForEach-Object { Get-Item $_ } |
| 69 | + } |
| 70 | + |
| 71 | + foreach ($file in $allFiles) { |
| 72 | + $oldText = Get-Content -Path $file.FullName -Raw |
| 73 | + $text = Expand-AliasesInText -text $oldText -aliases $aliases -ParserErrors $ParserErrors |
| 74 | + if ($oldText -cne $text) { |
| 75 | + [System.IO.File]::WriteAllText($file.FullName, $text, [System.Text.Encoding]::UTF8) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + else { |
| 80 | + $text = Expand-AliasesInText -text $text -aliases $aliases -ParserErrors $ParserErrors |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + END { |
| 85 | + if (!$moduleExists) { |
| 86 | + Remove-Module chocolateyInstaller -ErrorAction SilentlyContinue -Force |
| 87 | + } |
| 88 | + if (!$directory -and !$Files) { |
| 89 | + $Text |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments