Skip to content

Instantly share code, notes, and snippets.

@JPRuskin
Last active June 29, 2017 11:43
Show Gist options
  • Select an option

  • Save JPRuskin/a948681b4fc5d901c4ce9cae196a6a5f to your computer and use it in GitHub Desktop.

Select an option

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.
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