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

Commit

Permalink
Add -gopath flag to init
Browse files Browse the repository at this point in the history
- Separate network mode and GOPATH mode of operations.
- In GOPATH mode, fetch projectData by searching through the current
GOPATH. Solve the dependency constraints based on the projects found on
disk and use network to solve for projects not found on disk.
- In Network mode, do not check GOPATH or any ondisk projects. Solve the
dependency constraints completely over network.
  • Loading branch information
darkowlzz committed May 2, 2017
1 parent d010781 commit b9395aa
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ func (cmd *initCommand) Hidden() bool { return false }

func (cmd *initCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies")
}

type initCommand struct {
noExamples bool
gopath bool
}

func trimPathPrefix(p1, p2 string) string {
Expand Down Expand Up @@ -112,39 +114,50 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
sm.UseDefaultSignalHandling()
defer sm.Release()

pd, err := getProjectData(ctx, pkgT, cpr, sm)
if err != nil {
return err
}
// Create empty manifest, lock and projectData, and fill them according to
// init operation modes. If gopath flag is set, fetch projectData by searching
// through GOPATH, else operate in network mode, solve all the dependencies
// over network.
m := &dep.Manifest{
Dependencies: pd.constraints,
Dependencies: make(gps.ProjectConstraints),
}
l := &dep.Lock{}
var pd projectData

// Make an initial lock from what knowledge we've collected about the
// versions on disk
l := &dep.Lock{
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
}
if cmd.gopath {
pd, err = getProjectData(ctx, pkgT, cpr, sm)
if err != nil {
return err
}

for pr, v := range pd.ondisk {
// That we have to chop off these path prefixes is a symptom of
// a problem in gps itself
pkgs := make([]string, 0, len(pd.dependencies[pr]))
prslash := string(pr) + "/"
for _, pkg := range pd.dependencies[pr] {
if pkg == string(pr) {
pkgs = append(pkgs, ".")
} else {
pkgs = append(pkgs, trimPathPrefix(pkg, prslash))
}
m.Dependencies = pd.constraints

// Make an initial lock from what knowledge we've collected about the
// versions on disk
l = &dep.Lock{
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
}

l.P = append(l.P, gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
)
for pr, v := range pd.ondisk {
// That we have to chop off these path prefixes is a symptom of
// a problem in gps itself
pkgs := make([]string, 0, len(pd.dependencies[pr]))
prslash := string(pr) + "/"
for _, pkg := range pd.dependencies[pr] {
if pkg == string(pr) {
pkgs = append(pkgs, ".")
} else {
pkgs = append(pkgs, trimPathPrefix(pkg, prslash))
}
}

l.P = append(l.P, gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
)
}
}

// Run solver with project versions found on disk
// Run solver with the available knowledge to solve the dependency constraints
internal.Vlogf("Solving...")
params := gps.SolveParameters{
RootDir: root,
Expand All @@ -170,14 +183,22 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
}
l = dep.LockFromInterface(soln)

// Pick notondisk project constraints from solution and add to manifest
for k, _ := range pd.notondisk {
for _, x := range l.Projects() {
if k == x.Ident().ProjectRoot {
m.Dependencies[k] = getProjectPropertiesFromVersion(x.Version())
break
// Populate manifest based on operation mode.
if cmd.gopath {
// Pick notondisk project constraints from solution and add to manifest
for k, _ := range pd.notondisk {
for _, x := range l.Projects() {
if k == x.Ident().ProjectRoot {
m.Dependencies[k] = getProjectPropertiesFromVersion(x.Version())
break
}
}
}
} else {
// Pick all the solved projects from the above solution and add to manifest
for _, x := range l.Projects() {
m.Dependencies[x.Ident().ProjectRoot] = getProjectPropertiesFromVersion(x.Version())
}
}

// Run gps.Prepare with appropriate constraint solutions from solve run
Expand Down

0 comments on commit b9395aa

Please sign in to comment.