2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
5
- package main
5
+ package base
6
6
7
7
import (
8
8
"log"
@@ -13,29 +13,30 @@ import (
13
13
"github.com/pkg/errors"
14
14
)
15
15
16
- // baseImporter provides a common implementation for importing from other
16
+ // Importer provides a common implementation for importing from other
17
17
// dependency managers.
18
- type baseImporter struct {
19
- logger * log.Logger
20
- verbose bool
21
- sm gps.SourceManager
22
- manifest * dep.Manifest
23
- lock * dep.Lock
18
+ type Importer struct {
19
+ sm gps.SourceManager
20
+
21
+ Logger * log.Logger
22
+ Verbose bool
23
+ Manifest * dep.Manifest
24
+ Lock * dep.Lock
24
25
}
25
26
26
- // newBaseImporter creates a new baseImporter for embedding in an importer.
27
- func newBaseImporter (logger * log.Logger , verbose bool , sm gps.SourceManager ) * baseImporter {
28
- return & baseImporter {
29
- logger : logger ,
30
- verbose : verbose ,
31
- manifest : dep .NewManifest (),
32
- lock : & dep.Lock {},
27
+ // NewImporter creates a new Importer for embedding in an importer.
28
+ func NewImporter (logger * log.Logger , verbose bool , sm gps.SourceManager ) * Importer {
29
+ return & Importer {
30
+ Logger : logger ,
31
+ Verbose : verbose ,
32
+ Manifest : dep .NewManifest (),
33
+ Lock : & dep.Lock {},
33
34
sm : sm ,
34
35
}
35
36
}
36
37
37
38
// isTag determines if the specified value is a tag (plain or semver).
38
- func (i * baseImporter ) isTag (pi gps.ProjectIdentifier , value string ) (bool , gps.Version , error ) {
39
+ func (i * Importer ) isTag (pi gps.ProjectIdentifier , value string ) (bool , gps.Version , error ) {
39
40
versions , err := i .sm .ListVersions (pi )
40
41
if err != nil {
41
42
return false , nil , errors .Wrapf (err , "unable to list versions for %s(%s)" , pi .ProjectRoot , pi .Source )
@@ -58,7 +59,7 @@ func (i *baseImporter) isTag(pi gps.ProjectIdentifier, value string) (bool, gps.
58
59
// project based on the locked revision and the constraint from the manifest.
59
60
// First try matching the revision to a version, then try the constraint from the
60
61
// manifest, then finally the revision.
61
- func (i * baseImporter ) lookupVersionForLockedProject (pi gps.ProjectIdentifier , c gps.Constraint , rev gps.Revision ) (gps.Version , error ) {
62
+ func (i * Importer ) lookupVersionForLockedProject (pi gps.ProjectIdentifier , c gps.Constraint , rev gps.Revision ) (gps.Version , error ) {
62
63
// Find the version that goes with this revision, if any
63
64
versions , err := i .sm .ListVersions (pi )
64
65
if err != nil {
@@ -98,9 +99,9 @@ func (i *baseImporter) lookupVersionForLockedProject(pi gps.ProjectIdentifier, c
98
99
return rev , nil
99
100
}
100
101
101
- // importedPackage is a common intermediate representation of a package imported
102
+ // ImportedPackage is a common intermediate representation of a package imported
102
103
// from an external tool's configuration.
103
- type importedPackage struct {
104
+ type ImportedPackage struct {
104
105
// Required. The package path, not necessarily the project root.
105
106
Name string
106
107
@@ -118,11 +119,11 @@ type importedPackage struct {
118
119
// for the same project root.
119
120
type importedProject struct {
120
121
Root gps.ProjectRoot
121
- importedPackage
122
+ ImportedPackage
122
123
}
123
124
124
125
// loadPackages consolidates all package references into a set of project roots.
125
- func (i * baseImporter ) loadPackages (packages []importedPackage ) ([]importedProject , error ) {
126
+ func (i * Importer ) loadPackages (packages []ImportedPackage ) ([]importedProject , error ) {
126
127
// preserve the original order of the packages so that messages that
127
128
// are printed as they are processed are in a consistent order.
128
129
orderedProjects := make ([]importedProject , 0 , len (packages ))
@@ -161,7 +162,7 @@ func (i *baseImporter) loadPackages(packages []importedPackage) ([]importedProje
161
162
return orderedProjects , nil
162
163
}
163
164
164
- // importPackages loads imported packages into the manifest and lock.
165
+ // ImportPackages loads imported packages into the manifest and lock.
165
166
// - defaultConstraintFromLock specifies if a constraint should be defaulted
166
167
// based on the locked version when there wasn't a constraint hint.
167
168
//
@@ -172,7 +173,7 @@ func (i *baseImporter) loadPackages(packages []importedPackage) ([]importedProje
172
173
// * Revision constraints are ignored.
173
174
// * Versions that don't satisfy the constraint, drop the constraint.
174
175
// * Untagged revisions ignore non-branch constraint hints.
175
- func (i * baseImporter ) importPackages (packages []importedPackage , defaultConstraintFromLock bool ) (err error ) {
176
+ func (i * Importer ) ImportPackages (packages []ImportedPackage , defaultConstraintFromLock bool ) (err error ) {
176
177
projects , err := i .loadPackages (packages )
177
178
if err != nil {
178
179
return err
@@ -206,54 +207,54 @@ func (i *baseImporter) importPackages(packages []importedPackage, defaultConstra
206
207
version , err = i .lookupVersionForLockedProject (pc .Ident , pc .Constraint , revision )
207
208
if err != nil {
208
209
version = nil
209
- i .logger .Println (err )
210
+ i .Logger .Println (err )
210
211
}
211
212
}
212
213
213
214
// Default the constraint based on the locked version
214
215
if defaultConstraintFromLock && prj .ConstraintHint == "" && version != nil {
215
- props := getProjectPropertiesFromVersion (version )
216
- if props . Constraint != nil {
217
- pc .Constraint = props . Constraint
216
+ c := i . convertToConstraint (version )
217
+ if c != nil {
218
+ pc .Constraint = c
218
219
}
219
220
}
220
221
}
221
222
222
223
// Ignore pinned constraints
223
224
if i .isConstraintPinned (pc .Constraint ) {
224
- if i .verbose {
225
- i .logger .Printf (" Ignoring pinned constraint %v for %v.\n " , pc .Constraint , pc .Ident )
225
+ if i .Verbose {
226
+ i .Logger .Printf (" Ignoring pinned constraint %v for %v.\n " , pc .Constraint , pc .Ident )
226
227
}
227
228
pc .Constraint = gps .Any ()
228
229
}
229
230
230
231
// Ignore constraints which conflict with the locked revision, so that
231
232
// solve doesn't later change the revision to satisfy the constraint.
232
233
if ! i .testConstraint (pc .Constraint , version ) {
233
- if i .verbose {
234
- i .logger .Printf (" Ignoring constraint %v for %v because it would invalidate the locked version %v.\n " , pc .Constraint , pc .Ident , version )
234
+ if i .Verbose {
235
+ i .Logger .Printf (" Ignoring constraint %v for %v because it would invalidate the locked version %v.\n " , pc .Constraint , pc .Ident , version )
235
236
}
236
237
pc .Constraint = gps .Any ()
237
238
}
238
239
239
- i .manifest .Constraints [pc .Ident .ProjectRoot ] = gps.ProjectProperties {
240
+ i .Manifest .Constraints [pc .Ident .ProjectRoot ] = gps.ProjectProperties {
240
241
Source : pc .Ident .Source ,
241
242
Constraint : pc .Constraint ,
242
243
}
243
- fb .NewConstraintFeedback (pc , fb .DepTypeImported ).LogFeedback (i .logger )
244
+ fb .NewConstraintFeedback (pc , fb .DepTypeImported ).LogFeedback (i .Logger )
244
245
245
246
if version != nil {
246
247
lp := gps .NewLockedProject (pc .Ident , version , nil )
247
- i .lock .P = append (i .lock .P , lp )
248
- fb .NewLockedProjectFeedback (lp , fb .DepTypeImported ).LogFeedback (i .logger )
248
+ i .Lock .P = append (i .Lock .P , lp )
249
+ fb .NewLockedProjectFeedback (lp , fb .DepTypeImported ).LogFeedback (i .Logger )
249
250
}
250
251
}
251
252
252
253
return nil
253
254
}
254
255
255
256
// isConstraintPinned returns if a constraint is pinned to a specific revision.
256
- func (i * baseImporter ) isConstraintPinned (c gps.Constraint ) bool {
257
+ func (i * Importer ) isConstraintPinned (c gps.Constraint ) bool {
257
258
if version , isVersion := c .(gps.Version ); isVersion {
258
259
switch version .Type () {
259
260
case gps .IsRevision , gps .IsVersion :
@@ -264,7 +265,7 @@ func (i *baseImporter) isConstraintPinned(c gps.Constraint) bool {
264
265
}
265
266
266
267
// testConstraint verifies that the constraint won't invalidate the locked version.
267
- func (i * baseImporter ) testConstraint (c gps.Constraint , v gps.Version ) bool {
268
+ func (i * Importer ) testConstraint (c gps.Constraint , v gps.Version ) bool {
268
269
// Assume branch constraints are satisfied
269
270
if version , isVersion := c .(gps.Version ); isVersion {
270
271
if version .Type () == gps .IsBranch {
@@ -275,3 +276,18 @@ func (i *baseImporter) testConstraint(c gps.Constraint, v gps.Version) bool {
275
276
276
277
return c .Matches (v )
277
278
}
279
+
280
+ // convertToConstraint turns a version into a constraint.
281
+ // Semver tags are converted to a range with the caret operator.
282
+ func (i * Importer ) convertToConstraint (v gps.Version ) gps.Constraint {
283
+ if v .Type () == gps .IsSemver {
284
+ c , err := gps .NewSemverConstraintIC (v .String ())
285
+ if err != nil {
286
+ // This should never fail, because the type is semver.
287
+ // If it does fail somehow, don't let that impact the import.
288
+ return nil
289
+ }
290
+ return c
291
+ }
292
+ return v
293
+ }
0 commit comments