Add ability to parse local dependency

This commit is contained in:
Matthias Loibl 2019-07-22 19:42:43 +02:00
parent e5199342ea
commit 6ee790d911
No known key found for this signature in database
GPG key ID: 78A796CA74CA38BA
2 changed files with 86 additions and 23 deletions

View file

@ -20,6 +20,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/jsonnet-bundler/jsonnet-bundler/spec" "github.com/jsonnet-bundler/jsonnet-bundler/spec"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -92,20 +93,24 @@ func Main() int {
return 0 return 0
} }
func parseDependency(urlString string) *spec.Dependency { func parseDependency(path string) *spec.Dependency {
if spec := parseGitSSHDependency(urlString); spec != nil { if d := parseGitSSHDependency(path); d != nil {
return spec return d
} }
if spec := parseGithubDependency(urlString); spec != nil { if d := parseGithubDependency(path); d != nil {
return spec return d
}
if d := parseLocalDependency(path); d != nil {
return d
} }
return nil return nil
} }
func parseGitSSHDependency(urlString string) *spec.Dependency { func parseGitSSHDependency(p string) *spec.Dependency {
if !gitSSHRegex.MatchString(urlString) { if !gitSSHRegex.MatchString(p) {
return nil return nil
} }
@ -115,27 +120,27 @@ func parseGitSSHDependency(urlString string) *spec.Dependency {
repo := "" repo := ""
version := "master" version := "master"
if gitSSHWithPathAndVersionRegex.MatchString(urlString) { if gitSSHWithPathAndVersionRegex.MatchString(p) {
matches := gitSSHWithPathAndVersionRegex.FindStringSubmatch(urlString) matches := gitSSHWithPathAndVersionRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
repo = matches[3] repo = matches[3]
subdir = matches[4] subdir = matches[4]
version = matches[5] version = matches[5]
} else if gitSSHWithPathRegex.MatchString(urlString) { } else if gitSSHWithPathRegex.MatchString(p) {
matches := gitSSHWithPathRegex.FindStringSubmatch(urlString) matches := gitSSHWithPathRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
repo = matches[3] repo = matches[3]
subdir = matches[4] subdir = matches[4]
} else if gitSSHWithVersionRegex.MatchString(urlString) { } else if gitSSHWithVersionRegex.MatchString(p) {
matches := gitSSHWithVersionRegex.FindStringSubmatch(urlString) matches := gitSSHWithVersionRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
repo = matches[3] repo = matches[3]
version = matches[4] version = matches[4]
} else { } else {
matches := gitSSHRegex.FindStringSubmatch(urlString) matches := gitSSHRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
repo = matches[3] repo = matches[3]
@ -153,8 +158,8 @@ func parseGitSSHDependency(urlString string) *spec.Dependency {
} }
} }
func parseGithubDependency(urlString string) *spec.Dependency { func parseGithubDependency(p string) *spec.Dependency {
if !githubSlugRegex.MatchString(urlString) { if !githubSlugRegex.MatchString(p) {
return nil return nil
} }
@ -164,30 +169,30 @@ func parseGithubDependency(urlString string) *spec.Dependency {
subdir := "" subdir := ""
version := "master" version := "master"
if githubSlugWithPathRegex.MatchString(urlString) { if githubSlugWithPathRegex.MatchString(p) {
if githubSlugWithPathAndVersionRegex.MatchString(urlString) { if githubSlugWithPathAndVersionRegex.MatchString(p) {
matches := githubSlugWithPathAndVersionRegex.FindStringSubmatch(urlString) matches := githubSlugWithPathAndVersionRegex.FindStringSubmatch(p)
user = matches[1] user = matches[1]
repo = matches[2] repo = matches[2]
subdir = matches[3] subdir = matches[3]
version = matches[4] version = matches[4]
name = path.Base(subdir) name = path.Base(subdir)
} else { } else {
matches := githubSlugWithPathRegex.FindStringSubmatch(urlString) matches := githubSlugWithPathRegex.FindStringSubmatch(p)
user = matches[1] user = matches[1]
repo = matches[2] repo = matches[2]
subdir = matches[3] subdir = matches[3]
name = path.Base(subdir) name = path.Base(subdir)
} }
} else { } else {
if githubSlugWithVersionRegex.MatchString(urlString) { if githubSlugWithVersionRegex.MatchString(p) {
matches := githubSlugWithVersionRegex.FindStringSubmatch(urlString) matches := githubSlugWithVersionRegex.FindStringSubmatch(p)
user = matches[1] user = matches[1]
repo = matches[2] repo = matches[2]
name = repo name = repo
version = matches[3] version = matches[3]
} else { } else {
matches := githubSlugRegex.FindStringSubmatch(urlString) matches := githubSlugRegex.FindStringSubmatch(p)
user = matches[1] user = matches[1]
repo = matches[2] repo = matches[2]
name = repo name = repo
@ -205,3 +210,39 @@ func parseGithubDependency(urlString string) *spec.Dependency {
Version: version, Version: version,
} }
} }
func parseLocalDependency(p string) *spec.Dependency {
if p == "" {
return nil
}
if strings.HasPrefix(p, "github.com") {
return nil
}
if strings.HasPrefix(p, "git+ssh") {
return nil
}
clean := filepath.Clean(p)
info, err := os.Stat(clean)
if err != nil {
wd, _ := os.Getwd()
fmt.Println(err, wd)
return nil
}
if !info.IsDir() {
return nil
}
return &spec.Dependency{
Name: info.Name(),
Source: spec.Source{
GitSource: &spec.GitSource{
Remote: ".",
Subdir: clean,
},
},
Version: ".",
}
}

View file

@ -15,6 +15,7 @@
package main package main
import ( import (
"os"
"testing" "testing"
"github.com/jsonnet-bundler/jsonnet-bundler/spec" "github.com/jsonnet-bundler/jsonnet-bundler/spec"
@ -22,6 +23,13 @@ import (
) )
func TestParseDepedency(t *testing.T) { func TestParseDepedency(t *testing.T) {
const testFolder = "test/jsonnet/foobar"
err := os.MkdirAll(testFolder, os.ModePerm)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll("test")
tests := []struct { tests := []struct {
name string name string
path string path string
@ -65,6 +73,20 @@ func TestParseDepedency(t *testing.T) {
Version: "master", Version: "master",
}, },
}, },
{
name: "local",
path: testFolder,
want: &spec.Dependency{
Name: "foobar",
Source: spec.Source{
GitSource: &spec.GitSource{
Remote: ".",
Subdir: "test/jsonnet/foobar",
},
},
Version: ".",
},
},
} }
for _, tt := range tests { for _, tt := range tests {
_ = t.Run(tt.name, func(t *testing.T) { _ = t.Run(tt.name, func(t *testing.T) {