diff --git a/README.md b/README.md index dd3aaee..cb12874 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/GitlabCli/GitlabCli.psd1 b/src/GitlabCli/GitlabCli.psd1 index 1d5aed5..6549e5a 100644 --- a/src/GitlabCli/GitlabCli.psd1 +++ b/src/GitlabCli/GitlabCli.psd1 @@ -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') diff --git a/src/GitlabCli/RepositoryFiles.psm1 b/src/GitlabCli/RepositoryFiles.psm1 index d7b9306..3064711 100644 --- a/src/GitlabCli/RepositoryFiles.psm1 +++ b/src/GitlabCli/RepositoryFiles.psm1 @@ -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 } diff --git a/src/GitlabCli/_Init.ps1 b/src/GitlabCli/_Init.ps1 new file mode 100644 index 0000000..9397e69 --- /dev/null +++ b/src/GitlabCli/_Init.ps1 @@ -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 diff --git a/src/GitlabCli/lib/YamlDotNet.dll b/src/GitlabCli/lib/YamlDotNet.dll new file mode 100644 index 0000000..d49973d Binary files /dev/null and b/src/GitlabCli/lib/YamlDotNet.dll differ