Skip to content

Commit

Permalink
Install Winget, Chocolatey and WSL on DEMAND (#110 #112)
Browse files Browse the repository at this point in the history
- If the user wants to install something, instead of going directly to an error, install the missing package manager.
- The 1st time installing will prompt a bigger message (I don't know how this happened)
  • Loading branch information
LeDragoX committed Oct 10, 2023
1 parent 7e9743e commit 25a2f01
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
10 changes: 2 additions & 8 deletions WinDebloatTools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@ function Show-GUI() {
$InstallOpenSuse = New-CheckBox -Text "Open SUSE 42" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CheckBoxHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[3] -ElementBefore $InstallKaliLinux
$InstallSles = New-CheckBox -Text "SLES v12" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CheckBoxHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[3] -ElementBefore $InstallOpenSuse
$InstallUbuntu = New-CheckBox -Text "Ubuntu" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CheckBoxHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[3] -ElementBefore $InstallSles
$InstallUbuntu16Lts = New-CheckBox -Text "Ubuntu 16.04 LTS" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CheckBoxHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[3] -ElementBefore $InstallUbuntu
$InstallUbuntu18Lts = New-CheckBox -Text "Ubuntu 18.04 LTS" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CheckBoxHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[3] -ElementBefore $InstallUbuntu16Lts
$InstallUbuntu18Lts = New-CheckBox -Text "Ubuntu 18.04 LTS" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CheckBoxHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[3] -ElementBefore $InstallUbuntu
$InstallUbuntu20Lts = New-CheckBox -Text "Ubuntu 20.04 LTS" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CheckBoxHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[3] -ElementBefore $InstallUbuntu18Lts

$ClDevelopment = New-Label -Text "⌨ Development on Windows" -Width $LayoutT2.PanelElementWidth -Height $LayoutT2.CaptionLabelHeight -LocationX $LayoutT2.PanelElementX -FontSize $LayoutT2.Heading[2] -FontStyle 'Bold' -ElementBefore $InstallUbuntu20Lts
Expand Down Expand Up @@ -491,7 +490,7 @@ function Show-GUI() {
$T2Panel3.Controls.AddRange(@($ClRecordingAndStreaming, $InstallElgatoStreamDeck, $InstallHandBrake, $InstallObsStudio, $InstallStreamlabs))
$T2Panel3.Controls.AddRange(@($ClEmulation, $InstallBSnesHd, $InstallCemu, $InstallDolphin, $InstallKegaFusion, $InstallMGba, $InstallPPSSPP, $InstallProject64, $InstallRetroArch, $InstallRyujinx, $InstallSnes9x))
$T2Panel4.Controls.AddRange(@($ClTextEditors, $InstallJetBrainsToolbox, $InstallNotepadPlusPlus, $InstallVisualStudioCommunity, $InstallVSCode, $InstallVSCodium))
$T2Panel4.Controls.AddRange(@($ClWsl, $InstallWSL, $InstallArchWSL, $InstallDebian, $InstallKaliLinux, $InstallOpenSuse, $InstallSles, $InstallUbuntu, $InstallUbuntu16Lts, $InstallUbuntu18Lts, $InstallUbuntu20Lts))
$T2Panel4.Controls.AddRange(@($ClWsl, $InstallWSL, $InstallArchWSL, $InstallDebian, $InstallKaliLinux, $InstallOpenSuse, $InstallSles, $InstallUbuntu, $InstallUbuntu18Lts, $InstallUbuntu20Lts))
$T2Panel4.Controls.AddRange(@($ClDevelopment, $InstallWindowsTerminal, $InstallNerdFonts, $InstallGitGnupgSshSetup, $InstallAdb, $InstallAndroidStudio, $InstallDockerDesktop, $InstallInsomnia, $InstallJavaJdks, $InstallJavaJre, $InstallMySql, $InstallNodeJs, $InstallNodeJsLts, $InstallPostgreSql, $InstallPython3, $InstallPythonAnaconda3, $InstallRuby, $InstallRubyMsys, $InstallRustGnu, $InstallRustMsvc))

# <===== CLICK EVENTS =====>
Expand Down Expand Up @@ -1501,11 +1500,6 @@ function Show-GUI() {
$InstallUbuntu.CheckState = "Unchecked"
}

If ($InstallUbuntu16Lts.CheckState -eq "Checked") {
$AppsSelected.WSLDistros.Add("Ubuntu-16.04")
$InstallUbuntu16Lts.CheckState = "Unchecked"
}

If ($InstallUbuntu18Lts.CheckState -eq "Checked") {
$AppsSelected.WSLDistros.Add("Ubuntu-18.04")
$InstallUbuntu18Lts.CheckState = "Unchecked"
Expand Down
6 changes: 4 additions & 2 deletions src/lib/package-managers/Install-PackageManager.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ function Install-PackageManager() {
[ScriptBlock] $CheckExistenceBlock,
[Parameter(Position = 2, Mandatory)]
[ScriptBlock] $InstallCommandBlock,
[ScriptBlock] $PostInstallBlock
[ScriptBlock] $PostInstallBlock,
[Switch] $Force
)

Try {
$Err = (Invoke-Expression "$CheckExistenceBlock")
If (($LASTEXITCODE)) { throw $Err } # 0 = False, 1 = True
If (($LASTEXITCODE -or $Force)) { throw $Err } # 0 = False, 1 = True
Write-Status -Types "?", $PackageManagerFullName -Status "$PackageManagerFullName is already installed." -Warning
} Catch {
Write-Status -Types "@", $PackageManagerFullName -Status "FORCE INSTALLING $PackageManagerFullName." -Warning
Write-Status -Types "?", $PackageManagerFullName -Status "$PackageManagerFullName was not found." -Warning
Write-Status -Types "+", $PackageManagerFullName -Status "Downloading and Installing $PackageManagerFullName package manager."

Expand Down
23 changes: 17 additions & 6 deletions src/lib/package-managers/Manage-Chocolatey.psm1
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
Import-Module -DisableNameChecking "$PSScriptRoot\Install-PackageManager.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\Manage-DailyUpgradeJob.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\Manage-Software.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\Title-Templates.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\ui\Show-MessageDialog.psm1"

$Script:DoneTitle = "Information"
$Script:DoneMessage = "Process Completed!"

function Install-Chocolatey() {
[CmdletBinding()]
param (
[Switch] $Force
)

Begin {
$ChocolateyParams = @{
Name = "Chocolatey"
Expand All @@ -18,14 +22,21 @@ function Install-Chocolatey() {
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}
PostInstallBlock = { choco install --ignore-dependencies --yes "chocolatey-core.extension" "chocolatey-fastanswers.extension" "dependency-windows10" }
PostInstallBlock = { choco install --ignore-dependencies --yes "chocolatey-core.extension" "chocolatey-fastanswers.extension" }
}
}

Process {
# Install Chocolatey on Windows
Install-PackageManager -PackageManagerFullName $ChocolateyParams.Name -CheckExistenceBlock $ChocolateyParams.CheckExistenceBlock -InstallCommandBlock $ChocolateyParams.InstallCommandBlock -PostInstallBlock $ChocolateyParams.PostInstallBlock
Show-MessageDialog -Title "$DoneTitle" -Message "$DoneMessage"
If ($Force) {
# Install Chocolatey on Windows
Install-PackageManager -PackageManagerFullName $ChocolateyParams.Name -CheckExistenceBlock $ChocolateyParams.CheckExistenceBlock -InstallCommandBlock $ChocolateyParams.InstallCommandBlock -PostInstallBlock $ChocolateyParams.PostInstallBlock -Force
} Else {
Install-PackageManager -PackageManagerFullName $ChocolateyParams.Name -CheckExistenceBlock $ChocolateyParams.CheckExistenceBlock -InstallCommandBlock $ChocolateyParams.InstallCommandBlock -PostInstallBlock $ChocolateyParams.PostInstallBlock
}

If (!$Force) {
Show-MessageDialog -Title "$DoneTitle" -Message "$DoneMessage"
}
}
}

Expand Down Expand Up @@ -169,12 +180,12 @@ function Unregister-ChocolateyDailyUpgrade() {

function Remove-AllChocolateyPackage() {
Begin {
Import-Module -DisableNameChecking "$PSScriptRoot\Manage-Software.psm1"
$Ask = "Are you sure you want to remove:`n$((choco list) -match '\w \d.*\d' | ForEach-Object { "`n- $_" })`n`nPress YES to confirm."
$Question = Show-Question -Title "Remove ALL Chocolatey Packages?" -Message $Ask -BoxIcon "Warning"
}

Process {

switch ($Question) {
'Yes' {
Uninstall-Software -Name "All Chocolatey Packages" -Packages @("all") -PackageProvider 'Chocolatey'
Expand Down
30 changes: 28 additions & 2 deletions src/lib/package-managers/Manage-Software.psm1
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Import-Module -DisableNameChecking "$PSScriptRoot\Manage-Chocolatey.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\Manage-Winget.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\Open-File.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\Title-Templates.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\..\ui\Show-MessageDialog.psm1"

Expand All @@ -17,18 +20,41 @@ function Install-Software() {
[Switch] $NoDialog
)

$DoneTitle = "Information"
$DoneMessage = "$Name installed successfully!"
[ScriptBlock] $CheckVersionCommand

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Install-Software' returns an object of type 'System.Management.Automation.ScriptBlock' but this type is not declared in the OutputType attribute. Note

The cmdlet 'Install-Software' returns an object of type 'System.Management.Automation.ScriptBlock' but this type is not declared in the OutputType attribute.
$Script:DoneTitle = "Information"
$Script:DoneMessage = "$Name installed successfully!"

If ($PackageProvider -in @('Winget', 'MsStore')) {
$CheckVersionCommand = { winget --version }
}

If ($PackageProvider -eq 'Chocolatey') {
$CheckVersionCommand = { choco --version }
$InstallBlock = { choco install --ignore-dependencies --yes $Package }
Write-Status -Types "?" -Status "Chocolatey is configured to ignore dependencies (bloat), you may need to install it before using any program." -Warning
}

If ($PackageProvider -eq 'WSL') {
$CheckVersionCommand = { wsl --version }
$InstallBlock = { wsl --install --distribution $Package }
}

Try {
$Err = (Invoke-Expression "$CheckVersionCommand")
If (($LASTEXITCODE)) { throw $Err } # 0 = False, 1 = True
Write-Status -Types "?", $PackageProvider -Status "$PackageProvider is already installed." -Warning
} Catch {
If ($PackageProvider -in @('Winget', 'MsStore')) {
Install-Winget -Force
}
If ($PackageProvider -in @('Chocolatey')) {
Install-Chocolatey -Force
}
If ($PackageProvider -in @('WSL')) {
Open-PowerShellFilesCollection -RelativeLocation "src\scripts\other-scripts" -Scripts @("Install-WSL.ps1") -NoDialog
}
}

Write-Title "Installing $($Name) via $PackageProvider"
$DoneMessage += "`n`nInstalled via $PackageProvider`:`n"

Expand Down
22 changes: 18 additions & 4 deletions src/lib/package-managers/Manage-Winget.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ $Script:DoneTitle = "Information"
$Script:DoneMessage = "Process Completed!"

function Install-Winget() {
[CmdletBinding()]
param (
[Switch] $Force
)

Begin {
$WingetParams = @{
Name = "Winget"
Expand Down Expand Up @@ -61,12 +66,21 @@ function Install-Winget() {
}

Process {
# Install Winget on Windows (Method 1)
Install-PackageManager -PackageManagerFullName $WingetParams.Name -CheckExistenceBlock $WingetParams.CheckExistenceBlock -InstallCommandBlock $WingetParams.InstallCommandBlock
# Install Winget on Windows (Method 2)
If ($Force) {
# Install Winget on Windows (Method 1)
Install-PackageManager -PackageManagerFullName $WingetParams.Name -CheckExistenceBlock $WingetParams.CheckExistenceBlock -InstallCommandBlock $WingetParams.InstallCommandBlock -Force
# Install Winget on Windows (Method 2)
} Else {
# Install Winget on Windows (Method 1)
Install-PackageManager -PackageManagerFullName $WingetParams.Name -CheckExistenceBlock $WingetParams.CheckExistenceBlock -InstallCommandBlock $WingetParams.InstallCommandBlock
# Install Winget on Windows (Method 2)
}

Install-PackageManager -PackageManagerFullName $WingetParams2.Name -CheckExistenceBlock $WingetParams2.CheckExistenceBlock -InstallCommandBlock $WingetParams2.InstallCommandBlock

Show-MessageDialog -Title "$DoneTitle" -Message "$DoneMessage"
If (!$Force) {
Show-MessageDialog -Title "$DoneTitle" -Message "$DoneMessage"
}
}
}

Expand Down

0 comments on commit 25a2f01

Please sign in to comment.