From 22e18e91cf3b95e954d05e54199b0838943e3794 Mon Sep 17 00:00:00 2001 From: ztrhgf Date: Tue, 12 Oct 2021 20:09:37 +0200 Subject: [PATCH] warn about deleted used aliases --- repo_content_set_up/.githooks/pre-commit.ps1 | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/repo_content_set_up/.githooks/pre-commit.ps1 b/repo_content_set_up/.githooks/pre-commit.ps1 index c706117..b58ec40 100644 --- a/repo_content_set_up/.githooks/pre-commit.ps1 +++ b/repo_content_set_up/.githooks/pre-commit.ps1 @@ -793,7 +793,7 @@ try { # - # warn about deleted ps1 script (that defines function for module generation), in case it is used somewhere in repository + # warn about deleted function & alias in case it is used somewhere in repository if ($commitedDeletedPs1) { $commitedDeletedPs1 = $commitedDeletedPs1 -replace "/", "\" $commitedDeletedPs1 | Where-Object { $_ -match "scripts2module\\" } | ForEach-Object { @@ -808,6 +808,35 @@ try { _WarningAndExit "Deleted function $funcName is mentioned in following scripts:`n$($fileFuncUsed -join "`n")" } + + # get all aliases defined in last function version + $functionScriptUnixPath = $_ -replace "\\", "/" + $lastCommitContent = _startProcess git "show HEAD:$functionScriptUnixPath" + if (!$lastCommitContent -or $lastCommitContent -match "^fatal: ") { + Write-Warning "Previous version of function $funcName cannot be found (to check deleted aliases)." + } else { + $gitAST = [System.Management.Automation.Language.Parser]::ParseInput(($lastCommitContent -join "`n"), [ref]$null, [ref]$null) + + $deletedAlias = _getAliasAST $gitAST $funcName + } + + if ($deletedAlias) { + $deletedAlias | % { + $alias = $_ + $escFuncName = [regex]::Escape($funcName) + $escAlias = [regex]::Escape($alias) + # get all files where changed function is mentioned (even in comments) + $fileUsed = git.exe grep --ignore-case -l "\b$escAlias\b" + # exclude scripts where this alias is defined + $fileUsed = $fileUsed | Where-Object { $_ -notmatch "/$escFuncName\.ps1" } + + if ($fileUsed) { + $fileUsed = $fileUsed -replace "/", "\" + + _WarningAndExit "Alias '$alias' of function $funcName was deleted, but is still used in following scripts:`n$($fileUsed -join "`n")" + } + } + } } #TODO kontrola funkci v profile.ps1? viz AST sekce https://devblogs.microsoft.com/scripting/learn-how-it-pros-can-use-the-powershell-ast/ }