Skip to content
Snippets Groups Projects
Commit 9c25a860 authored by Benjamin Bollen's avatar Benjamin Bollen Committed by GitHub
Browse files

Merge pull request #614 from silasdavis/remove-hell

Remove hell
parents 6d2f5180 04bc5c43
No related branches found
No related tags found
No related merge requests found
...@@ -67,12 +67,6 @@ install_vendor: ...@@ -67,12 +67,6 @@ install_vendor:
go get github.com/Masterminds/glide go get github.com/Masterminds/glide
glide install glide install
# hell runs utility tool hell to selectively update glide dependencies
.PHONY: hell
hell:
go build -o ${REPO}/target/hell ./util/hell/cmd/hell/main.go
./target/hell $(filter-out $@,$(MAKECMDGOALS))
# Dumps Solidity interface contracts for SNatives # Dumps Solidity interface contracts for SNatives
.PHONY: snatives .PHONY: snatives
snatives: snatives:
......
...@@ -82,8 +82,6 @@ imports: ...@@ -82,8 +82,6 @@ imports:
version: c265cfa48dda6474e208715ca93e987829f572f8 version: c265cfa48dda6474e208715ca93e987829f572f8
- name: github.com/manucorporat/sse - name: github.com/manucorporat/sse
version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d
- name: github.com/Masterminds/glide
version: 869001d1571ce5f03fd0078bff188b9d3f272807
- name: github.com/mattn/go-colorable - name: github.com/mattn/go-colorable
version: ded68f7a9561c023e790de24279db7ebf473ea80 version: ded68f7a9561c023e790de24279db7ebf473ea80
- name: github.com/mattn/go-isatty - name: github.com/mattn/go-isatty
......
...@@ -28,8 +28,6 @@ import: ...@@ -28,8 +28,6 @@ import:
- package: github.com/Sirupsen/logrus - package: github.com/Sirupsen/logrus
version: ^0.11.0 version: ^0.11.0
- package: github.com/streadway/simpleuuid - package: github.com/streadway/simpleuuid
- package: github.com/Masterminds/glide
version: ~0.12.3
- package: github.com/Graylog2/go-gelf - package: github.com/Graylog2/go-gelf
- package: github.com/tendermint/tendermint - package: github.com/tendermint/tendermint
version: ~0.9.2 version: ~0.9.2
> Hell is other people's packages
While we wait for working package management in go we need a way to make
maintaining the glide.lock by hand less painful.
To interactively add a package run from the root:
```bash
go run ./util/hell/cmd/hell/main.go get --interactive github.com/tendermint/tendermint
```
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/hyperledger/burrow/util/hell"
"github.com/Masterminds/glide/action"
"github.com/Masterminds/glide/cache"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/msg"
"github.com/Masterminds/glide/path"
"github.com/Masterminds/glide/repo"
"github.com/Masterminds/glide/util"
"github.com/spf13/cobra"
)
func main() {
hellCmd := &cobra.Command{
Use: "hell",
Short: "Hell makes the most of it being warm",
Long: "",
Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
}
// Lock merge command
var baseGlideLockFile, depGlideLockFile string
lockMergeCmd := &cobra.Command{
Use: "lock-merge",
Short: "Merge glide.lock files together",
Long: "This command merges two glide.lock files into a single one by copying all dependencies " +
"from a base glide.lock and an override glide.lock to an output glide.lock with dependencies " +
"from override taking precedence over those from base.",
Run: func(cmd *cobra.Command, args []string) {
baseLockFile, err := cfg.ReadLockFile(baseGlideLockFile)
if err != nil {
fmt.Printf("Could not read file: %s\n", err)
os.Exit(1)
}
overrideLockFile, err := cfg.ReadLockFile(depGlideLockFile)
if err != nil {
fmt.Printf("Could not read file: %s\n", err)
os.Exit(1)
}
mergedLockFile, err := hell.MergeGlideLockFiles(baseLockFile, overrideLockFile)
if err != nil {
fmt.Printf("Could not merge lock files: %s\n", err)
os.Exit(1)
}
mergedBytes, err := mergedLockFile.Marshal()
if err != nil {
fmt.Printf("Could not marshal lock file: %s\n", err)
os.Exit(1)
}
os.Stdout.Write(mergedBytes)
},
}
lockMergeCmd.PersistentFlags().StringVarP(&baseGlideLockFile, "base", "b", "", "base lock file")
lockMergeCmd.PersistentFlags().StringVarP(&depGlideLockFile, "override", "o", "", "override lock file")
// Lock update
interactive := false
getTransitiveCmd := &cobra.Command{
Use: "get",
Short: "gets a remote dependency to this project along with its transtive dependencies.",
Long: "Gets a remote dependency and its transitive dependencies by adding the remote " +
"depednency to this project's glide.yaml and merging the remote dependency's " +
"glide.lock into this project's glide.lock",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
msg.Die("%s requires a single argument of the remote dependency\n", cmd.Name())
}
rootPackage, _ := util.NormalizeName(args[0])
// Add dependency to glide
installer := repo.NewInstaller()
action.Get(args, installer, false, true, false, !interactive, false)
// Now hunt down the repo cache
dep := action.EnsureConfig().Imports.Get(rootPackage)
key, err := cache.Key(dep.Remote())
if err != nil {
msg.Die("%s requires a single argument of the remote dependency\n", cmd.Name())
}
cacheDir := filepath.Join(cache.Location(), "src", key)
repos, err := dep.GetRepo(cacheDir)
if err != nil {
msg.Die("Could not get repo: %s", err)
}
version, err := repos.Version()
if err != nil {
msg.Die("Could not get version: %s", err)
}
dep.Pin = version
lockPath := filepath.Join(".", path.LockFile)
baseLockFile, err := cfg.ReadLockFile(lockPath)
if err != nil {
msg.Die("Could not read base lock file: %s", err)
}
overrideLockFile := &cfg.Lockfile{}
if path.HasLock(cacheDir) {
msg.Info("Found dependency lock file so merging into project lock file")
overrideLockFile, err = cfg.ReadLockFile(filepath.Join(cacheDir, path.LockFile))
if err != nil {
msg.Die("Could not read dependency lock file: %s", err)
}
}
// Add the package to glide lock too!
overrideLockFile.Imports = append(overrideLockFile.Imports, cfg.LockFromDependency(dep))
mergedLockFile, err := hell.MergeGlideLockFiles(baseLockFile, overrideLockFile)
if err != nil {
msg.Die("Could not merge lock files: %s\n", err)
}
err = mergedLockFile.WriteFile(lockPath)
if err != nil {
msg.Die("Could not write merged lock file: %s", err)
}
action.Install(installer, false)
},
}
getTransitiveCmd.PersistentFlags().BoolVarP(&interactive, "interactive", "i", false,
"set dependency verion interactively")
hellCmd.AddCommand(lockMergeCmd)
hellCmd.AddCommand(getTransitiveCmd)
lockMergeCmd.Execute()
}
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hell
import (
"crypto/sha256"
"fmt"
"sort"
"github.com/Masterminds/glide/cfg"
)
// Merges two glide lock files together, letting dependencies from 'base' be overwritten
// by those from 'override'. Returns the resultant glide lock file bytes
func MergeGlideLockFiles(baseLockFile, overrideLockFile *cfg.Lockfile) (*cfg.Lockfile, error) {
imports := make(map[string]*cfg.Lock, len(baseLockFile.Imports))
devImports := make(map[string]*cfg.Lock, len(baseLockFile.DevImports))
// Copy the base dependencies into a map
for _, lock := range baseLockFile.Imports {
imports[lock.Name] = lock
}
for _, lock := range baseLockFile.DevImports {
devImports[lock.Name] = lock
}
// Override base dependencies and add any extra ones
for _, lock := range overrideLockFile.Imports {
imports[lock.Name] = mergeLocks(imports[lock.Name], lock)
}
for _, lock := range overrideLockFile.DevImports {
devImports[lock.Name] = mergeLocks(imports[lock.Name], lock)
}
deps := make([]*cfg.Dependency, 0, len(imports))
devDeps := make([]*cfg.Dependency, 0, len(devImports))
// Flatten to Dependencies
for _, lock := range imports {
deps = append(deps, pinnedDependencyFromLock(lock))
}
for _, lock := range devImports {
devDeps = append(devDeps, pinnedDependencyFromLock(lock))
}
hasher := sha256.New()
hasher.Write(([]byte)(baseLockFile.Hash))
hasher.Write(([]byte)(overrideLockFile.Hash))
return cfg.NewLockfile(deps, devDeps, fmt.Sprintf("%x", hasher.Sum(nil)))
}
func mergeLocks(baseLock, overrideLock *cfg.Lock) *cfg.Lock {
lock := overrideLock.Clone()
if baseLock == nil {
return lock
}
// Merge and dedupe subpackages
subpackages := make([]string, 0, len(lock.Subpackages)+len(baseLock.Subpackages))
for _, sp := range lock.Subpackages {
subpackages = append(subpackages, sp)
}
for _, sp := range baseLock.Subpackages {
subpackages = append(subpackages, sp)
}
sort.Stable(sort.StringSlice(subpackages))
dedupeSubpackages := make([]string, 0, len(subpackages))
lastSp := ""
elided := 0
for _, sp := range subpackages {
if lastSp == sp {
elided++
} else {
dedupeSubpackages = append(dedupeSubpackages, sp)
lastSp = sp
}
}
lock.Subpackages = dedupeSubpackages[:len(dedupeSubpackages)-elided]
return lock
}
func pinnedDependencyFromLock(lock *cfg.Lock) *cfg.Dependency {
dep := cfg.DependencyFromLock(lock)
dep.Pin = lock.Version
return dep
}
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hell
import (
"strings"
"testing"
"github.com/Masterminds/glide/cfg"
"github.com/stretchr/testify/assert"
)
const baseLockYml = `imports:
- name: github.com/gogo/protobuf
version: 82d16f734d6d871204a3feb1a73cb220cc92574c
- name: github.com/tendermint/tendermint
version: aaea0c5d2e3ecfbf29f2608f9d43649ec7f07f50
subpackages:
- node
- proxy
- types
- version
- consensus
- rpc/core/types
- blockchain
- mempool
- rpc/core
- state
`
const overrideLockYml = `imports:
- name: github.com/tendermint/tendermint
version: 764091dfbb035f1b28da4b067526e04c6a849966
subpackages:
- benchmarks
- proxy
- types
- version
`
const expectedLockYml = `imports:
- name: github.com/gogo/protobuf
version: 82d16f734d6d871204a3feb1a73cb220cc92574c
- name: github.com/tendermint/tendermint
version: 764091dfbb035f1b28da4b067526e04c6a849966
subpackages:
- benchmarks
- blockchain
- consensus
- mempool
- node
- proxy
- rpc/core
- rpc/core/types
testImports: []
`
func TestMergeGlideLockFiles(t *testing.T) {
baseLockFile, err := cfg.LockfileFromYaml(([]byte)(baseLockYml))
assert.NoError(t, err, "Lockfile should parse")
overrideLockFile, err := cfg.LockfileFromYaml(([]byte)(overrideLockYml))
assert.NoError(t, err, "Lockfile should parse")
mergedLockFile, err := MergeGlideLockFiles(baseLockFile, overrideLockFile)
assert.NoError(t, err, "Lockfiles should merge")
mergedYmlBytes, err := mergedLockFile.Marshal()
assert.NoError(t, err, "Lockfile should marshal")
ymlLines := strings.Split(string(mergedYmlBytes), "\n")
// Drop the updated and hash lines
actualYml := strings.Join(ymlLines[2:], "\n")
assert.Equal(t, expectedLockYml, actualYml)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment