From 802c901a7f843e2eae3b21ed1733295a67d2cd44 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 12:31:03 -0600 Subject: [PATCH 01/16] Remove unused variable --- src/prototype/WingetCreateMakeDSC.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 29745b25..542e5a8a 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -34,7 +34,6 @@ if ($null -eq (Get-InstalledModule -Name powershell-yaml)) [System.Collections.ArrayList]$finalPackages = @() $configurationVersion = "0.2.0" -$continue = $true do { $appId = Read-Host "What is the Winget ID, or name of the package you want to add to the configuration file?" From da077112f209709f858c64ceed7a4f19fe7ac162 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 12:38:58 -0600 Subject: [PATCH 02/16] Use Pipeline for assigning indicies --- src/prototype/WingetCreateMakeDSC.ps1 | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 542e5a8a..d3efe310 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -41,13 +41,9 @@ do if ($findResult.count -ne 0) { - $Index=1 - foreach ($package in $findResult) - { - $packageDetails = "[$($Index)] $($package.Name) | $($package.Id) | $($package.Version)" - Write-Host $packageDetails - $index++ - } + # Assign an index to each package + $findResult | ForEach-Object { $i=1 } { Add-Member -InputObject $_ -NotePropertyName Index -NotePropertyValue $i; $i++ } + $findResult | Select-Object -Property Index,Name,Id,Version | Out-Host $selection = -1 $packageSelected = $false From a8ae8bd3634dd8db649bf96bef8bdb0dbb907175 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 12:40:23 -0600 Subject: [PATCH 03/16] Improve PS7 check --- src/prototype/WingetCreateMakeDSC.ps1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index d3efe310..7c669f46 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -1,10 +1,9 @@ # This script is a prototype for quickly creating DSC files. #Powershell 7 Required -$hostdata=host -if ($hostdata.version.major -lt 7) { +if ($(host).version.major -lt 7) { Write-host "This script requires powershell 7. You can update powershell by typing winget install Microsoft.Powershell." -ForegroundColor red - [Environment]::Exit(1) + Exit(1) } #Set output encoding to UTF-8 From 618454656dad919243cdac9472651f5791260845 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 12:44:11 -0600 Subject: [PATCH 04/16] Hide error on installed module check --- src/prototype/WingetCreateMakeDSC.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 7c669f46..4abc515b 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -9,7 +9,7 @@ if ($(host).version.major -lt 7) { #Set output encoding to UTF-8 $OutputEncoding = [ System.Text.Encoding]::UTF8 -if ($null -eq (Get-InstalledModule -Name Microsoft.Winget.Client)) +if ($null -eq (Get-InstalledModule -Name Microsoft.Winget.Client -ErrorAction 'SilentlyContinue')) { try { Install-Module Microsoft.Winget.Client } catch { @@ -19,7 +19,7 @@ if ($null -eq (Get-InstalledModule -Name Microsoft.Winget.Client)) } } -if ($null -eq (Get-InstalledModule -Name powershell-yaml)) +if ($null -eq (Get-InstalledModule -Name powershell-yaml -ErrorAction 'SilentlyContinue')) { try { Install-Module powershell-yaml From 70457855c66e23e7cd2ae6753e1b5d96dfa2bca0 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 12:53:06 -0600 Subject: [PATCH 05/16] Fix source selection for package --- src/prototype/WingetCreateMakeDSC.ps1 | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 4abc515b..8e668214 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -63,17 +63,7 @@ do } $selectedPackage = $findResult[$selection - 1] - - #Specify Source - #Winget currently has 2 sources. If the ID contains a period, we will assume winget. - #otherwise it is the MSSTORE. We are not accounting for private REPOs at this time. - If ($selectedPackage.Id -like "*.*") { - $source="winget" - } else { - $source="msstore" - } - - $unit = @{"resource" = "Microsoft.WinGet.DSC/WinGetPackage"; "directives" = @{"description" = $selectedPackage.Name; "allowPrerelease" = $true; }; "settings" = @{"id" = $selectedPackage.Id; "source"=$source }} + $unit = @{"resource" = "Microsoft.WinGet.DSC/WinGetPackage"; "directives" = @{"description" = $selectedPackage.Name; "allowPrerelease" = $true; }; "settings" = @{"id" = $selectedPackage.Id; "source"=$selectedPackage.Source }} $tempvar = $finalPackages.Add($unit) write-host Added $selectedPackage.Name -ForegroundColor blue From 963a9a902afb4892e4a0d8c472cfa978f09e84d7 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 12:58:45 -0600 Subject: [PATCH 06/16] Improve range check for selection --- src/prototype/WingetCreateMakeDSC.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 8e668214..74f59146 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -42,7 +42,7 @@ do { # Assign an index to each package $findResult | ForEach-Object { $i=1 } { Add-Member -InputObject $_ -NotePropertyName Index -NotePropertyValue $i; $i++ } - $findResult | Select-Object -Property Index,Name,Id,Version | Out-Host + $findResult | Select-Object -Property Index,Name,Id,Version | Format-Table | Out-Host $selection = -1 $packageSelected = $false @@ -52,7 +52,7 @@ do # TODO: We should capture against bad value. "string" # TODO: We should allow user to skip. Maybe hit S or X. $selection = [int](Read-Host "Input the number of the package you want to select") - if (($selection -gt $findResult.count) -or ($selection -lt 1)) + if ($selection -notin $findResult.Index) { Write-Host "Selection is out of range, try again." } @@ -62,7 +62,7 @@ do } } - $selectedPackage = $findResult[$selection - 1] + $selectedPackage = $findResult.Where({$_.Index -eq $selection}) $unit = @{"resource" = "Microsoft.WinGet.DSC/WinGetPackage"; "directives" = @{"description" = $selectedPackage.Name; "allowPrerelease" = $true; }; "settings" = @{"id" = $selectedPackage.Id; "source"=$selectedPackage.Source }} $tempvar = $finalPackages.Add($unit) write-host Added $selectedPackage.Name -ForegroundColor blue From e2b3181ed301b1091239cd7033208c06d0b9c8cb Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 12:59:28 -0600 Subject: [PATCH 07/16] Cast to void instead of unused var --- src/prototype/WingetCreateMakeDSC.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 74f59146..62216167 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -64,7 +64,7 @@ do $selectedPackage = $findResult.Where({$_.Index -eq $selection}) $unit = @{"resource" = "Microsoft.WinGet.DSC/WinGetPackage"; "directives" = @{"description" = $selectedPackage.Name; "allowPrerelease" = $true; }; "settings" = @{"id" = $selectedPackage.Id; "source"=$selectedPackage.Source }} - $tempvar = $finalPackages.Add($unit) + [void]$finalPackages.Add($unit) write-host Added $selectedPackage.Name -ForegroundColor blue From db69cb803302c6ce938d0fc779c6f8c7791da55c Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 13:10:04 -0600 Subject: [PATCH 08/16] Improve file output and encoding --- src/prototype/WingetCreateMakeDSC.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 62216167..280960fb 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -31,7 +31,9 @@ if ($null -eq (Get-InstalledModule -Name powershell-yaml -ErrorAction 'SilentlyC } [System.Collections.ArrayList]$finalPackages = @() -$configurationVersion = "0.2.0" +$configurationVersion = "0.2.0" +$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False +$DSCHeader = "# yaml-language-server: `$schema=https://aka.ms/configuration-dsc-schema/$($configurationVersion)" do { @@ -79,7 +81,8 @@ Write-host $fileName = Read-Host "Name of the configuration file (without extension)" $filePath = Join-Path -Path (Get-Location) -ChildPath "$($fileName).winget" -ConvertTo-Yaml @{"properties"= @{"resources"=$finalPackages; "configurationVersion"= $configurationVersion}} -OutFile $filePath -Force +$rawYaml = ConvertTo-Yaml @{"properties"= @{"resources"=$finalPackages; "configurationVersion"= $configurationVersion}} +[System.IO.File]::WriteAllLines($filePath, @($DSCHeader,'',$rawYaml.trim()), $Utf8NoBomEncoding) Write-Host Write-Host Testing resulting file. -ForegroundColor yellow From e093ac6c4a0604688af7a25b76074864b9f392e8 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 13:12:21 -0600 Subject: [PATCH 09/16] Lint file --- src/prototype/WingetCreateMakeDSC.ps1 | 82 ++++++++++++--------------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 280960fb..5b770bac 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -1,97 +1,87 @@ # This script is a prototype for quickly creating DSC files. #Powershell 7 Required -if ($(host).version.major -lt 7) { - Write-host "This script requires powershell 7. You can update powershell by typing winget install Microsoft.Powershell." -ForegroundColor red +if ($(Get-host).version.major -lt 7) { + Write-Host 'This script requires powershell 7. You can update powershell by typing winget install Microsoft.Powershell.' -ForegroundColor red Exit(1) } #Set output encoding to UTF-8 $OutputEncoding = [ System.Text.Encoding]::UTF8 -if ($null -eq (Get-InstalledModule -Name Microsoft.Winget.Client -ErrorAction 'SilentlyContinue')) -{ - try { Install-Module Microsoft.Winget.Client +if ($null -eq (Get-InstalledModule -Name Microsoft.Winget.Client -ErrorAction 'SilentlyContinue')) { + try { + Install-Module Microsoft.Winget.Client } catch { - #Pass the exception - throw [System.Net.WebException]::new("Error retrieving powershell module: Microsoft.Winget.Client. Check that you have installed the Windows Package Manager modules correctly.", $_.Exception) - #bugbug is this good enough? + #Pass the exception + throw [System.Net.WebException]::new('Error retrieving powershell module: Microsoft.Winget.Client. Check that you have installed the Windows Package Manager modules correctly.', $_.Exception) + #bugbug is this good enough? } } -if ($null -eq (Get-InstalledModule -Name powershell-yaml -ErrorAction 'SilentlyContinue')) -{ +if ($null -eq (Get-InstalledModule -Name powershell-yaml -ErrorAction 'SilentlyContinue')) { try { Install-Module powershell-yaml } catch { - #Pass the exception - throw [System.Net.WebException]::new("Error retrieving powershell module: powershell-yaml. Check that you have installed the Windows Package Manager modules correctly.", $_.Exception) - #bugbug is this good enough? + #Pass the exception + throw [System.Net.WebException]::new('Error retrieving powershell module: powershell-yaml. Check that you have installed the Windows Package Manager modules correctly.', $_.Exception) + #bugbug is this good enough? } } [System.Collections.ArrayList]$finalPackages = @() -$configurationVersion = "0.2.0" +$configurationVersion = '0.2.0' $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False $DSCHeader = "# yaml-language-server: `$schema=https://aka.ms/configuration-dsc-schema/$($configurationVersion)" -do -{ - $appId = Read-Host "What is the Winget ID, or name of the package you want to add to the configuration file?" +do { + $appId = Read-Host 'What is the Winget ID, or name of the package you want to add to the configuration file?' $findResult = Find-WinGetPackage $appId - if ($findResult.count -ne 0) - { + if ($findResult.count -ne 0) { # Assign an index to each package - $findResult | ForEach-Object { $i=1 } { Add-Member -InputObject $_ -NotePropertyName Index -NotePropertyValue $i; $i++ } - $findResult | Select-Object -Property Index,Name,Id,Version | Format-Table | Out-Host + $findResult | ForEach-Object { $i = 1 } { Add-Member -InputObject $_ -NotePropertyName Index -NotePropertyValue $i; $i++ } + $findResult | Select-Object -Property Index, Name, Id, Version | Format-Table | Out-Host $selection = -1 $packageSelected = $false - while (-not($packageSelected)) - { - write-host + while (-not($packageSelected)) { + Write-Host # TODO: We should capture against bad value. "string" # TODO: We should allow user to skip. Maybe hit S or X. - $selection = [int](Read-Host "Input the number of the package you want to select") - if ($selection -notin $findResult.Index) - { - Write-Host "Selection is out of range, try again." - } - else - { + $selection = [int](Read-Host 'Input the number of the package you want to select') + if ($selection -notin $findResult.Index) { + Write-Host 'Selection is out of range, try again.' + } else { $packageSelected = $true } } - $selectedPackage = $findResult.Where({$_.Index -eq $selection}) - $unit = @{"resource" = "Microsoft.WinGet.DSC/WinGetPackage"; "directives" = @{"description" = $selectedPackage.Name; "allowPrerelease" = $true; }; "settings" = @{"id" = $selectedPackage.Id; "source"=$selectedPackage.Source }} + $selectedPackage = $findResult.Where({ $_.Index -eq $selection }) + $unit = @{'resource' = 'Microsoft.WinGet.DSC/WinGetPackage'; 'directives' = @{'description' = $selectedPackage.Name; 'allowPrerelease' = $true; }; 'settings' = @{'id' = $selectedPackage.Id; 'source' = $selectedPackage.Source } } [void]$finalPackages.Add($unit) - write-host Added $selectedPackage.Name -ForegroundColor blue + Write-Host Added $selectedPackage.Name -ForegroundColor blue + } else { + Write-Host 'No package found matching input criteria.' -ForegroundColor DarkYellow } - else - { - Write-Host "No package found matching input criteria." -ForegroundColor DarkYellow - } -} while ($(Read-Host "Would you like to add another package? [y/n]") -eq 'y') +} while ($(Read-Host 'Would you like to add another package? [y/n]') -eq 'y') -Write-host -$fileName = Read-Host "Name of the configuration file (without extension)" +Write-Host +$fileName = Read-Host 'Name of the configuration file (without extension)' $filePath = Join-Path -Path (Get-Location) -ChildPath "$($fileName).winget" -$rawYaml = ConvertTo-Yaml @{"properties"= @{"resources"=$finalPackages; "configurationVersion"= $configurationVersion}} -[System.IO.File]::WriteAllLines($filePath, @($DSCHeader,'',$rawYaml.trim()), $Utf8NoBomEncoding) +$rawYaml = ConvertTo-Yaml @{'properties' = @{'resources' = $finalPackages; 'configurationVersion' = $configurationVersion } } +[System.IO.File]::WriteAllLines($filePath, @($DSCHeader, '', $rawYaml.trim()), $Utf8NoBomEncoding) Write-Host -Write-Host Testing resulting file. -ForegroundColor yellow +Write-Host Testing resulting file. -ForegroundColor yellow (&winget configure --help) > $null if ($LASTEXITCODE -eq 0) { winget configure validate --file $filePath -} -else { +} else { Write-Host "'winget configure' is not available, skipping validation." -ForegroundColor Yellow } From d75c22bca597148d8a7a2b1ca1686db4a1feb70c Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 13:22:44 -0600 Subject: [PATCH 10/16] Improve dependencies with custom exception type --- src/prototype/WingetCreateMakeDSC.ps1 | 33 +++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 5b770bac..e60b0bba 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -6,26 +6,41 @@ if ($(Get-host).version.major -lt 7) { Exit(1) } +# Create a custom exception type for our dependency management +class UnmetDependencyException : Exception { + UnmetDependencyException([string] $message) : base($message) {} + UnmetDependencyException([string] $message, [Exception] $exception) : base($message, $exception) {} +} + #Set output encoding to UTF-8 $OutputEncoding = [ System.Text.Encoding]::UTF8 -if ($null -eq (Get-InstalledModule -Name Microsoft.Winget.Client -ErrorAction 'SilentlyContinue')) { +if (-not(Get-Module -ListAvailable -Name Microsoft.Winget.Client)) { try { Install-Module Microsoft.Winget.Client } catch { - #Pass the exception - throw [System.Net.WebException]::new('Error retrieving powershell module: Microsoft.Winget.Client. Check that you have installed the Windows Package Manager modules correctly.', $_.Exception) - #bugbug is this good enough? + # If there was an exception while installing, pass it as an InternalException for further debugging + throw [UnmetDependencyException]::new("'Microsoft.Winget.Client' was not installed successfully", $_.Exception) + } finally { + # Check to be sure it acutally installed + if (-not(Get-Module -ListAvailable -Name Microsoft.Winget.Client)) { + throw [UnmetDependencyException]::new("`Microsoft.Winget.Client` was not found. Check that you have installed the Windows Package Manager modules correctly.") + } } } -if ($null -eq (Get-InstalledModule -Name powershell-yaml -ErrorAction 'SilentlyContinue')) { +if (-not(Get-Module -ListAvailable -Name powershell-yaml)) { try { - Install-Module powershell-yaml + Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force + Install-Module -Name powershell-yaml -Force -Repository PSGallery -Scope CurrentUser } catch { - #Pass the exception - throw [System.Net.WebException]::new('Error retrieving powershell module: powershell-yaml. Check that you have installed the Windows Package Manager modules correctly.', $_.Exception) - #bugbug is this good enough? + # If there was an exception while installing, pass it as an InternalException for further debugging + throw [UnmetDependencyException]::new("'powershell-yaml' was not installed successfully", $_.Exception) + } finally { + # Check to be sure it acutally installed + if (-not(Get-Module -ListAvailable -Name powershell-yaml)) { + throw [UnmetDependencyException]::new("`powershell-yaml` was not found. Check that you have installed the Windows Package Manager modules correctly.") + } } } From f650ac5db7babebfbf34228d9f71cc5a4ddea4cd Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 13:55:04 -0600 Subject: [PATCH 11/16] Allow selecting by Name, ID, or Index, and allow skipping selection --- src/prototype/WingetCreateMakeDSC.ps1 | 57 ++++++++++++++++++--------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index e60b0bba..bbf54234 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -1,7 +1,7 @@ # This script is a prototype for quickly creating DSC files. #Powershell 7 Required -if ($(Get-host).version.major -lt 7) { +if ($(Get-Host).version.major -lt 7) { Write-Host 'This script requires powershell 7. You can update powershell by typing winget install Microsoft.Powershell.' -ForegroundColor red Exit(1) } @@ -12,9 +12,7 @@ class UnmetDependencyException : Exception { UnmetDependencyException([string] $message, [Exception] $exception) : base($message, $exception) {} } -#Set output encoding to UTF-8 -$OutputEncoding = [ System.Text.Encoding]::UTF8 - +# Ensure the Winget PowerShell modules are installed if (-not(Get-Module -ListAvailable -Name Microsoft.Winget.Client)) { try { Install-Module Microsoft.Winget.Client @@ -29,6 +27,7 @@ if (-not(Get-Module -ListAvailable -Name Microsoft.Winget.Client)) { } } +# Ensure the powershell-yaml module is installed if (-not(Get-Module -ListAvailable -Name powershell-yaml)) { try { Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force @@ -50,33 +49,55 @@ $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False $DSCHeader = "# yaml-language-server: `$schema=https://aka.ms/configuration-dsc-schema/$($configurationVersion)" do { - $appId = Read-Host 'What is the Winget ID, or name of the package you want to add to the configuration file?' - $findResult = Find-WinGetPackage $appId + $findResult = Find-WinGetPackage $(Read-Host 'What is the Winget ID, or name of the package you want to add to the configuration file?') if ($findResult.count -ne 0) { # Assign an index to each package $findResult | ForEach-Object { $i = 1 } { Add-Member -InputObject $_ -NotePropertyName Index -NotePropertyValue $i; $i++ } $findResult | Select-Object -Property Index, Name, Id, Version | Format-Table | Out-Host - $selection = -1 $packageSelected = $false while (-not($packageSelected)) { Write-Host - # TODO: We should capture against bad value. "string" - # TODO: We should allow user to skip. Maybe hit S or X. - $selection = [int](Read-Host 'Input the number of the package you want to select') - if ($selection -notin $findResult.Index) { - Write-Host 'Selection is out of range, try again.' + # Prompt user for selection string + $selection = (Read-Host 'Select a package by Index, Name, or Id. Press enter to continue or skip') + $selectedPackage = $null + # If user didn't enter any selection string, set no package as selected and continue + if ( [string]::IsNullOrWhiteSpace($selection) ) { + $packageSelected = $true + } elseif ( $selection -in $findResult.Id ) { + # If the user entered a string which matches the Id, select that package + $selectedPackage = $findResult.Where({ $_.Id -eq $selection }) + $packageSelected = $true + } elseif ( $selection -in $findResult.Name ) { + # If the user entered a string which matches the Name, select that package + # Because names could conflict, take the first item in the list to avoid error + $selectedPackage = $findResult.Where({ $_.Name -eq $selection }) | Select-Object -First 1 + $packageSelected = $true } else { - $packageSelected = $true + # If the name and ID didn't match, try selecting by index + # This needs to be a try-catch to handle converting strings to integers + try { + $selectedPackage = $findResult.Where({ $_.Index -eq [int]$selection }) + # If the user selects an index out of range, don't set no package as selected. Instead, allow for correcting the index + # If the intent is to select no package, the user will be able to skip after being notified the index is out of range + if ($selectedPackage) { + $packageSelected = $true + } else { + Write-Host 'Index out of range, please try again' + } + } catch { + Write-Host 'Invalid entry, please try again' + } } } - $selectedPackage = $findResult.Where({ $_.Index -eq $selection }) - $unit = @{'resource' = 'Microsoft.WinGet.DSC/WinGetPackage'; 'directives' = @{'description' = $selectedPackage.Name; 'allowPrerelease' = $true; }; 'settings' = @{'id' = $selectedPackage.Id; 'source' = $selectedPackage.Source } } - [void]$finalPackages.Add($unit) - Write-Host Added $selectedPackage.Name -ForegroundColor blue - + # If a package was selected, add it to the package list; Otherwise, continue + if ($selectedPackage) { + $unit = @{'resource' = 'Microsoft.WinGet.DSC/WinGetPackage'; 'directives' = @{'description' = $selectedPackage.Name; 'allowPrerelease' = $true; }; 'settings' = @{'id' = $selectedPackage.Id; 'source' = $selectedPackage.Source } } + [void]$finalPackages.Add($unit) + Write-Host Added $selectedPackage.Name -ForegroundColor blue + } } else { Write-Host 'No package found matching input criteria.' -ForegroundColor DarkYellow From dc5ebc62037eebe2b16c92006d36faf8939f9084 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 14:01:25 -0600 Subject: [PATCH 12/16] Fix error message --- src/prototype/WingetCreateMakeDSC.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index bbf54234..195c8cec 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -38,7 +38,7 @@ if (-not(Get-Module -ListAvailable -Name powershell-yaml)) { } finally { # Check to be sure it acutally installed if (-not(Get-Module -ListAvailable -Name powershell-yaml)) { - throw [UnmetDependencyException]::new("`powershell-yaml` was not found. Check that you have installed the Windows Package Manager modules correctly.") + throw [UnmetDependencyException]::new("`powershell-yaml` was not found. Check that you have installed the module correctly.") } } } From 3cfdfdb58c1c26abc182d8cad6ce9e9efdbc6ae6 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 15:18:57 -0600 Subject: [PATCH 13/16] Update src/prototype/WingetCreateMakeDSC.ps1 Co-authored-by: Muhammad Danish --- src/prototype/WingetCreateMakeDSC.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 195c8cec..107c360c 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -112,7 +112,7 @@ $rawYaml = ConvertTo-Yaml @{'properties' = @{'resources' = $finalPackages; 'conf [System.IO.File]::WriteAllLines($filePath, @($DSCHeader, '', $rawYaml.trim()), $Utf8NoBomEncoding) Write-Host -Write-Host Testing resulting file. -ForegroundColor yellow +Write-Host 'Testing resulting file...' -ForegroundColor yellow (&winget configure --help) > $null if ($LASTEXITCODE -eq 0) { From 8d51bd756c10696a9dfe55070714672479168dea Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 15:19:25 -0600 Subject: [PATCH 14/16] Update src/prototype/WingetCreateMakeDSC.ps1 Co-authored-by: Muhammad Danish --- src/prototype/WingetCreateMakeDSC.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 107c360c..9d3b872a 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -96,7 +96,7 @@ do { if ($selectedPackage) { $unit = @{'resource' = 'Microsoft.WinGet.DSC/WinGetPackage'; 'directives' = @{'description' = $selectedPackage.Name; 'allowPrerelease' = $true; }; 'settings' = @{'id' = $selectedPackage.Id; 'source' = $selectedPackage.Source } } [void]$finalPackages.Add($unit) - Write-Host Added $selectedPackage.Name -ForegroundColor blue + Write-Host "Added $($selectedPackage.Name)" -ForegroundColor Blue } } else { From e7b4ae05aced1cebb3cd094016cced95335cca55 Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Sat, 2 Dec 2023 21:11:57 -0600 Subject: [PATCH 15/16] Explicitly set scope on variable --- src/prototype/WingetCreateMakeDSC.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index 9d3b872a..bf58d776 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -53,7 +53,7 @@ do { if ($findResult.count -ne 0) { # Assign an index to each package - $findResult | ForEach-Object { $i = 1 } { Add-Member -InputObject $_ -NotePropertyName Index -NotePropertyValue $i; $i++ } + $findResult | ForEach-Object { $script:i = 1 } { Add-Member -InputObject $_ -NotePropertyName Index -NotePropertyValue $i; $i++ } $findResult | Select-Object -Property Index, Name, Id, Version | Format-Table | Out-Host $packageSelected = $false From 2a135dda7998726e891fbe5907226ecf1fb96b2c Mon Sep 17 00:00:00 2001 From: Kaleb Luedtke Date: Mon, 4 Dec 2023 07:35:13 -0600 Subject: [PATCH 16/16] Trim trailing '.0' from schema header --- src/prototype/WingetCreateMakeDSC.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prototype/WingetCreateMakeDSC.ps1 b/src/prototype/WingetCreateMakeDSC.ps1 index bf58d776..b196ef61 100644 --- a/src/prototype/WingetCreateMakeDSC.ps1 +++ b/src/prototype/WingetCreateMakeDSC.ps1 @@ -46,7 +46,7 @@ if (-not(Get-Module -ListAvailable -Name powershell-yaml)) { [System.Collections.ArrayList]$finalPackages = @() $configurationVersion = '0.2.0' $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False -$DSCHeader = "# yaml-language-server: `$schema=https://aka.ms/configuration-dsc-schema/$($configurationVersion)" +$DSCHeader = "# yaml-language-server: `$schema=https://aka.ms/configuration-dsc-schema/$($configurationVersion -Replace '\.0$','')" do { $findResult = Find-WinGetPackage $(Read-Host 'What is the Winget ID, or name of the package you want to add to the configuration file?')