Skip to content

Commit

Permalink
Merge a28c2f4 into c5b19d3
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Sep 18, 2019
2 parents c5b19d3 + a28c2f4 commit 7bdd222
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
33 changes: 19 additions & 14 deletions evaluator/functions.go
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"crypto/rand"
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"math"
Expand Down Expand Up @@ -1574,11 +1575,26 @@ func sourceFn(tok token.Token, env *object.Environment, args ...object.Object) o
// require("file.abs")
var history = make(map[string]string)

type StringFn func(string) (string, error)
var packageAliases map[string]string
var packageAliasesLoaded bool

func requireFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
getAlias := Memoize(util.ReadAliasFromFile)
a, error := getAlias(args[0].Inspect())
if !packageAliasesLoaded {
a, err := ioutil.ReadFile("./packages.abs.json")

// We couldn't open the packages, file, possibly doesn't exists
// and the code shouldn't fail
if err == nil {
// Try to decode the packages file:
// if an error occurs we will simply
// ignore it
json.Unmarshal(a, &packageAliases)
}

packageAliasesLoaded = true
}

a, error := util.UnaliasPath(args[0].Inspect(), packageAliases)
if error != nil {
return newError(tok, "error resolving '%s': %s\n", args[0].Inspect(), error.Error())
}
Expand All @@ -1588,17 +1604,6 @@ func requireFn(tok token.Token, env *object.Environment, args ...object.Object)
return doSource(tok, e, file, args...)
}

func Memoize(fn StringFn) StringFn {
return func(str string) (string, error) {
if res, ok := history[str]; ok {
return res, nil
}
res, err := fn(str)
history[str] = res
return res, err
}
}

func doSource(tok token.Token, env *object.Environment, fileName string, args ...object.Object) object.Object {
err := validateArgs(tok, "source", args, 1, [][]string{{object.STRING_OBJ}})
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions install/install.go
Expand Up @@ -18,7 +18,6 @@ func valid(module string) bool {
}

func Install(module string) {

if !valid(module) {
fmt.Printf(`Error reading URL. Please use "github.com/USER/REPO" format to install`)
return
Expand All @@ -44,7 +43,7 @@ func Install(module string) {
return
}

func PrintLoader(done chan int64, message string) {
func printLoader(done chan int64, message string) {
var stop bool = false
symbols := []string{"🌑 ", "🌒 ", "🌓 ", "🌔 ", "🌕 ", "🌖 ", "🌗 ", "🌘 "}
i := 0
Expand Down Expand Up @@ -92,7 +91,7 @@ func getZip(module string) error {
url := fmt.Sprintf("https://%s/archive/master.zip", module)

done := make(chan int64)
go PrintLoader(done, "Downloading archive")
go printLoader(done, "Downloading archive")

req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down Expand Up @@ -189,34 +188,34 @@ func createAlias(module string) (string, error) {

data := make(map[string]string)
moduleName := filepath.Base(module)
// Appending "master" as Github zip file has "-master" suffix
modulePath := fmt.Sprintf("./vendor/%s-master", module)

// If package.abs.json file is empty
if len(b) == 0 {
// Appending "master" as Github zip file has "-master" suffix
// Add alias key-value pair to file
data[moduleName] = modulePath

} else {
err = json.Unmarshal(b, &data)
if err != nil {
fmt.Printf("Could not unmarshal alias json %s\n", err)
return "", err
}

// module already installed and aliased
if data[moduleName] == modulePath {
return moduleName, nil
}

if data[moduleName] != "" {
fmt.Printf("This module could not be aliased because module of same name exists\n")
moduleName = module
data[moduleName] = modulePath
return modulePath, nil
}

data[moduleName] = modulePath
}

newData, err := json.MarshalIndent(data, "", " ")

if err != nil {
fmt.Printf("Could not marshal alias json when installing module %s\n", err)
return "", err
Expand Down
38 changes: 28 additions & 10 deletions util/util.go
@@ -1,13 +1,12 @@
package util

import (
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/abs-lang/abs/object"
)
Expand Down Expand Up @@ -103,16 +102,35 @@ func UniqueStrings(slice []string) []string {
return list
}

func ReadAliasFromFile(path string) (string, error) {
var packageAlias map[string]string
a, _ := ioutil.ReadFile("./packages.abs.json")
err := json.Unmarshal(a, &packageAlias)
if err != nil {
return path, err
// UnaliasPath translates a path alias
// to the full path in the filesystem.
func UnaliasPath(path string, packageAlias map[string]string) (string, error) {
// An alias can come in different forms:
// - package
// - package/file.abs
// but we only really need to resolve the
// first path in the alias.
parts := strings.Split(path, string(os.PathSeparator))

if len(parts) < 1 {
return path, nil
}

if packageAlias[path] != "" {
return packageAlias[path], nil
if packageAlias[parts[0]] != "" {
// If we are able to resolve a path, then
// we should join in back with the rest of the
// paths
p := []string{packageAlias[parts[0]]}
p = append(p, parts[1:]...)

// If our path didn't end with an ABS file,
// let's assume it's a directory and we will
// auto-include the index.abs file from it
if filepath.Ext(path) != ".abs" {
p = append(p, "index.abs")
}

return filepath.Join(p...), nil
}
return path, nil
}

0 comments on commit 7bdd222

Please sign in to comment.