11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT License.
3- Function Add-WinGetManifest
4- {
3+ function Add-WinGetManifest {
54 <#
65 . SYNOPSIS
76 Submits a Manifest to the Windows Package Manager REST source.
87
9- . DESCRIPTION
8+ . DESCRIPTION
109 Submits a Manifest to the Windows Package Manager REST source.
11- Running this function will first connect to the Azure Tenant that hosts the Windows Package Manager REST source.
10+ Running this function will first connect to the Azure Tenant that hosts the Windows Package Manager REST source.
1211 The function will then collect the required URL before retrieving the contents of the Manifest for submission.
1312
1413 . PARAMETER FunctionName
@@ -41,75 +40,73 @@ Function Add-WinGetManifest
4140 specified Manifest (*.json) to the Windows Package Manager REST source.
4241 #>
4342
44- PARAM (
45- [Parameter (Position = 0 , Mandatory = $true )] [string ]$FunctionName ,
46- [Parameter (Position = 1 , Mandatory = $true , ValueFromPipeline = $true )] [string ]$Path ,
47- [Parameter (Mandatory = $false )] [string ]$SubscriptionName = " "
43+ param (
44+ [Parameter (Position = 0 , Mandatory = $true )] [string ]$FunctionName ,
45+ [Parameter (Position = 1 , Mandatory = $true , ValueFromPipeline = $true )] [string ]$Path ,
46+ [Parameter (Mandatory = $false )] [string ]$SubscriptionName = ' '
4847 )
49- BEGIN
50- {
48+ begin {
5149 [WinGetManifest []] $Return = @ ()
5250
5351 # ##############################
5452 # # Connects to Azure, if not already connected.
55- Write-Verbose - Message " Validating connection to azure, will attempt to connect if not already connected."
53+ Write-Verbose - Message ' Validating connection to azure, will attempt to connect if not already connected.'
5654 $Result = Connect-ToAzure - SubscriptionName $SubscriptionName
57- if (! ($Result )) {
58- Write-Error " Failed to connect to Azure. Please run Connect-AzAccount to connect to Azure, or re-run the cmdlet and enter your credentials." - ErrorAction Stop
55+ if (! ($Result )) {
56+ Write-Error ' Failed to connect to Azure. Please run Connect-AzAccount to connect to Azure, or re-run the cmdlet and enter your credentials.' - ErrorAction Stop
5957 }
6058
6159 # ##############################
6260 # # Gets Resource Group name of the Azure Function
63- Write-Verbose - Message " Determines the Azure Function Resource Group Name"
64- $ResourceGroupName = $ (Get-AzFunctionApp ).Where ({$_.Name -eq $FunctionName }).ResourceGroupName
65- if (! $ResourceGroupName ) {
61+ Write-Verbose - Message ' Determines the Azure Function Resource Group Name'
62+ $ResourceGroupName = $ (Get-AzFunctionApp ).Where ({ $_.Name -eq $FunctionName }).ResourceGroupName
63+ if (! $ResourceGroupName ) {
6664 Write-Error " Failed to confirm Azure Function exists in Azure. Please verify and try again. Function Name: $FunctionName " - ErrorAction Stop
6765 }
6866
6967 # ############################################
7068 # ############# REST api call ##############
7169
7270 # # Specifies the REST api call that will be performed
73- $ApiContentType = " application/json"
74- $ApiMethodPost = " Post"
75- $ApiMethodGet = " Get"
76- $ApiMethodPut = " Put"
71+ $ApiContentType = ' application/json'
72+ $ApiMethodPost = ' Post'
73+ $ApiMethodGet = ' Get'
74+ $ApiMethodPut = ' Put'
7775
7876 # # Retrieves the Azure Function URL used to add new manifests to the REST source
7977 Write-Verbose - Message " Retrieving the Azure Function $FunctionName to build out the REST API request."
8078 $FunctionApp = Get-AzFunctionApp - ResourceGroupName $ResourceGroupName - Name $FunctionName
8179
82- $FunctionAppId = $FunctionApp.Id
80+ $FunctionAppId = $FunctionApp.Id
8381 $DefaultHostName = $FunctionApp.DefaultHostName
8482 $FunctionKeyPost = (Invoke-AzResourceAction - ResourceId " $FunctionAppId /functions/ManifestPost" - Action listkeys - Force).default
8583 $FunctionKeyGet = (Invoke-AzResourceAction - ResourceId " $FunctionAppId /functions/ManifestGet" - Action listkeys - Force).default
8684 $FunctionKeyPut = (Invoke-AzResourceAction - ResourceId " $FunctionAppId /functions/ManifestPut" - Action listkeys - Force).default
8785
8886
8987 # # Creates the API Post Header
90- $ApiHeader = New-Object " System.Collections.Generic.Dictionary[[String],[String]]"
91- $ApiHeader.Add (" Accept" , ' application/json' )
88+ $ApiHeader = New-Object ' System.Collections.Generic.Dictionary[[String],[String]]'
89+ $ApiHeader.Add (' Accept' , ' application/json' )
9290
93- $AzFunctionURLBase = " https://" + $DefaultHostName + " /api/packageManifests/"
91+ $AzFunctionURLBase = ' https://' + $DefaultHostName + ' /api/packageManifests/'
9492 }
95- PROCESS
96- {
93+ process {
9794 $Path = [System.IO.Path ]::GetFullPath($Path , $pwd.Path )
9895
9996 # ##############################
10097 # # Gets the content from the Package Manifest (*.JSON, or *.YAML) file for posting to REST source.
101- Write-Verbose - Message " Retrieving a copy of the app Manifest file for submission to WinGet source."
98+ Write-Verbose - Message ' Retrieving a copy of the app Manifest file for submission to WinGet source.'
10299 $ApplicationManifest = Get-WinGetManifest - Path $Path
103- if ($ApplicationManifest.Count -ne 1 ) {
104- Write-Error " Failed to retrieve a proper manifest. Verify and try again."
100+ if ($ApplicationManifest.Count -ne 1 ) {
101+ Write-Error ' Failed to retrieve a proper manifest. Verify and try again.'
105102 return
106103 }
107104
108105 $Manifest = $ApplicationManifest [0 ]
109106 Write-Verbose - Message " Contents of manifest have been retrieved. Package Identifier: $ ( $Manifest.PackageIdentifier ) ."
110107
111108 Write-Verbose - Message " Confirming that the Package ID doesn't already exist in Azure for $ ( $Manifest.PackageIdentifier ) ."
112- $ApiHeader [" x-functions-key" ] = $FunctionKeyGet
109+ $ApiHeader [' x-functions-key' ] = $FunctionKeyGet
113110 $AzFunctionURL = $AzFunctionURLBase + $Manifest.PackageIdentifier
114111 $Response = Invoke-RestMethod $AzFunctionURL - Headers $ApiHeader - Method $ApiMethodGet - ErrorVariable ErrorInvoke
115112
@@ -119,20 +116,19 @@ Function Add-WinGetManifest
119116
120117 $ApiMethod = $ApiMethodPost
121118 $AzFunctionURL = $AzFunctionURLBase
122- $ApiHeader [" x-functions-key" ] = $FunctionKeyPost
123- }
124- else {
119+ $ApiHeader [' x-functions-key' ] = $FunctionKeyPost
120+ } else {
125121 # # Existing manifest retrieved, submit as update existing manifest
126122 Write-Verbose " Found manifest that matched. Package Identifier: $ ( $Manifest.PackageIdentifier ) "
127123
128- if ($Response.Data.Count -gt 1 ) {
124+ if ($Response.Data.Count -gt 1 ) {
129125 Write-Error " Found conflicting manifests. Package Identifier: $ ( $Manifest.PackageIdentifier ) "
130126 return
131127 }
132128
133129 $ApiMethod = $ApiMethodPut
134130 $AzFunctionURL = $AzFunctionURLBase + $Manifest.PackageIdentifier
135- $ApiHeader [" x-functions-key" ] = $FunctionKeyPut
131+ $ApiHeader [' x-functions-key' ] = $FunctionKeyPut
136132
137133 # # Merge with prior manifest
138134 $PriorManifest = [WinGetManifest ]::CreateFromObject($Response.Data [0 ])
@@ -144,7 +140,7 @@ Function Add-WinGetManifest
144140
145141 $Response = Invoke-RestMethod $AzFunctionURL - Headers $ApiHeader - Method $ApiMethod - Body $Manifest.GetJson () - ContentType $ApiContentType - ErrorVariable ErrorInvoke
146142
147- if ($ErrorInvoke ) {
143+ if ($ErrorInvoke ) {
148144 $ErrReturnObject = @ {
149145 AzFunctionURL = $AzFunctionURL
150146 ApiMethod = $ApiMethod
@@ -154,21 +150,19 @@ Function Add-WinGetManifest
154150 InvokeError = $ErrorInvoke
155151 }
156152
157- Write-Error - Message " Failed to add manifest." - TargetObject $ErrReturnObject
158- }
159- else {
153+ Write-Error - Message ' Failed to add manifest.' - TargetObject $ErrReturnObject
154+ } else {
160155 if ($Response.Data.Count -ne 1 ) {
161156 Write-Warning " Returned conflicting manifests after adding the manifest. Package Identifier: $ ( $Manifest.PackageIdentifier ) "
162157 }
163158
164- foreach ($ResponseData in $Response.Data ){
159+ foreach ($ResponseData in $Response.Data ) {
165160 Write-Verbose " Parsing through the returned results: $ResponseData "
166161 $Return += [WinGetManifest ]::CreateFromObject($ResponseData )
167162 }
168163 }
169164 }
170- END
171- {
165+ end {
172166 return $Return
173167 }
174- }
168+ }
0 commit comments