added SPM
This commit is contained in:
parent
17bb547c74
commit
9e5457c2ec
8 changed files with 779 additions and 18 deletions
109
installer.go
Normal file
109
installer.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"spitfire-installer/spm"
|
||||
)
|
||||
|
||||
// Installer manages the download, decompression, and installation processes.
|
||||
type Installer struct {
|
||||
// Progress info from SPM
|
||||
Progress int
|
||||
Task string
|
||||
|
||||
// Internal states
|
||||
IsDownloading bool
|
||||
IsInstalling bool
|
||||
DoneDownload bool
|
||||
DoneInstall bool
|
||||
LastError error
|
||||
|
||||
// Paths
|
||||
DownloadDir string
|
||||
TempDir string
|
||||
}
|
||||
|
||||
// NewInstaller creates a new Installer with initial state.
|
||||
func NewInstaller() *Installer {
|
||||
return &Installer{}
|
||||
}
|
||||
|
||||
// StartDownloadDecompress starts the download and decompression in a background goroutine.
|
||||
func (inst *Installer) StartDownloadDecompress() {
|
||||
inst.IsDownloading = true
|
||||
go func() {
|
||||
defer func() {
|
||||
inst.IsDownloading = false
|
||||
inst.DoneDownload = (inst.LastError == nil)
|
||||
}()
|
||||
|
||||
// Prepare download directory
|
||||
spm.UpdateProgress(0, "Preparing to download...")
|
||||
inst.DownloadDir = spm.GetTempDownloadDir()
|
||||
|
||||
// Download APPINDEX
|
||||
appIndexPath := filepath.Join(inst.DownloadDir, "APPINDEX")
|
||||
spm.UpdateProgress(0, "Downloading APPINDEX")
|
||||
if err := spm.DownloadAppIndex(appIndexPath); err != nil {
|
||||
inst.LastError = err
|
||||
return
|
||||
}
|
||||
|
||||
// Download package
|
||||
packageName := "spitfire-browser"
|
||||
release := "nightly"
|
||||
spm.UpdateProgress(0, "Downloading package...")
|
||||
if err := spm.DownloadPackageFromAppIndex(appIndexPath, packageName, release, inst.DownloadDir); err != nil {
|
||||
inst.LastError = err
|
||||
return
|
||||
}
|
||||
|
||||
// Decompress
|
||||
spm.UpdateProgress(0, "Decompressing...")
|
||||
packagePath := filepath.Join(inst.DownloadDir, "browser-amd64-nightly-linux.tar.gz")
|
||||
tempDir, err := spm.DecompressToTemp(packagePath)
|
||||
if err != nil {
|
||||
inst.LastError = err
|
||||
return
|
||||
}
|
||||
|
||||
inst.TempDir = tempDir
|
||||
}()
|
||||
}
|
||||
|
||||
// FinalInstall moves files to the final install directory in a background goroutine.
|
||||
func (inst *Installer) FinalInstall() {
|
||||
if !inst.DoneDownload {
|
||||
inst.LastError = fmt.Errorf("Cannot install: download and decompression are not complete")
|
||||
return
|
||||
}
|
||||
|
||||
inst.IsInstalling = true
|
||||
go func() {
|
||||
defer func() {
|
||||
inst.IsInstalling = false
|
||||
inst.DoneInstall = (inst.LastError == nil)
|
||||
}()
|
||||
|
||||
// Generate default install directory
|
||||
installDir, err := spm.GetDefaultInstallDir()
|
||||
if err != nil {
|
||||
inst.LastError = fmt.Errorf("failed to determine default install directory: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := spm.MoveFilesToInstallDir(inst.TempDir, installDir); err != nil {
|
||||
inst.LastError = err
|
||||
return
|
||||
}
|
||||
|
||||
spm.UpdateProgress(100, "Installation complete!")
|
||||
}()
|
||||
}
|
||||
|
||||
// PollProgress fetches the latest progress and task from SPM.
|
||||
func (inst *Installer) PollProgress() {
|
||||
p, t := spm.GetProgress()
|
||||
inst.Progress, inst.Task = p, t
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue