This commit is contained in:
Bernie White 2019-12-19 22:11:09 +11:00 коммит произвёл GitHub
Родитель 7b29a324b0
Коммит 40972f22b9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 390 добавлений и 0 удалений

242
pipeline.build.ps1 Normal file
Просмотреть файл

@ -0,0 +1,242 @@
[CmdletBinding()]
param (
[Parameter(Mandatory = $False)]
[String]$Build = '0.0.1',
[Parameter(Mandatory = $False)]
[String]$Configuration = 'Debug',
[Parameter(Mandatory = $False)]
[String]$ApiKey,
[Parameter(Mandatory = $False)]
[Switch]$CodeCoverage = $False,
[Parameter(Mandatory = $False)]
[String]$ArtifactPath = (Join-Path -Path $PWD -ChildPath out/modules)
)
Write-Host -Object "[Pipeline] -- PWD: $PWD" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- ArtifactPath: $ArtifactPath" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- BuildNumber: $($Env:BUILD_BUILDNUMBER)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- SourceBranch: $($Env:BUILD_SOURCEBRANCH)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- SourceBranchName: $($Env:BUILD_SOURCEBRANCHNAME)" -ForegroundColor Green;
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
if ($Env:BUILD_SOURCEBRANCH -like '*/tags/*' -and $Env:BUILD_SOURCEBRANCHNAME -like 'v0.*') {
$Build = $Env:BUILD_SOURCEBRANCHNAME.Substring(1);
}
$version = $Build;
$versionSuffix = [String]::Empty;
if ($version -like '*-*') {
[String[]]$versionParts = $version.Split('-', [System.StringSplitOptions]::RemoveEmptyEntries);
$version = $versionParts[0];
if ($versionParts.Length -eq 2) {
$versionSuffix = $versionParts[1];
}
}
Write-Host -Object "[Pipeline] -- Using version: $version" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- Using versionSuffix: $versionSuffix" -ForegroundColor Green;
if ($Env:coverage -eq 'true') {
$CodeCoverage = $True;
}
# Copy the PowerShell modules files to the destination path
function CopyModuleFiles {
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $True)]
[String]$DestinationPath
)
process {
$sourcePath = Resolve-Path -Path $Path;
Get-ChildItem -Path $sourcePath -Recurse -File -Include *.ps1,*.psm1,*.psd1,*.ps1xml,*.yaml | Where-Object -FilterScript {
($_.FullName -notmatch '(\.(cs|csproj)|(\\|\/)(obj|bin))')
} | ForEach-Object -Process {
$filePath = $_.FullName.Replace($sourcePath, $destinationPath);
$parentPath = Split-Path -Path $filePath -Parent;
if (!(Test-Path -Path $parentPath)) {
$Null = New-Item -Path $parentPath -ItemType Directory -Force;
}
Copy-Item -Path $_.FullName -Destination $filePath -Force;
};
}
}
task VersionModule ModuleDependencies, {
$modulePath = Join-Path -Path $ArtifactPath -ChildPath PSRule.Requirements;
$manifestPath = Join-Path -Path $modulePath -ChildPath PSRule.Requirements.psd1;
Write-Verbose -Message "[VersionModule] -- Checking module path: $modulePath";
if (![String]::IsNullOrEmpty($Build)) {
# Update module version
if (![String]::IsNullOrEmpty($version)) {
Write-Verbose -Message "[VersionModule] -- Updating module manifest ModuleVersion";
Update-ModuleManifest -Path $manifestPath -ModuleVersion $version;
}
# Update pre-release version
if (![String]::IsNullOrEmpty($versionSuffix)) {
Write-Verbose -Message "[VersionModule] -- Updating module manifest Prerelease";
Update-ModuleManifest -Path $manifestPath -Prerelease $versionSuffix;
}
}
$manifest = Test-ModuleManifest -Path $manifestPath;
$requiredModules = $manifest.RequiredModules | ForEach-Object -Process {
if ($_.Name -eq 'PSRule' -and $Configuration -eq 'Release') {
@{ ModuleName = 'PSRule'; ModuleVersion = '0.12.0' }
}
elseif ($_.Name -eq 'Requirements' -and $Configuration -eq 'Release') {
@{ ModuleName = 'Requirements'; ModuleVersion = '2.3.4' }
}
else {
@{ ModuleName = $_.Name; ModuleVersion = $_.Version }
}
};
Update-ModuleManifest -Path $manifestPath -RequiredModules $requiredModules;
}
# Synopsis: Publish to PowerShell Gallery
task ReleaseModule VersionModule, {
$modulePath = (Join-Path -Path $ArtifactPath -ChildPath PSRule.Requirements);
Write-Verbose -Message "[ReleaseModule] -- Checking module path: $modulePath";
if (!(Test-Path -Path $modulePath)) {
Write-Error -Message "[ReleaseModule] -- Module path does not exist";
}
elseif (![String]::IsNullOrEmpty($ApiKey)) {
Publish-Module -Path $modulePath -NuGetApiKey $ApiKey;
}
}
# Synopsis: Install NuGet provider
task NuGet {
if ($Null -eq (Get-PackageProvider -Name NuGet -ErrorAction Ignore)) {
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser;
}
}
# Synopsis: Install Pester module
task Pester NuGet, {
if ($Null -eq (Get-InstalledModule -Name Pester -MinimumVersion 4.9.0 -ErrorAction Ignore)) {
Install-Module -Name Pester -MinimumVersion 4.9.0 -Scope CurrentUser -Force -SkipPublisherCheck;
}
Import-Module -Name Pester -Verbose:$False;
}
# Synopsis: Install PSScriptAnalyzer module
task PSScriptAnalyzer NuGet, {
if ($Null -eq (Get-InstalledModule -Name PSScriptAnalyzer -MinimumVersion 1.18.3 -ErrorAction Ignore)) {
Install-Module -Name PSScriptAnalyzer -MinimumVersion 1.18.3 -Scope CurrentUser -Force;
}
Import-Module -Name PSScriptAnalyzer -Verbose:$False;
}
# Synopsis: Install PSRule
task PSRule NuGet, {
if ($Null -eq (Get-InstalledModule -Name PSRule -MinimumVersion 0.12.0 -ErrorAction Ignore)) {
Install-Module -Name PSRule -Repository PSGallery -MinimumVersion 0.12.0 -Scope CurrentUser -Force;
}
Import-Module -Name PSRule -Verbose:$False;
}
# Synopsis: Install PlatyPS module
task platyPS {
if ($Null -eq (Get-InstalledModule -Name PlatyPS -MinimumVersion 0.14.0 -ErrorAction Ignore)) {
Install-Module -Name PlatyPS -Scope CurrentUser -MinimumVersion 0.14.0 -Force;
}
Import-Module -Name PlatyPS -Verbose:$False;
}
task Requirements {
if ($Null -eq (Get-InstalledModule -Name Requirements -MinimumVersion 2.3.4 -ErrorAction Ignore)) {
Install-Module -Name Requirements -Scope CurrentUser -MinimumVersion 2.3.4 -Force;
}
Import-Module -Name Requirements -Verbose:$False;
}
# Synopsis: Install module dependencies
task ModuleDependencies NuGet, PSRule, Requirements, {
}
task CopyModule {
CopyModuleFiles -Path src/PSRule.Requirements -DestinationPath out/modules/PSRule.Requirements;
}
# Synopsis: Build modules only
task BuildModule CopyModule
task TestRules PSRule, Pester, PSScriptAnalyzer, {
# Run Pester tests
$pesterParams = @{ Path = (Join-Path -Path $PWD -ChildPath tests/PSRule.Requirements.Tests); OutputFile = 'reports/pester-unit.xml'; OutputFormat = 'NUnitXml'; PesterOption = @{ IncludeVSCodeMarker = $True }; PassThru = $True; };
if ($CodeCoverage) {
$pesterParams.Add('CodeCoverage', (Join-Path -Path $PWD -ChildPath 'out/modules/**/*.psm1'));
$pesterParams.Add('CodeCoverageOutputFile', (Join-Path -Path $PWD -ChildPath reports/pester-coverage.xml));
}
if (!(Test-Path -Path reports)) {
$Null = New-Item -Path reports -ItemType Directory -Force;
}
$results = Invoke-Pester @pesterParams;
# Throw an error if pester tests failed
if ($Null -eq $results) {
throw 'Failed to get Pester test results.';
}
elseif ($results.FailedCount -gt 0) {
throw "$($results.FailedCount) tests failed.";
}
}
# Synopsis: Run script analyzer
task Analyze Build, PSScriptAnalyzer, {
Invoke-ScriptAnalyzer -Path out/modules/PSRule.Requirements;
}
# Synopsis: Build help
task BuildHelp BuildModule, PlatyPS, {
}
task ScaffoldHelp Build, {
Import-Module (Join-Path -Path $PWD -ChildPath out/modules/PSRule.Requirements) -Force;
Update-MarkdownHelp -Path '.\docs\commands\PSRule.Requirements\en-US';
}
# Synopsis: Add shipit build tag
task TagBuild {
if ($Null -ne $Env:BUILD_DEFINITIONNAME) {
Write-Host "`#`#vso[build.addbuildtag]shipit";
}
}
# Synopsis: Remove temp files.
task Clean {
Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue;
}
task Build Clean, BuildModule, VersionModule, BuildHelp
task Test Build, TestRules
task Release ReleaseModule, TagBuild
task . Build, Test

