PowerCD.psm1

using namespace Microsoft.Powershell.Commands
using namespace NuGet.Versioning
using namespace System.Collections.Generic
using namespace System.IO
using namespace System.IO.Path
function BootstrapPSGetBeta {
    #PowerCD Module Directory for builds
    $SCRIPT:powercdModulePath = Join-Path ([Environment]::GetFolderpath('LocalApplicationData')) 'PowerCD'

    #Fetch PowerShellGet 3.0+ if not already present. We use this instead of Save-Module to avoid
    #loading the builtin version of PowershellGet

    #This is a temporary "fast fetch" for latest version of PowershellGet
    if (Get-Module -FullyQualifiedName @{ModuleName='PowershellGet';ModuleVersion='2.999'}) {
        Write-Verbose 'PowershellGet 3.0 Detected, skipping bootstrap'
        return
    }
    #Get latest published prelease version
    $moduleInfo = (Invoke-RestMethod -UseBasicParsing 'https://www.powershellgallery.com/api/v2/Packages?$filter=Id%20eq%20%27PowershellGet%27%20and%20IsAbsoluteLatestVersion%20eq%20true&$select=Id,Version,NormalizedVersion')
    $moduleVersion = $moduleInfo.properties.NormalizedVersion
    $moduleUri = $moduleInfo.content.src
    $psgetModulePath = Join-Path $powercdModulePath 'PowerShellGet'
    $moduleManifestPath = [IO.Path]::Combine($psGetModulePath, $moduleVersion, 'PowerShellGet.psd1')

    if (-not (Test-Path $ModuleManifestPath)) {
        Write-Verbose "Latest PowershellGet Not Found, Installing $moduleVersion..."
        if (Test-Path $psgetModulePath) {Remove-Item $psGetModulePath -recurse -force}
        $psGetZipPath = join-path $powercdModulePath "PowerShellGet.zip"
        New-Item -ItemType Directory -Path $powercdModulePath -Force > $null
        (New-Object Net.WebClient).DownloadFile($moduleURI, $psGetZipPath) > $null

        #Required due to a quirk in Windows Powershell 5.1: https://stackoverflow.com/questions/29007742/unable-to-use-system-io-compression-filesystem-dll/29022092


        # Add-Type -AssemblyName mscorlib
        # Add-Type -assembly "System.IO.Compression.Filesystem"
        # Add-Type -assembly "System.IO.Compression"
        #Write-Verbose ([System.IO.Compression.ZipFile].assembly)
        #[System.IO.Compression.ZipFile]::ExtractToDirectory($psGetZipPath, (Split-Path $ModuleManifestPath)) > $null
        $progressPreference = 'SilentlyContinue'
        #Prefer 7zip if available as it is much faster for extraction, as well as issue for Github Actions windows powershell build
        try {
            if (Get-Command '7z' -ErrorAction Stop) {
                & 7z x $psGetZipPath -y -o"$(Split-Path $ModuleManifestPath)" *>$null
            }
            if (-not (Test-Path $ModuleManifestPath)) {throw '7zip Extraction Failed'}
        } catch {
            Write-Debug "BootstrapPSGetBeta: 7z executable not found, falling back to Expand-Archive"
            #Fall back to legacy powershell extraction
            Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
            Expand-Archive -Path $psGetZipPath -DestinationPath (Split-Path $ModuleManifestPath)
        }

        $progressPreference = 'Continue'
    }

    Import-Module -Force $moduleManifestPath -Scope Global -ErrorAction Stop 4>$null

    #Register Powershell Gallery if not present
    try {
        if (-not (Get-PSResourceRepository -Name psgallery)) {
            Register-PSResourceRepository -PSGallery -Trusted
        }
    } catch [ArgumentException] {
        if ([String]$PSItem -match 'not able to successfully find xml') {
            Register-PSResourceRepository -PSGallery -Trusted
        }
    }
}
function GenerateAzDevopsMatrix {
    $os = @(
        'windows-latest'
        'vs2017-win2016'
        'ubuntu-latest'
        'macOS-latest'
    )

    $psversion = @(
        'pwsh'
        'powershell'
    )

    $exclude = 'ubuntu-latest-powershell','macOS-latest-powershell'

    $entries = @{}
    foreach ($osItem in $os) {
        foreach ($psverItem in $psversion) {
            $entries."$osItem-$psverItem" = @{os=$osItem;psversion=$psverItem}
        }
    }

    $exclude.foreach{
        $entries.Remove($PSItem)
    }

    $entries.keys | sort | foreach {
        " $PSItem`:"
        " os: $($entries[$PSItem].os)"
        " psversion: $($entries[$PSItem].psversion)"
    }

}


#requires -Version 2.0
<#
    .NOTES
    ===========================================================================
     Filename : Merge-Hashtables.ps1
     Created on : 2014-09-04
     Created by : Frank Peter Schultze
    ===========================================================================

    .SYNOPSIS
        Create a single hashtable from two hashtables where the second given
        hashtable will override.

    .DESCRIPTION
        Create a single hashtable from two hashtables. In case of duplicate keys
        the function the second hashtable's key values "win". Merge-Hashtables
        supports nested hashtables.

    .EXAMPLE
        $configData = Merge-Hashtables -First $defaultData -Second $overrideData

    .INPUTS
        None

    .OUTPUTS
        System.Collections.Hashtable
#>

function Merge-Hashtables
{
    [CmdletBinding()]
    Param
    (
        #Identifies the first hashtable
        [Parameter(Mandatory=$true)]
        [Hashtable]
        $First
    ,
        #Identifies the second hashtable
        [Parameter(Mandatory=$true)]
        [Hashtable]
        $Second
    )

    function Set-Keys ($First, $Second)
    {
        @($First.Keys) | Where-Object {
            $Second.ContainsKey($_)
        } | ForEach-Object {
            if (($First.$_ -is [Hashtable]) -and ($Second.$_ -is [Hashtable]))
            {
                Set-Keys -First $First.$_ -Second $Second.$_
            }
            else
            {
                $First.Remove($_)
                $First.Add($_, $Second.$_)
            }
        }
    }

    function Add-Keys ($First, $Second)
    {
        @($Second.Keys) | ForEach-Object {
            if ($First.ContainsKey($_))
            {
                if (($Second.$_ -is [Hashtable]) -and ($First.$_ -is [Hashtable]))
                {
                    Add-Keys -First $First.$_ -Second $Second.$_
                }
            }
            else
            {
                $First.Add($_, $Second.$_)
            }
        }
    }

    # Do not touch the original hashtables
    $firstClone  = $First.Clone()
    $secondClone = $Second.Clone()

    # Bring modified keys from secondClone to firstClone
    Set-Keys -First $firstClone -Second $secondClone

    # Bring additional keys from secondClone to firstClone
    Add-Keys -First $firstClone -Second $secondClone

    # return firstClone
    $firstClone
}


