Skip to content

Commit

Permalink
Added Sampler task
Browse files Browse the repository at this point in the history
  • Loading branch information
ykuijs committed Nov 6, 2023
1 parent 558e452 commit 39164e1
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 16 deletions.
179 changes: 179 additions & 0 deletions .build/tasks/Create-CompositeResource.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
param
(
# Project path
[Parameter()]
[System.String]
$ProjectPath = (property ProjectPath $BuildRoot),

[Parameter()]
# Base directory of all output (default to 'output')
[System.String]
$OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')),

[Parameter()]
[System.String]
$BuiltModuleSubdirectory = (property BuiltModuleSubdirectory ''),

[Parameter()]
[System.Management.Automation.SwitchParameter]
$VersionedOutputDirectory = (property VersionedOutputDirectory $true),

[Parameter()]
[System.String]
$ProjectName = (property ProjectName ''),

[Parameter()]
[System.String]
$PesterOutputFolder = (property PesterOutputFolder 'testResults'),

[Parameter()]
[System.String]
$PesterOutputFormat = (property PesterOutputFormat ''),

[Parameter()]
[System.Object[]]
$PesterScript = (property PesterScript ''),

[Parameter()]
[System.String[]]
$PesterTag = (property PesterTag @()),

[Parameter()]
[System.String[]]
$PesterExcludeTag = (property PesterExcludeTag @()),

[Parameter()]
[System.String]
$CodeCoverageThreshold = (property CodeCoverageThreshold ''),

# Build Configuration object
[Parameter()]
[System.Collections.Hashtable]
$BuildInfo = (property BuildInfo @{ })
)


