Create a merged psm1 file when published (#16) !deploy

* Added auto discovery fo files to monitor.
* Fixed scriptanalyzer rule
* Adjusted the parameter attributes
* testing feature specifications
* Added module building
* Exceptions and spacing
This commit is contained in:
Kevin Marquette
2017-05-03 00:08:41 -07:00
committed by GitHub
parent 5cacaa2ebd
commit 3fdc8a02d9
9 changed files with 161 additions and 103 deletions

View File

@@ -5,14 +5,14 @@ class Chronometer
[void]AddBreakpoint([string[]]$Path, [int[]]$LineNumber)
{
if(-not [string]::IsNullOrEmpty($Path))
if (-not [string]::IsNullOrEmpty($Path))
{
foreach($file in (Resolve-Path $Path -ea 0))
foreach ($file in (Resolve-Path $Path -ea 0))
{
$script = [MonitoredScript]@{Path=$file.Path}
$script = [MonitoredScript]@{Path = $file.Path}
$lines = $script.SetScript($file)
if($null -ne $LineNumber)
if ( $null -ne $LineNumber )
{
$bpLine = $LineNumber
}
@@ -26,7 +26,7 @@ class Chronometer
$breakpointParam = @{
Script = $file
Line = $bpLine
Action = {[ScriptProfiler]::RecordExecution( $_) }
Action = { [ScriptProfiler]::RecordExecution( $_) }
}
$this.breakPoint += Set-PSBreakpoint @breakpointParam
}
@@ -35,7 +35,7 @@ class Chronometer
[void]ClearBreakpoint()
{
if($null -ne $this.Breakpoint -and $this.Breakpoint.count -gt 0)
if ( $null -ne $this.Breakpoint -and $this.Breakpoint.count -gt 0 )
{
Remove-PSBreakpoint -Breakpoint $this.Breakpoint
}
@@ -45,7 +45,7 @@ class Chronometer
[void] AddExecution([hashtable]$Execution)
{
$script = $Execution.Breakpoint.Script
if($this.FileMap.ContainsKey($script))
if ( $this.FileMap.ContainsKey($script) )
{
# Each script tracks it's own execution times
$this.FileMap[$script].AddExecution($Execution)
@@ -54,7 +54,7 @@ class Chronometer
[MonitoredScript[]] GetResults()
{
foreach($node in $this.FileMap.Values)
foreach ( $node in $this.FileMap.Values )
{
$node.PostProcessing()
}

View File

@@ -17,7 +17,7 @@ class MonitoredScript
[int] SetScript([string]$Path)
{
$lineNumber = 0
foreach($command in ( Get-Content -Path $Path ))
foreach ( $command in ( Get-Content -Path $Path ) )
{
$this.Line.Add( [ScriptLine]::New($command, $path, $lineNumber) )
$lineNumber++
@@ -35,7 +35,7 @@ class MonitoredScript
$record.AddExecution($node)
# Calclate the delta in time
if($this.lastNode)
if ( $this.lastNode )
{
$duration = $node.Elapsed - $this.lastNode.Elapsed
}
@@ -45,7 +45,7 @@ class MonitoredScript
}
# The delta is how long the last command ran
if($this.lastRecord)
if ( $this.lastRecord )
{
$this.lastRecord.AddExecutionTime($duration)
$this.ExecutionTime += $duration
@@ -59,15 +59,15 @@ class MonitoredScript
{
$this.lastNode = $null
$this.ExecutionTime = [TimeSpan]::Zero
foreach($node in $this.line)
foreach ( $node in $this.line )
{
$command = $node.text -replace '\s',''
$command = $node.text -replace '\s', ''
switch -Regex ($command)
switch -Regex ( $command )
{
'^}$|^}#|^$'
{
if($node.HitCount -eq 0)
if ( $node.HitCount -eq 0 )
{
$node.HitCount = $this.lastNode.HitCount
}

View File

@@ -33,12 +33,12 @@ class ScriptLine
$this.HitCount += 1
$this.Average = $this.Duration.TotalMilliseconds / $this.HitCount
if($Duration -lt $this.Min)
if ( $Duration -lt $this.Min )
{
$this.Min = $Duration
}
if($Duration -gt $this.Max)
if ( $Duration -gt $this.Max )
{
$this.Max = $Duration
}

View File

@@ -1,7 +1,6 @@
function Write-ScriptLine
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost","")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
[cmdletbinding()]
param(
[scriptline]
@@ -10,18 +9,18 @@ function Write-ScriptLine
$ErrorAt = [int]::MaxValue
)
if($line)
if ( $line )
{
$Color = 'Green'
if($line.HitCount -eq 0)
if ( $line.HitCount -eq 0 )
{
$Color = 'Gray'
}
elseif($line.Average -ge $ErrorAt)
elseif ( $line.Average -ge $ErrorAt )
{
$Color = 'Red'
}
elseif($line.Average -ge $WarningAt)
elseif ( $line.Average -ge $WarningAt )
{
$Color = 'Yellow'
}

View File

@@ -1,5 +1,3 @@
function Format-Chronometer
{
<#
@@ -11,21 +9,21 @@ function Format-Chronometer
$resultes = Get-Chronometer -Path $script.fullname -ScriptBlock {Invoke-Pester C:\workspace\PSGraph}
$results | Format-Chronometer -WarnAt 20 -ErrorAt 200
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost","")]
[cmdletbinding(DefaultParameterSetName='Script')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
[cmdletbinding(DefaultParameterSetName = 'Script')]
param(
# This is a MonitoredScript object from Get-Chronometer
[Parameter(
ValueFromPipeline=$true,
ParameterSetName='Script'
ValueFromPipeline = $true,
ParameterSetName = 'Script'
)]
[MonitoredScript[]]
$InputObject,
# This is a ScriptLine object from a MonitoredScript object
# This is a ScriptLine object from a MonitoredScript object
[Parameter(
ValueFromPipeline=$true,
ParameterSetName='Line'
ValueFromPipeline = $true,
ParameterSetName = 'Line'
)]
[ScriptLine[]]
$Line,
@@ -45,21 +43,28 @@ function Format-Chronometer
process
{
foreach($script in $InputObject)
try
{
if($script.ExecutionTime -ne [TimeSpan]::Zero -or $ShowAll)
foreach ( $script in $InputObject )
{
Write-Host ''
Write-Host "Script: $($script.Path)" -ForegroundColor Green
Write-Host "Execution Time: $($script.ExecutionTime)" -ForegroundColor Green
if ( $script.ExecutionTime -ne [TimeSpan]::Zero -or $ShowAll )
{
Write-Host ''
Write-Host "Script: $($script.Path)" -ForegroundColor Green
Write-Host "Execution Time: $($script.ExecutionTime)" -ForegroundColor Green
$script.line | Format-Chronometer -WarningAt $WarningAt -ErrorAt $ErrorAt
$script.line | Format-Chronometer -WarningAt $WarningAt -ErrorAt $ErrorAt
}
}
foreach ( $command in $Line )
{
Write-ScriptLine $command -WarningAt $WarningAt -ErrorAt $ErrorAt
}
}
foreach($command in $Line)
catch
{
Write-ScriptLine $command -WarningAt $WarningAt -ErrorAt $ErrorAt
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
}

View File

@@ -23,47 +23,56 @@ function Get-Chronometer
$LineNumber = $null,
# The script to start the scrupt or execute other commands
[Parameter(Position=0)]
[alias('Script','CommandScript')]
[Parameter(Position = 0)]
[alias('Script', 'CommandScript')]
[scriptblock]
$ScriptBlock
)
if( $null -eq $Path )
process
{
$Path = Get-ChildItem -Recurse -Include *.psm1,*.ps1 -File
}
if($Path.FullName)
{
$Path = $Path.FullName
}
$Chronometer = [Chronometer]::New()
Write-Verbose "Setting breapoints"
$Chronometer.AddBreakpoint($Path,$LineNumber)
if($null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock)
{
Write-Verbose "Executing Script"
[ScriptProfiler]::Start()
[void] $ScriptBlock.Invoke($Path)
Write-Verbose "Clearing Breapoints"
$Chronometer.ClearBreakpoint()
Write-Verbose "Processing data"
foreach($node in [ScriptProfiler]::Queue.GetEnumerator())
try
{
$Chronometer.AddExecution($node)
}
if ( $null -eq $Path )
{
$Path = Get-ChildItem -Recurse -Include *.psm1, *.ps1 -File
}
Write-Output $Chronometer.GetResults()
}
else
{
Write-Warning "Parsing files did not result in any breakpoints"
if ( $Path.FullName )
{
$Path = $Path.FullName
}
$Chronometer = [Chronometer]::New()
Write-Verbose "Setting breapoints"
$Chronometer.AddBreakpoint($Path, $LineNumber)
if ( $null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock )
{
Write-Verbose "Executing Script"
[ScriptProfiler]::Start()
[void] $ScriptBlock.Invoke($Path)
Write-Verbose "Clearing Breapoints"
$Chronometer.ClearBreakpoint()
Write-Verbose "Processing data"
foreach ( $node in [ScriptProfiler]::Queue.GetEnumerator() )
{
$Chronometer.AddExecution($node)
}
Write-Output $Chronometer.GetResults()
}
else
{
Write-Warning "Parsing files did not result in any breakpoints"
}
}
catch
{
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
}

View File

@@ -0,0 +1,13 @@
Given 'We have functions to publish' {
"$psscriptroot\..\chronometer\public\*.ps1" | Should Exist
}
And 'We have a module' {
"$psscriptroot\..\chronometer\chronometer.psd1" | Should Exist
"$psscriptroot\..\chronometer\chronometer.psm1" | Should Exist
}
When 'The user searches for our module' {
Find-Module chronometer | Should Not BeNullOrEmpty
}
Then 'They can install the module' {
{Install-Module chronometer -Scope CurrentUser -WhatIf *>&1} | Should Not Throw
}

View File

@@ -0,0 +1,8 @@
Feature: We need distribute our module to the public
It should be published someplace that is easy to find
Scenario: A user needs to be able to find our module in the PSGallery
Given We have functions to publish
And We have a module
When The user searches for our module
Then They can install the module

View File

@@ -2,11 +2,11 @@
# Init some things
Properties {
# Find the build folder based on build system
$ProjectRoot = $ENV:BHProjectPath
if(-not $ProjectRoot)
{
$ProjectRoot = $PSScriptRoot
}
$ProjectRoot = $ENV:BHProjectPath
if (-not $ProjectRoot)
{
$ProjectRoot = $PSScriptRoot
}
$Timestamp = Get-date -uformat "%Y%m%d-%H%M%S"
$PSVersion = $PSVersionTable.PSVersion.Major
@@ -14,7 +14,7 @@ Properties {
$lines = '----------------------------------------------------------------------'
$Verbose = @{}
if($ENV:BHCommitMessage -match "!verbose")
if ( $ENV:BHCommitMessage -match "!verbose" )
{
$Verbose = @{Verbose = $True}
}
@@ -35,14 +35,14 @@ Task UnitTests -Depends Init {
'Running quick unit tests to fail early if there is an error'
$TestResults = Invoke-Pester -Path $ProjectRoot\Tests\*unit* -PassThru -Tag Build
if($TestResults.FailedCount -gt 0)
if ( $TestResults.FailedCount -gt 0 )
{
Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}
"`n"
}
Task Test -Depends UnitTests {
Task Test -Depends UnitTests {
$lines
"`n`tSTATUS: Testing with PowerShell $PSVersion"
@@ -50,11 +50,11 @@ Task Test -Depends UnitTests {
$TestResults = Invoke-Pester -Path $ProjectRoot\Tests -PassThru -OutputFormat NUnitXml -OutputFile "$ProjectRoot\$TestFile" -Tag Build
# In Appveyor? Upload our tests! #Abstract this into a function?
If($ENV:BHBuildSystem -eq 'AppVeyor')
If ( $ENV:BHBuildSystem -eq 'AppVeyor' )
{
[xml]$content = Get-Content "$ProjectRoot\$TestFile"
$content.'test-results'.'test-suite'.type = "Powershell"
$content.Save("$ProjectRoot\$TestFile")
$content.Save( "$ProjectRoot\$TestFile" )
"Uploading $ProjectRoot\$TestFile to AppVeyor"
"JobID: $env:APPVEYOR_JOB_ID"
@@ -65,7 +65,7 @@ Task Test -Depends UnitTests {
# Failed tests?
# Need to tell psake or it will proceed to the deployment. Danger!
if($TestResults.FailedCount -gt 0)
if ( $TestResults.FailedCount -gt 0 )
{
Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}
@@ -76,8 +76,8 @@ Task Build -Depends Test {
$lines
$functions = Get-ChildItem "$PSScriptRoot\$env:BHProjectName\Public\*.ps1" |
Where-Object{ $_.name -notmatch 'Tests'} |
Select-Object -ExpandProperty basename
Where-Object { $_.name -notmatch 'Tests'} |
Select-Object -ExpandProperty basename
# Load the module, read the exported functions, update the psd1 FunctionsToExport
Set-ModuleFunctions -Name $env:BHPSModuleManifest -FunctionsToExport $functions
@@ -85,21 +85,45 @@ Task Build -Depends Test {
# Bump the module version
$version = [version] (Step-Version (Get-Metadata -Path $env:BHPSModuleManifest))
$galleryVersion = Get-NextPSGalleryVersion -Name $env:BHProjectName
if($version -lt $galleryVersion)
if ( $version -lt $galleryVersion )
{
$version = $galleryVersion
}
$version = [version]::New($version.Major,$version.Minor,$version.Build,$env:BHBuildNumber)
$version = [version]::New($version.Major, $version.Minor, $version.Build, $env:BHBuildNumber)
Write-Host "Using version: $version"
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $version
$psm1 = "$PSScriptRoot\$env:BHProjectName\$env:BHProjectName.psm1"
# keep the first line in the psm1 file and clear the rest
( Get-Content -Path $psm1 -TotalCount 1 ) | Set-Content -Path $psm1
foreach ( $folder in ('Classes', 'Public', 'Private') )
{
Add-Content -Path $psm1 -Value "Write-Verbose 'Importing from [$folder]'"
$imports = Get-ChildItem "$PSScriptRoot\$env:BHProjectName\$folder\*.ps1" -Exclude *.Tests.ps1
foreach ( $file in $imports )
{
Add-Content -Path $psm1 -Value ''
Add-Content -Path $psm1 -Value "Write-Verbose ' Source [\$folder\$($file.name)]'"
[System.IO.File]::ReadAllText($file.fullname) | Add-Content -Path $psm1
}
# Remove folders after import
Remove-Item "$PSScriptRoot\$env:BHProjectName\$folder" -Recurse
}
# Add exports
Add-Content -Path $psm1 -Value "Export-ModuleMember -Function $($functions -join ', ')"
Import-Module $psm1 -Force
}
Task Deploy -Depends Build {
$lines
# Gate deployment
if(
if (
$ENV:BHBuildSystem -ne 'Unknown' -and
$ENV:BHBranchName -eq "master" -and
$ENV:BHCommitMessage -match '!deploy'