|
| 1 | +// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by the license in the LICENSE file. |
| 3 | + |
| 4 | +package lock |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "io/fs" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "go.jetpack.io/devbox/internal/build" |
| 12 | + "go.jetpack.io/devbox/internal/cachehash" |
| 13 | + "go.jetpack.io/devbox/internal/cuecfg" |
| 14 | +) |
| 15 | + |
| 16 | +// stateHashFile is a non-shared lock file that helps track the state of the |
| 17 | +// local devbox environment. It contains hashes that may not be the same across |
| 18 | +// machines (e.g. manifest hash). |
| 19 | +// When we do implement a shared lock file, it may contain some shared fields |
| 20 | +// with this one but not all. |
| 21 | +type stateHashFile struct { |
| 22 | + ConfigHash string `json:"config_hash"` |
| 23 | + DevboxVersion string `json:"devbox_version"` |
| 24 | + // fish has different generated scripts so we need to recompute them if user |
| 25 | + // changes shell. |
| 26 | + IsFish bool `json:"is_fish"` |
| 27 | + LockFileHash string `json:"lock_file_hash"` |
| 28 | + NixPrintDevEnvHash string `json:"nix_print_dev_env_hash"` |
| 29 | + NixProfileManifestHash string `json:"nix_profile_manifest_hash"` |
| 30 | +} |
| 31 | + |
| 32 | +type UpdateStateHashFileArgs struct { |
| 33 | + ProjectDir string |
| 34 | + ConfigHash string |
| 35 | + // IsFish is an arg because in the future we may allow the user |
| 36 | + // to specify shell in devbox.json which should be passed in here. |
| 37 | + IsFish bool |
| 38 | +} |
| 39 | + |
| 40 | +func UpdateAndSaveStateHashFile(args UpdateStateHashFileArgs) error { |
| 41 | + newLock, err := getCurrentStateHash(args) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + return cuecfg.WriteFile(stateHashFilePath(args.ProjectDir), newLock) |
| 47 | +} |
| 48 | + |
| 49 | +func isStateUpToDate(args UpdateStateHashFileArgs) (bool, error) { |
| 50 | + filesystemLock, err := readStateHashFile(args.ProjectDir) |
| 51 | + if err != nil { |
| 52 | + return false, err |
| 53 | + } |
| 54 | + newLock, err := getCurrentStateHash(args) |
| 55 | + if err != nil { |
| 56 | + return false, err |
| 57 | + } |
| 58 | + |
| 59 | + return *filesystemLock == *newLock, nil |
| 60 | +} |
| 61 | + |
| 62 | +func readStateHashFile(projectDir string) (*stateHashFile, error) { |
| 63 | + lockFile := &stateHashFile{} |
| 64 | + err := cuecfg.ParseFile(stateHashFilePath(projectDir), lockFile) |
| 65 | + if errors.Is(err, fs.ErrNotExist) { |
| 66 | + return lockFile, nil |
| 67 | + } |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + return lockFile, nil |
| 72 | +} |
| 73 | + |
| 74 | +func getCurrentStateHash(args UpdateStateHashFileArgs) (*stateHashFile, error) { |
| 75 | + nixHash, err := manifestHash(args.ProjectDir) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + printDevEnvCacheHash, err := printDevEnvCacheHash(args.ProjectDir) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + lockfileHash, err := getLockfileHash(args.ProjectDir) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + newLock := &stateHashFile{ |
| 91 | + ConfigHash: args.ConfigHash, |
| 92 | + DevboxVersion: build.Version, |
| 93 | + IsFish: args.IsFish, |
| 94 | + LockFileHash: lockfileHash, |
| 95 | + NixPrintDevEnvHash: printDevEnvCacheHash, |
| 96 | + NixProfileManifestHash: nixHash, |
| 97 | + } |
| 98 | + |
| 99 | + return newLock, nil |
| 100 | +} |
| 101 | + |
| 102 | +func stateHashFilePath(projectDir string) string { |
| 103 | + return filepath.Join(projectDir, ".devbox", "local.lock") |
| 104 | +} |
| 105 | + |
| 106 | +func manifestHash(profileDir string) (string, error) { |
| 107 | + return cachehash.JSONFile(filepath.Join(profileDir, ".devbox/nix/profile/default/manifest.json")) |
| 108 | +} |
| 109 | + |
| 110 | +func printDevEnvCacheHash(profileDir string) (string, error) { |
| 111 | + return cachehash.JSONFile(filepath.Join(profileDir, ".devbox/.nix-print-dev-env-cache")) |
| 112 | +} |
| 113 | + |
| 114 | +func getLockfileHash(projectDir string) (string, error) { |
| 115 | + return cachehash.JSONFile(lockFilePath(projectDir)) |
| 116 | +} |
0 commit comments