github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/system/packagemanager/pacman.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package packagemanager
     5  
     6  import (
     7  	"os/exec"
     8  	"regexp"
     9  	"strings"
    10  
    11  	"github.com/AlpineAIO/wails/v2/internal/shell"
    12  )
    13  
    14  // Pacman represents the Pacman package manager
    15  type Pacman struct {
    16  	name string
    17  	osid string
    18  }
    19  
    20  // NewPacman creates a new Pacman instance
    21  func NewPacman(osid string) *Pacman {
    22  	return &Pacman{
    23  		name: "pacman",
    24  		osid: osid,
    25  	}
    26  }
    27  
    28  // Packages returns the libraries that we need for Wails to compile
    29  // They will potentially differ on different distributions or versions
    30  func (p *Pacman) Packages() packagemap {
    31  	return packagemap{
    32  		"libgtk-3": []*Package{
    33  			{Name: "gtk3", SystemPackage: true, Library: true},
    34  		},
    35  		"libwebkit": []*Package{
    36  			{Name: "webkit2gtk", SystemPackage: true, Library: true},
    37  		},
    38  		"gcc": []*Package{
    39  			{Name: "gcc", SystemPackage: true},
    40  		},
    41  		"pkg-config": []*Package{
    42  			{Name: "pkgconf", SystemPackage: true},
    43  		},
    44  		"npm": []*Package{
    45  			{Name: "npm", SystemPackage: true},
    46  		},
    47  		"docker": []*Package{
    48  			{Name: "docker", SystemPackage: true, Optional: true},
    49  		},
    50  	}
    51  }
    52  
    53  // Name returns the name of the package manager
    54  func (p *Pacman) Name() string {
    55  	return p.name
    56  }
    57  
    58  // PackageInstalled tests if the given package name is installed
    59  func (p *Pacman) PackageInstalled(pkg *Package) (bool, error) {
    60  	if pkg.SystemPackage == false {
    61  		return false, nil
    62  	}
    63  	stdout, _, err := shell.RunCommand(".", "pacman", "-Q", pkg.Name)
    64  	if err != nil {
    65  		_, ok := err.(*exec.ExitError)
    66  		if ok {
    67  			return false, nil
    68  		}
    69  		return false, err
    70  	}
    71  
    72  	splitoutput := strings.Split(stdout, "\n")
    73  	for _, line := range splitoutput {
    74  		if strings.HasPrefix(line, pkg.Name) {
    75  			splitline := strings.Split(line, " ")
    76  			pkg.Version = strings.TrimSpace(splitline[1])
    77  		}
    78  	}
    79  
    80  	return true, err
    81  }
    82  
    83  // PackageAvailable tests if the given package is available for installation
    84  func (p *Pacman) PackageAvailable(pkg *Package) (bool, error) {
    85  	if pkg.SystemPackage == false {
    86  		return false, nil
    87  	}
    88  	output, _, err := shell.RunCommand(".", "pacman", "-Si", pkg.Name)
    89  	// We add a space to ensure we get a full match, not partial match
    90  	if err != nil {
    91  		_, ok := err.(*exec.ExitError)
    92  		if ok {
    93  			return false, nil
    94  		}
    95  		return false, err
    96  	}
    97  
    98  	reg := regexp.MustCompile(`.*Version.*?:\s+(.*)`)
    99  	matches := reg.FindStringSubmatch(output)
   100  	pkg.Version = ""
   101  	noOfMatches := len(matches)
   102  	if noOfMatches > 1 {
   103  		pkg.Version = strings.TrimSpace(matches[1])
   104  	}
   105  
   106  	return true, nil
   107  }
   108  
   109  // InstallCommand returns the package manager specific command to install a package
   110  func (p *Pacman) InstallCommand(pkg *Package) string {
   111  	if pkg.SystemPackage == false {
   112  		return pkg.InstallCommand[p.osid]
   113  	}
   114  	return "sudo pacman -S " + pkg.Name
   115  }