Functions/Private/GenerateDockerfile.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function GenerateDockerfile {

    <#
    .SYNOPSIS
    This function is responsible for generating a Dockerfile, based on a template.
 
    .PARAMETER ArtifactPath
    The filesystem path to the artifacts
 
    .PARAMETER ArtifactParam
    This is used for passing parameters to the resulting Generate functions.
    #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess",'')]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string] $ArtifactPath,
        
        [Parameter(Mandatory = $false)]
        [string[]] $Artifact,

        [Parameter(Mandatory = $false)]
        [string[]] $ArtifactParam

    )

    Write-Verbose -Message ('Generating Dockerfile based on discovered artifacts in :{0}' -f $Mount.Path)
    ### Verify that the Dockerfile template is available
    $DockerfileTemplate = '{0}\Resources\Dockerfile-template' -f $ModulePath

    if (!(Test-Path -Path $DockerfileTemplate)) {
        throw 'Couldn''t find the Dockerfile template. Please make sure this exists under: {0}' -f $DockerfileTemplate
    }

    ### Get the Dockerfile template
    $Dockerfile = Get-Content -Raw -Path $DockerfileTemplate
    $Dockerfile = $Dockerfile.Trim() + [System.Environment]::NewLine
    $Result = ''

    foreach ($item in $Artifact) {
        if (! $ArtifactParam) {
            $Result = & "Generate_$item" -ManifestPath $ArtifactPath 
        }
        else {
            $Result = & "Generate_$item" -ManifestPath $ArtifactPath -ArtifactParam $ArtifactParam            
        }
        $Dockerfile += '{0}{1}' -f [System.Environment]::NewLine, $Result.Trim()
    }

    $DockerfilePath = '{0}\Dockerfile' -f $ArtifactPath
    Set-Content -Path $DockerfilePath -Value $Dockerfile
}