Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic lockfile support #49

Merged
merged 5 commits into from
Sep 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func main() {
UpdateCommand,
DvcsDepsCommand,
LinkCommand,
LockGenCommand,

DevCopyCommand,
// Go tool compat:
Expand Down Expand Up @@ -129,6 +130,45 @@ var DepMapCommand = cli.Command{
},
}

var LockGenCommand = cli.Command{
Name: "lock-gen",
Usage: "Generate a lock file",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "ignore-conflicts",
Usage: "Does not error on conflicting import in sub-packages",
},
},
Action: func(c *cli.Context) error {
ignoreConflict := c.Bool("ignore-conflicts")

pkg, err := LoadPackageFile(gx.PkgFileName)
if err != nil {
return err
}

done := make(map[string]bool)
lockFile := gx.LockFile{
LockVersion: gx.LockVersion,
}
lockFile.Language = pkg.Language
lockFile.Deps = make(map[string]map[string]gx.Lock)
lockFile.Deps[pkg.Language] = make(map[string]gx.Lock)

if err := genLockDeps(pkg, lockFile.Deps[pkg.Language], done, ignoreConflict); err != nil {
return err
}

m, err := json.MarshalIndent(lockFile, "", " ")
if err != nil {
return err
}

_, err = os.Stdout.Write(m)
return err
},
}

var HookCommand = cli.Command{
Name: "hook",
Usage: "go specific hooks to be called by the gx tool",
Expand Down Expand Up @@ -783,6 +823,43 @@ var DevCopyCommand = cli.Command{
},
}

func genLockDeps(pkg *Package, deps map[string]gx.Lock, done map[string]bool, ignoreConflict bool) error {
for _, dep := range pkg.Dependencies {
if done[dep.Hash] {
continue
}

done[dep.Hash] = true

var cpkg Package
err := gx.LoadPackage(&cpkg, pkg.Language, dep.Hash)
if err != nil {
if os.IsNotExist(err) {
VLog("LoadPackage error: ", err)
return fmt.Errorf("package %s (%s) not found", dep.Name, dep.Hash)
}
return err
}

ref := fmt.Sprintf("/ipfs/%s/%s", dep.Hash, dep.Name)
Copy link
Collaborator

@schomatis schomatis Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be (along with the gx.LockDep{ line) the actual core of the function where we are transforming a Dependency to a LockDep/Ref, could we add a comment to highlight this? (And even better, make it into a function.)

if d, found := deps[cpkg.Gx.DvcsImport]; found {
if !ignoreConflict && ref != d.Ref {
return fmt.Errorf("Found a duplicate import %s for package %s", cpkg.Gx.DvcsImport, pkg.Name)
}
} else {
deps[cpkg.Gx.DvcsImport] = gx.Lock{
Ref: ref,
}
}

if err := genLockDeps(&cpkg, deps, done, ignoreConflict); err != nil {
return err
}
}

return nil
}

func devCopySymlinking(root string, pkg *Package, done map[string]bool) error {
for _, dep := range pkg.Dependencies {
if done[dep.Hash] {
Expand Down