Skip to content

Commit

Permalink
feat: basic support for global install (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
azukaar committed Aug 11, 2019
1 parent db07638 commit aa51f42
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gupm.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
}
},
"name": "gupm",
"version": "1.2.0"
"version": "1.2.1"
}
27 changes: 26 additions & 1 deletion src/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func (a *Arguments) AsList() []string {

return res
}
func (a *Arguments) Shift() {
i := 2

for (*a)["$"+strconv.Itoa(i)] != "" {
(*a)["$"+strconv.Itoa(i-1)] = (*a)["$"+strconv.Itoa(i)]
i++
}

(*a)["$"+strconv.Itoa(i-1)] = ""
}

func GetArgs(args []string) (string, Arguments) {
arguments := make(Arguments)
Expand Down Expand Up @@ -104,10 +114,13 @@ func getProvider(c string, args Arguments) string {
gupmConfig := utils.GupmConfig()
defaultProvider := "gupm"

if c == "install" {
if c == "install" || c == "global" {
defaultProvider = gupmConfig.DefaultProvider
}

fmt.Println(gupmConfig)
fmt.Println(gupmConfig.DefaultProvider)

if defaultProvider == "os" {
osName := utils.OSNAME()
if gupmConfig.OsProviders[osName] != "" {
Expand Down Expand Up @@ -154,6 +167,7 @@ func ExecCli(c string, args Arguments) (bool, error) {
"s": "self",
"t": "test",
"pl": "plugin",
"g": "global",
}

if shorthands[c] != "" {
Expand All @@ -172,6 +186,7 @@ func ExecCli(c string, args Arguments) (bool, error) {
fmt.Println("bootstrap / b :", "[--provider=]", "bootstrap a new project based on the model of your specific provider")
fmt.Println("test / t :", "[--provider=] Run project's tests in tests folder.")

fmt.Println("global / g :", "install or delete global packages")
fmt.Println("cache / c :", "clear or check the cache with \"cache clear\" or \"cache check\"")
fmt.Println("self / s :", "self manage gupm. Try g \"self upgrade\" or \"g self uninstall\"")
fmt.Println("plugin / pl :", "To install a plugin \"g pl install\". Then use \"g pl create\" to create a new one and \"g pl link\" to test your plugin")
Expand All @@ -187,6 +202,16 @@ func ExecCli(c string, args Arguments) (bool, error) {
err = Publish(".", args["$1"])
} else if c == "delete" {
err = RemoveDependency(".", args.AsList())
} else if c == "global" {
if args["$1"] == "install" || args["$1"] == "i" {
args.Shift()
GlobalAdd(args.AsList())
} else if args["$1"] == "delete" || args["$1"] == "d" {
args.Shift()
GlobalDelete(args.AsList())
} else {
ui.Error(notFound, args["$1"], "\n", "try install or delete")
}
} else if c == "plugin" {
if args["$1"] == "create" {
PluginCreate(".")
Expand Down
6 changes: 3 additions & 3 deletions src/defaultProvider/defaultProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func ExpandDependency(dependency map[string]interface{}) (map[string]interface{}
return dependency, nil
}

func BinaryInstall(path string, packagePath string) error {
func BinaryInstall(dest string, packagePath string) error {
packages, _ := utils.ReadDir(packagePath)

for _, dep := range packages {
Expand All @@ -150,7 +150,7 @@ func BinaryInstall(path string, packagePath string) error {
if config["binaries"] != nil {
bins := config["binaries"].(map[string]string)
for name, relPath := range bins {
os.Symlink(utils.Path("../gupm_modules/"+"/"+dep.Name()+relPath), ".bin/"+name)
os.Symlink(utils.Path("/../gupm_modules/"+"/"+dep.Name()+relPath), utils.Path(dest+"/"+name))
}
}
}
Expand All @@ -160,7 +160,7 @@ func BinaryInstall(path string, packagePath string) error {
}

func SaveDependencyList(path string, depList []map[string]interface{}) error {
config := GetPackageConfig("gupm.json")
config := GetPackageConfig(utils.Path(path + "/gupm.json"))
if config["dependencies"] == nil {
config["dependencies"] = make(map[string]interface{})
}
Expand Down
57 changes: 57 additions & 0 deletions src/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"os"
"path/filepath"

"./ui"
"./utils"
)

func installBins(path string) {
var GLOBAL = utils.Path(utils.HOMEDIR(".") + "/.gupm/global")
bins := utils.RecursiveFileWalkDir(GLOBAL + "/.bin")

for _, bin := range bins {
name := filepath.Base(bin)
if !utils.FileExists(path + "/" + name) {
os.Symlink(bin, path+"/"+name)
}
}
}

func GlobalAdd(rls []string) {
ui.Title("Installing global dependency...")
var GLOBAL = utils.Path(utils.HOMEDIR(".") + "/.gupm/global")

if !utils.FileExists(GLOBAL + utils.Path("/gupm.json")) {
os.MkdirAll(GLOBAL, os.ModePerm)
utils.WriteFile(GLOBAL+utils.Path("/gupm.json"), "{}")
}

ui.Log("Installing...")
AddDependency(GLOBAL, rls)
InstallProject(GLOBAL)

ui.Log("Add binaries...")
if utils.OSNAME() != "windows" {
if utils.FileExists("/usr/local/bin/") {
installBins("/usr/local/bin/")
} else {
installBins("/usr/bin/")
}
} else {
ui.Error("Global Installation not supported on Windows yet. Please add .gupm/global/.bin to your PATH")
}
}

func GlobalDelete(rls []string) {
var GLOBAL = utils.Path(utils.HOMEDIR(".") + "/.gupm/global")

if !utils.FileExists(GLOBAL + utils.Path("/gupm.json")) {
os.MkdirAll(GLOBAL, os.ModePerm)
utils.WriteFile(GLOBAL+utils.Path("/gupm.json"), "{}")
}

RemoveDependency(GLOBAL, rls)
}
12 changes: 6 additions & 6 deletions src/installProject.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ func installDep(path string, depList []map[string]interface{}) map[string]string
ui.Log("Installing " + path)
provider.InstallDependency(destination, dep)

if path == "." {
installPathsLock.Lock()
installPaths[dep["provider"].(string)] = depProviderConfig.Config.Default.InstallPath
installPathsLock.Unlock()
}
// if path == "." {
installPathsLock.Lock()
installPaths[dep["provider"].(string)] = utils.Path(path + "/" + depProviderConfig.Config.Default.InstallPath)
installPathsLock.Unlock()
// }

nextDepList, ok := depList[index]["dependencies"].([]map[string]interface{})

Expand Down Expand Up @@ -210,7 +210,7 @@ func InstallProject(path string) error {
installPaths := installDep(path, depList)

ui.Title("Install Binaries...")
err = provider.BinaryInstall(installPaths)
err = provider.BinaryInstall(path, installPaths)
if err != nil {
return err
}
Expand Down
19 changes: 11 additions & 8 deletions src/provider/install.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package provider

import (
"io/ioutil"
"os"
"regexp"

"../defaultProvider"
"../jsVm"
"../ui"
"../utils"
"io/ioutil"
"os"
"regexp"
)

func BinaryInstall(path map[string]string) error {
os.RemoveAll(".bin")
func BinaryInstall(path string, paths map[string]string) error {
dest := utils.Path(path + "/.bin")
os.RemoveAll(dest)
os.MkdirAll(dest, os.ModePerm)

for pr, prdir := range path {
for pr, prdir := range paths {
depProviderPath := GetProviderPath(pr)
var file = utils.FileExists(depProviderPath + utils.Path("/binaryInstall.gs"))
if pr != "gupm" && file {
input := make(map[string]interface{})
input["Destination"] = ".bin"
input["Destination"] = dest
input["Source"] = prdir

res, err := jsVm.Run(depProviderPath+utils.Path("/binaryInstall.gs"), input)
Expand All @@ -29,7 +32,7 @@ func BinaryInstall(path map[string]string) error {
_, err1 := res.ToString()
return err1
} else {
return defaultProvider.BinaryInstall(".bin", prdir)
return defaultProvider.BinaryInstall(dest, prdir)
}
}
return nil
Expand Down

0 comments on commit aa51f42

Please sign in to comment.