|
| 1 | +function update($app, $global, $force, $quiet = $false, $independent, $suggested, $use_cache = $true, $check_hash = $true) { |
| 2 | + $old_version = Select-CurrentVersion -AppName $app -Global:$global |
| 3 | + $old_manifest = installed_manifest $app $old_version $global |
| 4 | + $install = install_info $app $old_version $global |
| 5 | + |
| 6 | + # re-use architecture, bucket and url from first install |
| 7 | + $architecture = Format-ArchitectureString $install.architecture |
| 8 | + $bucket = $install.bucket |
| 9 | + if ($null -eq $bucket) { |
| 10 | + $bucket = 'main' |
| 11 | + } |
| 12 | + $url = $install.url |
| 13 | + |
| 14 | + $manifest = manifest $app $bucket $url |
| 15 | + $version = $manifest.version |
| 16 | + $is_nightly = $version -eq 'nightly' |
| 17 | + if ($is_nightly) { |
| 18 | + $version = nightly_version $quiet |
| 19 | + $check_hash = $false |
| 20 | + } |
| 21 | + |
| 22 | + if (!$force -and ($old_version -eq $version)) { |
| 23 | + if (!$quiet) { |
| 24 | + warn "The latest version of '$app' ($version) is already installed." |
| 25 | + } |
| 26 | + return |
| 27 | + } |
| 28 | + if (!$version) { |
| 29 | + # installed from a custom bucket/no longer supported |
| 30 | + error "No manifest available for '$app'." |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + Write-Host "Updating '$app' ($old_version -> $version)" |
| 35 | + |
| 36 | + #region Workaround for #2952 |
| 37 | + if (test_running_process $app $global) { |
| 38 | + Write-Host 'Running process detected, skip updating.' |
| 39 | + return |
| 40 | + } |
| 41 | + #endregion Workaround for #2952 |
| 42 | + |
| 43 | + # region Workaround |
| 44 | + # Workaround for https://github.com/ScoopInstaller/Scoop/issues/2220 until install is refactored |
| 45 | + # Remove and replace whole region after proper fix |
| 46 | + Write-Host 'Downloading new version' |
| 47 | + if (Test-Aria2Enabled) { |
| 48 | + Invoke-CachedAria2Download $app $version $manifest $architecture $cachedir $manifest.cookie $true $check_hash |
| 49 | + } else { |
| 50 | + $urls = script:url $manifest $architecture |
| 51 | + |
| 52 | + foreach ($url in $urls) { |
| 53 | + Invoke-CachedDownload $app $version $url $null $manifest.cookie $true |
| 54 | + |
| 55 | + if ($check_hash) { |
| 56 | + $manifest_hash = hash_for_url $manifest $url $architecture |
| 57 | + $source = cache_path $app $version $url |
| 58 | + $ok, $err = check_hash $source $manifest_hash $(show_app $app $bucket) |
| 59 | + |
| 60 | + if (!$ok) { |
| 61 | + error $err |
| 62 | + if (Test-Path $source) { |
| 63 | + # rm cached file |
| 64 | + Remove-Item -Force $source |
| 65 | + } |
| 66 | + if ($url.Contains('sourceforge.net')) { |
| 67 | + Write-Host -f yellow 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.' |
| 68 | + } |
| 69 | + abort $(new_issue_msg $app $bucket 'hash check failed') |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + # There is no need to check hash again while installing |
| 75 | + $check_hash = $false |
| 76 | + # endregion Workaround |
| 77 | + |
| 78 | + $dir = versiondir $app $old_version $global |
| 79 | + $persist_dir = persistdir $app $global |
| 80 | + |
| 81 | + Invoke-HookScript -HookType 'pre_uninstall' -Manifest $old_manifest -Arch $architecture |
| 82 | + |
| 83 | + Write-Host "Uninstalling '$app' ($old_version)" |
| 84 | + Invoke-Installer -Path $dir -Manifest $old_manifest -ProcessorArchitecture $architecture -Global:$global -Uninstall |
| 85 | + rm_shims $app $old_manifest $global $architecture |
| 86 | + |
| 87 | + # If a junction was used during install, that will have been used |
| 88 | + # as the reference directory. Otherwise it will just be the version |
| 89 | + # directory. |
| 90 | + $refdir = unlink_current $dir |
| 91 | + uninstall_psmodule $old_manifest $refdir $global |
| 92 | + env_rm_path $old_manifest $refdir $global $architecture |
| 93 | + env_rm $old_manifest $global $architecture |
| 94 | + |
| 95 | + if ($force -and ($old_version -eq $version)) { |
| 96 | + if (!(Test-Path "$dir/../_$version.old")) { |
| 97 | + Move-Item "$dir" "$dir/../_$version.old" |
| 98 | + } else { |
| 99 | + $i = 1 |
| 100 | + while (Test-Path "$dir/../_$version.old($i)") { |
| 101 | + $i++ |
| 102 | + } |
| 103 | + Move-Item "$dir" "$dir/../_$version.old($i)" |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + Invoke-HookScript -HookType 'post_uninstall' -Manifest $old_manifest -Arch $architecture |
| 108 | + |
| 109 | + if ($bucket) { |
| 110 | + # add bucket name it was installed from |
| 111 | + $app = "$bucket/$app" |
| 112 | + } |
| 113 | + if ($install.url) { |
| 114 | + # use the url of the install json if the application was installed through url |
| 115 | + $app = $install.url |
| 116 | + } |
| 117 | + |
| 118 | + if ($independent) { |
| 119 | + install_app $app $architecture $global $suggested $use_cache $check_hash |
| 120 | + } else { |
| 121 | + # Also add missing dependencies |
| 122 | + $apps = @(Get-Dependency $app $architecture) -ne $app |
| 123 | + ensure_none_failed $apps |
| 124 | + $apps.Where({ !(installed $_) }) + $app | ForEach-Object { install_app $_ $architecture $global $suggested $use_cache $check_hash } |
| 125 | + } |
| 126 | +} |
0 commit comments