updated spm package

This commit is contained in:
partisan 2025-02-04 17:11:27 +01:00
parent 3f67a0a6de
commit 7373e47c1d
11 changed files with 594 additions and 475 deletions

View file

@ -7,12 +7,12 @@ import (
)
// pendingUpdates holds info about packages that have been downloaded/decompressed
// but not yet moved to the final install location.
// but not yet moved to the final install location, cuz Windows has this stupid file locking mechanism
var pendingUpdates []AppIndexEntry
// AutoDownloadUpdates downloads the APPINDEX file, parses it, compares against
// currently installed packages, and if it finds a newer version, downloads
// and decompresses it into a temporary folder. The result is stored in pendingUpdates.
// and decompresses it into a temporary folder. The result is stored in pendingUpdates, so it can be used by AutoInstallUpdates().
func AutoDownloadUpdates() error {
// 1) Download the APPINDEX file to a temporary location
appIndexPath := filepath.Join(os.TempDir(), "APPINDEX")
@ -115,10 +115,10 @@ func AutoDownloadUpdates() error {
return nil
}
// AutoInstallUpdates installs any packages that were decompressed by AutoDownloadUpdates.
// AutoInstallUpdates installs any packages that were downloaded and decompressed by AutoDownloadUpdates.
// It moves files from their temp directories to the final location and updates installed.ini.
func AutoInstallUpdates() error {
installDir, err := GetDefaultInstallDir()
installDir, err := GetInstallDir()
if err != nil {
return err
}
@ -155,3 +155,107 @@ func AutoInstallUpdates() error {
pendingUpdates = nil
return nil
}
func AutoDownloadSpecified(specs []AppIndexEntry) error {
// 1) Download the APPINDEX file to a temporary location
appIndexPath := filepath.Join(os.TempDir(), "APPINDEX")
fmt.Println("[INFO] Starting APPINDEX download to:", appIndexPath)
if err := DownloadAppIndex(appIndexPath); err != nil {
return fmt.Errorf("[ERROR] Failed to download APPINDEX: %w", err)
}
fmt.Println("[INFO] APPINDEX downloaded successfully")
// 2) Parse the APPINDEX file
fmt.Println("[INFO] Parsing APPINDEX file:", appIndexPath)
entries, err := ParseAppIndex(appIndexPath)
if err != nil {
return fmt.Errorf("[ERROR] Failed to parse APPINDEX: %w", err)
}
fmt.Printf("[INFO] Parsed APPINDEX successfully, found %d entries\n", len(entries))
// 3) Get install directory to check for updates
installDir, err := GetInstallDir()
if err != nil {
return err
}
fmt.Println("[INFO] Install directory:", installDir)
// 4) For each item in the passed specs, attempt to download if update is needed
for _, spec := range specs {
fmt.Printf("[INFO] Checking requested package: %+v\n", spec)
// Find matching entry from the parsed APPINDEX
var matchingEntry *AppIndexEntry
for _, e := range entries {
if e.Name == spec.Name &&
e.Release == spec.Release &&
e.Type == spec.Type &&
e.OS == spec.OS &&
e.Arch == spec.Arch {
matchingEntry = &e
break
}
}
if matchingEntry == nil {
fmt.Printf("[WARN] No matching APPINDEX entry found for package: %s (%s)\n", spec.Name, spec.Release)
continue
}
fmt.Printf("[INFO] Found matching APPINDEX entry: %+v\n", *matchingEntry)
// // Check if an update is needed
// updateNeeded, err := IsUpdateNeeded(
// installDir,
// matchingEntry.Name,
// matchingEntry.Release,
// matchingEntry.Version,
// matchingEntry.Arch,
// matchingEntry.OS,
// )
// if err != nil {
// return fmt.Errorf("[ERROR] Failed to check if update is needed for %s: %w", matchingEntry.Name, err)
// }
// if !updateNeeded {
// fmt.Printf("[INFO] No update needed for package '%s'\n", matchingEntry.Name)
// continue
// }
// 5) Download the package
downloadDir := GetTempDir()
fmt.Printf("[INFO] Downloading package '%s' to temporary folder: %s\n", matchingEntry.Name, downloadDir)
if err := DownloadPackageFromAppIndex(
appIndexPath,
matchingEntry.Name,
matchingEntry.Release,
matchingEntry.Type,
downloadDir,
); err != nil {
return fmt.Errorf("[ERROR] Failed to download package '%s': %w", matchingEntry.Name, err)
}
fmt.Printf("[INFO] Package '%s' downloaded successfully to: %s\n", matchingEntry.Name, downloadDir)
// 6) Decompress the package
fmt.Printf("[INFO] Decompressing package '%s'\n", matchingEntry.Name)
tempDir, err := DecompressPackage(
downloadDir,
matchingEntry.Name,
matchingEntry.Arch,
matchingEntry.OS,
matchingEntry.Type,
matchingEntry.Release,
matchingEntry.Version,
)
if err != nil {
return fmt.Errorf("[ERROR] Failed to decompress package '%s': %w", matchingEntry.Name, err)
}
fmt.Printf("[INFO] Package '%s' decompressed successfully to: %s\n", matchingEntry.Name, tempDir)
// 7) Store in pendingUpdates for AutoInstallUpdates
fmt.Printf("[INFO] Adding '%s' to pending updates\n", matchingEntry.Name)
pendingUpdates = append(pendingUpdates, *matchingEntry)
}
fmt.Println("[INFO] AutoDownloadSpecifiedPackages completed successfully")
return nil
}