Skip to content

Commit 966028a

Browse files
Reset functionality of the Get-WinGetManifest (File) functionality (#80)
* Reset functionality of the Get-WinGetManifest (File) functionality Single Application Manifest(s) only. * Update descriptions * Typo Co-authored-by: jamespik <[email protected]>
1 parent 18af2de commit 966028a

File tree

1 file changed

+92
-52
lines changed

1 file changed

+92
-52
lines changed

Tools/PowershellModule/src/Library/Get-WinGetManifest.ps1

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Function Get-WinGetManifest
66
<#
77
.SYNOPSIS
88
Connects to the specified source Rest API, or local file system path to retrieve the application Manifests, returning
9-
an array of all Manifests found. Allows for retrieving results based on the name when targetting the Rest APIs.
9+
the manifest found. Allows for retrieving results based on the package identifier when targetting the Rest APIs.
1010
1111
.DESCRIPTION
1212
Connects to the specified source Rest API, or local file system path to retrieve the application Manifests, returning
13-
an array of all Manifests found. Allows for retrieving results based on the name.
13+
an array of all Manifests found. Allows for retrieving results based on the package identifier.
1414
1515
The following Azure Modules are used by this script:
1616
Az.Resources --> Invoke-AzResourceAction
@@ -19,7 +19,13 @@ Function Get-WinGetManifest
1919
Az.Functions --> Get-AzFunctionApp
2020
2121
.PARAMETER Path
22-
Path to a file (*.json) or folder containing *.yaml or *.json files.
22+
Points to either a folder containing a specific application's manifest of type .json or .yaml or to a specific .json or .yaml file.
23+
If you are processing a multi-file manifest, point to the folder that contains all yamls. Note: all yamls within the folder must be part of
24+
the same application.
25+
26+
.PARAMETER JSON
27+
A JSON String containing a single application's rest source package manifest that will be merged with locally processed files. This is
28+
used by the script infrastructure internally and is NOT expected to be useful to an end user using this command.
2329
2430
.PARAMETER URL
2531
Web URL to the host site containing the Rest APIs with access key (if required).
@@ -95,12 +101,19 @@ Function Get-WinGetManifest
95101
throw "Failed to connect to Azure. Please run Connect-AzAccount to connect to Azure, or re-run the cmdlet and enter your credentials."
96102
}
97103

104+
###############################
105+
## Verify Azure Resources Exist
98106
## Sets variables as if the Azure Function Name was provided.
107+
99108
$AzureResourceGroupName = $(Get-AzFunctionApp).Where({$_.Name -eq $FunctionName}).ResourceGroupName
100109

