Skip to content

Fix macosx ventura updater #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 15, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implemented the autoupdater for MacOS
  • Loading branch information
cmaglie committed Feb 14, 2023
commit f557b299abf39e77637bfd27a0d9b186f877b371
97 changes: 96 additions & 1 deletion updater/updater_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,105 @@

package updater

import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"io"
"os"

"github.com/arduino/go-paths-helper"
"github.com/codeclysm/extract/v3"
)

func start(src string) string {
return ""
}

func checkForUpdates(currentVersion string, updateAPIURL, updateBinURL string, cmdName string) (string, error) {
return "", nil
executablePath, err := os.Executable()
if err != nil {
return "", fmt.Errorf("could not app path: %w", err)
}
currentAppPath := paths.New(executablePath).Parent().Parent().Parent()
if currentAppPath.Ext() != ".app" {
return "", fmt.Errorf("could not find app root in %s", executablePath)
}
oldAppPath := currentAppPath.Parent().Join("ArdiunoCreateAgent.old.app")
if oldAppPath.Exist() {
return "", fmt.Errorf("temp app already exists: %s, cannot update", oldAppPath)
}

// Fetch information about updates
info, err := fetchInfo(updateAPIURL, cmdName)
if err != nil {
return "", err
}
if info.Version == currentVersion {
// No updates available, bye bye
return "", nil
}

tmp := paths.TempDir().Join("arduino-create-agent")
tmpZip := tmp.Join("update.zip")
tmpAppPath := tmp.Join("ArduinoCreateAgent-update.app")
defer tmp.RemoveAll()

// Download the update.
download, err := fetch(updateBinURL + cmdName + "/" + plat + "/" + info.Version + "/" + cmdName)
if err != nil {
return "", err
}
defer download.Close()

f, err := tmpZip.Create()
if err != nil {
return "", err
}
defer f.Close()

sha := sha256.New()
if _, err := io.Copy(io.MultiWriter(sha, f), download); err != nil {
return "", err
}
f.Close()

// Check the hash
if s := sha.Sum(nil); !bytes.Equal(s, info.Sha256) {
return "", fmt.Errorf("bad hash: %s (expected %s)", s, info.Sha256)
}

// Unzip the update
if err := tmpAppPath.MkdirAll(); err != nil {
return "", fmt.Errorf("could not create tmp dir to unzip update: %w", err)
}

f, err = tmpZip.Open()
if err != nil {
return "", fmt.Errorf("could not open archive for unzip: %w", err)
}
defer f.Close()
if err := extract.Archive(context.Background(), f, tmpAppPath.String(), nil); err != nil {
return "", fmt.Errorf("extracting archive: %w", err)
}

// Rename current app as .old
if err := currentAppPath.Rename(oldAppPath); err != nil {
return "", fmt.Errorf("could not rename old app as .old: %w", err)
}

// Install new app
if err := tmpAppPath.CopyDirTo(currentAppPath); err != nil {
// Try rollback changes
_ = currentAppPath.RemoveAll()
_ = oldAppPath.Rename(currentAppPath)
return "", fmt.Errorf("could not install app: %w", err)
}

// Remove old app
_ = oldAppPath.RemoveAll()

// Restart agent
return currentAppPath.Join("Contents", "MacOS", "Arduino_Create_Agent").String(), nil
}