function Select-HashTable {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory,ValueFromPipeline)][Hashtable]$Hashtable,
        [String[]]$Include,
        [String[]]$Exclude
    )

    if (-not $Include) {$Include = $HashTable.Keys}

    $filteredHashTable = @{}
    $HashTable.keys.where{
        $PSItem -in $Include
    }.where{
        $PSItem -notin $Exclude
    }.foreach{
        $filteredHashTable[$PSItem] = $HashTable[$PSItem]
    }
    return $FilteredHashTable
}
<#
.SYNOPSIS
This function prepares a powershell module from a source powershell` module directory
.DESCRIPTION
This function can also optionally "compile" the module, which is place all relevant powershell code in a single .psm1 file. This improves module load performance.
If you choose to compile, place any script lines you use to dot-source the other files in your .psm1 file into a #region SourceInit region block, and this function will replace it with the "compiled" scriptblock
#>

function Build-PowerCDModule {
    [CmdletBinding()]
    param (
        #Path to the Powershell Module Manifest representing the file you wish to compile
        $PSModuleManifest = $PCDSetting.BuildEnvironment.PSModuleManifest,
        #Path to the build destination. This should be non-existent or deleted by Clean prior
        $Destination = $pcdSetting.BuildModuleOutput,
        #By Default this command expects a nonexistent destination, specify this to allow for a "Dirty" copy
        [Switch]$Force,
        #By default, the build will consolidate all relevant module files into a single .psm1 file. This enables the module to load faster. Specify this if you want to instead copy the files as-is
        [Switch]$NoCompile,
        #If you chose compile, specify this for the region block in your .psm1 file to replace with the compiled code. If not specified, it will just append to the end of the file. Defaults to 'SourceInit' for #region SourceInit
        [String]$SourceRegionName = 'SourceInit',
        #Files that are considered for inclusion to the 'compiled' module. This by default includes .ps1 files only. Uses Filesystem Filter syntax
        [String[]]$PSFileInclude = '*.ps1',
        #Files that are considered for inclusion to the 'compiled' module. This excludes any files that have two periods before ps1 (e.g. .build.ps1, .tests.ps1). Uses Filesystem Filter syntax
        [String[]]$PSFileExclude = '*.*.ps1',
        #If a prerelease tag exists, the build will touch a prerelease warning file into the root of the module folder. Specify this parameter to disable this behavior.
        [Switch]$NoPreReleaseFile
    )

    $SourceModuleDir = Split-Path $PSModuleManifest

    #Verify a clean build folder
    try {
        $DestinationDirectory = New-Item -ItemType Directory -Path $Destination -ErrorAction Stop
    } catch [IO.IOException] {
        if ($PSItem.exception.message -match 'already exists\.$') {
            throw "Folder $Destination already exists. Make sure that you cleaned your Build Output directory. To override this behavior, specify -Force"
        } else {
            throw $PSItem
        }
    }

    #TODO: Use this one command and sort out the items later
    #$FilesToCopy = Get-ChildItem -Path $PSModuleManifestDirectory -Filter '*.ps*1' -Exclude '*.tests.ps1' -Recurse

    $SourceManifest = Import-PowershellDataFile -Path $PSModuleManifest

    #TODO: Allow .psm1 to be blank and generate it on-the-fly
    if (-not $SourceManifest.RootModule) {throw "The source manifest at $PSModuleManifest does not have a RootModule specified. This is required to build the module."}
    $SourceRootModulePath = Join-Path $SourceModuleDir $sourceManifest.RootModule
    $SourceRootModule = Get-Content -Raw $SourceRootModulePath

    $pcdSetting.ModuleManifest = $SourceManifest

    #Cannot use Copy-Item Directly because the filtering isn't advanced enough (can't exclude)
    $SourceFiles = Get-ChildItem -Path $SourceModuleDir -Include $PSFileInclude -Exclude $PSFileExclude -File -Recurse
    if (-not $NoCompile) {
        #TODO: Apply ordering if important (e.g. classes)

        #Collate the files, pulling out using lines because these have to go first
        [String[]]$UsingLines = @()
        [String]$CombinedSourceFiles = ((Get-Content -Raw $SourceFiles) -split '\r?\n' | Where-Object {
            if ($_ -match '^using .+$') {
                $UsingLines += $_
                return $false
            }
            return $true
        }) -join [Environment]::NewLine

        #If a SourceInit region was set, inject the files there, otherwise just append to the end.
        $sourceRegionRegex = "(?s)#region $SourceRegionName.+#endregion $SourceRegionName"
        if ($SourceRootModule -match $sourceRegionRegex) {
            #Need to escape the $ in the replacement string
            $RegexEscapedCombinedSourceFiles = [String]$CombinedSourceFiles.replace('$','$$')
            $SourceRootModule = $SourceRootModule -replace $sourceRegionRegex,$RegexEscapedCombinedSourceFiles
        } else {
            #Just add them to the end of the file
            $SourceRootModule += [Environment]::NewLine + $CombinedSourceFiles
        }

        #Use a stringbuilder to piece the portions of the config back together, with using statements up-front
        [Text.StringBuilder]$OutputRootModule = ''
        $UsingLines.trim() | Sort-Object -Unique | Foreach-Object {
            [void]$OutputRootModule.AppendLine($PSItem)
        }
        [void]$OutputRootModule.AppendLine($SourceRootModule)
        [String]$SourceRootModule = $OutputRootModule

        #Strip non-help-related comments and whitespace
        #[String]$SourceRootModule = Remove-CommentsAndWhiteSpace $SourceRootModule
    } else {
        #TODO: Track all files in the source directory to ensure none get missed on the second step

        #In order to get relative paths we have to be in the directory we want to be relative to
        Push-Location (Split-Path $PSModuleManifest)

        $SourceFiles | Foreach-Object {
            #Powershell 6+ Preferred way.
            #TODO: Enable when dropping support for building on 5.x
            #$RelativePath = [io.path]::GetRelativePath($SourceModuleDir,$PSItem.fullname)

            #Powershell 3.x compatible "Ugly" Regex method
            #$RelativePath = $PSItem.FullName -replace [Regex]::Escape($SourceModuleDir),''

            $RelativePath = Resolve-Path $PSItem.FullName -Relative

            #Copy-Item doesn't automatically create directory structures when copying files vs. directories
            $DestinationPath = Join-Path $DestinationDirectory $RelativePath
            $DestinationDir = Split-Path $DestinationPath
            if (-not (Test-Path $DestinationDir)) {New-Item -ItemType Directory $DestinationDir > $null}
            Copy-Item -Path $PSItem -Destination $DestinationPath
        }

        #Return after processing relative paths
        Pop-Location
    }

    #Output the (potentially) modified Root Module
    $SourceRootModule | Out-File -FilePath (join-path $DestinationDirectory $SourceManifest.RootModule)

    #If there is a "lib" folder, copy that as-is
    if (Test-Path "$SourceModuleDir/lib") {
        Write-Verbose "lib folder detected, copying entire contents"
        Copy-Item -Recurse -Force -Path "$SourceModuleDir/lib" -Destination $DestinationDirectory
    }

    #Copy the Module Manifest
    [String]$PCDSetting.OutputModuleManifest = Copy-Item -PassThru -Path $PSModuleManifest -Destination $DestinationDirectory
    $ENV:PowerCDModuleManifest = $PCDSetting.OutputModuleManifest

    #Add a prerelease
    if ($PCDSetting.PreRelease) {
        "This is a prerelease build and not meant for deployment!" > (Join-Path $DestinationDirectory "PRERELEASE-$($PCDSetting.VersionLabel)")
    }
}
function Compress-PowerCDModule {
    [CmdletBinding()]
    param(
        #Path to the directory to archive
        [Parameter(Mandatory)]$Path,
        #Output for Zip File Name
        [Parameter(Mandatory)]$Destination
    )

    $CompressArchiveParams = @{
        Path = $Path
        DestinationPath = $Destination
    }

    $CurrentProgressPreference = $GLOBAL:ProgressPreference
    $GLOBAL:ProgressPreference = 'SilentlyContinue'
    Compress-Archive @CompressArchiveParams
    $GLOBAL:ProgressPreference = $CurrentProgressPreference
    write-verbose ("Zip File Output:" + $CompressArchiveParams.DestinationPath)
}
<#
.SYNOPSIS
Fetch the names of public functions in the specified folder using AST
.DESCRIPTION
This is a better method than grabbing the names of the .ps1 file and "hoping" they line up.
This also only gets parent functions, child functions need not apply
#>


function Get-PowerCDPublicFunctions {
    [CmdletBinding()]
    param(
        #The path to the public module directory containing the modules. Defaults to the "Public" folder where the source module manifest resides.
        [String]$PublicModulePath = (Join-Path (Split-Path $PCDSetting.BuildEnvironment.PSModuleManifest) 'Public')
    )

    $PublicFunctionCode = Get-ChildItem $PublicModulePath -Filter '*.ps1'

    #using statements have to be first, so we have to pull them out and move them to the top
    [String[]]$UsingLines = @()
    [String]$PublicFunctionCodeWithoutUsing = (Get-Content $PublicFunctionCode.FullName | Where-Object {
        if ($_ -match '^using .+$') {
            $UsingLines += $_
            return $false
        }
        return $true
    }) -join [Environment]::NewLine

    #Rebuild PublicFunctionCode with a stringbuilder to put all the using up top
    [Text.StringBuilder]$PublicFunctionCode = ''
    $UsingLines | Select-Object -Unique | Foreach-Object {
        [void]$PublicFunctionCode.AppendLine($PSItem)
    }
    [void]$PublicFunctionCode.AppendLine($PublicFunctionCodeWithoutUsing)

    [ScriptBlock]::Create($PublicFunctionCode).AST.EndBlock.Statements | Where-Object {
        $PSItem -is [Management.Automation.Language.FunctionDefinitionAst]
    } | Foreach-Object Name
}
#TODO: Move this to Microsoft.Extensions.Configuration
function Get-PowerCDSetting {
    [CmdletBinding()]
    param (
        #Build Output Directory Name. Defaults to Get-BuildEnvironment Default which is 'BuildOutput'
        $BuildOutput = 'BuildOutput'
    )

    $Settings = [ordered]@{}

    $Settings.BuildEnvironment = (Get-BuildEnvironment -BuildOutput $BuildOutput -As Hashtable).AsReadOnly()

    $Settings.General = [ordered]@{
        # Root directory for the project
        ProjectRoot = $Settings.BuildEnvironment.ProjectPath

        # Root directory for the module
        SrcRootDir = $Settings.BuildEnvironment.ModulePath

        # The name of the module. This should match the basename of the PSD1 file
        ModuleName = $Settings.BuildEnvironment.ProjectName

        # Module version
        ModuleVersion = (Import-PowerShellDataFile -Path $Settings.BuildEnvironment.PSModuleManifest).ModuleVersion

        # Module manifest path
        ModuleManifestPath = $Settings.BuildEnvironment.PSModuleManifest
    }

    $Settings.Build = [ordered]@{
        Dependencies = @('StageFiles', 'BuildHelp')

        # Default Output directory when building a module
        OutDir = $Settings.BuildEnvironment.BuildOutput

        # Module output directory
        # This will be computed in 'Initialize-PSBuild' so we can allow the user to
        # override the top-level 'OutDir' above and compute the full path to the module internally
        ModuleOutDir = $Settings.BuildEnvironment.BuildOutput

        # Controls whether to "compile" module into single PSM1 or not
        CompileModule = $true

        # List of files to exclude from output directory
        Exclude = @()
    }


    $Settings.Test = [ordered]@{
        # Enable/disable Pester tests
        Enabled = $true

        # Directory containing Pester tests
        RootDir = Join-Path -Path $Settings.BuildEnvironment.ProjectPath -ChildPath tests

        # Specifies an output file path to send to Invoke-Pester's -OutputFile parameter.
        # This is typically used to write out test results so that they can be sent to a CI
        # system like AppVeyor.
        OutputFile = ([IO.Path]::Combine($Settings.Environment.BuildOutput,"$($Settings.Environment.ProjectName)-TestResults_PS$($psversiontable.psversion)`_$(get-date -format yyyyMMdd-HHmmss).xml"))

        # Specifies the test output format to use when the TestOutputFile property is given
        # a path. This parameter is passed through to Invoke-Pester's -OutputFormat parameter.
        OutputFormat = 'NUnitXml'

        ScriptAnalysis = [ordered]@{
            # Enable/disable use of PSScriptAnalyzer to perform script analysis
            Enabled = $true

            # When PSScriptAnalyzer is enabled, control which severity level will generate a build failure.
            # Valid values are Error, Warning, Information and None. "None" will report errors but will not
            # cause a build failure. "Error" will fail the build only on diagnostic records that are of
            # severity error. "Warning" will fail the build on Warning and Error diagnostic records.
            # "Any" will fail the build on any diagnostic record, regardless of severity.
            FailBuildOnSeverityLevel = 'Error'

            # Path to the PSScriptAnalyzer settings file.
            SettingsPath = Join-Path $PSScriptRoot -ChildPath ScriptAnalyzerSettings.psd1
        }

        CodeCoverage = [ordered]@{
            # Enable/disable Pester code coverage reporting.
            Enabled = $false

            # Fail Pester code coverage test if below this threshold
            Threshold = .75

            # CodeCoverageFiles specifies the files to perform code coverage analysis on. This property
            # acts as a direct input to the Pester -CodeCoverage parameter, so will support constructions
            # like the ones found here: https://github.com/pester/Pester/wiki/Code-Coverage.
            Files = @(
                Join-Path -Path $Settings.BuildEnvironment.ModulePath -ChildPath '*.ps1'
                Join-Path -Path $Settings.BuildEnvironment.ModulePath -ChildPath '*.psm1'
            )
        }
    }

    $Settings.Help  = [ordered]@{
        # Path to updateable help CAB
        UpdatableHelpOutDir = Join-Path -Path $Settings.Build.ModuleOutDir -ChildPath 'UpdatableHelp'

        # Default Locale used for help generation, defaults to en-US
        DefaultLocale = (Get-UICulture).Name

        # Convert project readme into the module about file
        ConvertReadMeToAboutHelp = $false
    }

    $Settings.Docs = [ordered]@{
        # Directory PlatyPS markdown documentation will be saved to
        RootDir = Join-Path -Path $Settings.Build.ModuleOutDir -ChildPath 'docs'
    }

    $Settings.Publish = [ordered]@{
        # PowerShell repository name to publish modules to
        PSRepository = 'PSGallery'

        # API key to authenticate to PowerShell repository with
        PSRepositoryApiKey = $env:PSGALLERY_API_KEY

        # Credential to authenticate to PowerShell repository with
        PSRepositoryCredential = $null
    }

    # Enable/disable generation of a catalog (.cat) file for the module.
    # [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
    # $catalogGenerationEnabled = $true

    # # Select the hash version to use for the catalog file: 1 for SHA1 (compat with Windows 7 and
    # # Windows Server 2008 R2), 2 for SHA2 to support only newer Windows versions.
    # [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
    # $catalogVersion = 2

    return $Settings
}

function Get-PowerCDVersion {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Version]$GitVersionVersion = '5.2.4'
    )
    # $ENV:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = $true
    # $ENV:DOTNET_NOLOGO = $true
    #Try Skipping first run experience
    [void](dotnet help *>&1)

    try {
        [String]$gitVersionStatus = dotnet tool install -g gitversion.tool --version 5.3.3 *>&1
    } catch {
        if ([String]$PSItem -notmatch 'is already installed') {
            throw $PSItem.exception
        }
    }

    #Reference Dotnet Local Tool directly rather than trying to go through .NET EXE
    #This appears to be an issue where dotnet is installed but the tools aren't added to the path for Linux
    $GitVersionExe = "$HOME/.dotnet/tools/dotnet-gitversion"
    $GitVersionParams = $null
    [String[]]$GitVersionParams += '/nofetch'
    if (-not (Test-Path (Join-Path $PCDSetting.BuildEnvironment.Projectpath 'GitVersion.yml' ))) {
        #Use the PowerCD Builtin
        $GitVersionConfigPath = Resolve-Path (Join-Path (Split-Path (Get-Module PowerCD).Path) '.\GitVersion.yml')
        $GitVersionParams += '/config'
        $GitVersionParams += $GitVersionConfigPath
    }
    try {
        $GitVersionOutput = & $GitVersionExe $GitVersionParams
        if (-not $GitVersionOutput) {throw "GitVersion returned no output. Are you sure it ran successfully?"}

        #Since GitVersion doesn't return error exit codes, we look for error text in the output
        if ($GitVersionOutput -match '^[ERROR|INFO] \[') {throw "An error occured when running GitVersion.exe in $buildRoot"}
        $SCRIPT:GitVersionInfo = $GitVersionOutput | ConvertFrom-JSON -ErrorAction stop

        if ($PCDSetting.Debug) {
            Invoke-Expression "$GitVersionExe /diag"  | write-debug
        }

        $GitVersionInfo | format-list | out-string | write-verbose

        #TODO: Older packagemanagement don't support hyphens in Nuget name for some reason. Restore when fixed
        #[String]$PCDSetting.PreRelease = $GitVersionInfo.NuGetPreReleaseTagV2
        #[String]$PCDSetting.VersionLabel = $GitVersionInfo.NuGetVersionV2
        #Remove separator characters for now, for instance in branch names
        [Version]$PCDSetting.Version     = $GitVersionInfo.MajorMinorPatch
        [String]$PCDSetting.PreRelease   = $GitVersionInfo.NuGetPreReleaseTagV2 -replace '[\/\\\-]',''
        [String]$PCDSetting.VersionLabel = $PCDSetting.Version,$PCDSetting.PreRelease -join '-'

        if ($PCDSetting.BuildEnvironment.BuildOutput) {
            #Dont use versioned folder
            #TODO: Potentially put this back
            # $PCDSetting.BuildModuleOutput = [io.path]::Combine($PCDSetting.BuildEnvironment.BuildOutput,$PCDSetting.BuildEnvironment.ProjectName,$PCDSetting.Version)
            $PCDSetting.BuildModuleOutput = [io.path]::Combine($PCDSetting.BuildEnvironment.BuildOutput,$PCDSetting.BuildEnvironment.ProjectName)
        }
    } catch {
        write-warning "There was an error when running GitVersion.exe $buildRoot`: $PSItem. The output of the command (if any) is below...`r`n$GitVersionOutput"
        & $GitVersionexe /diag
        throw 'Exiting due to failed Gitversion execution'
    } finally {
        #Restore the tag if it was present
        #TODO: Evaluate if this is still necessary
        # if ($currentTag) {
        # write-build DarkYellow "Task $($task.name) - Restoring tag $currentTag."
        # git tag $currentTag -a -m "Automatic GitVersion Release Tag Generated by Invoke-Build"
        # }
    }

    return $GitVersionOutput

    # #GA release detection
    # if ($BranchName -eq 'master') {
    # $Script:IsGARelease = $true
    # $Script:ProjectVersion = $ProjectBuildVersion
    # } else {
    # #The regex strips all hypens but the first one. This shouldn't be necessary per NuGet spec but Update-ModuleManifest fails on it.
    # $SCRIPT:ProjectPreReleaseVersion = $GitVersionInfo.nugetversion -replace '(?<=-.*)[-]'
    # $SCRIPT:ProjectVersion = $ProjectPreReleaseVersion
    # $SCRIPT:ProjectPreReleaseTag = $SCRIPT:ProjectPreReleaseVersion.split('-')[1]
    # }

    # write-build Green "Task $($task.name)` - Calculated Project Version: $ProjectVersion"

    # #Tag the release if this is a GA build
    # if ($BranchName -match '^(master|releases?[/-])') {
    # write-build Green "Task $($task.name)` - In Master/Release branch, adding release tag v$ProjectVersion to this build"

    # $SCRIPT:isTagRelease = $true
    # if ($BranchName -eq 'master') {
    # write-build Green "Task $($task.name)` - In Master branch, marking for General Availability publish"
    # [Switch]$SCRIPT:IsGARelease = $true
    # }
    # }

    # #Reset the build dir to the versioned release directory. TODO: This should probably be its own task.
    # $SCRIPT:BuildReleasePath = Join-Path $BuildProjectPath $ProjectBuildVersion
    # if (-not (Test-Path -pathtype Container $BuildReleasePath)) {New-Item -type Directory $BuildReleasePath | out-null}
    # $SCRIPT:BuildReleaseManifest = Join-Path $BuildReleasePath (split-path $env:BHPSModuleManifest -leaf)
    # write-build Green "Task $($task.name)` - Using Release Path: $BuildReleasePath"
}
<#
.SYNOPSIS
Retrieves the dotnet dependencies for a powershell module
.NOTES
This process basically builds a C# Powershell Standard Library and identifies the resulting assemblies. There is probably a more lightweight way to do this.
.EXAMPLE
Get-PSModuleNugetDependencies @{'System.Text.Json'='4.6.0'}
#>

function Get-PSModuleNugetDependencies {
    [CmdletBinding(SupportsShouldProcess,DefaultParameterSetName='String')]
    param (
        #A list of nuget packages to include. You can specify a nuget-style version with a / separator e.g. yamldotnet/3.2.*
        [Parameter(ParameterSetName='String',Mandatory,Position=0)][String[]]$PackageName,
        #Which packages and their associated versions to include, in hashtable form. Supports Nuget Versioning: https://docs.microsoft.com/en-us/nuget/concepts/package-versioning#version-ranges-and-wildcards
        [Parameter(ParameterSetName='Hashtable',Mandatory,Position=0)][HashTable]$Packages,
        #Which .NET Framework target to use. Defaults to .NET Standard 2.0 and is what you should use for PS5+ compatible modules
        [String]$Target = 'netstandard2.0',
        #Full name of the target framework, used for fetching the JSON-formatted dependencies TODO: Resolve this
        [String]$TargetFullName = '.NETStandard,Version=v2.0',
        #Where to output the resultant assembly files. Default is a new folder 'lib' in the current directory.
        [Parameter(Position=1)][String]$Destination,
        #Which PS Standard library to use. Defaults to 5.1.0.
        [String]$PowershellTarget = '5.1.0',
        [String]$BuildPath = (Join-Path ([io.path]::GetTempPath()) "PSModuleDeps-$((New-Guid).Guid)"),
        #Name of the build project. You normally don't need to change this.
        [String]$BuildProjectName = 'PSModuleDeps',
        #Whether to output the resultant copied file paths
        [Switch]$PassThru,
        #Whether to do an online restore check of the dependencies. Disable this to speed up the process at the risk of compatibility.
        [Switch]$NoRestore
    )

    if ($PSCmdlet.ParameterSetName -eq 'String') {
        $Packages = @{}
        $PackageName.Foreach{
            $PackageVersion = $PSItem -split '/'
            if ($PackageVersion.count -eq 2) {
                $Packages[$PackageVersion[0]] = $PackageVersion[1]
            } else {
                $Packages[$PSItem] = '*'
            }
        }
    }

    #Add Powershell Standard Library
    $Packages['PowerShellStandard.Library'] = $PowershellTarget

    if (-not ([version](dotnet --version) -ge 2.2)) {throw 'dotnet 2.2 or later is required. Make sure you have the .net core SDK 2.x+ installed'}

    #Add starter Project for netstandard 2.0
    $BuildProjectFile = Join-Path $BuildPath "$BuildProjectName.csproj"
    New-Item -ItemType Directory $BuildPath -Force > $null
@"
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
    <TargetFramework>$Target</TargetFramework>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="$PowerShellTarget">
  <PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
"@
 > $BuildProjectFile

    foreach ($ModuleItem in $Packages.keys) {

        $dotnetArgs = 'add',$BuildProjectFile,'package',$ModuleItem

        if ($Packages[$ModuleItem] -ne $true) {
            if ($NoRestore) {
                $dotNetArgs += '--no-restore'
            }
            $dotnetArgs += '--version'
            $dotnetArgs += $Packages[$ModuleItem]
        }
        write-verbose "Executing: dotnet $dotnetArgs"
        & dotnet $dotnetArgs | Write-Verbose
    }

    & dotnet publish -o $BuildPath $BuildProjectFile | Write-Verbose

    function ConvertFromModuleDeps ($Path) {
        $runtimeDeps = Get-Content -raw $Path | ConvertFrom-Json
        $depResult = [ordered]@{}
        $runtimeDeps.targets.$TargetFullName.psobject.Properties.name |
            Where-Object {$PSItem -notlike "$BuildProjectName*"} |
            Sort-Object |
            Foreach-Object {
                $depInfo = $PSItem -split '/'
                $depResult[$depInfo[0]] = $depInfo[1]
            }
        return $depResult
    }
    #Use return to end script here and don't actually copy the files
    $ModuleDeps = ConvertFromModuleDeps -Path $BuildPath/obj/project.assets.json

    if (-not $Destination) {
        #Output the Module Dependencies and end here
        Remove-Item $BuildPath -Force -Recurse
        return $ModuleDeps
    }

    if ($PSCmdlet.ShouldProcess($Destination,"Copy Resultant DLL Assemblies")) {
        New-Item -ItemType Directory $Destination -Force > $null
        $CopyItemParams = @{
            Path = "$BuildPath/*.dll"
            Exclude = "$BuildProjectName.dll"
            Destination = $Destination
            Force = $true
        }

        if ($PassThru) {$CopyItemParams.PassThru = $true}
        Copy-Item @CopyItemParams
        Remove-Item $BuildPath -Force -Recurse
    }
}
#Load Assemblies
function Import-Assembly {
    <#
    .SYNOPSIS
    Adds Binding Redirects for Certain Assemblies to make them more flexibly compatible with Windows Powershell
    #>

        [CmdletBinding()]
        param(
            #Path to the dependencies that you wish to add a binding redirect for
            [Parameter(Mandatory)][IO.FileInfo[]]$Path
        )
        if ($PSEdition -ne 'Desktop') {
            write-warning "Import-Assembly is only required on Windows Powershell and not Powershell Core. Skipping..."
            return
        }

        $pathAssemblies = $path.foreach{
            [reflection.assemblyname]::GetAssemblyName($PSItem)
        }
        $loadedAssemblies = [AppDomain]::CurrentDomain.GetAssemblies()
        #Bootstrap the required types in case this loads really early
        $null = Add-Type -AssemblyName mscorlib

        $onAssemblyResolveEventHandler = [ResolveEventHandler] {
            param($sender, $assemblyToResolve)

            try {
                $ErrorActionPreference = 'stop'
                [String]$assemblyToResolveStrongName = $AssemblyToResolve.Name
                [String]$assemblyToResolveName = $assemblyToResolveStrongName.split(',')[0]
                write-verbose "Import-Assembly: Resolving $AssemblyToResolveStrongName"

                #Try loading from our custom assembly list
                $bindingRedirectMatch = $pathAssemblies.where{
                    $PSItem.Name -eq $assemblyToResolveName
                }
                if ($bindingRedirectMatch) {
                    write-verbose "Import-Assembly: Creating a 'binding redirect' to $BindingRedirectMatch"
                    return [reflection.assembly]::LoadFrom($bindingRedirectMatch.CodeBase)
                }

                #Bugfix for System.Management.Automation.resources which comes up from time to time
                #TODO: Find the underlying reason why it asks for en instead of en-us
                if ($AssemblyToResolveStrongName -like 'System.Management.Automation.Resources*') {
                    $AssemblyToResolveStrongName = $AssemblyToResolveStrongName -replace 'Culture\=en\-us','Culture=en'
                    write-verbose "BUGFIX: $AssemblyToResolveStrongName"
                }

                Add-Type -AssemblyName $AssemblyToResolveStrongName -ErrorAction Stop
                return [System.AppDomain]::currentdomain.GetAssemblies() | where fullname -eq $AssemblyToResolveStrongName
                #Add Type doedsn'tAssume successful and return the object. This will be null if it doesn't exist and will fail resolution anyways

            } catch {
                write-host -fore red "Error finding $AssemblyToResolveName`: $($PSItem.exception.message)"
                return $null
            }

            #Return a null as a last resort
            return $null
        }
        [AppDomain]::CurrentDomain.add_AssemblyResolve($onAssemblyResolveEventHandler)

        Add-Type -Path $Path

        [System.AppDomain]::CurrentDomain.remove_AssemblyResolve($onAssemblyResolveEventHandler)
    }
<#
#TODO: Inject this code into the module psm1 file
    $ImportAssemblies = Get-Item "$PSScriptRoot/lib/*.dll"
    if ($PSEdition -eq 'Desktop') {
        Import-Assembly -Path $ImportAssemblies
    } else {
        Add-Type -Path $ImportAssemblies
    }

    #Add Back Extension Methods for ease of use
    #TODO: Make this a method


    try {
        Update-TypeData -Erroraction Stop -TypeName Microsoft.Extensions.Configuration.ConfigurationBuilder -MemberName AddYamlFile -MemberType ScriptMethod -Value {
            param([String]$Path)
            [Microsoft.Extensions.Configuration.YamlConfigurationExtensions]::AddYamlFile($this, $Path)
        }
    } catch {
        if ([String]$PSItem -match 'The member .+ is already present') {
            write-verbose "Extension Method already present"
            $return
        }
        #Write-Error $PSItem.exception
    }

    try {
        Update-TypeData -Erroraction Stop -TypeName Microsoft.Extensions.Configuration.ConfigurationBuilder -MemberName AddJsonFile -MemberType ScriptMethod -Value {
            param([String]$Path)
            [Microsoft.Extensions.Configuration.JsonConfigurationExtensions]::AddJsonFile($this, $Path)
        }
    } catch {
        if ([String]$PSItem -match 'The member .+ is already present') {
            write-verbose "Extension Method already present"
            $return
        }
        #Write-Error $PSItem.exception
    }
#>


function Import-PowerCDRequirement {
    <#
    .SYNOPSIS
    Installs modules from the Powershell Gallery. In the future this will support more once PSGet is more reliable
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        #Specify which modules to install. If a module needs to be a prerelease, specify the prerelease tag with __ after the module name e.g. PowershellGet__beta1
        [Parameter(Mandatory, ValueFromPipeline)][ModuleSpecification[]]$ModuleInfo,
        #Where to import the PowerCD Requirement. Defaults to the PowerCD folder in LocalAppData
        $Path = (Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'PowerCD')
    )
    begin {
        $modulesToInstall = [List[PSCustomObject]]@()

        #Make sure PSGet 3.0 is installed
        try {
            #This is an indirect way to load the nuget assemblies to get the NugetVersion type added to the session
            [Void](Get-PSResource -Name 'DummyModule')
        } catch {
            throw 'You need PowershellGet 3.0 or greater to use this command. Hint: BootstrapPSGetBeta'
        }
        function ConvertTo-NugetVersionRange ([ModuleSpecification]$ModuleSpecification) {
            try {
                #double-underscore is used as a prerelease delimiter, since ModuleSpecification doesn't currently have a prerelease field
                $preRelease = ($ModuleSpecification.Name -split '__')[1]
            } catch {
                $preRelease = $null
            }
            switch ($true) {
                ($null -ne $ModuleSpecification.RequiredVersion) {
                    if ($Prerelease) {
                        return "[$($ModuleSpecification.RequiredVersion)-$PreRelease]"
                    }
                    return "[$($ModuleSpecification.RequiredVersion)]"
                }

                ($ModuleSpecification.Version -and $ModuleSpecification.MaximumVersion) {
                    return "[$(ModuleSpecification.Version),$($ModuleSpecification.MaximumVersion)]"
                }

                ($ModuleSpecification.Version -and -not $ModuleSpecification.MaximumVersion) {
                    return [String]($ModuleSpecification.Version)
                }

                ($ModuleSpecification.MaximumVersion -and -not $ModuleSpecification.Version) {
                    return "(,$($ModuleSpecification.MaximumVersion)]"
                }
            }
        }
    }

    process {
        foreach ($ModuleInfoItem in $ModuleInfo) {
            $PSResourceParams = [Ordered]@{
                Name                = $ModuleInfoItem.Name.split('__')[0]
                IncludeDependencies = $true
            }
            if (Get-Module -FullyQualifiedName $ModuleInfoItem) {
                Write-Verbose "Module $(($ModuleInfoItem.Name,$ModuleInfoItem.Version -join ' ').trim()) is currently loaded. Skipping..."
                continue
            }
            $moduleVersion = ConvertTo-NugetVersionRange $ModuleInfoItem
            if ($ModuleVersion) { $PSResourceParams.Version = $ModuleVersion }

            [Bool]$IsPrerelease = try {
                [Bool](([NugetVersion]($ModuleVersion -replace '[\[\]]','')).IsPrerelease)
            } catch {
                $false
            }

            try {
                #TODO: Once PSGetv3 Folders are more stable, do a local check for the resource with PSModulePath first
                $modulesToInstall.Add((Find-PSResource @PSResourceParams -Prerelease:$IsPrerelease -ErrorAction Stop))
            } catch [NullReferenceException] {
                Write-Warning "Found nothing on the powershell gallery for $($PSResourceParams.Name) $($PSResourceParams.Version)"
            }
        }
    }

    end {
        foreach ($moduleItem in ($modulesToInstall | Sort-Object -Property Name,Version -Unique)) {
            $ModuleManifestPath = [IO.Path]::Combine($Path, $moduleItem.Name, $ModuleItem.Version, "$($ModuleItem.Name).psd1")
            $IsPrerelease = $moduleItem.Version.IsPrerelease
            #Check for the module existence in an efficient manner
            $moduleExists = if (Test-Path $ModuleManifestPath) {
                if ($IsPrerelease) {
                    ((Import-PowershellDataFile $ModuleManifestPath).privatedata.psdata.prerelease -eq $moduleItem.Version.Release)
                } else {
                    $true
                }
            } else {$false}
            if ($moduleExists) {
                Write-Verbose "Module $($ModuleItem.Name) $($ModuleItem.Version) already installed. Skipping..."
            } else {
                if ($PSCmdlet.ShouldProcess($Path,"Installing PowerCD Requirement $($ModuleItem.Name) $($ModuleItem.Version)")) {
                    try {
                        if ($isLinux) {
                            #FIXME: Remove after https://github.com/PowerShell/PowerShellGet/issues/123 is closed
                            Save-Module -RequiredVersion $ModuleItem.Version -Name $ModuleItem.Name -Path $Path -Force -AllowPrerelease:$IsPrerelease -ErrorAction Stop
                            #Save-Module doesn't save with the prelease tag name, so we need to import via the non-prerelease version folder instead
                            $ModuleManifestPath = [IO.Path]::Combine($Path, $moduleItem.Name, $ModuleItem.Version.Version.ToString(3), "$($ModuleItem.Name).psd1")
                        } else {
                            Save-PSResource -Path $Path -Name $ModuleItem.Name -Version "[$($ModuleItem.Version)]" -Prerelease:$IsPrerelease -ErrorAction Stop
                        }
                    } catch [IO.IOException] {
                        if ([string]$PSItem -match 'Cannot create a file when that file already exists') {
                            Write-Warning "Module $($ModuleItem.Name) $($ModuleItem.Version) already exists. This is probably a bug because the manifest wasn't detected. Skipping..."
                        } else {
                            throw $PSItem
                        }
                    }
                }
            }

            try {
                #Only try to import if not already loaded, speeds up repeat attempts
                if (-not (Get-Module $ModuleItem.Name).Path -eq ($ModuleManifestPath -replace 'psd1$','psm1')) {
                    Import-Module $ModuleManifestPath -Global -ErrorAction Stop -Verbose:$false > $null
                }
            } catch {
                $caughtError = $PSItem
                #Catch common issues
                switch -regex ([String]$PSItem) {
                    'Error in TypeData "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.RuleInfo"' {
                        throw [InvalidOperationException]'Detected an incompatible PSScriptAnalyzer was already loaded. Please restart your Powershell session.'
                    }
                    'assertion operator .+ has been added multiple times' {
                        throw [InvalidOperationException]'Detected an incompatible Pester was already loaded. Please restart your Powershell session.'
                    }
                    default {
                        throw $CaughtError.Exception
                    }
                }
            }
        }
        #Pester 5 check
        if (-not (Get-Module -FullyQualifiedName @{ModuleName='Pester';ModuleVersion='4.9999'})) {
            throw 'A loaded Pester version less than 5.0 was detected. Please restart your Powershell session'
        }
    }
    #Use this for Save-Module




    # }
    # process { foreach ($ModuleName in $ModuleName) {
    # #Get a temporary directory
    # $tempModulePath = [io.path]::Combine([io.path]::GetTempPath(), 'PowerCD', $ModuleName)
    # $ModuleManifestPath = Join-Path $tempModulePath "$ModuleName.psd1"
    # $tempfile = join-path $tempModulePath "$ModuleName.zip"

    # if ((Test-Path $ModuleManifestPath) -and -not $Force) {
    # if ($Version) {

    # }
    # Write-Verbose "$ModuleName already found in $tempModulePath"
    # continue
    # }

    # New-Item -ItemType Directory -Path $tempModulePath > $null

    # #Fetch and import the module
    # [uri]$baseURI = 'https://powershellgallery.com/api/v2/package/'
    # if ($Package) {
    # [uri]$baseURI = 'https://www.nuget.org/api/v2/package/'
    # }

    # [uri]$moduleURI = [uri]::new($baseURI, "$ModuleName/")

    # if ($Version) {
    # #Ugly syntax for what is effectively "Join-Path" for URIs
    # $moduleURI = [uri]::new($moduleURI,"$version/")
    # }

    # Write-Verbose "Fetching $ModuleName from $moduleURI"
    # (New-Object Net.WebClient).DownloadFile($moduleURI, $tempfile)
    # if ($PSEdition -eq 'Core') {
    # #Newer overwrite extraction method
    # [System.io.Compression.ZipFile]::ExtractToDirectory(
    # [String]$tempfile, #sourceArchiveFileName
    # [String]$tempModulePath, #destinationDirectoryName
    # [bool]$true #overwriteFiles
    # )
    # } else {
    # #Legacy behavior
    # Remove-Item $tempModulePath/* -Recurse -Force
    # [ZipFile]::ExtractToDirectory($tempfile, $tempModulePath)
    # }

    # if (-not (Test-Path $ModuleManifestPath)) {throw "Installation of $ModuleName failed"}

    # Import-Module $ModuleManifestPath -Force -Scope Global 4>&1 | Where-Object {$_ -match '^Loading Module.+psd1.+\.$'} | Write-Verbose

    # }} #Process foreach
}
<#
.SYNOPSIS
Initializes the build environment and detects various aspects of the environment
#>


function Initialize-PowerCD {
    [CmdletBinding()]
    param (
        #Specify this if you don't want initialization to switch to the folder build root
        [Switch]$SkipSetBuildRoot
    )
    Write-Host -fore cyan "Task PowerCD.Initialize"
    $bootstrapTimer = [Diagnostics.Stopwatch]::StartNew()

    #Fix a module import bug if powershell was started from pwsh. This is now fixed in PWSH7
    # if ($PSEdition -eq 'Desktop') {
    # Reset-WinPSModules
    # }


    #PS5.1: Load a fairly new version of newtonsoft.json to maintain compatibility with other tools, if not present
    if ($PSEdition -eq 'Desktop') {
        [bool]$newtonsoftJsonLoaded = try {
            [bool]([newtonsoft.json.jsonconvert].assembly)
        } catch {
            $false
        }
        if (-not $NewtonsoftJsonLoaded) {
            #TODO: Remove this when PSGetv3 properly supports Powershell 5.1 - https://github.com/PowerShell/PowerShellGet/issues/122
            Write-Verbose "PowerCD: Newtonsoft.Json not loaded, bootstrapping for Windows Powershell and PSGetV3"

            $jsonAssemblyPath = Join-Path (Split-Path (Get-Module powercd).path) 'lib/Newtonsoft.Json.dll'
            if ($PowerCDMetaBuild) {
                $jsonAssemblyPath = Join-Path (Split-Path $PowerCDMetaBuild) 'lib/Newtonsoft.Json.dll'
                Write-Verbose "PowerCD: Meta Build Detected, Moving Newtonsoft.Json to Temporary Location"
                #Move the DLL to the localappdata folder to prevent an issue with zipping up the completed build
                $tempJsonAssemblyPath = Join-Path ([Environment]::GetFolderpath('LocalApplicationData')) 'PowerCD/Newtonsoft.Json.dll'
                New-Item -ItemType Directory -Force (Split-Path $tempJsonAssemblyPath) > $null
                Copy-Item $jsonAssemblyPath $tempJsonAssemblyPath -force > $null
                $jsonAssemblyPath = $tempJsonAssemblyPath
            }
            Add-Type -Path $jsonAssemblyPath

            #Add a binding redirect to force any additional newtonsoft loads to this version
            # [Appdomain]::CurrentDomain.Add_AssemblyResolve({
            # param($sender,$assembly)
            # $assemblyName = $assembly.name
            # if ($assemblyName -match 'Newtonsoft') {
            # return [newtonsoft.json.jsonconvert].assembly
            # } else {
            # return [System.AppDomain]::CurrentDomain.GetAssemblies() | where fullname -match $assemblyName
            # }
            # })

        }
    }

    #Make sure that PSGet Beta is available
    BootstrapPSGetBeta

    #Import Prerequisites
    #To specify prerelease, you must use requiredversion and the prefix added to the modulename with '__'
    Import-PowerCDRequirement -ModuleInfo @(
        'Pester'
        'BuildHelpers'
        'PSScriptAnalyzer'
        'Configuration'
        #FIXME: Powerconfig doesn't work on Windows Powershell due to assembly differences
        #@{ModuleName='PowerConfig__beta0010';RequiredVersion='0.1.1'}
    )

    #Start a new PowerConfig, using PowerCDSetting as a base
    $PCDDefaultSetting = Get-PowerCDSetting

    # FIXME: Powerconfig doesn't work on Windows Powershell due to assembly differences
    # $PCDConfig = New-PowerConfig | Add-PowerConfigObject -Object $PCDDefaultSetting
    # $null = $PCDConfig | Add-PowerConfigYamlSource -Path (Join-Path $PCDDefaultSetting.BuildEnvironment.ProjectPath 'PSModule.build.settings.yml')
    # $null = $PCDConfig | Add-PowerConfigEnvironmentVariableSource -Prefix 'POWERCD_'

    #. $PSScriptRoot\Get-PowerCDSetting.ps1
    Set-Variable -Name PCDSetting -Scope Global -Option ReadOnly -Force -Value $PCDDefaultSetting

    #Test if dotnet is installed
    try {
        [Version]$dotnetVersion = (dotnet --info | where {$_ -match 'Version:'} | select -first 1).trim() -split (' +') | select -last 1
            } catch {
        throw 'PowerCD requires dotnet 3.0 or greater to be installed. Hint: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script'
    }
    if ($dotnetVersion -lt '3.0.0') {throw "PowerCD detected dotnet $dotnetVersion but 3.0 or greater is required. Hint: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script'"}

    # $defaultToolsFilePath = (Join-Path $pcdsetting.general.projectroot '.config/dotnet-tools.json')
    # if (-not (Test-Path $defaultToolsFilePath)) {
    # $manifestPath = Get-ChildItem -Recurse -Path (Split-Path (Get-Module -Name 'PowerCD').path) -Include 'dotnet-tools.json'
    # }
    # if ($manifestPath) {
    # $manifestPath = '--tool-manifest',$manifestPath
    # }
    # [String]$restoreResult = dotnet tool restore $manifestPath *>&1
    # if ($restoreResult -notmatch 'Restore was successful') {throw "Dotnet Tool Restore Failed: $restoreResult"}

    #Detect if we are in a continuous integration environment (Appveyor, etc.) or otherwise running noninteractively
    if ($ENV:CI -or $CI -or ($PCDSetting.BuildEnvironment.buildsystem -and $PCDSetting.BuildEnvironment.buildsystem -ne 'Unknown')) {
        #write-build Green 'Build Initialization - Detected a Noninteractive or CI environment, disabling prompt confirmations'
        $SCRIPT:CI = $true
        $ConfirmPreference = 'None'
        #Disabling Progress speeds up the build because Write-Progress can be slow and can also clutter some CI displays
        $ProgressPreference = 'SilentlyContinue'
    }

    Write-Host -fore cyan "Done PowerCD.Initialize $([string]$bootstrapTimer.elapsed)"
}
function Invoke-PowerCDClean {
    [CmdletBinding()]
    param (
        $buildProjectPath = $PCDSetting.BuildEnvironment.ProjectPath,
        $buildOutputPath  = $PCDSetting.BuildEnvironment.BuildOutput,
        $buildProjectName = $PCDSetting.BuildEnvironment.ProjectName,
        [Switch]$Prerequisites
    )

    #Taken from Invoke-Build because it does not preserve the command in the scope this function normally runs
    #Copyright (c) Roman Kuzmin
    function Remove-BuildItem([Parameter(Mandatory=1)][string[]]$Path) {
        if ($Path -match '^[.*/\\]*$') {*Die 'Not allowed paths.' 5}
        $v = $PSBoundParameters['Verbose']
        try {
            foreach($_ in $Path) {
                if (Get-Item $_ -Force -ErrorAction 0) {
                    if ($v) {Write-Verbose "remove: removing $_" -Verbose}
                    Remove-Item $_ -Force -Recurse -ErrorAction stop
                }
                elseif ($v) {Write-Verbose "remove: skipping $_" -Verbose}
            }
        }
        catch {
            throw $_
        }
    }

    #Reset the BuildOutput Directory
    if (test-path $buildOutputPath) {
        Write-Verbose "Removing and resetting Build Output Path: $buildOutputPath"
        Remove-BuildItem $buildOutputPath -Verbose:$false
    }

    if ($Prerequisites) {
        $PrerequisitePath = (Join-Path ([Environment]::GetFolderpath('LocalApplicationData')) 'PowerCD')
        Write-Verbose "Removing and resetting PowerCD Prerequisites: $PrerequisitePath"
        Remove-BuildItem $buildOutputPath -Verbose:$false
    }

    New-Item -Type Directory $BuildOutputPath > $null

    #Unmount any modules named the same as our module
    Remove-Module $buildProjectName -Verbose:$false -erroraction silentlycontinue
}
function New-PowerCDNugetPackage {
    [CmdletBinding()]
    param (
        #Path to the module to build
        [Parameter(Mandatory)][IO.FileInfo]$Path,
        #Where to output the new module package. Specify a folder
        [Parameter(Mandatory)][IO.DirectoryInfo]$Destination
    )

    $ModuleManifest = Get-Item $Path/*.psd1 | Where-Object { (Get-Content -Raw $PSItem) -match "ModuleVersion ?= ?\'.+\'" } | Select-Object -First 1
    if (-not $ModuleManifest) { throw "No module manifest found in $Path. Please ensure a powershell module is present in this directory." }
    $ModuleName = $ModuleManifest.basename

    #TODO: Get this to work with older packagemanagement
    # $ModuleMetadata = Import-PowerShellDataFile $ModuleManifest

    # #Use some PowershellGet private methods to create a nuspec file and create a nupkg. This is much faster than the "slow" method referenced below
    # $NewNuSpecFileParams = @{
    # OutputPath = $Path
    # Id = $ModuleName
    # Version = ($ModuleMetaData.ModuleVersion,$ModuleMetaData.PrivateData.PSData.Prerelease -join '-')
    # Description = $ModuleMetaData.Description
    # Authors = $ModuleMetaData.Author
    # }

    # #Fast Method but skips some metadata. Doesn't matter for non-powershell gallery publishes
    # #TODO: Add all the metadata from the publish process
    # $NuSpecPath = & (Get-Module PowershellGet) New-NuSpecFile @NewNuSpecFileParams
    # #$DotNetCommandPath = & (Get-Module PowershellGet) {$DotnetCommandPath}
    # #$NugetExePath = & (Get-Module PowershellGet) {$NugetExePath}
    # $NugetExePath = (command nuget -All -erroraction stop | where name -match 'nuget(.exe)?$').Source
    # $NewNugetPackageParams = @{
    # NuSpecPath = $NuSpecPath
    # NuGetPackageRoot = $Destination
    # }

    # if ($DotNetCommandPath) {
    # $NewNugetPackageParams.UseDotNetCli = $true
    # } elseif ($NugetExePath) {
    # $NewNugetPackageParams.NugetExePath = $NuGetExePath
    # }else {
    # throw "Neither nuget or dotnet was detected by PowershellGet. Please check you have one or the other installed."
    # }

    # $nuGetPackagePath = & (Get-Module PowershellGet) New-NugetPackage @NewNugetPackageParams
    # write-verbose "Created NuGet Package at $nuGetPackagePath"
    # #Slow Method, maybe fallback to this
    # #Creates a temporary repository and registers it, uses publish-module which results in a nuget package

    try {
        $SCRIPT:tempRepositoryName = "$ModuleName-build-$(Get-Date -format 'yyyyMMdd-hhmmss')"
        Unregister-PSRepository -Name $tempRepositoryName -ErrorAction SilentlyContinue -Verbose:$false *>$null
        Register-PSRepository -Name $tempRepositoryName -SourceLocation ([String]$Destination) -Verbose:$false *>$null
        If (Get-Item -ErrorAction SilentlyContinue (Join-Path $Path "$ModuleName*.nupkg")) {
            Write-Debug Green "Nuget Package for $ModuleName already generated. Skipping. Delete the package to retry"
        } else {
            $CurrentProgressPreference = $GLOBAL:ProgressPreference
            $GLOBAL:ProgressPreference = 'SilentlyContinue'
            Publish-Module -Repository $tempRepositoryName -Path $Path -Force -Verbose:$false
            $GLOBAL:ProgressPreference = $CurrentProgressPreference
        }
    } catch { Write-Error $PSItem }
    finally {
        Unregister-PSRepository $tempRepositoryName *>$null
    }
}

<#
.SYNOPSIS
Removes Comments and whitespace not related to comment-based help to "minify" a powershell script
.NOTES
Original Script From: https://www.madwithpowershell.com/2017/09/remove-comments-and-whitespace-from.html
#>

function Remove-CommentsAndWhiteSpace {
    # We are not restricting scriptblock type as Tokenize() can take several types
    Param (
        [parameter( ValueFromPipeline = $True )]
        $Scriptblock
    )

    Begin {
        # Intialize collection
        $Items = @()
    }

    Process {
        # Collect all of the inputs together
        $Items += $Scriptblock
    }

    End {
        ## Process the script as a single unit

        # Convert input to a single string if needed
        $OldScript = $Items -join [environment]::NewLine

        # If no work to do
        # We're done
        If ( -not $OldScript.Trim( " `n`r`t" ) ) { return }

        # Use the PowerShell tokenizer to break the script into identified tokens
        $Tokens = [System.Management.Automation.PSParser]::Tokenize( $OldScript, [ref]$Null )

        # Define useful, allowed comments
        $AllowedComments = @(
            'requires'
            '.SYNOPSIS'
            '.DESCRIPTION'
            '.PARAMETER'
            '.EXAMPLE'
            '.INPUTS'
            '.OUTPUTS'
            '.NOTES'
            '.LINK'
            '.COMPONENT'
            '.ROLE'
            '.FUNCTIONALITY'
            '.FORWARDHELPCATEGORY'
            '.REMOTEHELPRUNSPACE'
            '.EXTERNALHELP' )

        # Strip out the Comments, but not useful comments
        # (Bug: This will break comment-based help that uses leading # instead of multiline <#,
        # because only the headings will be left behind.)

        $Tokens = $Tokens.ForEach{
            If ( $_.Type -ne 'Comment' ) {
                $_
            } Else {
                $CommentText = $_.Content.Substring( $_.Content.IndexOf( '#' ) + 1 )
                $FirstInnerToken = [System.Management.Automation.PSParser]::Tokenize( $CommentText, [ref]$Null ) |
                    Where-Object { $_.Type -ne 'NewLine' } |
                    Select-Object -First 1
                If ( $FirstInnerToken.Content -in $AllowedComments ) {
                    $_
                }
            } }

        # Initialize script string
        $NewScriptText = ''
        $SkipNext = $False

        # If there are at least 2 tokens to process...
        If ( $Tokens.Count -gt 1 ) {
            # For each token (except the last one)...
            ForEach ( $i in ( 0..($Tokens.Count - 2) ) ) {
                # If token is not a line continuation and not a repeated new line or semicolon...
                If (    -not $SkipNext -and
                    $Tokens[$i  ].Type -ne 'LineContinuation' -and (
                        $Tokens[$i  ].Type -notin ( 'NewLine', 'StatementSeparator' ) -or
                        $Tokens[$i + 1].Type -notin ( 'NewLine', 'StatementSeparator', 'GroupEnd' ) ) ) {
                    # Add Token to new script
                    # For string and variable, reference old script to include $ and quotes
                    If ( $Tokens[$i].Type -in ( 'String', 'Variable' ) ) {
                        $NewScriptText += $OldScript.Substring( $Tokens[$i].Start, $Tokens[$i].Length )
                    } Else {
                        $NewScriptText += $Tokens[$i].Content
                    }

                    # If the token does not never require a trailing space
                    # And the next token does not never require a leading space
                    # And this token and the next are on the same line
                    # And this token and the next had white space between them in the original...
                    If (    $Tokens[$i  ].Type -notin ( 'NewLine', 'GroupStart', 'StatementSeparator' ) -and
                        $Tokens[$i + 1].Type -notin ( 'NewLine', 'GroupEnd', 'StatementSeparator' ) -and
                        $Tokens[$i].EndLine -eq $Tokens[$i + 1].StartLine -and
                        $Tokens[$i + 1].StartColumn - $Tokens[$i].EndColumn -gt 0 ) {
                        # Add a space to new script
                        $NewScriptText += ' '
                    }

                    # If the next token is a new line or semicolon following
                    # an open parenthesis or curly brace, skip it
                    $SkipNext = $Tokens[$i].Type -eq 'GroupStart' -and $Tokens[$i + 1].Type -in ( 'NewLine', 'StatementSeparator' )
                }

                # Else (Token is a line continuation or a repeated new line or semicolon)...
                Else {
                    # [Do not include it in the new script]

                    # If the next token is a new line or semicolon following
                    # an open parenthesis or curly brace, skip it
                    $SkipNext = $SkipNext -and $Tokens[$i + 1].Type -in ( 'NewLine', 'StatementSeparator' )
                }
            }
        }

        # If there is a last token to process...
        If ( $Tokens ) {
            # Add last token to new script
            # For string and variable, reference old script to include $ and quotes
            If ( $Tokens[$i].Type -in ( 'String', 'Variable' ) ) {
                $NewScriptText += $OldScript.Substring( $Tokens[-1].Start, $Tokens[-1].Length )
            } Else {
                $NewScriptText += $Tokens[-1].Content
            }
        }

        # Trim any leading new lines from the new script
        $NewScriptText = $NewScriptText.TrimStart( "`n`r;" )

        # Return the new script as the same type as the input
        If ( $Items.Count -eq 1 ) {
            If ( $Items[0] -is [scriptblock] ) {
                # Return single scriptblock
                return [scriptblock]::Create( $NewScriptText )
            } Else {
                # Return single string
                return $NewScriptText
            }
        } Else {
            # Return array of strings
            return $NewScriptText.Split( "`n`r", [System.StringSplitOptions]::RemoveEmptyEntries )
        }
    }
}
function Reset-WinPSModules {
    if ($PSEdition -eq 'Desktop' -and $env:PSModulePath -match '\\Powershell\\') {
        Get-Module | Where-Object CompatiblePSEditions -eq 'Core' | Where-Object compatiblepseditions -notcontains 'desktop' | Foreach-Object {
            $moduleToImport = Get-Module $PSItem.Name -ListAvailable | Where-Object CompatiblePSEditions -match 'Desktop' | Sort-Object Version -Descending | Select-Object -First 1
            if ($moduleToImport) {
                write-verbose "Reloading $($PSItem.Name) with Windows Powershell-compatible version $($moduleToImport.version)"
                Remove-Module $PSItem -Verbose:$false
                Import-Module $moduleToImport -Scope Global -Force -WarningAction SilentlyContinue -Verbose:$false 4>$null
            } else {
                throw "A core-only version of the $($PSItem.Name) module was detected as loaded and no Windows Powershell Desktop-compatible equivalent was found in the PSModulePath. Please copy a Desktop-Compatible version of the module to your PSModulePath."
            }
        }
    }
}
function Resolve-PowerCDModuleManifest {

    Get-PSModuleManifest -WarningVariable GetPSModuleManifestWarning -WarningAction SilentlyContinue
}
<#
.SYNOPSIS
Sets the version on a powershell Module
#>

function Set-PowerCDVersion {
    [CmdletBinding()]
    param (
        #Path to the module manifest to update
        [String]$Path = $PCDSetting.OutputModuleManifest,
        #Version to set for the module
        [Version]$Version = $PCDSetting.Version,
        #Prerelease tag to add to the module, if any
        [String]$PreRelease= $PCDSetting.Prerelease
    )
    #Default is to update version so no propertyname specified
    Configuration\Update-Metadata -Path $Path -Value $Version

    Configuration\Update-Metadata -Path $Path -PropertyName PreRelease -Value $PreRelease
}
function Test-PowerCDPester {
    [CmdletBinding(DefaultParameterSetName='Default')]
    param (
        #Path where the Pester tests are located
        [Parameter(ParameterSetName='Default')][String]$Path = [String]$pwd,
        #Path where the coverage files should be output. Defaults to the build output path.
        [Parameter(ParameterSetName='Default')][String]$OutputPath = [String]$pwd,
        #A PesterConfiguration to use instead of the intelligent defaults. For advanced usage only.
        [Parameter(ParameterSetName='Configuration')][PesterConfiguration]$Configuration
    )
    if (-not $Configuration) {
        [PesterConfiguration]$Configuration = [PesterConfiguration]::Default
        #If we are in vscode, add the VSCodeMarkers
        if ($host.name -match 'Visual Studio Code') {
            Write-Verbose "Detected Visual Studio Code, adding Pester test markers"
            $Configuration.Debug.WriteVSCodeMarker = $true
            $Configuration.Debug.ShowNavigationMarkers = $true
        }
        #Temporary workaround for -CI not saving to variable
        #TODO: Remove when https://github.com/pester/Pester/issues/1527 is closed
        $Configuration.Output.Verbosity = 'Normal'
        $Configuration.Run.PassThru = $true
        $Configuration.Run.Path = $Path
        $Configuration.CodeCoverage.Enabled = $true
        $Configuration.CodeCoverage.OutputPath = "$OutputPath/CodeCoverage.xml"
        $Configuration.TestResult.Enabled = $true
        $Configuration.TestResult.OutputPath = "$OutputPath/TEST-Results.xml"
        #Exclude the output folder in case we copied any tests there to avoid duplicate testing. This should generally only matter for "meta" like PowerCD
        #FIXME: Specify just the directory instead of a path search when https://github.com/pester/Pester/issues/1575 is fixed
        $Configuration.Run.ExcludePath = [String[]](Get-ChildItem -Recurse $OutputPath -Include '*.Tests.ps1')

        $GLOBAL:TestResults = Invoke-Pester -Configuration $Configuration
    }

    if ($TestResults.failedcount -isnot [int] -or $TestResults.FailedCount -gt 0) {
        $testFailedMessage = "Failed '$($TestResults.FailedCount)' tests, build failed"
        throw $testFailedMessage
        #TODO: Rewrite to use BuildHelpers
        # if ($isAzureDevOps) {
        # Write-Host "##vso[task.logissue type=error;]$testFailedMessage"
        # }
        $SCRIPT:SkipPublish = $true
    }

    return

    # #Pester Configuration Setup
    # if (-not $Configuration) {
    # $Configuration = [PesterConfiguration]::Default
    # }
    # $Configuration.OutputPath = $PesterResultFile

    # $Configuration.PesterResultFile = $Configuration
    # $Configuration.PesterResultFile = $Configuration
    # Invoke-Pester -Configuration $Configuration

    # [String[]]$Exclude = 'PowerCD.tasks.ps1',
    # $CodeCoverage = (Get-ChildItem -Path (Join-Path $ModuleDirectory '*') -Include *.ps1,*.psm1 -Exclude $Exclude -Recurse),
    # $Show = 'None',
    # [Switch]$UseJob
    #Try autodetecting the "furthest out module manifest"
    # if (-not $ModuleManifestPath) {
    # try {
    # $moduleManifestCandidatePath = Join-Path (Join-Path $PWD '*') '*.psd1'
    # $moduleManifestCandidates = Get-Item $moduleManifestCandidatePath -ErrorAction stop
    # $moduleManifestPath = ($moduleManifestCandidates | Select-Object -last 1).fullname
    # } catch {
    # throw "Did not detect any module manifests in $BuildProjectPath. Did you run 'Invoke-Build Build' first?"
    # }
    # }

    #TODO: Update for new logging method
    #write-verboseheader "Starting Pester Tests..."

    $PesterParams = @{
        #TODO: Fix for source vs built object
        # Script = @{
        # Path = "Tests"
        # Parameters = @{
        # ModulePath = (Split-Path $moduleManifestPath)
        # }
        # }
        OutputFile   = $PesterResultFile
        OutputFormat = 'NunitXML'
        PassThru     = $true
        OutVariable  = 'TestResults'
        Show         = $Show
    }

    if ($CodeCoverage) {
        $PesterParams.CodeCoverage = $CodeCoverage
        $PesterParams.CodeCoverageOutputFile = $CodeCoverageOutputFile
    }



    if ($UseJob) {
        #Bootstrap PowerCD Prereqs
        $PowerCDModules = get-item (Join-Path ([io.path]::GetTempPath()) '/PowerCD/*/*/*.psd1')

        $PesterJob = {
            #Move to same folder as was started
            Set-Location $USING:PWD
            #Prepare the Destination Module Directory Environment
            $ENV:PowerCDModuleManifest = $USING:ModuleManifestPath
            #Bring in relevant environment
            $USING:PowerCDModules | Import-Module -Verbose:$false | Where-Object {$_ -match '^Loading Module.+psd1.+\.$'}
            $PesterParams = $USING:PesterParams
            Invoke-Pester @PesterParams
        }

        $TestResults = Start-Job -ScriptBlock $PesterJob | Receive-Job -Wait
    } else {
        $ENV:PowerCDModuleManifest = $ModuleManifestPath
        $TestResults = Invoke-Pester @PesterParams
    }

    # In Appveyor? Upload our test results!
    #TODO: Consolidate Test Result Upload
    # If ($ENV:APPVEYOR) {
    # $UploadURL = "https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)"
    # write-verbose "Detected we are running in AppVeyor! Uploading Pester Results to $UploadURL"
    # (New-Object 'System.Net.WebClient').UploadFile(
    # "https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)",
    # $PesterResultFile )
    # }

    #TODO: Fix to fail
    # Failed tests?
    # Need to error out or it will proceed to the deployment. Danger!
    if ($TestResults.failedcount -isnot [int] -or $TestResults.FailedCount -gt 0) {
        $testFailedMessage = "Failed '$($TestResults.FailedCount)' tests, build failed"
        throw $testFailedMessage
        #TODO: Rewrite to use BuildHelpers
        # if ($isAzureDevOps) {
        # Write-Host "##vso[task.logissue type=error;]$testFailedMessage"
        # }
        $SCRIPT:SkipPublish = $true
    }
    # "`n"
}
<#
.SYNOPSIS
This function sets a module manifest for the various function exports that are present in a module such as private/public functions, classes, etc.
#>


function Update-PowerCDPublicFunctions {
    param(
        #Path to the module manifest to update
        [String]$Path = $PCDSetting.OutputModuleManifest,
        #Specify to override the auto-detected function list
        [String[]]$Functions = $PCDSetting.Functions,
        #Paths to the module public function files
        [String]$PublicFunctionPath = (Join-Path $PCDSetting.BuildEnvironment.ModulePath 'Public')
    )

    if (-not $Functions) {
        write-verbose "Autodetecting Public Functions in $Path"
        $Functions = Get-PowerCDPublicFunctions $PublicFunctionPath
    }

    if (-not $Functions) {
        write-warning "No functions found in the powershell module. Did you define any yet? Create a new one called something like New-MyFunction.ps1 in the Public folder"
        return
    }

    Configuration\Update-Metadata -Path $Path -PropertyName FunctionsToExport -Value $Functions
}

#Module Startup
#PowerCD.Tasks may be in different folders, hence why we do the search here
Set-Alias PowerCD.Tasks ([String](Get-ChildItem -recurse $PSScriptRoot -include PowerCD.tasks.ps1)[0])

if (-not $PublicFunctions) {
    $ModuleManifest = Join-Path $PSScriptRoot 'PowerCD.psd1'
    # $PublicFunctions = if (Get-Command Import-PowershellDataFile -ErrorAction Silently Continue) {
    # Import-PowershellDataFile -Path $ModuleManifest
    # } else {

    # #Some Powershell Installs don't have microsoft.powershell.utility for some reason.
    # #TODO: Bootstrap microsoft.powershell.utility maybe?
    # #Last Resort
    # #Import-LocalizedData -BaseDirectory $PSScriptRoot -FileName 'powercd.psd1'
    # }
    $PublicFunctions = Import-PowershellDataFile -Path $ModuleManifest
    Export-ModuleMember -Alias PowerCD.Tasks -Function $publicFunctions.FunctionsToExport
}

Export-ModuleMember -Alias PowerCD.Tasks -Function $publicFunctions