Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 4a51515

Browse files
committed
Read other dependency mgrs cfg during init
Extract initial root manifest generation into an analyzer which combines evaluating the contents of the GOPATH with importing cfg from other managers.
1 parent e7bd278 commit 4a51515

19 files changed

+723
-83
lines changed

cmd/dep/compositeAnalyzer.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"github.com/golang/dep"
9+
"github.com/golang/dep/internal/gps"
10+
"github.com/pkg/errors"
11+
)
12+
13+
// compositeAnalyzer overlays configuration from multiple analyzers
14+
type compositeAnalyzer struct {
15+
// Analyzers is the set of analyzers to apply, last one wins any conflicts
16+
Analyzers []rootProjectAnalyzer
17+
}
18+
19+
func (a compositeAnalyzer) DeriveRootManifestAndLock(path string, n gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
20+
var rootM *dep.Manifest
21+
var rootL *dep.Lock
22+
23+
for _, a := range a.Analyzers {
24+
m, l, err := a.DeriveRootManifestAndLock(path, n)
25+
if err != nil {
26+
return nil, nil, errors.Wrapf(err, "%T.DeriveRootManifestAndLock", a)
27+
}
28+
29+
if rootM == nil && rootL == nil {
30+
rootM = m
31+
rootL = l
32+
} else {
33+
// Overlay the changes from the analyzer on-top of the previous analyzer's work
34+
if m != nil {
35+
for pkg, prj := range m.Dependencies {
36+
rootM.Dependencies[pkg] = prj
37+
}
38+
for pkg, prj := range m.Ovr {
39+
rootM.Ovr[pkg] = prj
40+
}
41+
for _, pkg := range m.Required {
42+
if !contains(rootM.Required, pkg) {
43+
rootM.Required = append(rootM.Required, pkg)
44+
}
45+
}
46+
for _, pkg := range m.Ignored {
47+
if !contains(rootM.Ignored, pkg) {
48+
rootM.Ignored = append(rootM.Ignored, pkg)
49+
}
50+
}
51+
}
52+
53+
if l != nil {
54+
for _, lp := range l.P {
55+
for i, existingLP := range rootL.P {
56+
if lp.Ident().ProjectRoot == existingLP.Ident().ProjectRoot {
57+
rootL.P[i] = lp
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
return rootM, rootL, nil
66+
}
67+
68+
func (a compositeAnalyzer) PostSolveShenanigans(m *dep.Manifest, l *dep.Lock) {
69+
for _, a := range a.Analyzers {
70+
a.PostSolveShenanigans(m, l)
71+
}
72+
}

cmd/dep/compositeAnalyzer_test.go

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"testing"
9+
10+
"github.com/golang/dep"
11+
"github.com/golang/dep/internal/gps"
12+
)
13+
14+
type testRootProjectAnalyzer struct {
15+
*dep.Manifest
16+
*dep.Lock
17+
}
18+
19+
func (a testRootProjectAnalyzer) DeriveRootManifestAndLock(path string, n gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
20+
return a.Manifest, a.Lock, nil
21+
}
22+
23+
func (a testRootProjectAnalyzer) PostSolveShenanigans(*dep.Manifest, *dep.Lock) {
24+
// do nothing
25+
}
26+
27+
func TestCompositeAnalyzer_ManifestDependencies(t *testing.T) {
28+
pkg := gps.ProjectRoot("github.com/sdboyer/deptest")
29+
m1 := &dep.Manifest{
30+
Dependencies: gps.ProjectConstraints{
31+
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^1.0.0")},
32+
},
33+
}
34+
m2 := &dep.Manifest{
35+
Dependencies: gps.ProjectConstraints{
36+
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^2.0.0")},
37+
},
38+
}
39+
c := compositeAnalyzer{
40+
Analyzers: []rootProjectAnalyzer{
41+
testRootProjectAnalyzer{Manifest: m1},
42+
testRootProjectAnalyzer{Manifest: m2},
43+
},
44+
}
45+
46+
rm, _, err := c.DeriveRootManifestAndLock("", "")
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
51+
if rm == nil {
52+
t.Fatal("Expected the root manifest to not be nil")
53+
}
54+
55+
dep, has := rm.Dependencies[pkg]
56+
if !has {
57+
t.Fatal("Expected the root manifest to contain the test project")
58+
}
59+
60+
wantC := "^2.0.0"
61+
gotC := dep.Constraint.String()
62+
if wantC != gotC {
63+
t.Fatalf("Expected the test project to be constrained to '%s', got '%s'", wantC, gotC)
64+
}
65+
}
66+
67+
func TestCompositeAnalyzer_ManifestOverrides(t *testing.T) {
68+
pkg := gps.ProjectRoot("github.com/sdboyer/deptest")
69+
m1 := &dep.Manifest{
70+
Ovr: gps.ProjectConstraints{
71+
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^1.0.0")},
72+
},
73+
}
74+
m2 := &dep.Manifest{
75+
Ovr: gps.ProjectConstraints{
76+
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^2.0.0")},
77+
},
78+
}
79+
c := compositeAnalyzer{
80+
Analyzers: []rootProjectAnalyzer{
81+
testRootProjectAnalyzer{Manifest: m1},
82+
testRootProjectAnalyzer{Manifest: m2},
83+
},
84+
}
85+
86+
rm, _, err := c.DeriveRootManifestAndLock("", "")
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
91+
if rm == nil {
92+
t.Fatal("Expected the root manifest to not be nil")
93+
}
94+
95+
dep, has := rm.Ovr[pkg]
96+
if !has {
97+
t.Fatal("Expected the root manifest to contain the test project override")
98+
}
99+
100+
wantC := "^2.0.0"
101+
gotC := dep.Constraint.String()
102+
if wantC != gotC {
103+
t.Fatalf("Expected the test project to be overridden to '%s', got '%s'", wantC, gotC)
104+
}
105+
}
106+
107+
func TestCompositeAnalyzer_ManifestRequired(t *testing.T) {
108+
pkg1 := "github.com/sdboyer/deptest"
109+
pkg2 := "github.com/sdboyer/deptestdos"
110+
m1 := &dep.Manifest{
111+
Required: []string{pkg1},
112+
}
113+
m2 := &dep.Manifest{
114+
Required: []string{pkg2},
115+
}
116+
c := compositeAnalyzer{
117+
Analyzers: []rootProjectAnalyzer{
118+
testRootProjectAnalyzer{Manifest: m1},
119+
testRootProjectAnalyzer{Manifest: m2},
120+
},
121+
}
122+
123+
rm, _, err := c.DeriveRootManifestAndLock("", "")
124+
if err != nil {
125+
t.Fatal(err)
126+
}
127+
128+
if rm == nil {
129+
t.Fatal("Expected the root manifest to not be nil")
130+
}
131+
132+
if len(rm.Required) != 2 {
133+
t.Fatalf("Expected the root manifest to contain 2 required packages, got %d", len(rm.Required))
134+
}
135+
136+
if rm.Required[0] != pkg1 {
137+
t.Fatalf("Expected the first required package to be '%s', got '%s'", pkg1, rm.Required[0])
138+
}
139+
140+
if rm.Required[1] != pkg2 {
141+
t.Fatalf("Expected the second required package to be '%s', got '%s'", pkg2, rm.Required[1])
142+
}
143+
}
144+
145+
func TestCompositeAnalyzer_ManifestIgnored(t *testing.T) {
146+
pkg1 := "github.com/sdboyer/deptest"
147+
pkg2 := "github.com/sdboyer/deptestdos"
148+
m1 := &dep.Manifest{
149+
Ignored: []string{pkg1},
150+
}
151+
m2 := &dep.Manifest{
152+
Ignored: []string{pkg2},
153+
}
154+
c := compositeAnalyzer{
155+
Analyzers: []rootProjectAnalyzer{
156+
testRootProjectAnalyzer{Manifest: m1},
157+
testRootProjectAnalyzer{Manifest: m2},
158+
},
159+
}
160+
161+
rm, _, err := c.DeriveRootManifestAndLock("", "")
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
166+
if rm == nil {
167+
t.Fatal("Expected the root manifest to not be nil")
168+
}
169+
170+
if len(rm.Ignored) != 2 {
171+
t.Fatalf("Expected the root manifest to contain 2 ignored packages, got %d", len(rm.Ignored))
172+
}
173+
174+
if rm.Ignored[0] != pkg1 {
175+
t.Fatalf("Expected the first ignored package to be '%s', got '%s'", pkg1, rm.Ignored[0])
176+
}
177+
178+
if rm.Ignored[1] != pkg2 {
179+
t.Fatalf("Expected the second ignored package to be '%s', got '%s'", pkg2, rm.Ignored[1])
180+
}
181+
}

cmd/dep/glideConfig.go

+37-31
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717

1818
type glideFiles struct {
1919
yaml glideYaml
20-
lock glideLock
21-
loggers *Loggers
20+
lock *glideLock
21+
loggers *dep.Loggers
2222
}
2323

24-
func newGlideFiles(loggers *Loggers) glideFiles {
25-
return glideFiles{loggers: loggers}
24+
func newGlideFiles(loggers *dep.Loggers) *glideFiles {
25+
return &glideFiles{loggers: loggers}
2626
}
2727

2828
type glideYaml struct {
@@ -44,7 +44,7 @@ type glidePackage struct {
4444
Repository string `yaml:"repo"`
4545
}
4646

47-
func (files glideFiles) load(projectDir string) error {
47+
func (files *glideFiles) load(projectDir string) error {
4848
y := filepath.Join(projectDir, glideYamlName)
4949
if files.loggers.Verbose {
5050
files.loggers.Err.Printf("dep: Loading %s", y)
@@ -59,22 +59,24 @@ func (files glideFiles) load(projectDir string) error {
5959
}
6060

6161
l := filepath.Join(projectDir, glideLockName)
62-
if files.loggers.Verbose {
63-
files.loggers.Err.Printf("dep: Loading %s", l)
64-
}
65-
lb, err := ioutil.ReadFile(l)
66-
if err != nil {
67-
return errors.Wrapf(err, "Unable to read %s", l)
68-
}
69-
err = yaml.Unmarshal(lb, &files.lock)
70-
if err != nil {
71-
return errors.Wrapf(err, "Unable to parse %s", l)
62+
if exists, _ := dep.IsRegular(l); exists {
63+
if files.loggers.Verbose {
64+
files.loggers.Err.Printf("dep: Loading %s", l)
65+
}
66+
lb, err := ioutil.ReadFile(l)
67+
if err != nil {
68+
return errors.Wrapf(err, "Unable to read %s", l)
69+
}
70+
err = yaml.Unmarshal(lb, &files.lock)
71+
if err != nil {
72+
return errors.Wrapf(err, "Unable to parse %s", l)
73+
}
7274
}
7375

7476
return nil
7577
}
7678

77-
func (files glideFiles) convert(projectName string) (*dep.Manifest, *dep.Lock, error) {
79+
func (files *glideFiles) convert(projectName string) (*dep.Manifest, *dep.Lock, error) {
7880
manifest := &dep.Manifest{
7981
Dependencies: make(gps.ProjectConstraints),
8082
}
@@ -105,24 +107,28 @@ func (files glideFiles) convert(projectName string) (*dep.Manifest, *dep.Lock, e
105107
}
106108
}
107109

108-
lock := &dep.Lock{}
110+
var lock *dep.Lock
111+
if files.lock != nil {
112+
lock = &dep.Lock{}
113+
114+
lockDep := func(pkg glidePackage) {
115+
id := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Name)}
116+
c, has := manifest.Dependencies[id.ProjectRoot]
117+
if has {
118+
id.Source = c.Source
119+
}
120+
version := gps.Revision(pkg.Reference)
109121

110-
lockDep := func(pkg glidePackage) {
111-
id := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Name)}
112-
c, has := manifest.Dependencies[id.ProjectRoot]
113-
if has {
114-
id.Source = c.Source
122+
lp := gps.NewLockedProject(id, version, nil)
123+
lock.P = append(lock.P, lp)
115124
}
116-
version := gps.Revision(pkg.Reference)
117125

118-
lp := gps.NewLockedProject(id, version, nil)
119-
lock.P = append(lock.P, lp)
120-
}
121-
for _, pkg := range files.lock.Imports {
122-
lockDep(pkg)
123-
}
124-
for _, pkg := range files.lock.TestImports {
125-
lockDep(pkg)
126+
for _, pkg := range files.lock.Imports {
127+
lockDep(pkg)
128+
}
129+
for _, pkg := range files.lock.TestImports {
130+
lockDep(pkg)
131+
}
126132
}
127133

128134
return manifest, lock, nil

0 commit comments

Comments
 (0)