Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c7bfad
commit a057cbb
Showing
6 changed files
with
291 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package action | ||
|
||
import ( | ||
"github.com/Masterminds/glide/gom" | ||
"github.com/Masterminds/glide/msg" | ||
) | ||
|
||
// ImportGom imports a Gomfile. | ||
func ImportGom(dest string) { | ||
base := "." | ||
config := EnsureConfig() | ||
if !gom.Has(base) { | ||
msg.Die("No gom data found.") | ||
} | ||
deps, err := gom.Parse(base) | ||
if err != nil { | ||
msg.Die("Failed to extract Gomfile: %s", err) | ||
} | ||
appendImports(deps, config) | ||
writeConfigToFileOrStdout(config, dest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package gom | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/Masterminds/glide/cfg" | ||
"github.com/Masterminds/glide/msg" | ||
"github.com/Masterminds/glide/util" | ||
) | ||
|
||
// Has returns true if this dir has a Gomfile. | ||
func Has(dir string) bool { | ||
path := filepath.Join(dir, "Gomfile") | ||
fi, err := os.Stat(path) | ||
return err == nil && !fi.IsDir() | ||
} | ||
|
||
// Parse parses a Gomfile. | ||
func Parse(dir string) ([]*cfg.Dependency, error) { | ||
path := filepath.Join(dir, "Gomfile") | ||
if fi, err := os.Stat(path); err != nil || fi.IsDir() { | ||
return []*cfg.Dependency{}, nil | ||
} | ||
|
||
msg.Info("Found Gomfile.\n") | ||
buf := []*cfg.Dependency{} | ||
|
||
goms, err := parseGomfile(path) | ||
if err != nil { | ||
return []*cfg.Dependency{}, err | ||
} | ||
|
||
for _, gom := range goms { | ||
// Do we need to skip this dependency? | ||
if val, ok := gom.options["skipdep"]; ok && val.(string) == "true" { | ||
continue | ||
} | ||
|
||
// Check for custom cloning command | ||
if _, ok := gom.options["command"]; ok { | ||
return []*cfg.Dependency{}, errors.New("Glide does not support custom Gomfile commands") | ||
} | ||
|
||
// Check for groups/environments | ||
if val, ok := gom.options["group"]; ok { | ||
groups := toStringSlice(val) | ||
if !stringsContain(groups, "development") && !stringsContain(groups, "production") { | ||
// right now we only support development and production | ||
msg.Info("Skipping dependency '%s' because it isn't in the development or production group", gom.name) | ||
continue | ||
} | ||
} | ||
|
||
pkg, sub := util.NormalizeName(gom.name) | ||
|
||
dep := &cfg.Dependency{ | ||
Name: pkg, | ||
} | ||
|
||
if len(sub) > 0 { | ||
dep.Subpackages = []string{sub} | ||
} | ||
|
||
// Check for a specific revision | ||
if val, ok := gom.options["commit"]; ok { | ||
dep.Reference = val.(string) | ||
} | ||
if val, ok := gom.options["tag"]; ok { | ||
dep.Reference = val.(string) | ||
} | ||
if val, ok := gom.options["branch"]; ok { | ||
dep.Reference = val.(string) | ||
} | ||
|
||
// Parse goos and goarch | ||
if val, ok := gom.options["goos"]; ok { | ||
dep.Os = toStringSlice(val) | ||
} | ||
if val, ok := gom.options["goarch"]; ok { | ||
dep.Arch = toStringSlice(val) | ||
} | ||
|
||
buf = append(buf, dep) | ||
} | ||
|
||
return buf, nil | ||
} | ||
|
||
func stringsContain(v []string, key string) bool { | ||
for _, s := range v { | ||
if s == key { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func toStringSlice(v interface{}) []string { | ||
if v, ok := v.(string); ok { | ||
return []string{v} | ||
} | ||
|
||
if v, ok := v.([]string); ok { | ||
return v | ||
} | ||
|
||
return []string{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package gom | ||
|
||
// This is copied + slightly adapted from gom's `gomfile.go` file. | ||
// | ||
// gom's license is MIT-style. | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var qx = `'[^']*'|"[^"]*"` | ||
var kx = `:[a-z][a-z0-9_]*` | ||
var ax = `(?:\s*` + kx + `\s*|,\s*` + kx + `\s*)` | ||
var re_group = regexp.MustCompile(`\s*group\s+((?:` + kx + `\s*|,\s*` + kx + `\s*)*)\s*do\s*$`) | ||
var re_end = regexp.MustCompile(`\s*end\s*$`) | ||
var re_gom = regexp.MustCompile(`^\s*gom\s+(` + qx + `)\s*((?:,\s*` + kx + `\s*=>\s*(?:` + qx + `|\s*\[\s*` + ax + `*\s*\]\s*))*)$`) | ||
var re_options = regexp.MustCompile(`(,\s*` + kx + `\s*=>\s*(?:` + qx + `|\s*\[\s*` + ax + `*\s*\]\s*)\s*)`) | ||
|
||
func unquote(name string) string { | ||
name = strings.TrimSpace(name) | ||
if len(name) > 2 { | ||
if (name[0] == '\'' && name[len(name)-1] == '\'') || (name[0] == '"' && name[len(name)-1] == '"') { | ||
return name[1 : len(name)-1] | ||
} | ||
} | ||
return name | ||
} | ||
|
||
func parseOptions(line string, options map[string]interface{}) { | ||
ss := re_options.FindAllStringSubmatch(line, -1) | ||
re_a := regexp.MustCompile(ax) | ||
for _, s := range ss { | ||
kvs := strings.SplitN(strings.TrimSpace(s[0])[1:], "=>", 2) | ||
kvs[0], kvs[1] = strings.TrimSpace(kvs[0]), strings.TrimSpace(kvs[1]) | ||
if kvs[1][0] == '[' { | ||
as := re_a.FindAllStringSubmatch(kvs[1][1:len(kvs[1])-1], -1) | ||
a := []string{} | ||
for i := range as { | ||
it := strings.TrimSpace(as[i][0]) | ||
if strings.HasPrefix(it, ",") { | ||
it = strings.TrimSpace(it[1:]) | ||
} | ||
if strings.HasPrefix(it, ":") { | ||
it = strings.TrimSpace(it[1:]) | ||
} | ||
a = append(a, it) | ||
} | ||
options[kvs[0][1:]] = a | ||
} else { | ||
options[kvs[0][1:]] = unquote(kvs[1]) | ||
} | ||
} | ||
} | ||
|
||
type Gom struct { | ||
name string | ||
options map[string]interface{} | ||
} | ||
|
||
func parseGomfile(filename string) ([]Gom, error) { | ||
f, err := os.Open(filename + ".lock") | ||
if err != nil { | ||
f, err = os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
br := bufio.NewReader(f) | ||
|
||
goms := make([]Gom, 0) | ||
|
||
n := 0 | ||
skip := 0 | ||
valid := true | ||
var envs []string | ||
for { | ||
n++ | ||
lb, _, err := br.ReadLine() | ||
if err != nil { | ||
if err == io.EOF { | ||
return goms, nil | ||
} | ||
return nil, err | ||
} | ||
line := strings.TrimSpace(string(lb)) | ||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
|
||
name := "" | ||
options := make(map[string]interface{}) | ||
var items []string | ||
if re_group.MatchString(line) { | ||
envs = strings.Split(re_group.FindStringSubmatch(line)[1], ",") | ||
for i := range envs { | ||
envs[i] = strings.TrimSpace(envs[i])[1:] | ||
} | ||
valid = true | ||
continue | ||
} else if re_end.MatchString(line) { | ||
if !valid { | ||
skip-- | ||
if skip < 0 { | ||
return nil, fmt.Errorf("Syntax Error at line %d", n) | ||
} | ||
} | ||
valid = false | ||
envs = nil | ||
continue | ||
} else if skip > 0 { | ||
continue | ||
} else if re_gom.MatchString(line) { | ||
items = re_gom.FindStringSubmatch(line)[1:] | ||
name = unquote(items[0]) | ||
parseOptions(items[1], options) | ||
} else { | ||
return nil, fmt.Errorf("Syntax Error at line %d", n) | ||
} | ||
if envs != nil { | ||
options["group"] = envs | ||
} | ||
goms = append(goms, Gom{name, options}) | ||
} | ||
return goms, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters