Skip to content

Commit

Permalink
Make yaml dependency self-contained
Browse files Browse the repository at this point in the history
powershell-yaml wraps YamlDotNet, but hasn't been updated in a while.  the mechanism it uses for ConvertFrom-Yaml uses Parser which throws duplicate key exceptions.  Deserialize does not throw duplicate key exceptions.

Given it's common to have duplication in gitlab CI yaml, allowing overlap/merging is better than throwing
  • Loading branch information
chris-peterson committed Nov 18, 2021
1 parent 66d078d commit 7679106
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,6 @@ Your application
## References / Acknowledgements

* [PSGitLab](https://github.com/ngetchell/PSGitLab)
* [python-gitlab CLI documentation](https://python-gitlab.readthedocs.io/en/stable/)
* [GitLab API docs](https://docs.gitlab.com/ee/api/)
* [python-gitlab CLI documentation](https://python-gitlab.readthedocs.io/en/stable)
* [GitLab API docs](https://docs.gitlab.com/ee/api)
* [powershell-yaml](https://github.com/cloudbase/powershell-yaml)
4 changes: 2 additions & 2 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @('powershell-yaml')
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
ScriptsToProcess = @('_Init.ps1')

# Type files (.ps1xml) to be loaded when importing this module
TypesToProcess = @('Types.ps1xml')
Expand Down
8 changes: 7 additions & 1 deletion src/GitlabCli/RepositoryFiles.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,11 @@ function Get-GitlabCiYml {
$WhatIf
)

New-Object 'psobject' -Property $(ConvertFrom-Yaml $(Get-GitlabRepositoryFileContent -ProjectId $ProjectId -Ref $Ref -FilePath '.gitlab-ci.yml' -SiteUrl $SiteUrl -WhatIf:$WhatIf))
$Yml = $(Get-GitlabRepositoryFileContent -ProjectId $ProjectId -Ref $Ref -FilePath '.gitlab-ci.yml' -SiteUrl $SiteUrl -WhatIf:$WhatIf)
$Hash = $([YamlDotNet.Serialization.Deserializer].GetMethods() |
Where-Object { $_.Name -eq 'Deserialize' -and $_.ReturnType.Name -eq 'T' -and $_.GetParameters().ParameterType.Name -eq 'String' }). `
MakeGenericMethod(
[object]). `
Invoke($(New-Object 'YamlDotNet.Serialization.Deserializer'), $Yml)
New-Object 'psobject' -Property $Hash
}
33 changes: 33 additions & 0 deletions src/GitlabCli/_Init.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Adapted from
# https://github.com/cloudbase/powershell-yaml/blob/master/Load-Assemblies.ps1

$Here = Split-Path -Parent $MyInvocation.MyCommand.Path

function Initialize-Assembly {
$LibDir = Join-Path $Here "lib"

return [Reflection.Assembly]::LoadFrom($(Join-Path $libDir "YamlDotNet.dll"))
}

function Initialize-Assemblies {
$RequiredTypes = @(
"Parser", "MergingParser", "YamlStream",
"YamlMappingNode", "YamlSequenceNode",
"YamlScalarNode", "ChainedEventEmitter",
"Serializer", "Deserializer", "SerializerBuilder",
"StaticTypeResolver"
)

$YamlDotNet = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location -Match "YamlDotNet.dll"
if (!$YamlDotNet) {
return Initialize-Assembly
}

foreach ($i in $RequiredTypes){
if ($i -notin $YamlDotNet.DefinedTypes.Name) {
throw "YamlDotNet is loaded but missing required types ($i). Older version installed on system?"
}
}
}

Initialize-Assemblies | Out-Null
Binary file added src/GitlabCli/lib/YamlDotNet.dll
Binary file not shown.

0 comments on commit 7679106

Please sign in to comment.