Skip to content

Commit

Permalink
initial commit from PackageProviderBootstrap re-work
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelcolas committed Oct 5, 2017
0 parents commit 849310a
Show file tree
Hide file tree
Showing 61 changed files with 3,878 additions and 0 deletions.
100 changes: 100 additions & 0 deletions .build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[cmdletBinding()]
Param (
[Parameter(Position=0)]
$Tasks,

[switch]
$ResolveDependency,

[String]
$BuildOutput = "BuildOutput",

[String[]]
$GalleryRepository,

[Uri]
$GalleryProxy,

[Switch]
$ForceEnvironmentVariables = [switch]$true,

$MergeList = @('enum*',[PSCustomObject]@{Name='class*';order={(Import-PowerShellDataFile .\SampleModule\Classes\classes.psd1).order.indexOf($_.BaseName)}},'priv*','pub*')
,$CodeCoverageThreshold = 0
)

Process {
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
Invoke-Build $Tasks $MyInvocation.MyCommand.Path @PSBoundParameters
return
}

Get-ChildItem -Path "$PSScriptRoot/.build/" -Recurse -Include *.ps1 -Verbose |
Foreach-Object {
"Importing file $($_.BaseName)" | Write-Verbose
. $_.FullName
}
task none {}
task . Clean,
SetBuildEnvironment,
UnitTests,
UploadUnitTestResultsToAppVeyor,
FailBuildIfFailedUnitTest,
FailIfLastCodeConverageUnderThreshold,
CopySourceToModuleOut,
MergeFilesToPSM1,
CleanOutputEmptyFolders,
IntegrationTests,
QualityTestsStopOnFail

task testAll UnitTests, IntegrationTests, QualityTestsStopOnFail
}


begin {
Pushd $PSScriptRoot
function Resolve-Dependency {
[CmdletBinding()]
param()

if (!(Get-PackageProvider -Name NuGet -ForceBootstrap)) {
$providerBootstrapParams = @{
Name = 'nuget'
force = $true
ForceBootstrap = $true
}
if($PSBoundParameters.ContainsKey('verbose')) { $providerBootstrapParams.add('verbose',$verbose)}
if ($GalleryProxy) { $providerBootstrapParams.Add('Proxy',$GalleryProxy) }
$null = Install-PackageProvider @providerBootstrapParams
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
}

if (!(Get-Module -Listavailable PSDepend)) {
Write-verbose "BootStrapping PSDepend"
"Parameter $BuildOutput"| Write-verbose
$InstallPSDependParams = @{
Name = 'PSDepend'
AllowClobber = $true
Confirm = $false
Force = $true
Scope = 'CurrentUser'
}
if($PSBoundParameters.ContainsKey('verbose')) { $InstallPSDependParams.add('verbose',$verbose)}
if ($GalleryRepository) { $InstallPSDependParams.Add('Repository',$GalleryRepository) }
if ($GalleryProxy) { $InstallPSDependParams.Add('Proxy',$GalleryProxy) }
if ($GalleryCredential) { $InstallPSDependParams.Add('ProxyCredential',$GalleryCredential) }
Install-Module @InstallPSDependParams
}

$PSDependParams = @{
Force = $true
Path = "$PSScriptRoot\Dependencies.psd1"
}
if($PSBoundParameters.ContainsKey('verbose')) { $PSDependParams.add('verbose',$verbose)}
Invoke-PSDepend @PSDependParams -Verbose
Write-Verbose "Project Bootstrapped, returning to Invoke-Build"
}

if ($ResolveDependency) {
Resolve-Dependency
}
}
38 changes: 38 additions & 0 deletions .build/Appveyor/TestResultUpload.Appveyor.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Param (
[io.DirectoryInfo]
$ProjectPath = (property ProjectPath (Join-Path $PSScriptRoot '../..' -Resolve -ErrorAction SilentlyContinue)),

[string]
$BuildOutput = (property BuildOutput 'C:\BuildOutput'),

[string]
$ProjectName = (property ProjectName (Split-Path -Leaf (Join-Path $PSScriptRoot '../..')) ),

[string]
$PesterOutputFormat = (property PesterOutputFormat 'NUnitXml'),

[string]
$APPVEYOR_JOB_ID = $(try {property APPVEYOR_JOB_ID} catch {})
)

task UploadUnitTestResultsToAppVeyor -If {(property BuildSystem 'unknown') -eq 'AppVeyor'} {

if (![io.path]::IsPathRooted($BuildOutput)) {
$BuildOutput = Join-Path -Path $ProjectPath.FullName -ChildPath $BuildOutput
}

$TestOutputPath = [system.io.path]::Combine($BuildOutput,'testResults','unit',$PesterOutputFormat)
$TestResultFiles = Get-ChildItem -Path $TestOutputPath -Filter *.xml
$TestResultFiles | Add-TestResultToAppveyor
}

task UploadUnit2TestResultsToAppVeyor -If {(property BuildSystem 'unknown') -eq 'AppVeyor'} {

if (![io.path]::IsPathRooted($BuildOutput)) {
$BuildOutput = Join-Path -Path $ProjectPath.FullName -ChildPath $BuildOutput
}

$TestOutputPath = [system.io.path]::Combine($BuildOutput,'testResults','unit2',$PesterOutputFormat)
$TestResultFiles = Get-ChildItem -Path $TestOutputPath -Filter *.xml
$TestResultFiles | Add-TestResultToAppveyor
}
34 changes: 34 additions & 0 deletions .build/BuildHelpers/Clean.BuildHelpers.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Param (

[io.DirectoryInfo]
$ProjectPath = (property ProjectPath (Join-Path $PSScriptRoot '../..' -Resolve -ErrorAction SilentlyContinue)),

[string]
$BuildOutput = (property BuildOutput 'C:\BuildOutput'),

[string]
$LineSeparation = (property LineSeparation ('-' * 78))
)

task Clean {
$LineSeparation
"`t`t`t CLEAN UP"
$LineSeparation

if (![io.path]::IsPathRooted($BuildOutput)) {
$BuildOutput = Join-Path -Path $ProjectPath.FullName -ChildPath $BuildOutput
}
if (Test-Path $BuildOutput) {
"Removing $BuildOutput\*"
Gci .\BuildOutput\ -Exclude modules | Remove-Item -Force -Recurse
}

}

task CleanModule {
if (![io.path]::IsPathRooted($BuildOutput)) {
$BuildOutput = Join-Path -Path $ProjectPath.FullName -ChildPath $BuildOutput
}
"Removing $BuildOutput\*"
Gci .\BuildOutput\ | Remove-Item -Force -Recurse -Verbose -ErrorAction Stop
}
19 changes: 19 additions & 0 deletions .build/BuildHelpers/Environment.BuildHelpers.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Param (
[string]
$LineSeparation = (property LineSeparation ('-' * 78)),

[string]
$VariableNamePrefix = $(try {property VariableNamePrefix} catch {''}),

[switch]
$ForceEnvironmentVariables = $(try {property ForceEnvironmentVariables} catch {$false})
)

task SetBuildEnvironment {
$LineSeparation
'Set-BuildEnvironment'
Set-BuildEnvironment -variableNamePrefix $VariableNamePrefix -ErrorVariable err -ErrorAction SilentlyContinue -Force:$ForceEnvironmentVariables -Verbose
foreach ($e in $err) {
Write-Host $e
}
}
43 changes: 43 additions & 0 deletions .build/Dsc/Update-DscResourceFromDefinition.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Update-DscResourceFromObjectMetadata {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipelineByPropertyName)]
[io.DirectoryInfo]$SourceFolder,

[PSCustomObject]
$DscResourceMetadata = (Get-Content -Raw "$((Resolve-Path $SourceFolder).Path)\DscResources\DSCResourcesDefinitions.json"| ConvertFrom-Json)
)

if (![io.path]::IsPathRooted($SourceFolder)) {
$SourceFolder = (Resolve-Path $SourceFolder).Path
}
foreach ($Resource in $DscResourceMetadata)
{
$DscProperties = @()
$ResourceName = $Resource.psobject.Properties.Name
Write-Verbose "Preparing $ResourceName"
foreach ($DscProperty in $Resource.($ResourceName)) {
$resourceParams = @{}
$DscProperty.psobject.properties | % { $resourceParams[$_.Name] = $_.value }
$DscProperties += New-xDscResourceProperty @resourceParams
}

if (Test-Path "$SourceFolder\DscResources\$ResourceName") {
$DscResourceParams = @{
Property = $DscProperties
Path = "$SourceFolder\DscResources\$ResourceName"
FriendlyName = $ResourceName
}
Update-xDscResource @DscResourceParams -Force
}
else {
$DscResourceParams = @{
Name = $ResourceName
Property = $DscProperties
Path = "$SourceFolder\"
FriendlyName = $ResourceName
}
New-xDscResource @DscResourceParams
}
}
}
26 changes: 26 additions & 0 deletions .build/Dsc/UpdateDscResourceFromDefinition.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Param (

[io.DirectoryInfo]
$ProjectPath = (property ProjectPath (Join-Path $PSScriptRoot '../..' -Resolve -ErrorAction SilentlyContinue)),

[string]
$ProjectName = (property ProjectName (Split-Path -Leaf (Join-Path $PSScriptRoot '../..')) ),

[string]
$LineSeparation = (property LineSeparation ('-' * 78))
)

task UpdateDscResource {
$LineSeparation
"`t`t`t UPDATING DSC SCRIPT RESOURCE SCHEMAS"
$LineSeparation
. $PSScriptRoot\Update-DscResourceFromDefinition.ps1

$SourceFolder = Join-Path -Path $ProjectPath.FullName -ChildPath $ProjectName

if (Test-Path $SourceFolder) {
Update-DscResourceFromObjectMetadata -SourceFolder $SourceFolder
}
}

task updateDscSchema UpdateDscResource
45 changes: 45 additions & 0 deletions .build/Pester/IntegrationTests.pester.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#Requires -Modules Pester
Param (
[io.DirectoryInfo]
$ProjectPath = (property ProjectPath (Join-Path $PSScriptRoot '../..' -Resolve -ErrorAction SilentlyContinue)),

[string]
$ProjectName = (property ProjectName (Split-Path -Leaf (Join-Path $PSScriptRoot '../..')) ),

[string]
$RelativePathToIntegrationTests = (property RelativePathToIntegrationTests 'tests/Integration'),

[string]
$LineSeparation = (property LineSeparation ('-' * 78))
)
task IntegrationTests {
$LineSeparation
"`t`t`t RUNNING INTEGRATION TESTS"
$LineSeparation
"`tProject Path = $ProjectPath"
"`tProject Name = $ProjectName"
"`tIntegration Tests = $RelativePathToIntegrationTests"
$IntegrationTestPath = [io.DirectoryInfo][system.io.path]::Combine($ProjectPath,$ProjectName,$RelativePathToIntegrationTests)
"`tIntegration Tests = $IntegrationTestPath"

if (!$IntegrationTestPath.Exists -and
( #Try a module structure where the
($IntegrationTestPath = [io.DirectoryInfo][system.io.path]::Combine($ProjectPath,$RelativePathToIntegrationTests)) -and
!$IntegrationTestPath.Exists
)
)
{
Write-Warning ('Integration tests Path Not found {0}' -f $IntegrationTestPath)
}
else {
"`tIntegrationTest Path: $IntegrationTestPath"
''
Push-Location $IntegrationTestPath

Import-module Pester
Invoke-Pester -ErrorAction Stop

Pop-Location
}

}
66 changes: 66 additions & 0 deletions .build/Pester/QualityTests.pester.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#Requires -Modules Pester
Param (
[io.DirectoryInfo]
$ProjectPath = (property ProjectPath (Join-Path $PSScriptRoot '../..' -Resolve -ErrorAction SilentlyContinue)),

[string]
$BuildOutput = (property BuildOutput 'C:\BuildOutput'),

[string]
$ProjectName = (property ProjectName (Split-Path -Leaf (Join-Path $PSScriptRoot '../..')) ),

[string]
$RelativePathToQualityTests = (property RelativePathToQualityTests 'tests/QA'),

[string]
$LineSeparation = (property LineSeparation ('-' * 78))
)

task QualityTests {
$LineSeparation
"`t`t`t RUNNING Quality TESTS"
$LineSeparation
"`tProject Path = $ProjectPath"
"`tProject Name = $ProjectName"
"`tQuality Tests = $RelativePathToQualityTests"

$QualityTestPath = [io.DirectoryInfo][system.io.path]::Combine($ProjectPath,$ProjectName,$RelativePathToQualityTests)

if (!$QualityTestPath.Exists -and
( #Try a module structure where the
($QualityTestPath = [io.DirectoryInfo][system.io.path]::Combine($ProjectPath,$RelativePathToQualityTests)) -and
!$QualityTestPath.Exists
)
)
{
Write-Warning ('Cannot Execute Quality tests, Path Not found {0}' -f $QualityTestPath)
return
}

"`tQualityTest Path: $QualityTestPath"
if (![io.path]::IsPathRooted($BuildOutput)) {
$BuildOutput = Join-Path -Path $ProjectPath.FullName -ChildPath $BuildOutput
}
# $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\nonexist\foo.txt")
$PSVersion = $PSVersionTable.PSVersion.Major
$Timestamp = Get-date -uformat "%Y%m%d-%H%M%S"
$FileName = "TestResults_QA_PS$PSVersion`_$TimeStamp.xml"
$TestFilePath = Join-Path -Path $BuildOutput -ChildPath $FileName

if (!(Test-Path $BuildOutput)) {
mkdir $BuildOutput -Force
}

Push-Location $QualityTestPath

Import-module Pester
$script:QualityTestResults = Invoke-Pester -ErrorAction Stop -OutputFormat NUnitXml -OutputFile $TestFilePath -PassThru

Pop-Location
}

task FailBuildIfFailedQualityTest -If ($CodeCoverageThreshold -ne 0) {
assert ($script:QualityTestResults.FailedCount -eq 0) ('Failed {0} Quality tests. Aborting Build' -f $script:QualityTestResults.FailedCount)
}

task QualityTestsStopOnFail QualityTests,FailBuildIfFailedQualityTest
Loading

0 comments on commit 849310a

Please sign in to comment.