From 394210d41fdad5ae80f8761d62b3b1cc90d34341 Mon Sep 17 00:00:00 2001 From: ili101 Date: Mon, 7 Oct 2019 16:09:37 +0300 Subject: [PATCH] Module.Template 2.0.4 Update --- {__tests__ => CI}/CI.ps1 | 68 ++++++++++++++++++++++++- {__tests__ => CI}/InstallPowerShell.ps1 | 0 {__tests__ => CI}/Publish.ps1 | 1 + appveyor.yml | 18 +++---- azure-pipelines.yml | 12 +++-- 5 files changed, 84 insertions(+), 15 deletions(-) rename {__tests__ => CI}/CI.ps1 (71%) rename {__tests__ => CI}/InstallPowerShell.ps1 (100%) rename {__tests__ => CI}/Publish.ps1 (96%) diff --git a/__tests__/CI.ps1 b/CI/CI.ps1 similarity index 71% rename from __tests__/CI.ps1 rename to CI/CI.ps1 index 5a0e6c5..43b92ce 100644 --- a/__tests__/CI.ps1 +++ b/CI/CI.ps1 @@ -11,7 +11,9 @@ param # AppVeyor Only - Upload results to AppVeyor "Tests" tab. [Switch]$Finalize, # AppVeyor and Azure - Upload module as AppVeyor Artifact. - [Switch]$Artifact + [Switch]$Artifact, + # Azure - Runs PsScriptAnalyzer against one or more folders and pivots the results to form a report. + [Switch]$Analyzer ) $ErrorActionPreference = 'Stop' if ($Initialize) { @@ -139,4 +141,68 @@ if ($Artifact) { #Write-Host "##vso[task.setvariable variable=ModuleName]$ModuleName" Copy-Item -Path $ModulePath -Destination $env:Build_ArtifactStagingDirectory -Recurse } +} +if ($Analyzer) { + if (!(Get-Module -Name PSScriptAnalyzer -ListAvailable)) { + '[Progress] Installing PSScriptAnalyzer.' + Install-Module -Name PSScriptAnalyzer -Force + } + + if ($env:System_PullRequest_TargetBranch) { + '[Progress] Get target branch.' + $TempGitClone = Join-Path ([IO.Path]::GetTempPath()) (New-Guid) + Copy-Item -Path $PWD -Destination $TempGitClone -Recurse + (Get-Item (Join-Path $TempGitClone '.git')).Attributes += 'Hidden' + "[Progress] git clean." + git -C $TempGitClone clean -f + "[Progress] git reset." + git -C $TempGitClone reset --hard + "[Progress] git checkout." + git -C $TempGitClone checkout -q $env:System_PullRequest_TargetBranch + + $DirsToProcess = @{ 'Pull Request' = $PWD ; $env:System_PullRequest_TargetBranch = $TempGitClone } + } + else { + $DirsToProcess = @{ 'GitHub' = $PWD } + } + + "[Progress] Running Script Analyzer." + $AnalyzerResults = $DirsToProcess.GetEnumerator() | ForEach-Object { + $DirName = $_.Key + Write-Verbose "[Progress] Running Script Analyzer on $DirName." + Invoke-ScriptAnalyzer -Path $_.Value -Recurse -ErrorAction SilentlyContinue | + Add-Member -MemberType NoteProperty -Name Location -Value $DirName -PassThru + } + + if ($AnalyzerResults) { + if (!(Get-Module -Name ImportExcel -ListAvailable)) { + '[Progress] Installing ImportExcel.' + Install-Module -Name ImportExcel -Force + } + '[Progress] Creating ScriptAnalyzer.xlsx.' + $ExcelParams = @{ + Path = 'ScriptAnalyzer.xlsx' + WorksheetName = 'FullResults' + Now = $true + Activate = $true + Show = $false + } + $PivotParams = @{ + PivotTableName = 'BreakDown' + PivotData = @{RuleName = 'Count' } + PivotRows = 'Severity', 'RuleName' + PivotColumns = 'Location' + PivotTotals = 'Rows' + } + Remove-Item -Path $ExcelParams['Path'] -ErrorAction SilentlyContinue + + $PivotParams['PivotChartDefinition'] = New-ExcelChartDefinition -ChartType 'BarClustered' -Column (1 + $DirsToProcess.Count) -Title "Script analysis" -LegendBold + $ExcelParams['PivotTableDefinition'] = New-PivotTableDefinition @PivotParams + + $AnalyzerResults | Export-Excel @ExcelParams + '[Progress] Analyzer finished.' + } + else { + "[Info] Invoke-ScriptAnalyzer didn't return any problems." + } } \ No newline at end of file diff --git a/__tests__/InstallPowerShell.ps1 b/CI/InstallPowerShell.ps1 similarity index 100% rename from __tests__/InstallPowerShell.ps1 rename to CI/InstallPowerShell.ps1 diff --git a/__tests__/Publish.ps1 b/CI/Publish.ps1 similarity index 96% rename from __tests__/Publish.ps1 rename to CI/Publish.ps1 index 625d5bf..61fc671 100644 --- a/__tests__/Publish.ps1 +++ b/CI/Publish.ps1 @@ -2,6 +2,7 @@ .SYNOPSIS Deploy module to PowerShellGallery. #> +[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "Success")] [CmdletBinding(DefaultParameterSetName = 'ModuleName')] Param ( diff --git a/appveyor.yml b/appveyor.yml index 47012cc..fde5af1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -28,20 +28,20 @@ install: - ps: 'Install-Module -Name Pester -Force -SkipPublisherCheck' - ps: 'Install-Module -Name Assert -Force' # PowerShell Core - - ps: '& .\__tests__\InstallPowerShell.ps1 -Version "7.0.0-preview.3"' # Install other PowerShell Core version (Optional) + - ps: '& .\CI\InstallPowerShell.ps1 -Version "7.0.0-preview.4"' # Install other PowerShell Core version (Optional) - pwsh: 'Install-Module -Name Pester -Force' - pwsh: 'Install-Module -Name Assert -Force' # To run your custom scripts instead of automatic tests test_script: - - ps: '& .\__tests__\CI.ps1 -Test' - - pwsh: '& .\__tests__\CI.ps1 -Test' - - ps: '& .\__tests__\CI.ps1 -Finalize' # Collect and upload results + - ps: '& .\CI\CI.ps1 -Test' + - pwsh: '& .\CI\CI.ps1 -Test' + - ps: '& .\CI\CI.ps1 -Finalize' # Collect and upload results # Deploy deploy_script: - - ps: '& .\__tests__\CI.ps1 -Artifact' - #- ps: '$null = Install-PackageProvider -Name NuGet -Force ; & .\__tests__\Publish.ps1' + - ps: '& .\CI\CI.ps1 -Artifact' + - ps: '$null = Install-PackageProvider -Name NuGet -Force ; & .\CI\Publish.ps1' # Linux setup for: @@ -55,13 +55,13 @@ for: - sh: 'export LANG=en_US.UTF-8' # Fix for PowerShell 7.0.0-preview.2, Remove if using other version. # Scripts that run after cloning repository install: - - pwsh: '& .\__tests__\CI.ps1 -Initialize' # Set AppVeyor build version + - pwsh: '& .\CI\CI.ps1 -Initialize' # Set AppVeyor build version - pwsh: 'Install-Module -Name Pester -Force' - pwsh: 'Install-Module -Name Assert -Force' # To run your custom scripts instead of automatic tests test_script: - - pwsh: '& .\__tests__\CI.ps1 -Test' - - pwsh: '& .\__tests__\CI.ps1 -Finalize' # Collect and upload results + - pwsh: '& .\CI\CI.ps1 -Test' + - pwsh: '& .\CI\CI.ps1 -Finalize' # Collect and upload results # Skip Deploy deploy_script: - pwsh: '"Deploy skiped on Linux."' \ No newline at end of file diff --git a/azure-pipelines.yml b/azure-pipelines.yml index c125bce..e48d3c2 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -22,7 +22,7 @@ jobs: steps: - powershell: 'Install-Module -Name Pester -Force -SkipPublisherCheck' displayName: 'Update Pester' - - powershell: './__tests__/CI.ps1 -Test' + - powershell: './CI/CI.ps1 -Test' displayName: 'Install and Test' - task: PublishTestResults@2 @@ -31,12 +31,14 @@ jobs: testResultsFiles: '**/TestResults*.xml' failTaskOnFailedTests: true - - powershell: './__tests__/CI.ps1 -Artifact' + - powershell: './CI/CI.ps1 -Artifact' displayName: 'Prepare Artifact' - task: PublishPipelineArtifact@1 inputs: targetPath: '$(Build.ArtifactStagingDirectory)' artifact: 'Modules' + - powershell: './CI/CI.ps1 -Analyzer' + displayName: 'Invoke ScriptAnalyzer' - task: PublishPipelineArtifact@1 inputs: targetPath: '$(Build.SourcesDirectory)' @@ -49,7 +51,7 @@ jobs: steps: - pwsh: 'Install-Module -Name Pester -Force' displayName: 'Update Pester' - - pwsh: './__tests__/CI.ps1 -Test' + - pwsh: './CI/CI.ps1 -Test' displayName: 'Install and Test' - task: PublishTestResults@2 @@ -65,7 +67,7 @@ jobs: steps: - powershell: 'Install-Module -Name Pester -Force' displayName: 'Update Pester' - - powershell: './__tests__/CI.ps1 -Test' + - powershell: './CI/CI.ps1 -Test' displayName: 'Install and Test' - task: PublishTestResults@2 @@ -83,7 +85,7 @@ jobs: displayName: 'Install mono-libgdiplus' - powershell: 'Install-Module -Name Pester -Force' displayName: 'Update Pester' - - powershell: './__tests__/CI.ps1 -Test' + - powershell: './CI/CI.ps1 -Test' displayName: 'Install and Test' - task: PublishTestResults@2