-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.ps1
91 lines (78 loc) · 3.84 KB
/
build.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
[CmdletBinding()]
param(
$ModuleName = 'dsc_api'
)
$importDir = Join-Path $PSScriptRoot 'import'
$templateDir = Join-Path $PSScriptRoot 'templates'
$moduleDir = Join-Path $importDir $ModuleName
# import dsc resources from psgallery
$dscResourceSheet = Join-Path $PSScriptRoot 'import.csv'
$downloadedDscResources = Join-Path $importDir 'dsc_resources'
$downloadedDscResourcesTmp = "$($downloadedDscResources)_tmp"
if(-not(Test-Path $downloadedDscResources)){
if(-not(Test-Path $downloadedDscResources)){
mkdir $downloadedDscResources
}
if(-not(Test-Path $downloadedDscResourcesTmp)){
mkdir $downloadedDscResourcesTmp
}
$items = Import-Csv -Path $dscResourceSheet
$items | ForEach-Object {
Save-Module -Name $_.Name -Path $downloadedDscResourcesTmp -RequiredVersion $_.Version
Move-Item -Path "$($downloadedDscResourcesTmp)/$($_.Name)/$($_.Version)" -Destination "$($downloadedDscResources)/$($_.Name)"
}
}
# create new pdk module
if(-not(Test-Path $importDir)){
mkdir $importDir
}
if(Test-Path $moduleDir){
Remove-Item -Path $moduleDir -Force -Recurse
}
Push-Location $importDir
pdk new module --skip-interview --template-url "https://github.com/puppetlabs/pdk-templates" $ModuleName
Pop-Location
## copy pdk specific files
Copy-Item -Path (Join-Path -Path $templateDir 'pdk/*') -Destination $moduleDir -Recurse -Force
$metadatajson = Get-Content -Path (Join-Path $moduleDir "metadata.json") | ConvertFrom-Json
$metadatajson.dependencies = @( @{ "name" = "puppetlabs/pwshlib"; "version_requirement" = ">= 0.1.0 < 2.0.0" } )
$metadatajson | ConvertTo-Json | Set-Content -Path (Join-Path $moduleDir "metadata.json") -Encoding UTF8
# copy resource_api base classes
Get-ChildITem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'templates/resource_api/*') | Copy-Item -Destination $moduleDir -Recurse -Force
# build puppet types from dsc resources
[string]$puppetTypeTemplate = [IO.Path]::Combine($PSScriptRoot, 'templates', 'dsc_type.eps')
[string]$puppetProviderTemplate = [IO.Path]::Combine($PSScriptRoot, 'templates', 'dsc_provider.eps')
[string]$dscResourcePowerShellTypes = [IO.Path]::Combine($PSScriptRoot, 'templates', 'dsc_resource_types.ps1xml')
[string]$puppetTypeDir = [IO.Path]::Combine($moduleDir, 'lib', 'puppet', 'type')
[string]$puppetProviderDir = [IO.Path]::Combine($moduleDir, 'lib', 'puppet', 'provider')
Update-TypeData -PrependPath $dscResourcePowerShellTypes
$oldPsModulePath = $env:PSModulePath
$env:PSModulePath = "$($downloadedDscResources);" + $env:PSModulePath
$global:resources = Get-DscResource
# ipmo C:\Users\james\src\puppetlabs\eps\EPS\EPS.psd1
iF(!(Get-Module -Name 'EPS')){
Install-Module -Name 'EPS'
}
Import-Module -Name 'EPS'
# EPS requires global variables to keep them in accessible scope
# Also need to set the variable to null inside the loop
# Files are written using UTF8, but newlines will need to addressed
foreach($resource in $resources){
$global:resource = $resource
$dscResourceName = "dsc_$($resource.Name.ToLowerInvariant())"
if(-not(Test-Path $puppetTypeDir)){
mkdir $puppetTypeDir | Out-Null
}
[string]$puppetTypeFileName = [IO.Path]::Combine($puppetTypeDir, "$($dscResourceName).rb")
$puppetTypeText = Invoke-EpsTemplate -Path $puppetTypeTemplate
[IO.File]::WriteAllText($puppetTypeFileName, $puppetTypeText, [Text.Encoding]::UTF8)
[string]$puppetTypeProviderDir = Join-Path $puppetProviderDir "$($dscResourceName)"
[string]$puppetProviderFileName = [IO.Path]::Combine($puppetProviderDir, "$($dscResourceName)", "$($dscResourceName).rb")
if(-not(Test-Path $puppetTypeProviderDir)){
mkdir $puppetTypeProviderDir | Out-Null
}
$puppetProviderText = Invoke-EpsTemplate -Path $puppetProviderTemplate
[IO.File]::WriteAllText($puppetProviderFileName, $puppetProviderText, [Text.Encoding]::UTF8)
$resource = $Null
}
$env:PSModulePath = $oldPsModulePath