Last active
June 29, 2017 11:43
-
-
Save JPRuskin/a948681b4fc5d901c4ce9cae196a6a5f to your computer and use it in GitHub Desktop.
Converts a given script or directory of PS1 files to One True Brace Style formatting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function ConvertTo-OTBStyle { | |
| #Requires -Modules @{ModuleName='PSScriptAnalyzer';ModuleVersion='1.15.0'} | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory, ParameterSetName = 'Directory')] | |
| [ValidateScript( {Test-Path -Path $_ -PathType Container})] | |
| [IO.DirectoryInfo]$Directory, | |
| [Parameter(ParameterSetName = 'Directory')] | |
| [Switch]$Recurse, | |
| [Parameter(Mandatory, ParameterSetName = 'File', ValueFromPipeline, ValueFromPipelineByPropertyName)][Alias('FullName')] | |
| [IO.FileInfo[]]$File | |
| ) | |
| begin { | |
| if ($PSCmdlet.ParameterSetName -eq 'Directory') { | |
| $File = Get-ChildItem -Path $Directory -Recurse:$Recurse -Filter '*.ps*1' | |
| Write-Verbose "[$($File.Count)] eligible files found in [$Directory]" | |
| } | |
| $RulesDirectory = Join-Path -Path (Get-Module PSScriptAnalyzer -ListAvailable).ModuleBase -ChildPath 'Settings' | |
| $Settings = Import-PowerShellDataFile (Join-Path $RulesDirectory 'CodeFormattingOTBS.psd1') | |
| } | |
| process { | |
| foreach ($Script in $File) { | |
| Write-Verbose "Formatting [$($Script.FullName)]" | |
| $Content = Get-Content -Path $Script.FullName -Raw | |
| Invoke-Formatter -Settings $Settings -ScriptDefinition $Content | Out-File $Script.FullName | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment