RVR.AZPipelineTools.psm1

function New-RVRModuleManifest {
    <#
       .SYNOPSIS
       This function creates a new PowerShell module manifest.
     
       .DESCRIPTION
       This function creates a new PowerShell module manifest.
 
       It's merely a wrapper around new-ModuleManifest with some parameters preset.
     
       .PARAMETER ModuleName
       The name of the module.
 
       .PARAMETER ModuleVersion
       The module version.
 
       .PARAMETER Description
       The description of the module.
 
       .PARAMETER Author
       The author of the module
 
       .PARAMETER ReleaseNotes
       The releaseNotes
 
       .PARAMETER FunctionsToExport
       The functions that this module will export. Please do not use wildcards.
 
       .PARAMETER DscResourcesToExport
       The DSC Resources that this module will export. Please do not use wildcards.
     
       .EXAMPLE
       The example below will create a new manifest for this module.
 
       PS> New-RVRModuleManifest -Verbose -ModuleName $modulename -ModuleVersion '0.0.1' -Description 'Sample Description' -FunctionsToExport 'New-RVRModuleManifest', 'New-RVRModulePesterTestScript'
     
    #>


    [CmdletBinding(SupportsShouldProcess)]
    
    param(
        [parameter(Mandatory)]
        [string]$ModuleName,
        
        [parameter(Mandatory)]
        $ModuleVersion,
        
        [parameter(Mandatory)]
        [string]$Description,

        $Author = "Robert van Reems",

        $ReleaseNotes = "Some things changed",

        $FunctionsToExport, 

        $DscResourcesToExport
    )

    Begin{
        # Display some troubleshoot info
        Write-Verbose "My Invocation info:
         
        MyCommand: $($MyInvocation.MyCommand)
        ScriptName: $($MyInvocation.ScriptName)
        PSScriptRoot: $($MyInvocation.PSScriptRoot)
        PSCommandPath: $($MyInvocation.PSCommandPath)
        InvocationName: $($MyInvocation.InvocationName)
        "


        # Some vars
        $ModulePath = ".\$ModuleName"

        # Apend disclaimer to the description
        $Description += "This module contains Roberts custom PS Scriptanalyzer rules.
 
        **********************************************************************************************************************
        DISCLAIMER
 
        THIS CODE AND INFORMATION ARE PROVIDED 'AS IS' WITHOUT WARRANTY OF ANY
        KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
        PARTICULAR PURPOSE
        ************************************************************************************************************************* "

    }

    Process{
        #Generate the module manifest
        Write-Verbose 'Generating the module manifest'


        If ($PSCmdlet.ShouldProcess("$ModuleName")) { 
            New-ModuleManifest -Path ($ModulePath + "\" + "$ModuleName" + ".psd1") -ModuleVersion $ModuleVersion `
                -PowerShellVersion 5.0 `
                -Description $Description `
                -Author $author `
                -ReleaseNotes $releaseNotes `
                -RootModule "$($ModuleName + '.psm1')" `
                -DscResourcesToExport '' `
                -FunctionsToExport $FunctionsToExport `
                -CmdletsToExport "" `
                -AliasesToExport ""
        }
    }

    End{
        Write-Verbose "$($MyInvocation.MyCommand) ends normally"
    }
}

function New-RVRModulePesterTestScript{
    <#
       .SYNOPSIS
       Installs severall Pester test scripts to your Azure build pipeline.
     
       .DESCRIPTION
       This function contains some precanned Pester test scripts that you might use in Azure build pipeline.
       At the moment of writing it contains:
       - PS Script analyzer tests
       - Module validation tests
 
       I Expect to add more tests in time.
 
       The function copies the tests to folder .\PesterTestScripts. You may use this folder for your own custom tests
      
       .EXAMPLE
       Just run the command
 
       PS> New-RVRModulePesterTestScript
    #>

    
    [CmdletBinding(SupportsShouldProcess)]
    
    param(
        
    )

    Begin{
        # Display some troubleshoot info
        Write-Verbose "My Invocation info:
         
        MyCommand: $($MyInvocation.MyCommand)
        ScriptName: $($MyInvocation.ScriptName)
        PSScriptRoot: $($MyInvocation.PSScriptRoot)
        PSCommandPath: $($MyInvocation.PSCommandPath)
        InvocationName: $($MyInvocation.InvocationName)
        "


        #Some vars
        $PesterTestScriptFolderPath = ".\PesterTestScripts"
        $PesterTestScriptSourcesFolderPath = "$PSScriptRoot\PesterTestScriptSources\"

        try{
            # Create the PesterTestScriptFolder if it doesn't allready exists
            if(-not (Test-Path $PesterTestScriptFolderPath) ){
                Write-Verbose "Creating folder $PesterTestScriptFolderPath"

                If ($PSCmdlet.ShouldProcess("Create new folder $PesterTestScriptFolderPath")) { 
                    New-Item -Path $PesterTestScriptFolderPath -ItemType Directory | Out-Null
                }
            }
        }
        catch{
            throw "Unexpected fatal error: $_"
            exit
        }
    }

    Process{
        Write-Verbose 'Coping Pester Test scripts'

        try{
            Write-Verbose "Copying contents of $PesterTestScriptSourcesFolderPath to $PesterTestScriptFolderPath"
            If ($PSCmdlet.ShouldProcess("Copying contents of $PesterTestScriptSourcesFolderPath to $PesterTestScriptFolderPath")) { 
                Copy-Item -Path "$PesterTestScriptSourcesFolderPath\*.ps1" -Destination $PesterTestScriptFolderPath 
            }
        }
        catch{
            throw "Failed to Copy items from $PesterTestScriptSourcesFolderPath to $PesterTestScriptFolderPath. With error: $_"
            exit
        }
    }

    End{
        Write-Verbose "$($MyInvocation.MyCommand) ends normally"
    }
}

function New-RVRNugetSpec {

    <#
       .SYNOPSIS
       This function creates a new nuget Specfile.
     
       .DESCRIPTION
       This function creates a new nuget Specfile. This is used to package this module to a .nuget package.
     
       .PARAMETER ModuleName
       The name of the module.
 
       .PARAMETER ModuleVersion
       The module version.
 
       .PARAMETER Description
       The description of the module.
 
       .PARAMETER Author
       The author of the module
 
       .PARAMETER ReleaseNotes
       The releaseNotes
     
       .EXAMPLE
       The example below will create a new nuget specfile for this module.
 
       PS> New-RVRNugetSpec -ModuleName $ModuleName -ModuleVersion 0.0.1 -Description 'sample text' -Author 'Robert van Reems' -ReleaseNotes 'first release'
     
    #>


    [CmdletBinding(SupportsShouldProcess)]

    param(
        [parameter(Mandatory)]
        [string]$ModuleName,
        
        [parameter(Mandatory)]
        $ModuleVersion,
        
        [parameter(Mandatory)]
        [string]$Description,

        $Author = "Robert van Reems",

        $ReleaseNotes = "Some things changed"
    )

    If ($PSCmdlet.ShouldProcess("$ModuleName")) { 

        # remove old specfile
        if (Test-Path ( ".\" + $ModuleName + ".nuspec" ) ) {
            Remove-Item (".\" + $ModuleName + ".nuspec" )
        }

        $NugetSpecContent = "<?xml version=`"1.0`"?>
        <package >
        <metadata>
            <id>$ModuleName</id>
            <version>$ModuleVersion</version>
            <authors>$author</authors>
            <owners>$author</owners>
            <requireLicenseAcceptance>false</requireLicenseAcceptance>
            <description>$Description</description>
            <releaseNotes>$releaseNotes</releaseNotes>
            <copyright>Copyright 2019</copyright>
            <tags>Tag1 Tag2</tags>
            <dependencies>
            </dependencies>
        </metadata>
        </package>
        "


        # Create the nuspec file
        New-Item -Path (".\" + $ModuleName + ".nuspec" )

        Add-Content -Path (".\" + $ModuleName + ".nuspec" ) $NugetSpecContent
        #endregion
    }
}