vendoring: support multiple modules
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
134
state/project.go
134
state/project.go
@@ -10,7 +10,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gofrs/flock"
|
||||
"github.com/rs/zerolog/log"
|
||||
"go.dagger.io/dagger/keychain"
|
||||
"go.dagger.io/dagger/pkg"
|
||||
@@ -32,7 +31,6 @@ const (
|
||||
planDir = "plan"
|
||||
manifestFile = "values.yaml"
|
||||
computedFile = "computed.json"
|
||||
lockFilePath = "dagger.lock"
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
@@ -57,7 +55,7 @@ func Init(ctx context.Context, dir string) (*Project, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := VendorUniverse(ctx, root); err != nil {
|
||||
if err := pkg.Vendor(ctx, root); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -354,133 +352,3 @@ func (w *Project) cleanPackageName(ctx context.Context, pkg string) (string, err
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func cueModInit(ctx context.Context, parentDir string) error {
|
||||
lg := log.Ctx(ctx)
|
||||
|
||||
modDir := path.Join(parentDir, "cue.mod")
|
||||
modFile := path.Join(modDir, "module.cue")
|
||||
if _, err := os.Stat(modFile); err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
lg.Debug().Str("mod", parentDir).Msg("initializing cue.mod")
|
||||
|
||||
if err := os.WriteFile(modFile, []byte("module: \"\"\n"), 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.Mkdir(path.Join(modDir, "usr"), 0755); err != nil {
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(path.Join(modDir, "pkg"), 0755); err != nil {
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func VendorUniverse(ctx context.Context, p string) error {
|
||||
if p == "" {
|
||||
p = getCueModParent()
|
||||
}
|
||||
|
||||
cueModDir := path.Join(p, "cue.mod")
|
||||
if err := os.Mkdir(cueModDir, 0755); err != nil {
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(cueModDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lockFilePath := path.Join(cueModDir, lockFilePath)
|
||||
fileLock := flock.New(lockFilePath)
|
||||
if err := fileLock.Lock(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
fileLock.Unlock()
|
||||
os.Remove(lockFilePath)
|
||||
}()
|
||||
|
||||
// ensure cue module is initialized
|
||||
if err := cueModInit(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// add universe and lock file to `.gitignore`
|
||||
if err := os.WriteFile(
|
||||
path.Join(p, "cue.mod", "pkg", ".gitignore"),
|
||||
[]byte(fmt.Sprintf("# generated by dagger\n%s\ndagger.lock\n", pkg.AlphaModule)),
|
||||
0600,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe")
|
||||
|
||||
// Vendor in a temporary directory
|
||||
tmp, err := os.MkdirTemp(path.Join(p, "cue.mod", "pkg"), "vendor-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := pkg.Vendor(ctx, tmp); err != nil {
|
||||
// FIXME(samalba): disabled install remote stdlib temporarily
|
||||
// if _, err := mod.Install(ctx, p, stdlib.ModuleName, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Semi-atomic swap of the vendor directory
|
||||
// The following basically does:
|
||||
// rm -rf cue.mod/pkg/MODULE.old
|
||||
// mv cue.mod/pkg/MODULE cue.mod/pkg/MODULE.old
|
||||
// mv VENDOR cue.mod/pkg/MODULE
|
||||
// rm -rf cue.mod/pkg/MODULE.old
|
||||
newStdlib := path.Join(p, "cue.mod", "pkg", pkg.AlphaModule)
|
||||
oldStdlib := newStdlib + ".old"
|
||||
if err := os.RemoveAll(oldStdlib); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(newStdlib, oldStdlib); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(oldStdlib)
|
||||
|
||||
if err := os.Rename(path.Join(tmp, pkg.AlphaModule), newStdlib); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getCueModParent() string {
|
||||
cwd, _ := os.Getwd()
|
||||
parentDir := cwd
|
||||
|
||||
// traverse the directory tree up through ancestors looking for a cue.mod folder
|
||||
for {
|
||||
if _, err := os.Stat(path.Join(parentDir, "cue.mod")); !errors.Is(err, os.ErrNotExist) {
|
||||
break // found it!
|
||||
}
|
||||
|
||||
parentDir = filepath.Dir(parentDir)
|
||||
|
||||
if parentDir == string(os.PathSeparator) {
|
||||
// reached the root
|
||||
parentDir = cwd // reset to working directory
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return parentDir
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"go.dagger.io/dagger/compiler"
|
||||
"go.dagger.io/dagger/pkg"
|
||||
"go.dagger.io/dagger/plancontext"
|
||||
)
|
||||
|
||||
@@ -55,7 +56,7 @@ func (s *State) CompilePlan(ctx context.Context) (*compiler.Value, error) {
|
||||
// 2) For backward compatibility: if the project was `dagger
|
||||
// init`-ed before we added support for vendoring universe, it might not
|
||||
// contain a `cue.mod`.
|
||||
if err := VendorUniverse(ctx, w); err != nil {
|
||||
if err := pkg.Vendor(ctx, w); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user