OCram85/DroneHelper
OCram85
/
DroneHelper
Archived
1
0
Fork 0
This repository has been archived on 2023-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
DroneHelper/src/Helper/Set-EOL.ps1

60 lines
1.8 KiB
PowerShell

function Set-EOL {
<#
.SYNOPSIS
Helper function to set the EOL sequence to LF or CRLF.
.DESCRIPTION
Helper for changing the EOL independent to the current OS defaults.
.PARAMETER Style
Optional style parameter for `unix` or `win.`. Default is `unix`.
.PARAMETER Path
Mandatory path for target file.
.INPUTS
[None] No pipeline input.
.OUTPUTS
[DroneHelper.Repo.Path] Returns a folder structured like object with relevant full paths.s
.EXAMPLE
Import-Module -Name DroneHelper; Set-EOL -Path './Readme.md'
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions',
'',
Justification = 'system state does not change permanent in temp build clients.'
)]
param (
[Parameter(Mandatory = $false)]
[ValidateSet('unix', 'win')]
[String]$Style = 'unix',
[Parameter(Mandatory = $true)]
[System.IO.FileInfo]$Path
)
process {
if (!(Test-Path $Path.FullName)) {
Write-Error -Message ('{0} not found!' -f $Path.FullName) -ErrorAction Stop
}
switch ($Style) {
'unix' {
$eol = "`n"
Write-Verbose -Message ('Reading {0}' -f $Path.FullName)
$text = [IO.File]::ReadAllText($Path.FullName) -replace "`r`n", $eol
Write-Debug -Message $text
}
'win' {
$eol = "`r`n"
$text = [IO.File]::ReadAllText($Path.FullName) -replace "`n", $eol
}
}
Write-Verbose -Message ("Writing back {0}" -f $Path.FullName)
[IO.File]::WriteAllText($Path.FullName, $text)
}
}