Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions PSCloudFormation/PSCloudFormation.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,8 @@

$ErrorActionPreference = 'Stop'

# .NET compression libraries
'System.IO.Compression', 'System.IO.Compression.FileSystem' |
Foreach-Object {
[System.Reflection.Assembly]::LoadWithPartialName($_) | Out-Null
}

# Check for YAML support
$psyaml = Get-Module -ListAvailable |
Where-Object {
$_.Name -ieq 'powershell-yaml'
}

$Script:yamlSupport = $false

if ($psyaml)
{
if ($PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTable.PSEdition -eq 'Core' -and -not ($psyaml | Where-Object { $_.Version -ge [Version]'0.4.0' } ))
{
Write-Warning "powershell-yaml >= 0.4.0 required in this version."
Write-Warning "Please upgrade to enable YAML support."
}
else
{
Import-Module powershell-yaml
$script:yamlSupport = $true
}
}
else
{
Write-Warning 'YAML support unavailable'
Write-Warning 'To enable, install powershell-yaml from the gallery'
Write-Warning 'Install-Module -Name powershell-yaml'
}
# Import assemblies
. (Join-Path $PSScriptRoot 'lib/Initialize-Assemblies.ps1')

# Get cfn-flip if present. That's the only safe way to convert short-form intrinsics
$script:cfnFlip = $(
Expand Down
6 changes: 1 addition & 5 deletions PSCloudFormation/Private/Compress-UnixZip.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ function Compress-UnixZip

# Account for powershell and OS current directory not being the same
# as .NET objects like ZipFile will use OS path
$osZipPath = $ZipFile

New-Item -Path $osZipPath -ItemType File | Out-Null
$osZipPath = (Resolve-Path -Path $osZipPath).Path
Remove-Item $osZipPath -Force
$osZipPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($ZipFile)

try
{
Expand Down
63 changes: 0 additions & 63 deletions PSCloudFormation/Private/ConvertTo-YamlIntrinsics.ps1

This file was deleted.

94 changes: 71 additions & 23 deletions PSCloudFormation/Private/Get-TemplateParameters.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,82 @@ function Get-TemplateParameters

$template = $TemplateResolver.ReadTemplate()

# Check YAML/JSON
try
switch (Get-TemplateFormat -TemplateBody $template)
{
$templateObject = $template | ConvertFrom-Json

if ($templateObject.PSObject.Properties.Name -contains 'Parameters')
{
return $templateObject.Parameters
}
else
'JSON'
{
# No parameters
return
$templateObject = $template | ConvertFrom-Json

if ($templateObject.PSObject.Properties.Name -contains 'Parameters')
{
return $templateObject.Parameters
}
else
{
# No parameters
return
}
}
}
catch
{
if (-not $Script:yamlSupport)

'YAML'
{
throw "Template cannot be parsed as JSON and YAML support unavailable"
}
}
$yaml = New-Object YamlDotNet.RepresentationModel.YamlStream
$input = New-Object System.IO.StringReader($template)

# Try YAML - convert it through JSON and back to ensure the final object looks the same as one converted directly from JSON
$templateObject = $template | ConvertFrom-Yaml | ConvertTo-Json | ConvertFrom-Json
$yaml.Load($input)

if ($templateObject.PSObject.Properties.Name -contains 'Parameters')
{
return $templateObject.Parameters
$root = [YamlDotNet.RepresentationModel.YamlMappingNode]$yaml.Documents[0].RootNode

if ($null -eq $root)
{
throw "Empty document or not YAML"
}

$parametersKey = New-Object YamlDotNet.RepresentationModel.YamlScalarNode("Parameters")

if (-not $root.Children.ContainsKey($parametersKey))
{
# No parameters
return
}

$parameters = [YamlDotNet.RepresentationModel.YamlMappingNode]$root.Children[$parametersKey]

# Now create a PSObject that looks like parameters parsed from JSON
$returnParameters = New-Object PSObject

foreach ($parameterNode in $parameters.Children.GetEnumerator())
{
$parameterBody = $parameterNode.Value

$parameterData = New-Object psobject

foreach ($parameterPropertyNode in $parameterBody.Children.GetEnumerator())
{
if ($parameterPropertyNode.Value -is [YamlDotNet.RepresentationModel.YamlScalarNode])
{
$parameterData | Add-Member -MemberType NoteProperty -Name $parameterPropertyNode.Key.ToString() -Value $parameterPropertyNode.Value.ToString()
}
elseif ($parameterPropertyNode.Value -is [YamlDotNet.RepresentationModel.YamlSequenceNode])
{
$values = @()
foreach ($seqNode in $parameterPropertyNode.Value.Children)
{
$values += $seqNode.Value.ToString()
}

$parameterData | Add-Member -MemberType NoteProperty -Name $parameterPropertyNode.Key.ToString() -Value $values
}
else
{
throw "Unexpected type $($parameterPropertyNode.Value.GetType().Name) in parameter block"
}
}

$returnParameters | Add-Member -MemberType NoteProperty -Name $parameterNode.Key.ToString() -Value $parameterData
}

return $returnParameters
}
}
}
12 changes: 1 addition & 11 deletions PSCloudFormation/Private/Out-FileWithoutBOM.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@ function Out-FileWithoutBOM
)

# Handle difference between provider path and .NET path
$fullPath = $(
try
{
(Resolve-Path -Path $FilePath).Path
}
catch
{
New-Item -Path $FilePath -ItemType File | Out-Null
(Resolve-Path -Path $FilePath).Path
}
)
$fullPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FilePath)

