From 195a7b644bb0fb275cca294a7b81210082cc2323 Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Wed, 29 Aug 2018 15:54:29 -0700 Subject: [PATCH] initial commit of module (#14) * initial commit of module * address feedback and move module into worker * address Dongbo's feedback * remove psd1 fields and change Set logic * don't expose vars in psd1 * alpha sort functions * helper function refactoring --- ...re.Functions.PowerShell.Worker.Module.psd1 | 84 ++++++++++++ ...re.Functions.PowerShell.Worker.Module.psm1 | 126 ++++++++++++++++++ .../Azure.Functions.PowerShell.Worker.csproj | 3 + 3 files changed, 213 insertions(+) create mode 100644 src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psd1 create mode 100644 src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psm1 diff --git a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psd1 b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psd1 new file mode 100644 index 0000000..150be33 --- /dev/null +++ b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psd1 @@ -0,0 +1,84 @@ +@{ + +# Script module or binary module file associated with this manifest. +RootModule = 'Azure.Functions.PowerShell.Worker.Module.psm1' + +# Version number of this module. +ModuleVersion = '0.1.0' + +# Supported PSEditions +CompatiblePSEditions = @('Desktop', 'Core') + +# ID used to uniquely identify this module +GUID = 'f0149ba6-bd6f-4dbd-afe5-2a95bd755d6c' + +# Author of this module +Author = 'Microsoft Corporation' + +# Company or vendor of this module +CompanyName = 'Microsoft Corporation' + +# Copyright statement for this module +Copyright = '(c) Microsoft Corporation. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'The module used in an Azure Functions environment for setting and retrieving Output Bindings.' + +# Minimum version of the PowerShell engine required by this module +PowerShellVersion = '5.1' + +# Modules that must be imported into the global environment prior to importing this module +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 = @() + +# Type files (.ps1xml) to be loaded when importing this module +TypesToProcess = @() + +# Format files (.ps1xml) to be loaded when importing this module +FormatsToProcess = @() + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +NestedModules = @() + +# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. +FunctionsToExport = @('Push-OutputBinding', 'Get-OutputBinding') + +# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. +CmdletsToExport = @() + +# Variables to export from this module +VariablesToExport = @() + +# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. +AliasesToExport = @() + +# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + Tags = @('Microsoft', 'Azure', 'Functions', 'Serverless', 'Cloud') + + # A URL to the license for this module. + LicenseUri = 'https://github.com/Azure/azure-functions-powershell-worker/blob/dev/LICENSE' + + # A URL to the main website for this project. + ProjectUri = 'https://github.com/Azure/azure-functions-powershell-worker' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + ReleaseNotes = '# 0.1.0 +Initial Release. +' + + } +} +} diff --git a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psm1 b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psm1 new file mode 100644 index 0000000..31e5d3b --- /dev/null +++ b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.Module/Azure.Functions.PowerShell.Worker.Module.psm1 @@ -0,0 +1,126 @@ +# +# Copyright (c) Microsoft. All rights reserved. +# Licensed under the MIT license. See LICENSE file in the project root for full license information. +# + +# This holds the current state of the output bindings +$script:_OutputBindings = @{} + +<# +.SYNOPSIS + Gets the hashtable of the output bindings set so far. +.DESCRIPTION + Gets the hashtable of the output bindings set so far. +.EXAMPLE + PS > Get-OutputBinding + Gets the hashtable of all the output bindings set so far. +.EXAMPLE + PS > Get-OutputBinding -Name res + Gets the hashtable of specific output binding. +.EXAMPLE + PS > Get-OutputBinding -Name r* + Gets the hashtable of output bindings that match the wildcard. +.PARAMETER Name + The name of the output binding you want to get. Supports wildcards. +.OUTPUTS + The hashtable of binding names to their respective value. +#> +function Get-OutputBinding { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] + [string[]] + $Name = '*' + ) + begin { + $bindings = @{} + } + process { + $script:_OutputBindings.GetEnumerator() | Where-Object Name -Like $Name | ForEach-Object { $null = $bindings.Add($_.Name, $_.Value) } + } + end { + return $bindings + } +} + +# Helper private function that sets an OutputBinding. +function Push-KeyValueOutputBinding { + param ( + [Parameter(Mandatory=$true)] + [string] + $Name, + + [Parameter(Mandatory=$true)] + [object] + $Value, + + [switch] + $Force + ) + if(!$script:_OutputBindings.ContainsKey($Name) -or $Force.IsPresent) { + $script:_OutputBindings[$Name] = $Value + } else { + throw "Output binding '$Name' is already set. To override the value, use -Force." + } +} + +<# +.SYNOPSIS + Sets the value for the specified output binding. +.DESCRIPTION + Sets the value for the specified output binding. +.EXAMPLE + PS > Push-OutputBinding -Name res -Value "my output" + The output binding of "res" will have the value of "my output" +.PARAMETER Name + The name of the output binding you want to set. +.PARAMETER Value + The value of the output binding you want to set. +.PARAMETER InputObject + The hashtable that contains the output binding names to their respective value. +.PARAMETER Force + (Optional) If specified, will force the value to be set for a specified output binding. +#> +function Push-OutputBinding { + [CmdletBinding()] + param ( + [Parameter( + Mandatory=$true, + ParameterSetName="NameValue", + Position=0, + ValueFromPipelineByPropertyName=$true)] + [string] + $Name, + + [Parameter( + Mandatory=$true, + ParameterSetName="NameValue", + Position=1, + ValueFromPipelineByPropertyName=$true)] + [object] + $Value, + + [Parameter( + Mandatory=$true, + ParameterSetName="InputObject", + Position=0, + ValueFromPipeline=$true)] + [hashtable] + $InputObject, + + [switch] + $Force + ) + process { + switch ($PSCmdlet.ParameterSetName) { + NameValue { + Push-KeyValueOutputBinding -Name $Name -Value $Value -Force:$Force.IsPresent + } + InputObject { + $InputObject.GetEnumerator() | ForEach-Object { + Push-KeyValueOutputBinding -Name $_.Name -Value $_.Value -Force:$Force.IsPresent + } + } + } + } +} diff --git a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj index 841e198..4ed1e15 100644 --- a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj +++ b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj @@ -29,6 +29,9 @@ PreserveNewest + + PreserveNewest +