Skip to content

Commit f08727b

Browse files
committed
Improve function Get-Packages-Categories
Make the function more readable, it returns a hashtable of custom PSObjects instead of strings.
1 parent f9a7679 commit f08727b

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

install.ps1

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ if (-not $noGui.IsPresent) {
481481
}
482482

483483
function Get-Packages-Categories {
484-
$vmPackagesUrl = "https://www.myget.org/F/vm-packages/api/v2/Packages?$filter=IsLatestVersion%20eq%20true"
484+
# MyGet API
485+
$vmPackagesUrl = "https://www.myget.org/F/vm-packages/api/v2/Packages"
485486
$vmPackagesFile = "${Env:VM_COMMON_DIR}\vm-packages.xml"
486487
$packagesByCategory=@{}
487488
do {
@@ -504,18 +505,26 @@ if (-not $noGui.IsPresent) {
504505

505506
# Extract package information from the XML
506507
$vm_packages.SelectNodes("//atom:entry", $ns) | ForEach-Object {
507-
$isLatestVersion = $_.SelectSingleNode("m:properties/d:IsLatestVersion", $ns).InnerText
508-
if ($isLatestVersion -eq "true") {
509-
$packageName = $_.SelectSingleNode("m:properties/d:Id", $ns).InnerText
508+
$isLatestVersion = $_.SelectSingleNode("m:properties/d:IsLatestVersion", $ns).InnerText
509+
$category = $_.SelectSingleNode("m:properties/d:Tags", $ns).InnerText
510+
511+
#we select only packages that have the latest version and contain a category
512+
if ($isLatestVersion -eq "true" -and $category) {
513+
$packageName = $_.SelectSingleNode("m:properties/d:Id", $ns).InnerText
510514
$description = $_.SelectSingleNode("m:properties/d:Description", $ns).InnerText
511-
$category = $_.SelectSingleNode("m:properties/d:Tags", $ns).InnerText
515+
516+
if (-not ($packagesByCategory.ContainsKey($category))) {
517+
# Initialize as an empty array
518+
$packagesByCategory[$category] = @()
519+
}
520+
# category should not be empty, this condition should be removed after all the old packages in nuget are removed
512521
if ($category -ne ""){
513-
if (-not ($packagesByCategory.ContainsKey($category))) {
514-
$packagesByCategory[$category] = ""
515-
}
516-
$packagesByCategory[$category] += $packageName + ":" + $description + "`n"
522+
$packagesByCategory[$category] += [PSCustomObject]@{
523+
PackageName = $packageName
524+
PackageDescription = $description
525+
}
517526
}
518-
}
527+
}
519528
}
520529
# Check if there is a next link in the XML and set the API URL to that link if it exists
521530
$nextLink = $vm_packages.SelectSingleNode("//atom:link[@rel='next']/@href", $ns)
@@ -769,17 +778,7 @@ if (-not $noGui.IsPresent) {
769778
param (
770779
[string]$category
771780
)
772-
$packages = @()
773-
$lines = $packagesByCategory[$category] -split "`n"
774-
foreach ($line in $lines) {
775-
if ($line.Trim()){
776-
$package, $description = $line -split '\|'
777-
$packages += [PSCustomObject]@{
778-
PackageName = $package
779-
PackageDescription = $description
780-
}
781-
}
782-
}
781+
$packages = $packagesByCategory[$category]
783782
return $packages
784783
}
785784

@@ -822,8 +821,8 @@ if (-not $noGui.IsPresent) {
822821
$allPackagesButton.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
823822
$allPackagesButton.Add_Click({Select-AllPackages})
824823

825-
$clearPackagesButton = New-Object system.Windows.Forms.Button
826-
$clearPackagesButton.text = "Select All"
824+
$clearPackagesButton = New-Object system.Windows.Forms.Button
825+
$clearPackagesButton.text = "Deselect All"
827826
$clearPackagesButton.AutoSize = $true
828827
$clearPackagesButton.location = New-Object System.Drawing.Point(210,750)
829828
$clearPackagesButton.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
@@ -877,9 +876,6 @@ if (-not $noGui.IsPresent) {
877876
$checkBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',9)
878877
$checkBox.AutoSize = $true
879878
$checkBox.Location = New-Object System.Drawing.Point(40, $verticalPosition2)
880-
if ($package.PackageName -in $packagesToInstall){
881-
$checkBox.Checked = $true
882-
}
883879
$checkBox.Name = "checkBox$numCheckBoxPackages"
884880
$checkboxesPackages.Add($checkBox)
885881
$Panel_Categories.Controls.Add($checkBox)
@@ -890,6 +886,10 @@ if (-not $noGui.IsPresent) {
890886
$numCategories ++
891887
}
892888

889+
#select packages that are in the config.xml
890+
Set-InitialPackages
891+
892+
893893
$FormCategories.controls.AddRange(@($categories_label,$Panel_Categories,$ContinueButton,$resetButton,$allPackagesButton,$cancelButton,$clearPackagesButton))
894894
$FormCategories.Add_Shown({$FormCategories.Activate()})
895895
$resultCategories = $FormCategories.ShowDialog()

0 commit comments

Comments
 (0)