diff --git a/odin.ps1 b/odin.ps1 new file mode 100644 index 0000000..01f3b5f --- /dev/null +++ b/odin.ps1 @@ -0,0 +1,124 @@ +function Test-WinUtilPackageManager { + <# + + .SYNOPSIS + Checks if Winget is installed + + .PARAMETER winget + Check if Winget is installed + + #> + + Param( + [System.Management.Automation.SwitchParameter]$winget + ) + + $status = "not-installed" + + if ($winget) { + # Check if Winget is available while getting it's Version if it's available + $wingetExists = $true + try { + $wingetVersionFull = winget --version + } catch [System.Management.Automation.CommandNotFoundException], [System.Management.Automation.ApplicationFailedException] { + Write-Warning "Winget was not found due to un-availablity reasons" + $wingetExists = $false + } catch { + Write-Warning "Winget was not found due to un-known reasons, The Stack Trace is:`n$($psitem.Exception.StackTrace)" + $wingetExists = $false + } + + # If Winget is available, Parse it's Version and give proper information to Terminal Output. + # If it isn't available, the return of this funtion will be "not-installed", indicating that + # Winget isn't installed/available on The System. + if ($wingetExists) { + # Check if Preview Version + if ($wingetVersionFull.Contains("-preview")) { + $wingetVersion = $wingetVersionFull.Trim("-preview") + $wingetPreview = $true + } else { + $wingetVersion = $wingetVersionFull + $wingetPreview = $false + } + + # Check if Winget's Version is too old. + $wingetCurrentVersion = [System.Version]::Parse($wingetVersion.Trim('v')) + # Grabs the latest release of Winget from the Github API for version check process. + $response = Invoke-RestMethod -Uri "https://api.github.com/repos/microsoft/Winget-cli/releases/latest" -Method Get -ErrorAction Stop + $wingetLatestVersion = [System.Version]::Parse(($response.tag_name).Trim('v')) #Stores version number of latest release. + $wingetOutdated = $wingetCurrentVersion -lt $wingetLatestVersion + Write-Host "===========================================" -ForegroundColor Green + Write-Host "--- Winget is installed ---" -ForegroundColor Green + Write-Host "===========================================" -ForegroundColor Green + Write-Host "Version: $wingetVersionFull" -ForegroundColor White + + if (!$wingetPreview) { + Write-Host " - Winget is a release version." -ForegroundColor Green + } else { + Write-Host " - Winget is a preview version. Unexpected problems may occur." -ForegroundColor Yellow + } + + if (!$wingetOutdated) { + Write-Host " - Winget is Up to Date" -ForegroundColor Green + $status = "installed" + } + else { + Write-Host " - Winget is Out of Date" -ForegroundColor Red + $status = "outdated" + } + } else { + Write-Host "===========================================" -ForegroundColor Red + Write-Host "--- Winget is not installed ---" -ForegroundColor Red + Write-Host "===========================================" -ForegroundColor Red + $status = "not-installed" + } + } + + return $status +} + +function ConfirmBaseRun { + $isWingetInstalled = Test-WinUtilPackageManager -winget + + if (!($isWingetInstalled -eq "installed")) { + Write-Warning "!!! Winget is not installed." + + Write-Host "You haven't run the base.ps1 script yet. Please visit https://learncodethehardway.com/setup/installers.html for more." + + Exit + } +} + +ConfirmBaseRun + +$odinPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$env:APPDATA\..\Local\Programs\Odin") + +if(Test-Path -Path $odinPath) { + Write-Warning "#=========================================================#" + Write-Warning "!! The directory $odinPath already exists, which means" + Write-Warning "!! you already have odin installed. Here's the version:" + cat "$odinPath\mingw64\version_info.txt" + Write-Warning "#=========================================================#" + Write-Warning "Here's the version that Winget will install:" + winget search odin-lang.Odin + Write-Warning "--- If you want me to replace your install with this version, then stop and" + Write-Warning "delete the $odinPath directory before hitting ENTER to continue." + Write-Warning "!!! If you do NOT want to replace this directory then hit CTRL-C now." + Read-Host -Prompt "Press any key to continue, or CTRL-C to abort" | Out-Null +} + +Write-Warning "!! Installing Odin in $odinPath" +Write-Warning "This has to be installed this way because winget won't add it to the path so I have to do it manually. If you don't want this, then hit CTRL-C and install Odin yourself." +Read-Host -Prompt "Press any key to continue, or CTRL-C to abort" | Out-Null + +winget install --location "$odinPath" -e odin-lang.Odin + +if($env:PATH.contains($odinPath)) { + Write-Warning "!! Detected $odinPath in your PATH. Will not update your PATH." + Write-Warning "!! This is probably because you already installed it. You'll have" + Write-Warning "!! to manually remove this part of your path if you want to change it." + Write-Warning "!! But, it will probably still work since that's where I installed it." +} else { + $newPath = $env:PATH + ";$odinPath" + [Environment]::SetEnvironmentVariable("Path", $newPath, "User") +}