2018-04-25 09:01:00 +01:00
|
|
|
// Copyright 2018 jsonnet-bundler authors
|
|
|
|
|
//
|
|
|
|
|
// 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 pkg
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2019-03-11 14:21:49 +01:00
|
|
|
"github.com/fatih/color"
|
2018-04-25 09:01:00 +01:00
|
|
|
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
JsonnetFile = "jsonnetfile.json"
|
|
|
|
|
JsonnetLockFile = "jsonnetfile.lock.json"
|
|
|
|
|
VersionMismatch = errors.New("multiple colliding versions specified")
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-24 18:20:16 +02:00
|
|
|
func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string, m spec.JsonnetFile, dir string) (*spec.JsonnetFile, error) {
|
|
|
|
|
lockfile := &spec.JsonnetFile{}
|
2018-04-25 09:01:00 +01:00
|
|
|
for _, dep := range m.Dependencies {
|
2019-04-24 18:20:16 +02:00
|
|
|
|
2018-04-25 09:01:00 +01:00
|
|
|
tmp := filepath.Join(dir, ".tmp")
|
2019-04-24 18:20:16 +02:00
|
|
|
err := os.MkdirAll(tmp, os.ModePerm)
|
2018-04-25 09:01:00 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to create general tmp dir")
|
|
|
|
|
}
|
|
|
|
|
tmpDir, err := ioutil.TempDir(tmp, fmt.Sprintf("jsonnetpkg-%s-%s", dep.Name, dep.Version))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to create tmp dir")
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
|
|
subdir := ""
|
|
|
|
|
var p Interface
|
|
|
|
|
if dep.Source.GitSource != nil {
|
|
|
|
|
p = NewGitPackage(dep.Source.GitSource)
|
|
|
|
|
subdir = dep.Source.GitSource.Subdir
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lockVersion, err := p.Install(ctx, tmpDir, dep.Version)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to install package")
|
|
|
|
|
}
|
2018-08-01 13:42:47 +02:00
|
|
|
|
2019-03-11 14:21:49 +01:00
|
|
|
color.Green(">>> Installed %s version %s\n", dep.Name, dep.Version)
|
|
|
|
|
|
2018-04-25 09:01:00 +01:00
|
|
|
destPath := path.Join(dir, dep.Name)
|
|
|
|
|
|
|
|
|
|
err = os.MkdirAll(path.Dir(destPath), os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to create parent path")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.RemoveAll(destPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to clean previous destination path")
|
|
|
|
|
}
|
|
|
|
|
err = os.Rename(path.Join(tmpDir, subdir), destPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to move package")
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 18:20:16 +02:00
|
|
|
lockfile.Dependencies, err = insertDependency(lockfile.Dependencies, spec.Dependency{
|
2018-08-02 08:47:43 +02:00
|
|
|
Name: dep.Name,
|
|
|
|
|
Source: dep.Source,
|
|
|
|
|
Version: lockVersion,
|
|
|
|
|
DepSource: dependencySourceIdentifier,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to insert dependency to lock dependencies")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If dependencies are being installed from a lock file, the transitive
|
|
|
|
|
// dependencies are not questioned, the locked dependencies are just
|
|
|
|
|
// installed.
|
|
|
|
|
if isLock {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 13:42:47 +02:00
|
|
|
filepath, isLock, err := ChooseJsonnetFile(destPath)
|
2018-05-13 06:55:13 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
depsDeps, err := LoadJsonnetfile(filepath)
|
|
|
|
|
// It is ok for depedencies not to have a JsonnetFile, it just means
|
|
|
|
|
// they do not have transitive dependencies of their own.
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-04-25 09:01:00 +01:00
|
|
|
|
2018-08-01 13:42:47 +02:00
|
|
|
depsInstalledByDependency, err := Install(ctx, isLock, filepath, depsDeps, dir)
|
2018-05-13 06:55:13 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-04-25 09:01:00 +01:00
|
|
|
|
2018-05-13 06:55:13 -07:00
|
|
|
for _, d := range depsInstalledByDependency.Dependencies {
|
2019-04-24 18:20:16 +02:00
|
|
|
lockfile.Dependencies, err = insertDependency(lockfile.Dependencies, d)
|
2018-05-13 06:55:13 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to insert dependency to lock dependencies")
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 18:20:16 +02:00
|
|
|
return lockfile, nil
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func insertDependency(deps []spec.Dependency, newDep spec.Dependency) ([]spec.Dependency, error) {
|
2018-04-25 09:22:17 +01:00
|
|
|
if len(deps) == 0 {
|
|
|
|
|
return []spec.Dependency{newDep}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 09:01:00 +01:00
|
|
|
res := []spec.Dependency{}
|
2018-05-13 06:55:13 -07:00
|
|
|
newDepPreviouslyPresent := false
|
2018-04-25 09:01:00 +01:00
|
|
|
for _, d := range deps {
|
|
|
|
|
if d.Name == newDep.Name {
|
|
|
|
|
if d.Version != newDep.Version {
|
2018-05-13 06:55:13 -07:00
|
|
|
return nil, fmt.Errorf("multiple colliding versions specified for %s: %s (from %s) and %s (from %s)", d.Name, d.Version, d.DepSource, newDep.Version, newDep.DepSource)
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|
|
|
|
|
res = append(res, d)
|
2018-05-13 06:55:13 -07:00
|
|
|
newDepPreviouslyPresent = true
|
2018-04-25 09:01:00 +01:00
|
|
|
} else {
|
|
|
|
|
res = append(res, d)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-13 06:55:13 -07:00
|
|
|
if !newDepPreviouslyPresent {
|
|
|
|
|
res = append(res, newDep)
|
|
|
|
|
}
|
2018-04-25 09:01:00 +01:00
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 10:35:20 +02:00
|
|
|
func FileExists(path string) (bool, error) {
|
|
|
|
|
_, err := os.Stat(path)
|
2018-05-13 06:55:13 -07:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 13:42:47 +02:00
|
|
|
func ChooseJsonnetFile(dir string) (string, bool, error) {
|
2018-05-13 06:55:13 -07:00
|
|
|
lockfile := path.Join(dir, JsonnetLockFile)
|
|
|
|
|
jsonnetfile := path.Join(dir, JsonnetFile)
|
|
|
|
|
filename := lockfile
|
2018-08-01 13:42:47 +02:00
|
|
|
isLock := true
|
2018-05-13 06:55:13 -07:00
|
|
|
|
2018-08-08 10:35:20 +02:00
|
|
|
lockExists, err := FileExists(filepath.Join(dir, JsonnetLockFile))
|
2018-05-13 06:55:13 -07:00
|
|
|
if err != nil {
|
2018-08-01 13:42:47 +02:00
|
|
|
return "", false, err
|
2018-05-13 06:55:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !lockExists {
|
|
|
|
|
filename = jsonnetfile
|
2018-08-01 13:42:47 +02:00
|
|
|
isLock = false
|
2018-05-13 06:55:13 -07:00
|
|
|
}
|
|
|
|
|
|
2018-08-01 13:42:47 +02:00
|
|
|
return filename, isLock, err
|
2018-05-13 06:55:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LoadJsonnetfile(filepath string) (spec.JsonnetFile, error) {
|
2018-04-25 09:01:00 +01:00
|
|
|
m := spec.JsonnetFile{}
|
|
|
|
|
|
2018-05-13 06:55:13 -07:00
|
|
|
if _, err := os.Stat(filepath); err != nil {
|
|
|
|
|
return m, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f, err := os.Open(filepath)
|
2018-04-25 09:01:00 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return m, err
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
err = json.NewDecoder(f).Decode(&m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return m, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|