Skip to content

Instantly share code, notes, and snippets.

@belotn
Created October 26, 2020 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belotn/eccc4202b8675de435d26f02e78dab89 to your computer and use it in GitHub Desktop.
Save belotn/eccc4202b8675de435d26f02e78dab89 to your computer and use it in GitHub Desktop.
Script to check formatting and best practice before committing
######################################################################
# SCRIPT: Test-CodeQuality.ps1 #
######################################################################
# Purpose #
# Script to check formatting and best practice before committing #
######################################################################
# Author : Nicolas #
######################################################################
# ChangeLog #
# 20201026 - Initial Release #
# TODO: Prevent commit in case of analyze error #
# FIXME: Formatting and Analyze informations isn't send to repository#
# git add -u <path> : do not seems to work #
# git update-index neither #
# FIXED/ forget to copy new file version under hooks #
######################################################################
Param(
[switch]$StandAlone,
[string]$Files
)
######################################################################
# Modules #
######################################################################
Import-Module PSScriptAnalyzer
######################################################################
# Variable #
######################################################################
$PSSA = (Get-Module PSScriptAnalyzer -ListAvailable).ModuleBase
$PSSASettings = "$PSSA\Settings\CodeFormattingOTBS.psd1"
######################################################################
# Main #
######################################################################
if (-not $standalone) {
Write-Host "From git commit"
$RootDir = git rev-parse --show-toplevel
$FileList = @(git diff --cached --name-only --diff-filter=ACM) | ForEach-Object {
"$Rootdir/$_"
}
} else {
$FileList = $Files
}
# First Step Code Formating
$FileList | ForEach-Object {
Write-Host "Formatting $_"
$Content = Get-Content $_ -Raw
$Content = Invoke-Formatter -ScriptDefinition $Content -Settings $PSSASettings
Set-Content -Value $Content -Path $_
}
# Second Append Warning
$FileList | ForEach-Object {
Write-Host "Analyzing $_"
$status = Invoke-ScriptAnalyzer $_ -Severity Warning -ExcludeRule PSAvoidUsingWriteHost
# Si nous avons déjà fait une analyse, nous la supprimons
$Content = Get-Content $_
$info = $Content | Select-String "^# Analyze"
if ($info) {
#Utilisation de l'operateur range pour récuperer la parti que l'on souhaite
$Content = $content[0..$($info.LineNumber - 4)]
Set-Content -Value $Content -Path $_
}
$StringStatus = "`r`n"
$StringStatus += ("#" * 70) + "`r`n"
$CartoucheAnalyze = "# Analyze "
$StringStatus += $CartoucheAnalyze + (' ' * (70 - $CartoucheAnalyze.length - 1)) + "#`r`n"
$StringStatus += ("#" * 70) + "`r`n"
if ($status) {
$status | group RuleName | sort Name | % {
$StringAnalyze = "# $($_.NAme) occured $($_.Count)"
$StringStatus += $StringAnalyze + (' ' * (70 - $StringAnalyze.length - 1)) + "#`r`n"
}
} else {
$stringAnalyze = "# No report"
$StringStatus += $StringAnalyze + (' ' * (70 - $StringAnalyze.length - 1)) + "#`r`n"
}
$StringStatus += ("#" * 70) + "`r`n"
Add-Content -Value $StringStatus -Path $_
if (-not $StandAlone) {
# $hash = git hash-object -w "$_"
# Write-Host "Hash : $hash"
# git update-index --add --cacheinfo 100644 "$hash" "$_"
git add -u $_
}
}
Exit 0
######################################################################
# Analyze #
######################################################################
# PSAvoidUsingCmdletAliases occured 3 #
# PSUseBOMForUnicodeEncodedFile occured 1 #
######################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment