forked from aws/aws-lambda-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_ProjectCreationFunctions.ps1
More file actions
204 lines (169 loc) · 7.47 KB
/
_ProjectCreationFunctions.ps1
File metadata and controls
204 lines (169 loc) · 7.47 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<#
.DESCRIPTION
Initializes the PowerShell script file containing our Lambda function from the
blueprint contents. The name of the script file containing the function is
returned to the caller, to be used in further initialization of the unpacked
blueprint (eg when adding the wrapper project files).
#>
function _initializeScriptFromTemplate
{
param
(
# The name of the template to source the new script or project based Lambda function from.
[Parameter(Mandatory = $true)]
[string]$Template,
# The output folder to place the generated content into.
[Parameter(Mandatory = $true)]
[string]$Directory,
# Optional basename to use for the Lambda function script file. If not specified the
# name of the template is used.
[Parameter()]
[string]$BaseName
)
if ($BaseName)
{
$_baseName = $BaseName
}
else
{
$_baseName = $Template
}
$_lambdaFunctionFile = _unpackBlueprintContents -Template $Template -Directory $Directory -BaseName $_baseName -ContentProcessor {
param([string]$fileExt, [string]$content)
if (($fileExt -ieq '.ps1') -Or ($fileExt -ieq '.psm1'))
{
# Check to see if the script has stated a requirement on AWSPowerShell.NetCore or AWS.Tools.* modules and if so make the
# version referenced match the version installed locally.
$ast = [System.Management.Automation.Language.Parser]::ParseInput($content, [ref]$null, [ref]$null)
if ($ast.ScriptRequirements.RequiredModules)
{
$ast.ScriptRequirements.RequiredModules | ForEach-Object -Process {
if (!($_.Name -ieq 'AWSPowerShell.NetCore') -and !($_.Name.StartsWith("AWS.Tools.")))
{
return
}
$installedVersion = _getVersionOfLocalInstalledModule -Module $_.Name
if (!($installedVersion))
{
Write-Warning "This script requires the $_.Name module which is not installed locally."
Write-Warning "To use the AWS CmdLets execute ""Install-Module $_.Name"" and then update the #Requires statement to the version installed. If you are not going to use the AWS CmdLets then remove the #Requires statement from the script."
}
elseif ($installedVersion -ne $_.Version.ToString())
{
$lines = $content.Split('\n')
for ($i = 0; $i -lt $lines.Length; $i++ )
{
if ($lines[$i].Contains('#Requires') -and $lines[$i].Contains($_.Name) -and $lines[$i].Contains($_.Version.ToString()) )
{
$lines[$i] = $lines.Replace($_.Version.ToString(), $installedVersion)
Write-Host ("Configuring script to use installed version $installedVersion of ($_.Name)")
break
}
}
$content = [System.String]::Join('\n', $lines)
}
}
}
}
return $content
}
return $_lambdaFunctionFile
}
# Checks for an already installed dependency module and returns the version if found.
function _getVersionOfLocalInstalledModule
{
param
(
[Parameter(Mandatory = $true)]
[string]$Module
)
$localModule = Get-Module -ListAvailable -Name $Module | Sort-Object -Property Version -Descending | Select-Object -First 1
if ($localModule)
{
$localModule.Version.ToString()
}
}
function _addPowerShellLambdaProjectContent
{
param
(
# The name the user selected for the project, or the template name if the user
# did not supply a value.
[Parameter()]
[string]$ProjectName,
# The name of the generated file containing the actual Lambda function.
[Parameter()]
[string]$ScriptFile,
# The output folder containing the generated project.
[Parameter()]
[string]$Directory,
# User-specified credential profile name to seed into the generated defaults file to be used on deployment.
[Parameter()]
[string]$ProfileName,
# User-specified region to seed into the generated defaults file to be used on deployment.
[Parameter()]
[string]$Region,
# The version of Microsoft.PowerShell.SDK to configure for the project
[Parameter()]
[string]$PowerShellSdkVersion,
[Parameter(Mandatory = $true)]
[bool]$QuietMode
)
if(!($PowerShellSdkVersion))
{
$PowerShellSdkVersion = $AwsPowerShellDefaultSdkVersion
}
# Setup project file
$csprojContent = _getBlueprintsContent 'projectfile.csproj.txt'
$csprojContent = $csprojContent.Replace('SCRIPT_FILE', $ScriptFile)
$csprojContent = $csprojContent.Replace('POWERSHELL_SDK_VERSION', $PowerShellSdkVersion)
Write-Host "Configuring PowerShell to version $PowerShellSdkVersion"
$filename = Join-Path -Path $Directory -ChildPath "$ProjectName.csproj"
if (!$QuietMode)
{
Write-Host "Generating C# project $filename used to create Lambda function bundle."
}
Out-File -FilePath $filename -InputObject $csprojContent -Encoding UTF8 -Force
# Setup bootstrap code
$namespaceName = _makeSafeNamespaceName ([System.IO.Path]::GetFileName($ProjectName))
$bootstrapContent = _getBlueprintsContent 'bootstrap.cs.txt'
$bootstrapContent = $bootstrapContent.Replace('NAMESPACE_NAME', $namespaceName)
$bootstrapContent = $bootstrapContent.Replace('SCRIPT_FILE', $ScriptFile)
$filename = Join-Path -Path $Directory -ChildPath 'Bootstrap.cs'
if (!$QuietMode)
{
Write-Host "Generating $filename to load PowerShell script and required modules in Lambda environment."
}
Out-File -FilePath $filename -InputObject $bootstrapContent -Encoding UTF8 -Force
if (!$QuietMode)
{
Write-Host 'Generating aws-lambda-tools-defaults.json config file with default values used when publishing project.'
}
$defaultsContent = _getBlueprintsContent 'aws-lambda-tools-defaults.txt'
$defaultsContent = $defaultsContent.Replace('CONFIGURED_PROFILE', $ProfileName)
$defaultsContent = $defaultsContent.Replace('CONFIGURED_REGION', $Region)
$defaultsContent = $defaultsContent.Replace('PROJECT_NAME', $ProjectName)
$defaultsContent = $defaultsContent.Replace('DEFAULT_MEMORY', $DefaultFunctionMemory)
$defaultsContent = $defaultsContent.Replace('DEFAULT_TIMEOUT', $DefaultFunctionTimeout)
$filename = Join-Path -Path $Directory -ChildPath 'aws-lambda-tools-defaults.json'
Out-File -FilePath $filename -InputObject $defaultsContent -Encoding UTF8 -Force
$newDirectory = Join-Path -Path $Directory -ChildPath $ProjectModuleDirectory
New-Item -ItemType directory -Path $newDirectory -Force | Out-Null
}
function _makeSafeNamespaceName
{
param
(
[Parameter(Mandatory = $true)]
[string]$OriginalName
)
$namespaceName = $OriginalName
$namespaceName = $namespaceName -replace '-', '_'
$namespaceName = $namespaceName -replace ' ', '_'
$namespaceName = $namespaceName -replace '\.', '_'
if([Char]::IsDigit($namespaceName[0]))
{
$namespaceName = "PS_$namespaceName"
}
return $namespaceName
}