Просмотреть файл

@ -0,0 +1,117 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
@{
# Script module or binary module file associated with this manifest.
# RootModule = 'PSRule.Requirements.psm1'
# Version number of this module.
ModuleVersion = '0.0.1'
# Supported PSEditions
CompatiblePSEditions = 'Core', 'Desktop'
# ID used to uniquely identify this module
GUID = '8e1d8dbc-e2d8-40ae-9668-ac21b82b4e12'
# Author of this module
Author = 'Microsoft Corporation'
# Company or vendor of this module
CompanyName = 'Microsoft Corporation'
# Copyright statement for this module
Copyright = '(c) Microsoft Corporation. All rights reserved.'
# Description of the functionality provided by this module
# Description = ''
# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.1'
# Name of the PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
DotNetFrameworkVersion = '4.7.2'
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @(
@{ ModuleName = 'PSRule'; ModuleVersion = '0.0.1' }
@{ ModuleName = 'Requirements'; ModuleVersion = '0.0.1' }
)
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
# Variables to export from this module
VariablesToExport = @()
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()
# A URL to the license for this module.
LicenseUri = 'https://github.com/Microsoft/PSRule.Requirements/blob/master/LICENSE'
# A URL to the main website for this project.
ProjectUri = 'https://github.com/Microsoft/PSRule.Requirements'
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
ReleaseNotes = 'https://github.com/Microsoft/PSRule.Requirements/blob/master/CHANGELOG.md'
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}

Просмотреть файл

@ -0,0 +1,31 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# Unit tests for core PSRule.Requirements cmdlets
#
[CmdletBinding()]
param (
)
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
# Setup tests paths
$rootPath = $PWD;
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule.Requirements) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
$outputPath = Join-Path -Path $rootPath -ChildPath out/tests/PSRule.Requirements.Tests/Common;
Remove-Item -Path $outputPath -Force -Recurse -Confirm:$False -ErrorAction Ignore;
$Null = New-Item -Path $outputPath -ItemType Directory -Force;
## Add tests