Skip to content

Commit

Permalink
improve publishing workflow a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Mar 23, 2016
1 parent e0eec5a commit fb5381d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 9 deletions.
4 changes: 4 additions & 0 deletions gxutil/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (pm *PM) GetPackageTo(hash, out string) (*Package, error) {
}

func FindPackageInDir(pkg interface{}, dir string) error {
if err := LoadPackageFile(pkg, filepath.Join(dir, PkgFileName)); err == nil {
return nil
}

name, err := PackageNameInDir(dir)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions gxutil/pkgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

. "github.com/whyrusleeping/stump"
log "github.com/whyrusleeping/stump"
)

type PackageBase struct {
Expand All @@ -19,7 +19,7 @@ type PackageBase struct {
Build string `json:"build,omitempty"`
Test string `json:"test,omitempty"`
Language string `json:"language,omitempty"`
Copyright string `json:"copyright,omitempty"`
License string `json:"license"`
Issues string `json:"issues_url"`
GxVersion string `json:"gx_version"`
}
Expand Down Expand Up @@ -79,11 +79,12 @@ func (pkg *PackageBase) FindDep(ref string) *Dependency {
}

func (pkg *PackageBase) ForEachDep(cb func(dep *Dependency, pkg *Package) error) error {
log.VLog(" - foreachdep: %s", pkg.Name)
for _, dep := range pkg.Dependencies {
var cpkg Package
err := LoadPackage(&cpkg, pkg.Language, dep.Hash)
if err != nil {
VLog("LoadPackage error: ", err)
log.VLog("LoadPackage error: ", err)
return fmt.Errorf("package %s (%s) not found", dep.Name, dep.Hash)
}

Expand Down
10 changes: 10 additions & 0 deletions gxutil/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
)

func (pm *PM) PublishPackage(dir string, pkg *PackageBase) (string, error) {
// make sure we have the actual package dir, and not a hashdir
if _, err := os.Stat(filepath.Join(dir, PkgFileName)); err != nil {
// try appending the package name
_, err = os.Stat(filepath.Join(dir, pkg.Name, PkgFileName))
if err != nil {
return "", fmt.Errorf("%s did not contain a package!")
}
dir = filepath.Join(dir, pkg.Name)
}

gitig, err := gi.CompileIgnoreFile(filepath.Join(dir, ".gitignore"))
if err != nil && !os.IsNotExist(err) {
return "", err
Expand Down
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ EXAMPLE:
Name: "global",
Usage: "install new package in global namespace",
},
cli.BoolFlag{
Name: "with-deps",
Usage: "experimental feature to recursively update child deps too",
},
},
Action: func(c *cli.Context) {
if len(c.Args()) < 2 {
Expand All @@ -390,6 +394,7 @@ EXAMPLE:
if olddep == nil {
log.Fatal("unknown package: ", existing)
}
oldhash = olddep.Hash

ipath, err := gx.InstallPath(pkg.Language, cwd, c.Bool("global"))
if err != nil {
Expand All @@ -411,14 +416,26 @@ continue?`, olddep.Name, olddep.Hash, npkg.Name, target)
}
}

log.VLog("checking for potential package naming collisions...")
err = updateCollisionCheck(pkg, olddep, nil)
log.VLog("running pre update hook...")
err = gx.TryRunHook("pre-update", pkg.Language, existing)
if err != nil {
log.Fatal("update sanity check: ", err)
log.Fatal(err)
}

if c.Bool("with-deps") {
err := RecursiveDepUpdate(pkg, oldhash, target)
if err != nil {
log.Fatal(err)
}
} else {
log.VLog("checking for potential package naming collisions...")
err = updateCollisionCheck(pkg, olddep, nil)
if err != nil {
log.Fatal("update sanity check: ", err)
}
log.VLog(" - no collisions found for updated package")
}
log.VLog(" - no collisions found for updated package")

oldhash = olddep.Hash
olddep.Hash = target
olddep.Version = npkg.Version

Expand Down
2 changes: 1 addition & 1 deletion tests/t0010-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_description="test package init"
test_init_ipfs
test_launch_ipfs_daemon

pkg_hash="QmV2yyUF4AAkU8rCbCtH32qt3Tch2KHJQ2ugv37ipVWY4t"
pkg_hash="QmUHAqmEZoin6G4vsNEriA6ocNUKYssbeCCRJacPQmzsoA"

test_expect_success "setup test package" '
mkdir mypkg &&
Expand Down
100 changes: 100 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"

gx "github.com/whyrusleeping/gx/gxutil"
log "github.com/whyrusleeping/stump"
)

func RecursiveDepUpdate(pkg *gx.Package, from, to string) error {
log.Log("recursively updating %s to %s", from, to)
todo := map[string]string{
from: to,
}
cwd, err := os.Getwd()
if err != nil {
return err
}

checked := make(map[string]bool)

_, err = cascadingUpdate(pkg, cwd, todo, checked)
if err != nil {
return err
}

return nil
}

func cascadingUpdate(cur *gx.Package, dir string, updates map[string]string, checked map[string]bool) (bool, error) {
log.Log("cascading update of package %s in %s", cur.Name, dir)
var changed bool
err := cur.ForEachDep(func(dep *gx.Dependency, child *gx.Package) error {
if checked[dep.Hash] {
return nil
}
log.Log(" - processing %s...", dep.Name)

if to, ok := updates[dep.Hash]; ok {
log.Log(" ==> updating dep %s on %s", dep.Name, cur.Name)
dep.Hash = to
dep.Version = child.Version
changed = true
} else {
nchild, err := fetchAndUpdate(dep.Hash, updates, checked)
if err != nil {
return err
}

if nchild != "" {
updates[dep.Hash] = nchild
dep.Hash = nchild
changed = true
} else {
checked[dep.Hash] = true
}
}

return nil
})

if err != nil {
return false, err
}

return changed, nil
}

func fetchAndUpdate(tofetch string, updates map[string]string, checked map[string]bool) (string, error) {
log.Log("fetch and update: %s", tofetch)
dir, err := ioutil.TempDir("", "gx-update")
if err != nil {
return "", err
}

dir = filepath.Join(dir, tofetch)

pkg, err := pm.GetPackageTo(tofetch, dir)
if err != nil {
return "", err
}

changed, err := cascadingUpdate(pkg, dir, updates, checked)
if err != nil {
return "", err
}

if changed {
err := gx.SavePackageFile(pkg, filepath.Join(dir, pkg.Name, gx.PkgFileName))
if err != nil {
return "", err
}

return pm.PublishPackage(dir, &pkg.PackageBase)
}

return "", nil
}

0 comments on commit fb5381d

Please sign in to comment.