yay/install.go

176 lines
4.0 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"os/exec"
rpc "github.com/mikkeloscar/aur"
2017-08-04 11:26:53 +02:00
gopkg "github.com/mikkeloscar/gopkgbuild"
)
// Install handles package installs
func install(pkgs []string, flags []string) error {
2017-08-04 11:26:53 +02:00
aurs, repos, _ := packageSlices(pkgs)
if len(repos) != 0 {
//TODO
// err := passToPacman("-S", repos, flags)
// if err != nil {
// fmt.Println("Error installing repo packages.")
// }
}
if len(aurs) != 0 {
2017-08-04 11:26:53 +02:00
err := aurInstall(aurs, flags)
if err != nil {
fmt.Println("Error installing aur packages.")
}
}
return nil
}
// Install sends system commands to make and install a package from pkgName
2017-08-04 11:26:53 +02:00
func aurInstall(pkgName []string, flags []string) (err error) {
q, err := rpc.Info(pkgName)
if err != nil {
return
}
if len(q) != len(pkgName) {
fmt.Printf("Some packages from list\n%+v\n do not exist", pkgName)
}
var finalrm []string
for _, i := range q {
mrm, err := PkgInstall(&i, flags)
if err != nil {
fmt.Println("Error installing", i.Name, ":", err)
}
finalrm = append(finalrm, mrm...)
}
if len(finalrm) != 0 {
err = removeMakeDeps(finalrm)
}
return err
}
2017-10-02 17:54:34 +02:00
func setupPackageSpace(a *rpc.Pkg) (dir string, pkgbuild *gopkg.PKGBUILD, err error) {
dir = config.BuildDir + a.PackageBase + "/"
if _, err = os.Stat(dir); !os.IsNotExist(err) {
2017-08-04 11:26:53 +02:00
if !continueTask("Directory exists. Clean Build?", "yY") {
_ = os.RemoveAll(config.BuildDir + a.PackageBase)
}
}
2017-08-04 11:26:53 +02:00
if err = downloadAndUnpack(baseURL+a.URLPath, config.BuildDir, false); err != nil {
return
}
2017-08-04 11:26:53 +02:00
if !continueTask("Edit PKGBUILD?", "yY") {
editcmd := exec.Command(editor(), dir+"PKGBUILD")
editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
editcmd.Run()
}
pkgbuild, err = gopkg.ParseSRCINFO(dir + ".SRCINFO")
if err == nil {
for _, pkgsource := range pkgbuild.Source {
2017-08-04 11:26:53 +02:00
owner, repo := parseSource(pkgsource)
if owner != "" && repo != "" {
2017-08-04 11:26:53 +02:00
err = branchInfo(a.Name, owner, repo)
if err != nil {
fmt.Println(err)
}
}
}
}
return
}
// PkgInstall handles install from Info Result.
func PkgInstall(a *rpc.Pkg, flags []string) (finalmdeps []string, err error) {
fmt.Printf("\x1b[1;32m==> Installing\x1b[33m %s\x1b[0m\n", a.Name)
if a.Maintainer == "" {
fmt.Println("\x1b[1;31;40m==> Warning:\x1b[0;;40m This package is orphaned.\x1b[0m")
}
2017-10-02 17:54:34 +02:00
dir, _, err := setupPackageSpace(a)
if err != nil {
return
}
if specialDBsauce {
return
}
2017-08-04 11:26:53 +02:00
runDeps, makeDeps, err := pkgDependencies(a)
if err != nil {
return
}
repoDeps := append(runDeps[0], makeDeps[0]...)
aurDeps := append(runDeps[1], makeDeps[1]...)
finalmdeps = append(finalmdeps, makeDeps[0]...)
finalmdeps = append(finalmdeps, makeDeps[1]...)
if len(aurDeps) != 0 || len(repoDeps) != 0 {
2017-08-04 11:26:53 +02:00
if !continueTask("Continue?", "nN") {
return finalmdeps, fmt.Errorf("user did not like the dependencies")
}
}
aurQ, _ := rpc.Info(aurDeps)
if len(aurQ) != len(aurDeps) {
2017-08-04 11:26:53 +02:00
(aurQuery)(aurQ).missingPackage(aurDeps)
if !continueTask("Continue?", "nN") {
return finalmdeps, fmt.Errorf("unable to install dependencies")
}
}
var depArgs []string
if config.NoConfirm {
depArgs = []string{"--asdeps", "--noconfirm"}
} else {
depArgs = []string{"--asdeps"}
}
// Repo dependencies
if len(repoDeps) != 0 {
// TODO
// errR := passToPacman("-S", repoDeps, depArgs)
// if errR != nil {
// return finalmdeps, errR
// }
}
// Handle AUR dependencies
for _, dep := range aurQ {
finalmdepsR, errA := PkgInstall(&dep, depArgs)
finalmdeps = append(finalmdeps, finalmdepsR...)
if errA != nil {
2017-08-04 11:26:53 +02:00
cleanRemove(repoDeps)
cleanRemove(aurDeps)
return finalmdeps, errA
}
}
args := []string{"-sri"}
args = append(args, flags...)
makepkgcmd := exec.Command(config.MakepkgBin, args...)
makepkgcmd.Stdin, makepkgcmd.Stdout, makepkgcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
2017-10-02 17:54:34 +02:00
makepkgcmd.Dir = dir
err = makepkgcmd.Run()
if err == nil {
2017-08-04 11:26:53 +02:00
_ = saveVCSInfo()
if config.CleanAfter {
fmt.Println("\x1b[1;32m==> CleanAfter enabled. Deleting source folder.\x1b[0m")
os.RemoveAll(dir)
}
}
return
}