5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 18:29:53 +08:00
wails/v2/internal/system/packagemanager/apt.go
stffabi b02dbfaddf
[v2] NSIS installer support for Windows (#1184)
* [v2] Add support for post build hooks

Currently only supports build-level hooks

* [v2] Improve build assets handling and use single source for manifest generation

The manifest asset files are now a go template and data will be
resolved before they are included into the build output.

Breaking Change: Windows manifest file must be named
“wails.exe.manifest” and doesn’t depend on the project name
anymore.

* [v2, windows] NSIS installer generation
2022-03-02 19:44:31 +11:00

102 lines
2.6 KiB
Go

//go:build linux
// +build linux
package packagemanager
import (
"regexp"
"strings"
"github.com/wailsapp/wails/v2/internal/shell"
)
// Apt represents the Apt manager
type Apt struct {
name string
osid string
}
// NewApt creates a new Apt instance
func NewApt(osid string) *Apt {
return &Apt{
name: "apt",
osid: osid,
}
}
// Packages returns the libraries that we need for Wails to compile
// They will potentially differ on different distributions or versions
func (a *Apt) Packages() packagemap {
return packagemap{
"libgtk-3": []*Package{
{Name: "libgtk-3-dev", SystemPackage: true, Library: true},
},
"libwebkit": []*Package{
{Name: "libwebkit2gtk-4.0-dev", SystemPackage: true, Library: true},
},
"gcc": []*Package{
{Name: "build-essential", SystemPackage: true},
},
"pkg-config": []*Package{
{Name: "pkg-config", SystemPackage: true},
},
"npm": []*Package{
{Name: "npm", SystemPackage: true},
},
"docker": []*Package{
{Name: "docker.io", SystemPackage: true, Optional: true},
},
"nsis": []*Package{
{Name: "nsis", SystemPackage: true, Optional: true},
},
}
}
// Name returns the name of the package manager
func (a *Apt) Name() string {
return a.name
}
// PackageInstalled tests if the given package name is installed
func (a *Apt) PackageInstalled(pkg *Package) (bool, error) {
if pkg.SystemPackage == false {
return false, nil
}
stdout, _, err := shell.RunCommand(".", "apt", "-qq", "list", pkg.Name)
return strings.Contains(stdout, "[installed]"), err
}
// PackageAvailable tests if the given package is available for installation
func (a *Apt) PackageAvailable(pkg *Package) (bool, error) {
if pkg.SystemPackage == false {
return false, nil
}
stdout, _, err := shell.RunCommand(".", "apt", "-qq", "list", pkg.Name)
// We add a space to ensure we get a full match, not partial match
output := a.removeEscapeSequences(stdout)
installed := strings.HasPrefix(output, pkg.Name)
a.getPackageVersion(pkg, output)
return installed, err
}
// InstallCommand returns the package manager specific command to install a package
func (a *Apt) InstallCommand(pkg *Package) string {
if pkg.SystemPackage == false {
return pkg.InstallCommand[a.osid]
}
return "sudo apt install " + pkg.Name
}
func (a *Apt) removeEscapeSequences(in string) string {
escapechars, _ := regexp.Compile(`\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])`)
return escapechars.ReplaceAllString(in, "")
}
func (a *Apt) getPackageVersion(pkg *Package, output string) {
splitOutput := strings.Split(output, " ")
if len(splitOutput) > 1 {
pkg.Version = splitOutput[1]
}
}