Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions openadapt/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def start(fullscreen: bool = False) -> None:
ui.icon("share").tooltip("Share").on(
"click", lambda: (_ for _ in ()).throw(Exception(NotImplementedError))
)
ui.icon("update").tooltip("Update App").on(
"click", partial(Popen, ["python", "-m", "openadapt.update"])
)

with ui.splitter(value=20) as splitter:
splitter.classes("w-full h-full")
Expand Down
36 changes: 36 additions & 0 deletions openadapt/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import importlib.metadata
import subprocess
import sys

from nicegui import ui
from packaging import version
import requests


def main() -> None:
api_url = "https://api.github.com/repos/OpenAdaptAI/OpenAdapt/releases"

response = requests.get(f"{api_url}?per_page=30&page=1")
releases = response.json()
if not releases:
print("No releases found")
sys.exit(1)

latest_release = releases[0]["name"]
curr_version = importlib.metadata.version("openadapt")

if version.parse(curr_version) < version.parse(latest_release):
try:
if sys.platform.startswith("win"):
subprocess.call("powershell -File scripts/update_app.ps1", shell=True)
else:
subprocess.check_call(["./scripts/update_app.sh"])

ui.notify("OpenAdapt has been updated")
except subprocess.CalledProcessError as e:
print(f"Error updating the app: {e}")
sys.exit(1)


if __name__ == "__main__":
main()
68 changes: 68 additions & 0 deletions scripts/update_app.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function Invoke-LatestReleaseDownload {
# Check if required utilities are installed
if (-not (Get-Command jq -ErrorAction SilentlyContinue)) {
Write-Host "jq is required but not installed. Please install jq to proceed."
exit 1
}

if (-not (Get-Command curl -ErrorAction SilentlyContinue)) {
Write-Host "curl is required but not installed. Please install curl to proceed."
exit 1
}

# GitHub repository in the format 'owner/repo'
$repo = "OpenAdaptAI/OpenAdapt"

# GitHub API URL to fetch the latest release
$apiUrl = "https://api.github.com/repos/$repo/releases/latest"

# Fetch the latest release information
Write-Host "Fetching latest release information for $repo..."
$response = Invoke-RestMethod -Uri $apiUrl

# Check if the response contains an error
if ($response.message) {
Write-Host "Error: $($response.message)"
exit 1
}

# Extract the release tag name and assets URL
$tagName = $response.tag_name
$assetsUrl = $response.assets_url

Write-Host "Latest release: $tagName"

# Fetch the assets information
$assets = Invoke-RestMethod -Uri $assetsUrl

# Download each asset
Write-Host "Downloading assets..."
foreach ($asset in $assets) {
$assetUrl = $asset.browser_download_url
Write-Host "Downloading $assetUrl..."
& Invoke-WebRequest -o $(Split-Path -Leaf $assetUrl) $assetUrl
}

Write-Host "All assets downloaded successfully."

# Remove older versions
Remove-Item -Recurse -Force dist/OpenAdapt.zip, dist/OpenAdapt.app.zip
Remove-Item -Recurse -Force dist/OpenAdapt, dist/OpenAdapt.app

# Rename the downloaded assets
Rename-Item "OpenAdapt-$tagName.zip" "OpenAdapt.zip"
Rename-Item "OpenAdapt-$tagName.app.zip" "OpenAdapt.app.zip"

# Move assets to dist folder
Move-Item "OpenAdapt.zip" "dist/"
Move-Item "OpenAdapt.app.zip" "dist/"

# Unzip the downloaded assets
Expand-Archive -Force -Path "dist/OpenAdapt.zip" -DestinationPath "dist/"
Expand-Archive -Force -Path "dist/OpenAdapt.app.zip" -DestinationPath "dist/"
}

# Download the assets
Invoke-LatestReleaseDownload

Write-Host "Updated the OpenAdapt app successfully!"
77 changes: 77 additions & 0 deletions scripts/update_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Function to download assets from the latest release of a GitHub repository
download_latest_release_assets() {
# Check if required utilities are installed
if ! command -v jq &> /dev/null; then
echo "jq is required but not installed. Please install jq to proceed."
exit 1
fi

if ! command -v curl &> /dev/null; then
echo "curl is required but not installed. Please install curl to proceed."
exit 1
fi

# GitHub repository in the format 'owner/repo'
REPO="OpenAdaptAI/OpenAdapt"

# GitHub API URL to fetch the latest release
API_URL="https://api.github.com/repos/$REPO/releases/latest"

# Fetch the latest release information
echo "Fetching latest release information for $REPO..."
response=$(curl -s "$API_URL")

# Check if the response contains an error
if echo "$response" | jq -e '.message' &> /dev/null; then
echo "Error: $(echo "$response" | jq -r '.message')"
exit 1
fi

# Extract the release tag name and assets URL
tag_name=$(echo "$response" | jq -r '.tag_name')
assets_url=$(echo "$response" | jq -r '.assets_url')

echo "Latest release: $tag_name"

# Fetch the assets information
assets=$(curl -s "$assets_url")

# Download each asset
echo "Downloading assets..."
echo "$assets" | jq -r '.[].browser_download_url' | while read -r asset_url; do
echo "Downloading $asset_url..."
curl -LO "$asset_url"
done

echo "All assets downloaded successfully."
}

# Download the assets
download_latest_release_assets

# Remove older versions
rm -rf dist/OpenAdapt.zip
rm -rf dist/OpenAdapt.app.zip

rm -rf dist/OpenAdapt
rm -rf dist/OpenAdapt.app

# Rename the downloaded assets
mv OpenAdapt-$tag_name.zip OpenAdapt.zip
mv OpenAdapt-$tag_name.app.zip OpenAdapt.app.zip

# Move assets to dist folder
mv OpenAdapt.zip dist/
mv OpenAdapt.app.zip dist/

# Unzip the downloaded assets
unzip -o dist/OpenAdapt.zip
unzip -o dist/OpenAdapt.app.zip

# Move the unzipped app to the Applications folder
mv OpenAdapt.app dist/
mv OpenAdapt dist/

echo "Updated the OpenAdapt app successfully!"