1
+
2
+ <#
3
+ . SYNOPSIS
4
+ Packages a Azure Functions project, ready to release.
5
+
6
+ . DESCRIPTION
7
+ Packages a Azure Functions project, ready to release.
8
+ Should be part of the release pipeline, after ensuring validation.
9
+
10
+ Look into the 'AzureFunctionRest' template for generating functions for the module if you do.
11
+
12
+ . PARAMETER WorkingDirectory
13
+ The root folder to work from.
14
+
15
+ . PARAMETER Repository
16
+ The name of the repository to use for gathering dependencies from.
17
+ #>
18
+ param (
19
+ $WorkingDirectory = " $ ( $env: SYSTEM_DEFAULTWORKINGDIRECTORY ) \_þnameþ" ,
20
+
21
+ $Repository = ' PSGallery'
22
+ )
23
+
24
+ $moduleName = ' þnameþ'
25
+
26
+ # Prepare Paths
27
+ Write-PSFMessage - Level Host - Message " Creating working folders"
28
+ $moduleRoot = Join-Path - Path $WorkingDirectory - ChildPath ' publish'
29
+ $workingRoot = New-Item - Path $WorkingDirectory - Name ' working' - ItemType Directory
30
+ $modulesFolder = New-Item - Path $workingRoot.FullName - Name Modules - ItemType Directory
31
+
32
+ # Fill out the modules folder
33
+ Write-PSFMessage - Level Host - Message " Transfering built module data into working directory"
34
+ Copy-Item - Path " $moduleRoot \$moduleName " - Destination $modulesFolder.FullName - Recurse - Force
35
+ foreach ($dependency in (Import-PowerShellDataFile - Path " $moduleRoot \$moduleName \$moduleName .psd1" ).RequiredModules)
36
+ {
37
+ $param = @ {
38
+ Repository = $Repository
39
+ Name = $dependency.ModuleName
40
+ Path = $modulesFolder.FullName
41
+ }
42
+ if ($dependency -is [string ]) { $param [' Name' ] = $dependency }
43
+ if ($dependency.RequiredVersion )
44
+ {
45
+ $param [' RequiredVersion' ] = $dependency.RequiredVersion
46
+ }
47
+ Write-PSFMessage - Level Host - Message " Preparing Dependency: $ ( $param [' Name' ]) "
48
+ Save-Module @param
49
+ }
50
+
51
+ # Generate function configuration
52
+ Write-PSFMessage - Level Host - Message ' Generating function configuration'
53
+ foreach ($functionName in (Get-ChildItem - Path " $ ( $moduleRoot ) \$moduleName \functions" - Recurse - Filter ' *.ps1' ))
54
+ {
55
+ Write-PSFMessage - Level Host - Message " Processing function: $functionName "
56
+ $condensedName = $functionName.BaseName -replace ' -' , ' '
57
+ $functionFolder = New-Item - Path $workingRoot.FullName - Name $condensedName - ItemType Directory
58
+
59
+ Set-Content - Path " $ ( $functionFolder.FullName ) \function.json" - Value @"
60
+ {
61
+ "entryPoint": "$ ( $functionName.BaseName ) ",
62
+ "scriptFile": "../Modules/$ ( $moduleName ) /$ ( $moduleName ) .psm1",
63
+ "bindings": [
64
+ {
65
+ "authLevel": "function",
66
+ "type": "httpTrigger",
67
+ "direction": "in",
68
+ "name": "Request",
69
+ "methods": [
70
+ "get",
71
+ "post"
72
+ ]
73
+ },
74
+ {
75
+ "type": "http",
76
+ "direction": "out",
77
+ "name": "Response"
78
+ }
79
+ ],
80
+ "disabled": false
81
+ }
82
+ "@
83
+ # Implement overrides where specified by the user
84
+ if (Test-Path - Path " $ ( $WorkingDirectory ) \azFunctionResources\$ ( $functionName.BaseName ) .json" )
85
+ {
86
+ Copy-Item - Path " $ ( $WorkingDirectory ) \azFunctionResources\$ ( $functionName.BaseName ) .json" - Destination " $ ( $functionFolder.FullName ) \function.json" - Force
87
+ }
88
+ if (Test-Path - Path " $ ( $WorkingDirectory ) \azFunctionResources\$ ( $condensedName ) .json" )
89
+ {
90
+ Copy-Item - Path " $ ( $WorkingDirectory ) \azFunctionResources\$ ( $condensedName ) .json" - Destination " $ ( $functionFolder.FullName ) \function.json" - Force
91
+ }
92
+ }
93
+
94
+ # Transfer common files
95
+ Write-PSFMessage - Level Host - Message " Transfering core function data"
96
+ Copy-Item - Path " $ ( $WorkingDirectory ) \azFunctionResources\host.json" - Destination " $ ( $workingroot.FullName ) \"
97
+ Copy-Item - Path " $ ( $WorkingDirectory ) \azFunctionResources\local.settings.json" - Destination " $ ( $workingroot.FullName ) \"
98
+ Copy-Item - Path " $ ( $WorkingDirectory ) \azFunctionResources\profile.ps1" - Destination " $ ( $workingroot.FullName ) \"
99
+ Copy-Item - Path " $ ( $WorkingDirectory ) \azFunctionResources\root.psm1" - Destination " $ ( $workingroot.FullName ) \"
100
+
101
+ # Zip It
102
+ Write-PSFMessage - Level Host - Message " Creating function archive in '$ ( $WorkingDirectory ) \$moduleName .zip'"
103
+ Compress-Archive - Path " $ ( $workingroot.FullName ) \*" - DestinationPath " $ ( $WorkingDirectory ) \$moduleName .zip" - Force
0 commit comments