Add Invoke-PSScriptAnalyzer call to remove whitespace from files

This commit is contained in:
Ben Claussen 2023-03-10 14:39:29 -05:00
parent e2a5157790
commit 5d6fb23e91

View file

@ -7,10 +7,10 @@
into a single PSM1 file in the NetboxPS directory. into a single PSM1 file in the NetboxPS directory.
By default, this script will increment version by 0.0.1 By default, this script will increment version by 0.0.1
.PARAMETER SkipVersion .PARAMETER SkipVersion
Do not increment the version. Do not increment the version.
.PARAMETER VersionIncrease .PARAMETER VersionIncrease
Increase the version by a user defined amount Increase the version by a user defined amount
@ -19,10 +19,10 @@
.PARAMETER Environment .PARAMETER Environment
A description of the Environment parameter. A description of the Environment parameter.
.PARAMETER ResetCurrentEnvironment .PARAMETER ResetCurrentEnvironment
A description of the ResetCurrentEnvironment parameter. A description of the ResetCurrentEnvironment parameter.
.EXAMPLE .EXAMPLE
Use all defaults and concatenate all files Use all defaults and concatenate all files
@ -46,17 +46,18 @@ param
[Parameter(ParameterSetName = 'SetVersion')] [Parameter(ParameterSetName = 'SetVersion')]
[version]$NewVersion, [version]$NewVersion,
[ValidateSet('dev', 'development', 'prod', 'production', IgnoreCase = $true)] [ValidateSet('dev', 'development', 'prod', 'production', IgnoreCase = $true)]
[string]$Environment = 'development', [string]$Environment = 'development',
[switch]$ResetCurrentEnvironment [switch]$ResetCurrentEnvironment
) )
Write-Host "Beginning deployment" -ForegroundColor Green Write-Host "Beginning deployment" -ForegroundColor Green
Write-Host "Importing required modules" -ForegroundColor Green Write-Host "Importing required modules" -ForegroundColor Green
Import-Module "Microsoft.PowerShell.Utility" -ErrorAction Stop
Import-Module "PSScriptAnalyzer", "Microsoft.PowerShell.Utility" -ErrorAction Stop
$ModuleName = 'NetboxPS' $ModuleName = 'NetboxPS'
$ConcatenatedFilePath = "$PSScriptRoot\concatenated.ps1" $ConcatenatedFilePath = "$PSScriptRoot\concatenated.ps1"
$FunctionPath = "$PSScriptRoot\Functions" $FunctionPath = "$PSScriptRoot\Functions"
@ -64,12 +65,17 @@ $OutputDirectory = "$PSScriptRoot\$ModuleName"
$PSD1OutputPath = "$OutputDirectory\$ModuleName.psd1" $PSD1OutputPath = "$OutputDirectory\$ModuleName.psd1"
$PSM1OutputPath = "$OutputDirectory\$ModuleName.psm1" $PSM1OutputPath = "$OutputDirectory\$ModuleName.psm1"
Write-Host "Removing whitespace from files" -ForegroundColor Green
Invoke-ScriptAnalyzer -Path $FunctionPath -IncludeRule 'PSAvoidTrailingWhitespace' -Recurse -Fix
Write-Host "Concatenating [$($PS1FunctionFiles.Count)] PS1 files from $FunctionPath"
$PS1FunctionFiles = Get-ChildItem $FunctionPath -Filter "*.ps1" -Recurse | Sort-Object Name $PS1FunctionFiles = Get-ChildItem $FunctionPath -Filter "*.ps1" -Recurse | Sort-Object Name
"" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 "" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8
$Counter = 0 $Counter = 0
Write-Host "Concatenating [$($PS1FunctionFiles.Count)] PS1 files from $FunctionPath"
foreach ($File in $PS1FunctionFiles) { foreach ($File in $PS1FunctionFiles) {
$Counter++ $Counter++
@ -89,17 +95,11 @@ foreach ($File in $PS1FunctionFiles) {
"" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 -Append "" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 -Append
if (-not (Test-Path $OutputDirectory)) {
try {
Write-Warning "Creating path [$OutputDirectory]"
$null = New-Item -Path $OutputDirectory -ItemType Directory -Force
} catch {
throw "Failed to create output directory [$OutputDirectory]: $($_.Exception.Message)"
}
}
Write-Host " Adding psm1" Write-Host " Adding psm1"
Get-Content "$PSScriptRoot\$ModuleName.psm1" | Out-File -FilePath $ConcatenatedFilePath -Encoding UTF8 -Append Get-Content "$PSScriptRoot\$ModuleName.psm1" | Out-File -FilePath $ConcatenatedFilePath -Encoding UTF8 -Append
$PSDManifest = Import-PowerShellDataFile -Path "$PSScriptRoot\$ModuleName.psd1" $PSDManifest = Import-PowerShellDataFile -Path "$PSScriptRoot\$ModuleName.psd1"
# Get the version from the PSD1 # Get the version from the PSD1
[version]$CurrentVersion = $PSDManifest.ModuleVersion [version]$CurrentVersion = $PSDManifest.ModuleVersion
@ -115,6 +115,8 @@ if ($Environment -ilike 'dev*') {
} else { } else {
$UpdateModuleManifestSplat['FunctionsToExport'] = ($PS1FunctionFiles.BaseName | Where-Object { $_ -like '*-*' }) $UpdateModuleManifestSplat['FunctionsToExport'] = ($PS1FunctionFiles.BaseName | Where-Object { $_ -like '*-*' })
} }
Write-Host "Comparing versions" Write-Host "Comparing versions"
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {
"SkipVersion" { "SkipVersion" {
@ -146,24 +148,37 @@ switch ($PSCmdlet.ParameterSetName) {
} }
} }
Write-Host "Updating Module Manifest" Write-Host "Updating Module Manifest"
Update-ModuleManifest @UpdateModuleManifestSplat Update-ModuleManifest @UpdateModuleManifestSplat
if (-not (Test-Path $OutputDirectory)) {
try {
Write-Warning "Creating output directory [$OutputDirectory]"
$null = New-Item -Path $OutputDirectory -ItemType Directory -Force
} catch {
throw "Failed to create output directory [$OutputDirectory]: $($_.Exception.Message)"
}
}
Write-Host " Copying psd1" Write-Host " Copying psd1"
Copy-Item -Path "$PSScriptRoot\$ModuleName.psd1" -Destination $PSD1OutputPath -Force Copy-Item -Path "$PSScriptRoot\$ModuleName.psd1" -Destination $PSD1OutputPath -Force
Write-Host " Copying psm1" Write-Host " Copying psm1"
Copy-Item -Path $ConcatenatedFilePath -Destination $PSM1OutputPath -Force Copy-Item -Path $ConcatenatedFilePath -Destination $PSM1OutputPath -Force
Write-Host "Deployment complete" -ForegroundColor Green Write-Host "Deployment complete" -ForegroundColor Green
if ($ResetCurrentEnvironment) { if ($ResetCurrentEnvironment) {
Write-Warning "Running commands to reset current environment" Write-Warning "Running commands to reset current environment"
if (Get-Module 'NetboxPS') { if (Get-Module 'NetboxPS') {
Remove-Module NetboxPS -Force Remove-Module NetboxPS -Force
} }
Write-Host " Reimporting module" Write-Host " Reimporting module"
Import-Module $PSM1OutputPath, $PSD1OutputPath -Force -ErrorAction Stop Import-Module $PSM1OutputPath, $PSD1OutputPath -Force -ErrorAction Stop
Write-Host "Reset complete" -ForegroundColor Green Write-Host "Reset complete" -ForegroundColor Green
} }