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 4 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
76 changes: 76 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,44 @@ 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{
Language: pkg.Language,
LockVersion: gx.LockVersion,
}
lockFile.Deps = make(map[string]gx.LockDep)

if err := genLockDeps(pkg, lockFile.Deps, 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 +822,43 @@ var DevCopyCommand = cli.Command{
},
}

func genLockDeps(pkg *Package, deps map[string]gx.LockDep, done map[string]bool, ignoreConflict bool) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use the DependencyQueue here? (I'm not sure, since it belongs to the gx repo).

Ideally I would like to have a function that just returns all the (unique) dependencies of a package. From what I'm seeing in the gx/gx-go code bases we're doing a lot of "get me all your direct and transitive dependencies" (converting Dependency to Package) types of operation.

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.LockDep{
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