winget/InstallApps.ps1
2024-09-08 02:57:43 +00:00

137 lines
5.4 KiB
PowerShell

# Get the current directory where the script is located
$currentDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Define the path for logging installed applications
$logFilePath = "/winget/installedApps.txt"
# Function to display a menu and get user input
function Show-Menu {
param (
[string]$prompt, # The prompt message to display
[array]$options # The list of options to present to the user
)
# Display the prompt and options
Write-Host $prompt
for ($i = 0; $i -lt $options.Length; $i++) {
Write-Host "$($i + 1). $($options[$i])" # Display each option with a number
}
# Get the user's choice
$choice = Read-Host "Select an option (1-$($options.Length))"
return $choice
}
# Function to allow multiple selections using the spacebar
function Select-MultipleOptions {
param (
[string]$prompt, # The prompt message to display
[array]$options # The list of options to present to the user for multiple selection
)
Write-Host $prompt
$selectedOptions = @() # Array to hold the selected options
# Display options with selection state (checked/unchecked)
for ($i = 0; $i -lt $options.Length; $i++) {
Write-Host "$($i + 1). $($options[$i]) [ ]" # Initial unchecked state for each option
}
Write-Host "Use spacebar to select/unselect and Enter to confirm."
# Capture user input for multiple selection
while ($true) {
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") # Read key input from the user
if ($key.Character -match '\d') { # Check if the input is a number
$index = [int]$key.Character - 1 # Convert character to index
if ($index -ge 0 -and $index -lt $options.Length) { # Validate index range
# Toggle selection state for the chosen option
if ($selectedOptions.Contains($index)) {
$selectedOptions.Remove($index) # Remove from selected options if already selected
} else {
$selectedOptions.Add($index) # Add to selected options if not already selected
}
}
} elseif ($key.VirtualKeyCode -eq 13) { # Enter key to confirm selection
break
}
}
# Return the list of selected options
return $selectedOptions | ForEach-Object { $options[$_] }
}
# Function to log installed applications
function Log-InstalledApp {
param (
[string]$appName # The name of the installed application
)
# Append the application name to the log file
Add-Content -Path $logFilePath -Value $appName
}
# Main script logic starts here
$mainMenuChoice = Show-Menu "Do you want to:" @("Update all apps", "Install apps")
# Handle the main menu choices
switch ($mainMenuChoice) {
1 {
# Option 1: Update all apps
Write-Host "Updating all apps..."
Start-Process winget -ArgumentList "upgrade --all" -NoNewWindow -Wait # Run winget upgrade command
}
2 {
# Option 2: Install apps
$installChoice = Show-Menu "Choose an installation option:" @("Fresh install of ALL apps", "Select package groups", "Install select packages only")
# Handle the sub-menu choices for app installation
switch ($installChoice) {
1 {
# Sub-option 2a: Fresh install of ALL apps
Write-Host "Performing fresh install of all apps..."
# Execute all scripts in the groups folder
Get-ChildItem -Path "$currentDir\install\groups\*.ps1" | ForEach-Object {
& $_.FullName
# Log the installed app (assuming script file names are app names)
Log-InstalledApp -appName $_.Name
}
}
2 {
# Sub-option 2b: Select package groups for installation
# Get the list of group scripts available in the groups folder
$packageGroups = Get-ChildItem -Path "$currentDir\install\groups\*.ps1" | Select-Object -ExpandProperty Name
# Allow the user to select multiple groups
$selectedGroups = Select-MultipleOptions "Select package groups to install:" $packageGroups
# Run the selected group scripts
foreach ($group in $selectedGroups) {
Write-Host "Installing group: $group"
& "$currentDir\install\groups\$group"
# Log the installed group (assuming group script names are group names)
Log-InstalledApp -appName $group
}
}
3 {
# Sub-option 2c: Install select packages only
# Read the list of individual packages from the file
$packages = Get-Content -Path "$currentDir\install\individual\list.ps1"
# Allow the user to select multiple packages
$selectedPackages = Select-MultipleOptions "Select individual packages to install:" $packages
# Run the selected package installation scripts
foreach ($package in $selectedPackages) {
Write-Host "Installing package: $package"
& $package
# Log the installed package
Log-InstalledApp -appName $package
}
}
}
}
default {
# Handle invalid choice
Write-Host "Invalid choice, please try again."
}
}