functions/template-helpers/New-ContainerBuildScript.ps1

# <copyright file="New-ContainerBuildScript.ps1" company="Endjin Limited">
# Copyright (c) Endjin Limited. All rights reserved.
# </copyright>

<#
.SYNOPSIS
    Generates a build script.
.DESCRIPTION
    Generates a build script, from a template, that uses InvokeBuild and functionality available in this module.
.EXAMPLE
    PS C:\> New-ContainerBuildScript -DockerfilePath ./Dockerfile -ImageName myimg -ContainerRegistryType acr -Path ./build.ps1
    Generates a build script in the current directory configured for building & publishing Bicep modules.
.PARAMETER DockerfilePath
    The path to the Dockerfile to be built
.PARAMETER ImageName,
    The default image name used when building the container image
.PARAMETER ContainerRegistryType
    The type of container registry where the image will be published (acr, ghcr, docker or <blank>)
.PARAMETER Path
    The destination path for the generated build script.
.PARAMETER Force
    When true, any existing file.
#>

function New-ContainerBuildScript
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $DockerfilePath,

        [Parameter(Mandatory=$true)]
        [string] $ImageName,

        [Parameter(Mandatory=$true)]
        [ValidateSet(
            "",
            "acr",
            "docker",
            "ghcr"
        )]
        [AllowEmptyString()]
        [string] $ContainerRegistryType,

        [Parameter()]
        [string] $OutputPath = "build.ps1",

        [Parameter()]
        [switch] $Force
    )

    $templatePath = "$PSScriptRoot/../../templates/containerbuild.tmpl.ps1"

    # If we're using the default value of OutputPath (i.e. we've not specified it when calling this function),
    # then it won't be part of the PSBoundParameters collection, so we need to add it before calling the
    # underlying template helper
    if ("OutputPath" -notin $PSBoundParameters.Keys) {
        $PSBoundParameters.Add("OutputPath", $OutputPath)
    }

    _executeTemplate -TemplatePath $templatePath @PSBoundParameters
}