101-
###############################
102-
## Verify Azure Resources Exist
103-
$Result = Test-AzureResource -FunctionName $FunctionName -ResourceGroup $AzureResourceGroupName
110+
if($AzureResourceGroupName) {
111+
$Result = Test-AzureResource -FunctionName $FunctionName -ResourceGroup $AzureResourceGroupName
112+
}
113+
else {
114+
throw "Unable to locate Function (""$FunctionName"") in the Azure Tenant."
115+
}
116+
104117
if(!$Result) {
105118
throw "Failed to confirm resources exist in Azure. Please verify and try again."
106119
}
@@ -124,44 +137,93 @@ Function Get-WinGetManifest
124137
"File" {
125138
$ManifestFileExists = Test-Path -Path $Path
126139

127-
IF(!$ManifestFileExists) {
140+
if(!$ManifestFileExists) {
141+
## The target path does not exist
128142
$ErrReturnObject = @{
129143
FilePath = $Path
130144
ManifestFileExists = $ManifestFileExists
131145
}
132146

133147
Write-Error -Message "Target path did not point to an object." -TargetObject $ErrReturnObject
134-
Return
148+
return
135149
}
136150

137151
$PathProperties = Get-ItemProperty $Path
138152
$ManifestFile = ""
139153

140-
## If $Path variable is pointing at a directory
141-
if($PathProperties.Attributes -eq "Directory") {
154+
if($PathProperties.Attributes -like "*Directory*") {
155+
## $Path variable is pointing at a directory
142156
$PathChildItemsJSON = Get-ChildItem -Path $Path -Filter "*.json"
143157
$PathChildItemsYAML = Get-ChildItem -Path $Path -Filter "*.yaml"
144158

145159
$VerboseMessage = "Path pointed to a directory, found $($PathChildItemsJSON.count) JSON files, and $($PathChildItemsYAML.count) YAML files."
146160
Write-Verbose -Message $VerboseMessage
147161

148-
$ApplicationManifest = ""
149-
$ManifestFile = Get-Item -Path $Path
150-
$ManifestFileType = "Directory"
162+
## Validating found objects
163+
if($PathChildItemsJSON.count -eq 0 -and $PathChildItemsYAML.count -eq 0) {
164+
## No JSON or YAML files were found in the directory.
165+
$ErrorMessage = "Directory does not contain any combination of JSON and YAML files."
166+
$ErrReturnObject = @{
167+
JSONFiles = $PathChildItemsJSON
168+
YAMLFiles = $PathChildItemsYAML
169+
JSONCount = $PathChildItemsJSON.count
170+
YAMLCount = $PathChildItemsYAML.count
171+
}
172+
173+
$ManifestFileType = "Error"
174+
Write-Error -Message $ErrorMessage -TargetObject $ErrReturnObject
175+
}
176+
elseif($PathChildItemsJSON.count -gt 0 -and $PathChildItemsYAML.count -gt 0) {
177+
## A combination of JSON and YAML Files were found.
178+
$ErrorMessage = "Directory contains a combination of JSON and YAML files."
179+
$ErrReturnObject = @{
180+
JSONFiles = $PathChildItemsJSON
181+
YAMLFiles = $PathChildItemsYAML
182+
JSONCount = $PathChildItemsJSON.count
183+
YAMLCount = $PathChildItemsYAML.count
184+
}
185+
186+
$ManifestFileType = "Error"
187+
Write-Error -Message $ErrorMessage -TargetObject $ErrReturnObject
188+
}
189+
elseif($PathChildItemsJSON.count -gt 1) {
190+
## More than one Application's JSON file was found.
191+
$ErrorMessage = "Directory contains more than one JSON file."
192+
$ErrReturnObject = @{
193+
JSONFiles = $PathChildItemsJSON
194+
YAMLFiles = $PathChildItemsYAML
195+
JSONCount = $PathChildItemsJSON.count
196+
YAMLCount = $PathChildItemsYAML.count
197+
}
198+
199+
$ManifestFileType = "Error"
200+
Write-Error -Message $ErrorMessage -TargetObject $ErrReturnObject
201+
}
202+
elseif($PathChildItemsJSON.count -eq 1) {
203+
## Single JSON has been found in the target folder.
204+
Write-Verbose -Message "Single JSON has been found in the specified directory."
205+
$ManifestFile = $PathChildItemsJSON
206+
$ApplicationManifest = Get-Content -Path $PathChildItemsJSON.FullName -Raw
207+
$ManifestFileType = $PathChildItemsJSON.Extension
208+
}
209+
elseif($PathChildItemsYAML.count -gt 0) {
210+
Write-Verbose -Message "Single YAML has been found in the specified directory."
211+
## YAML has been found in the target folder.
212+
$ManifestFile = $PathChildItemsYAML
213+
$ManifestFileType = ".yaml"
214+
$ApplicationManifest = Get-Content -Path $PathChildItemsYAML[0].FullName -Raw
215+
}
151216
}
152-
## If $Path variable is pointing at a file
153217
else {
154-
## Single file was provided
218+
## $Path variable is pointing at a file
155219
Write-Verbose -Message "Retrieving the Application Manifest for: $Path"
156220

157-
if($ManifestFileExists) {
158-
## Gets the Manifest object and contents of the Manifest - identifying the manifest file extension.
159-
$ApplicationManifest = Get-Content -Path $Path -Raw
160-
$ManifestFile = Get-Item -Path $Path
161-
$ManifestFileType = $ManifestFile.Extension
221+
## Gets the Manifest object and contents of the Manifest - identifying the manifest file extension.
222+
$ApplicationManifest = Get-Content -Path $Path -Raw
223+
$ManifestFile = Get-Item -Path $Path
224+
$ManifestFileType = $ManifestFile.Extension
162225

163-
Write-Verbose -Message "Retrieved content from the manifest ($($ManifestFile.Name))."
164-
}
226+
Write-Verbose -Message "Retrieved content from the manifest ($($ManifestFile.Name))."
165227
}
166228
}
167229
}
@@ -211,44 +273,22 @@ Function Get-WinGetManifest
211273
## If the path resolves to a YAML file
212274
".yaml" {
213275
## Directory - *.yaml files included within.
214-
$Result = Test-WinGetManifest -Manifest $ApplicationManifest
215-
if($Result) {
216-
IF($WinGetDesktopAppInstallerLibLoaded) {
217-
Write-Verbose -Message "YAML Files have been found in the target directory. Building a JSON manifest with found files."
218-
if($Json){
219-
$Return += [Microsoft.WinGet.RestSource.PowershellSupport.YamlToRestConverter]::AddManifestToPackageManifest($Path, $JSON.GetJson());
220-
}
221-
else{
222-
$Return += [Microsoft.WinGet.RestSource.PowershellSupport.YamlToRestConverter]::AddManifestToPackageManifest($Path, "");
223-
}
224-
}
225-
226-
## Sets the return result to be the contents of the JSON file if the Manifest test passed.
227-
$Return = $ApplicationManifest
228-
Write-Verbose -Message "Returned Manifest from YAML file: $($Return.PackageIdentifier)"
229-
}
230-
}
231-
## If the path resolves to a Directory
232-
"Directory"{
233-
## If a directory is provided, parse through the directory items for Manifest files
234-
if($PathChildItemsJSON.Count -gt 0) {
235-
Write-Verbose -Message "Multiple JSON files have been found. Will retrieve all WinGet manifests in the directory."
236-
foreach ($Item in $PathChildItemsJSON) {
237-
## Re-runs current Function for each individual file.
238-
$Return += [WinGetManifest]::New($(Get-WinGetManifest -Path $Item.FullName))
239-
}
240-
241-
Write-Verbose "Found ($($Return.Count)) Manifests that matched."
242-
}
243-
if($PathChildItemsYAML.Count -gt 0) {
276+
if($WinGetDesktopAppInstallerLibLoaded) {
244277
Write-Verbose -Message "YAML Files have been found in the target directory. Building a JSON manifest with found files."
245278
if($Json){
246279
$Return += [Microsoft.WinGet.RestSource.PowershellSupport.YamlToRestConverter]::AddManifestToPackageManifest($Path, $JSON.GetJson());
247280
}
248281
else{
249282
$Return += [Microsoft.WinGet.RestSource.PowershellSupport.YamlToRestConverter]::AddManifestToPackageManifest($Path, "");
250283
}
284+
285+
Write-Verbose -Message "Returned Manifest from YAML file: $($Return.PackageIdentifier)"
251286
}
287+
else {
288+
Write-Error -Message "Unable to process YAML files. Re-import the module to reload the required dependencies." -Category ResourceUnavailable
289+
}
290+
}
291+
"Error" {
252292
}
253293
default {
254294
if($ManifestFileExists) {

0 commit comments

Comments
 (0)