diff --git a/revanced.ps1 b/revanced.ps1 index 67d3fb8..990819b 100644 --- a/revanced.ps1 +++ b/revanced.ps1 @@ -80,83 +80,84 @@ function renderForm([String[]]$items, [String]$label, [String]$title, [System.Wi } class config { - [hashtable]$version = @{} + [String]$patches = "none" + [String]$integrations = "none" + [String]$cli = "none" [System.Collections.ArrayList]$excluded = (New-Object System.Collections.ArrayList) -} -class configFile { - [String]$path = ".\config.json" - [config]$config = [config]::new() - - [void] load() { - $fileContent = ConvertFrom-Json ((Get-Content $this.path) -Join ' ').ToString() - - $version = @{} - foreach ($prop in $fileContent.version.PsObject.Properties) { - $version.Add($prop.Name, $prop.Value) - } - $fileContent.version = $null - - $this.config = $fileContent - $this.config.version = $version + [void] load($Path) { + $fileContent = ConvertFrom-Json ((Get-Content $Path) -Join ' ').ToString() + + $this.patches = $fileContent.patches + $this.integrations = $fileContent.integrations + $this.cli = $fileContent.cli + $this.excluded = $fileContent.excluded } - [void] save() { - Out-File $this.path -InputObject (ConvertTo-Json $this.config) + [void] save($Path) { + Out-File $Path -InputObject (ConvertTo-Json $this) } } -function UpdateFromGithub { - param ( - [Parameter(Mandatory)][String]$Repository, - [Parameter(Mandatory)][configFile]$Config, - [scriptblock]$AssetsFilter = {$true}, - [Parameter(Mandatory)][String]$OutFile - ) - - [String]$InstalledTag = $Config.config.version[$Repository] - $LatestRelease - try { - $LatestRelease = ConvertFrom-Json (Invoke-WebRequest "https://api.github.com/repos/$Repository/releases/latest").Content - } - catch [System.Net.WebException] { - Write-Host "Couldn't check for update $Repository" - return $false - } - - [String]$UpdateTag = $LatestRelease.tag_name - - if ($InstalledTag -eq $UpdateTag) { - Write-Host "$Repository up to date! ($InstalledTag)" - return $false - } - - $Assets = $LatestRelease.assets | Where-Object -FilterScript $AssetsFilter - - Write-Host "Downloading $Repository ($InstalledTag -> $UpdateTag)" - Write-Host "Changelog: https://github.com/$Repository/compare/$InstalledTag...$UpdateTag" - Invoke-WebRequest $Assets[0].browser_download_url -OutFile $OutFile - $Config.config.version[$Repository] = $UpdateTag - $Config.save() - - return $true -} - function main { - $config = [configFile]::new() + + $config = New-Object config + + # Get latest releases + try { - $config.load() - } - catch [System.IO.FileNotFoundException] { - Write-Host ".\config.json not found" - $config.save() - } + $patchesRelease = ConvertFrom-Json (Invoke-WebRequest https://api.github.com/repos/revanced/revanced-patches/releases/latest).Content + $integrationsRelease = ConvertFrom-Json (Invoke-WebRequest https://api.github.com/repos/revanced/revanced-integrations/releases/latest).Content + $cliRelease = ConvertFrom-Json (Invoke-WebRequest https://api.github.com/repos/revanced/revanced-cli/releases/latest).Content - # Update + # Check versions - UpdateFromGithub -Repository "revanced/revanced-patches" -Config $config -AssetsFilter {$_.content_type -like "application/*"} -OutFile ".\app\revanced-patches.jar" | Out-Null - UpdateFromGithub -Repository "revanced/revanced-integrations" -Config $config -AssetsFilter {$_.content_type -like "application/*"} -OutFile ".\app\app-release-unsigned.apk" | Out-Null - UpdateFromGithub -Repository "revanced/revanced-cli" -Config $config -AssetsFilter {$_.content_type -like "application/*"} -OutFile ".\app\revanced-cli.jar" | Out-Null + [Bool[]]$install = $true, $true, $true + + try { + $config.load(".\config.json") + + if ($config.patches -eq $patchesRelease.tag_name) {$install[0] = $false} + if ($config.integrations -eq $integrationsRelease.tag_name) {$install[1] = $false} + if ($config.cli -eq $cliRelease.tag_name) {$install[2] = $false} + } + catch [System.Management.Automation.ItemNotFoundException] { + Write-Output "currentVersion.json not found, downloading everything" + } + + # Download latest releases + + New-Item -type Directory -Path ".\app" -ErrorAction SilentlyContinue | Out-Null + + # TODO: use where-object to search for filename + + if ($install[0]) { + Write-Output "Downloading Patches ($($config.patches) -> $($patchesRelease.tag_name))" + Write-Output "changelog: https://github.com/revanced/revanced-patches/compare/$($config.patches)...$($patchesRelease.tag_name)" + Invoke-WebRequest $patchesRelease.assets[1].browser_download_url -OutFile ".\app\revanced-patches.jar" + $config.patches = $patchesRelease.tag_name + } + else {Write-Output "Patches up to date $($config.patches)"} + if ($install[1]) { + Write-Output "Downloading Integrations ($($config.integrations) -> $($integrationsRelease.tag_name))" + Write-Output "changelog: https://github.com/revanced/revanced-patches/compare/$($config.integrations)...$($integrationsRelease.tag_name)" + Invoke-WebRequest $integrationsRelease.assets[0].browser_download_url -OutFile ".\app\app-release-unsigned.apk" + $config.integrations = $integrationsRelease.tag_name + } + else {Write-Output "Integrations up to date $($config.integrations)"} + if ($install[2]) { + Write-Output "Downloading cli ($($config.cli) -> $($cliRelease.tag_name))" + Write-Output "changelog: https://github.com/revanced/revanced-patches/compare/$($config.cli)...$($cliRelease.tag_name)" + Invoke-WebRequest $cliRelease.assets[0].browser_download_url -OutFile ".\app\revanced-cli.jar" + $config.cli = $cliRelease.tag_name + } + else {Write-Output "cli up to date! $($config.cli)"} + + $config.save(".\config.json") + } + catch [System.Net.WebException] { + Write-Output "Couldn't get newest files, using locals" + } if ($NoDeploy) {return} @@ -199,7 +200,7 @@ function main { $excludedIntegrations = New-Object System.Collections.ArrayList [System.Collections.ArrayList]$savedExclusions = New-Object System.Collections.ArrayList - $savedExclusionsNames = $config.config.excluded + $savedExclusionsNames = $config.excluded foreach ($i in $integrationsList) { if (($savedExclusionsNames.Count -gt 1) -and $savedExclusionsNames.Contains($i.id)) { [void] $savedExclusions.Add($true) @@ -214,19 +215,17 @@ function main { [void] $excludedIntegrations.Add($i.Split(' ')[0]) #FIXME: hacky way to do this, but it works } - $config.config.excluded = $excludedIntegrations - $config.save() + $config.excluded = $excludedIntegrations + $config.save(".\config.json") # devices $devices = adb devices - if ($devices.Length -eq 2) { - throw "No devices found" - } $devices = $devices[1..($devices.length-2)] [String]$selectedDevice = '' - if ($devices.Length -eq 1) { + if ($devices.Length -eq 0) {throw "No devices found"} + elseif ($devices.Length -eq 1) { $selectedDevice = ($devices | ForEach-Object {$_.split()[0]})[0] } else {