$enc = New-Object System.Text.UTF8Encoding -ArgumentList $false

Expand Down
7 changes: 1 addition & 6 deletions PSCloudFormation/Public/New-PSCFNPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ function New-PSCFNPackage

'YAML'
{
if (-not $script:yamlSupport)
{
throw "YAML support unavailable."
}

# Do using raw yaml stream so as not to bollox any short form intrinsics
# https://github.com/aws/aws-extensions-for-dotnet-cli/blob/master/src/Amazon.Lambda.Tools/LambdaUtilities.cs line 283

Expand All @@ -285,7 +280,7 @@ function New-PSCFNPackage

if (-not $root.Children.ContainsKey($resourcesKey))
{
# TODO return doc
throw "Invalid template. It contains no resources"
}

$resources = [YamlDotNet.RepresentationModel.YamlMappingNode]$root.Children[$resourcesKey]
Expand Down
58 changes: 58 additions & 0 deletions PSCloudFormation/lib/Initialize-Assemblies.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path

function Import-YamlAssembly
{
$assemblies = @{
"core" = Join-Path $here "netstandard1.3\YamlDotNet.dll";
"net45" = Join-Path $here "net45\YamlDotNet.dll";
"net35" = Join-Path $here "net35\YamlDotNet.dll";
}

if ($PSVersionTable.PSEdition -eq "Core")
{
return [Reflection.Assembly]::LoadFrom($assemblies["core"])
}
elseif ($PSVersionTable.PSVersion.Major -ge 4)
{
return [Reflection.Assembly]::LoadFrom($assemblies["net45"])
}
else
{
return [Reflection.Assembly]::LoadFrom($assemblies["net35"])
}
}


function Initialize-Assemblies
{
# .NET compression libraries
'System.IO.Compression', 'System.IO.Compression.FileSystem' |
Foreach-Object {
[System.Reflection.Assembly]::LoadWithPartialName($_)
}

# YamlDotNet
$requiredTypes = @(
"YamlStream"
"YamlMappingNode"
"YamlSequenceNode"
"YamlScalarNode"
)

$yaml = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location -Match "YamlDotNet.dll"

if (!$yaml)
{
return Import-YamlAssembly
}

# TODO - Handle multiple versions of the assembly being present
<#
if ($yaml.ManifestModule.Assembly.GetName().Version -lt [Version]"6.0.0.0")
{
throw "Version $($yaml.ManifestModule.Assembly.GetName().Version) has been loaded by something else. At least version 6.0.0.0 is required. Reset your session and load this module first."
}
#>
}

Initialize-Assemblies | Out-Null
Binary file added PSCloudFormation/lib/net35/YamlDotNet.dll
Binary file not shown.
Binary file added PSCloudFormation/lib/net45/YamlDotNet.dll
Binary file not shown.
Binary file not shown.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ This module provides the following stack modification cmdlets

### Template Support

By default, only JSON templates are supported. YAML support is enabled by installing `powershell-yaml` from the [PowerShell Gallery](https://www.powershellgallery.com/packages/powershell-yaml).

Oversize templates in your local file system (file size >= 51,200 bytes) are directly supported. They will be siliently uploaded to an S3 bucket which is created as necessary prior to processing. The bucket is named `cf-templates-pscloudformation-region-accountid` where
* `region` is the region you are building the stack in, e.g. `eu-west-1`.
* `accountid` is the numeric ID of the AWS account in which you are building the stack.
Expand Down Expand Up @@ -133,4 +131,4 @@ Thanks to

* [ramblingcookiemonster](http://ramblingcookiemonster.github.io/) for `New-DynamicParam` and build stuff.
* [Ahmad Gad](http://ahmad.jempress.com) for `Write-PSObject` that provides coloured output of stack events.
* [Gabriel Samfira](https://github.com/gabriel-samfira) for `powershell-yaml` and his help in [getting to the bottom of an assembly version clash](https://github.com/cloudbase/powershell-yaml/issues/47) when loading it.
* [Antoine Aubry](https://github.com/aaubry/YamlDotNet) for `YamlDotNet`
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ image:
- Ubuntu

install:
- ps: . ./cfn-flip-install.ps1
- ps: . ./build/cfn-flip-install.ps1
- ps: . ./build/psdeploy-install.ps1

# Does the whole process
test_script:
Expand Down
3 changes: 3 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ try

Import-Module PSDepend

# Custom version
Import-Module PSDeploy

$psDependTags = $(
if (Test-Path -Path variable:PSEdition)
{
Expand Down
11 changes: 0 additions & 11 deletions build.requirements.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,11 @@
}
}

'powershell-yaml' = @{
DependencyType = 'PSGalleryModule'
Version = '0.4.0'
Tags = @('Desktop', 'Core')
}
'psake' = @{
DependencyType = 'PSGalleryModule'
Version = '4.7.4'
Tags = @('Desktop', 'Core')
}
'PSDeploy' = @{
DependencyType = 'PSGalleryModule'
Version = '1.0.1'
DependsOn = 'powershell-yaml' # Must install psyaml first to avoid dll hell.
Tags = @('Desktop', 'Core')
}
'BuildHelpers' = @{
DependencyType = 'PSGalleryModule'
Version = '2.0.7'
Expand Down
File renamed without changes.
Loading