# Synopsis: Merging several code coverage files together.
task Generate_Composite_Resource_Module {
# Get the vales for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
. Set-SamplerTaskVariable

"`tOutput Path = '$OutputDirectory'"

Write-Build White 'Generating Composite Resource Module'

$result = New-CompositeResourceModule -OutputPath $OutputDirectory

if ($result)
{
Write-Build Green "Module generation completed."
}
else
{
throw "Error occurred during module generation! Please check the log output."
}

# $osShortName = Get-OperatingSystemShortName

# $powerShellVersion = 'PSv.{0}' -f $PSVersionTable.PSVersion

# $moduleFileName = '{0}.psm1' -f $ProjectName

# $PesterOutputFolder = Get-SamplerAbsolutePath -Path $PesterOutputFolder -RelativeTo $OutputDirectory

# "`tPester Output Folder = '$PesterOutputFolder'"

# $GetCodeCoverageThresholdParameters = @{
# RuntimeCodeCoverageThreshold = $CodeCoverageThreshold
# BuildInfo = $BuildInfo
# }

# $CodeCoverageThreshold = Get-CodeCoverageThreshold @GetCodeCoverageThresholdParameters

# if (-not $CodeCoverageThreshold)
# {
# $CodeCoverageThreshold = 0
# }

# "`tCode Coverage Threshold = '$CodeCoverageThreshold'"

# if ($CodeCoverageThreshold -gt 0)
# {
# $getPesterOutputFileFileNameParameters = @{
# ProjectName = $ProjectName
# ModuleVersion = $ModuleVersion
# OsShortName = $osShortName
# PowerShellVersion = $powerShellVersion
# }

# $pesterOutputFileFileName = Get-PesterOutputFileFileName @getPesterOutputFileFileNameParameters

# $getCodeCoverageOutputFile = @{
# BuildInfo = $BuildInfo
# PesterOutputFolder = $PesterOutputFolder
# }

# $CodeCoverageOutputFile = Get-SamplerCodeCoverageOutputFile @getCodeCoverageOutputFile

# if (-not $CodeCoverageOutputFile)
# {
# $CodeCoverageOutputFile = (Join-Path -Path $PesterOutputFolder -ChildPath "CodeCov_$pesterOutputFileFileName")
# }

# "`tCode Coverage Output File = $CodeCoverageOutputFile"

# $CodeCoverageMergedOutputFile = 'CodeCov_Merged.xml'

# if ($BuildInfo.CodeCoverage.CodeCoverageMergedOutputFile)
# {
# $CodeCoverageMergedOutputFile = $BuildInfo.CodeCoverage.CodeCoverageMergedOutputFile
# }

# $CodeCoverageMergedOutputFile = Get-SamplerAbsolutePath -Path $CodeCoverageMergedOutputFile -RelativeTo $PesterOutputFolder

# "`tCode Coverage Merge Output File = $CodeCoverageMergedOutputFile"

# $CodeCoverageFilePattern = 'Codecov*.xml'

# if ($BuildInfo.ContainsKey('CodeCoverage') -and $BuildInfo.CodeCoverage.ContainsKey('CodeCoverageFilePattern'))
# {
# $CodeCoverageFilePattern = $BuildInfo.CodeCoverage.CodeCoverageFilePattern
# }

# "`tCode Coverage File Pattern = $CodeCoverageFilePattern"

# if (-not [System.String]::IsNullOrEmpty($CodeCoverageFilePattern))
# {
# $codecovFiles = Get-ChildItem -Path $PesterOutputFolder -Include $CodeCoverageFilePattern -Recurse
# }

# "`tMerging Code Coverage Files = '{0}'" -f ($codecovFiles.FullName -join ', ')
# ""

# if (Test-Path -Path $CodeCoverageMergedOutputFile)
# {
# Write-Build Yellow "File $CodeCoverageMergedOutputFile found, deleting file."

# Remove-Item -Path $CodeCoverageMergedOutputFile -Force
# }

# Write-Build White "Processing folder: $OutputDirectory"

# if ($codecovFiles.Count -gt 1)
# {
# Write-Build DarkGray "Started merging $($codecovFiles.Count) code coverage files!"

# Start-CodeCoverageMerge -Files $codecovFiles -TargetFile $CodeCoverageMergedOutputFile

# Write-Build Green "Merge completed. Saved merge result to: $CodeCoverageMergedOutputFile"
# }
# else
# {
# throw "Found $($codecovFiles.Count) code coverage file. Need at least two files to merge."
# }
# }
# else
# {
# Write-Build White 'Code coverage is not enabled, skipping.'
# }
}
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added first version
- Added Composite Resource generator code
- Added Sampler task, to use the CRG in a pipeline
24 changes: 14 additions & 10 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
# SourcePath: ./Sampler/Sampler.psd1
# Output Directory where ModuleBuilder will build the Module, relative to module manifest
# OutputDirectory: ../output/Sampler
BuiltModuleSubdirectory: module
#BuiltModuleSubdirectory: module
CopyPaths:
- en-US
- Dependencies.psd1
- ../.build/tasks
suffix: suffix.ps1
Encoding: UTF8
# Can be used to manually specify module's semantic version if the preferred method of
# using GitVersion is not available, and it is not possible to set the session environment
Expand Down Expand Up @@ -74,6 +76,7 @@ BuildWorkflow:
publish:
- publish_module_to_gallery
- Publish_Release_To_GitHub
- Create_ChangeLog_GitHub_PR

####################################################
# PESTER Configuration #
Expand All @@ -83,6 +86,7 @@ Pester:
OutputFormat: NUnitXML
# Excludes one or more paths from being used to calculate code coverage.
ExcludeFromCodeCoverage:
- tasks

# If no scripts are defined the default is to use all the tests under the project's
# tests folder or source folder (if present). Test script paths can be defined to
Expand Down Expand Up @@ -115,14 +119,6 @@ Pester:
#CodeCoverageFilePattern: JaCoCo_Merge.xml # the pattern used to search all pipeline test job artifacts
#CodeCoverageMergedOutputFile: JaCoCo_coverage.xml # the file that is created for the merged code coverage

