internal/functions/Initialize-PuppetModule.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
55
56
57
58
59
function Initialize-PuppetModule {
  <#
    .SYNOPSIS
      Scafold a Puppet module
    .DESCRIPTION
      Scaffold a Puppet module and copy over static files.
    .PARAMETER OutputFolderPath
      The path, relative or literal, to the Puppet module's root folder.
    .PARAMETER PuppetModuleName
      The name of the Puppet module to create.
    .PARAMETER Confirm
      Prompts for confirmation before overwriting the file
    .PARAMETER WhatIf
      Shows what would happen if the function runs.
    .EXAMPLE
      Update-PuppetModuleFixture -PuppetModuleFolderPath ./import/powershellget
      This command will update `./import/powershellget/.fixtures.yml`, adding a
      key to the Forge Modules fixture for puppetlabs/pwshlib.
  #>

  [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
  param (
    [Parameter(Mandatory=$True)]
    [string]$OutputFolderPath,
    [Parameter(Mandatory=$True)]
    [string]$PuppetModuleName
  )

  begin {
    $Command = "pdk new module $PuppetModuleName --skip-interview --template-url https://github.com/puppetlabs/pdk-templates"
    $ModuleFolderPath = Join-Path -Path $OutputFolderPath -ChildPath $PuppetModuleName
    $TemplateFolder = Join-Path -Path (Split-Path -Path $MyInvocation.MyCommand.Module.Path -Parent) -ChildPath 'internal/templates/static'
  }

  process {
    Try {
      $ErrorActionPreference = 'Stop'
      # Handle folder scaffolding
      If (!(Test-Path $OutputFolderPath)) {
        $null = New-Item -Path $OutputFolderPath -ItemType Directory -Force
      }
      # Clean previously scaffolded folder
      If (Test-Path $ModuleFolderPath){
        If ($PSCmdlet.ShouldProcess($ModuleFolderPath, "Remove existing Puppet Module Folder")) {
          Remove-Item -Path $ModuleFolderPath -Force -Recurse
        }
      }
      Invoke-PdkCommand -Path $OutputFolderPath -Command $Command -SuccessFilterScript {
        $_ -match "Module '$PuppetModuleName' generated at path"
      }
      # Copy static template files
      Copy-Item -Path "$TemplateFolder/*" -Destination $ModuleFolderPath -Recurse -Force
    } Catch {
      $PSCmdlet.ThrowTerminatingError($PSItem)
    }
  }

  end {}
}