DscTest:
ExcludeTag:
- "Common Tests - New Error-Level Script Analyzer Rules"
Tag:
ExcludeSourceFile:
- output
# - Templates

# Import ModuleBuilder tasks from a specific PowerShell module using the build
# task's alias. Wildcard * can be used to specify all tasks that has a similar
# prefix and or suffix. The module contain the task must be added as a required
Expand All @@ -133,7 +129,6 @@ ModuleBuildTasks:
Sampler.GitHubTasks:
- '*.ib.tasks'


# Invoke-Build Header to be used to 'decorate' the terminal output of the tasks.
TaskHeader: |
param($Path)
Expand All @@ -145,3 +140,12 @@ TaskHeader: |
Write-Build DarkGray " $Path"
Write-Build DarkGray " $($Task.InvocationInfo.ScriptName):$($Task.InvocationInfo.ScriptLineNumber)"
""
GitHubConfig:
ReleaseAssets:
- 'output/CHANGELOG.md'
GitHubFilesToAdd:
- 'CHANGELOG.md'
GitHubConfigUserName: ykuijs
GitHubConfigUserEmail: [email protected]
UpdateChangelogOnPrerelease: false
2 changes: 1 addition & 1 deletion source/M365DSC.CRG.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ CmdletsToExport = @('New-CompositeResourceModule')
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 = @()
AliasesToExport = '*'

# DSC resources to export from this module
DscResourcesToExport = @()
Expand Down
6 changes: 3 additions & 3 deletions source/Private/Initialize-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Initialize-Module
Initializes the module by creating all required files and folders
.Description
This function initializes the M365Config module by creating the
This function initializes the M365DSC.CompositeResources module by creating the
required folder structure and the module manifest file.
.Parameter Version
Expand Down Expand Up @@ -33,7 +33,7 @@ function Initialize-Module
process
{
# Create the folder structure
$modulePath = Join-Path -Path $OutputPath -ChildPath "M365Config\$Version"
$modulePath = Join-Path -Path $OutputPath -ChildPath "M365DSC.CompositeResources\$Version"
if ((Test-Path -Path $modulePath) -eq $false)
{
$null = New-Item -Path $modulePath -ItemType Directory
Expand Down Expand Up @@ -74,7 +74,7 @@ function Initialize-Module
[void]$moduleManifestString.AppendLine(' }')
[void]$moduleManifestString.AppendLine('}')

$moduleManifestFileName = 'M365Config.psd1'
$moduleManifestFileName = 'M365DSC.CompositeResources.psd1'
$moduleManifestFilePath = Join-Path -Path $modulePath -ChildPath $moduleManifestFileName

# Save the module manifest content to file
Expand Down
2 changes: 1 addition & 1 deletion source/Private/Save-Resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Save-Resource

process
{
$modulePath = Join-Path -Path $OutputPath -ChildPath "M365Config\$Version"
$modulePath = Join-Path -Path $OutputPath -ChildPath "M365DSC.CompositeResources\$Version"
$dscResourcesPath = Join-Path -Path $modulePath -ChildPath 'DscResources'

# Save the schema file of the composite resource
Expand Down
9 changes: 9 additions & 0 deletions source/suffix.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Inspired from https://github.com/nightroman/Invoke-Build/blob/64f3434e1daa806814852049771f4b7d3ec4d3a3/Tasks/Import/README.md#example-2-import-from-a-module-with-tasks
Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'tasks\*') -Include '*.build.*' |
ForEach-Object -Process {
$ModuleName = ([System.IO.FileInfo] $MyInvocation.MyCommand.Name).BaseName
$taskFileAliasName = "$($_.BaseName).$ModuleName.ib.tasks"
Set-Alias -Name $taskFileAliasName -Value $_.FullName

Export-ModuleMember -Alias $taskFileAliasName
}

0 comments on commit 39164e1

Please sign in to comment.