|
| 1 | +package cuemod |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "slices" |
| 8 | + |
| 9 | + "cuelang.org/go/mod/modfile" |
| 10 | + "github.com/terassyi/tomei/internal/verify" |
| 11 | +) |
| 12 | + |
| 13 | +// UpdateResult represents the result of updating a single dependency. |
| 14 | +type UpdateResult struct { |
| 15 | + ModulePath string |
| 16 | + OldVersion string |
| 17 | + NewVersion string |
| 18 | + Updated bool |
| 19 | +} |
| 20 | + |
| 21 | +// ParseModuleFile reads and parses cue.mod/module.cue from the given cue.mod directory. |
| 22 | +// Unlike verify.ParseModuleFile, this returns a user-friendly error when the file is missing. |
| 23 | +func ParseModuleFile(cueModDir string) (*modfile.File, error) { |
| 24 | + f, err := verify.ParseModuleFile(cueModDir) |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + if f == nil { |
| 29 | + return nil, fmt.Errorf("module.cue not found in %s (run 'tomei cue init' first)", cueModDir) |
| 30 | + } |
| 31 | + return f, nil |
| 32 | +} |
| 33 | + |
| 34 | +// UpdateDeps updates first-party (tomei.terassyi.net) dependencies in the module file |
| 35 | +// to the given latest version. Returns the list of update results. |
| 36 | +func UpdateDeps(f *modfile.File, latestVersion string) ([]UpdateResult, error) { |
| 37 | + // Collect first-party module paths for deterministic order. |
| 38 | + var firstPartyPaths []string |
| 39 | + for modPath := range f.Deps { |
| 40 | + if verify.IsFirstParty(modPath) { |
| 41 | + firstPartyPaths = append(firstPartyPaths, modPath) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if len(firstPartyPaths) == 0 { |
| 46 | + return nil, fmt.Errorf("no first-party tomei dependencies found in module.cue") |
| 47 | + } |
| 48 | + |
| 49 | + slices.Sort(firstPartyPaths) |
| 50 | + |
| 51 | + var results []UpdateResult |
| 52 | + for _, modPath := range firstPartyPaths { |
| 53 | + dep := f.Deps[modPath] |
| 54 | + oldVersion := dep.Version |
| 55 | + updated := oldVersion != latestVersion |
| 56 | + if updated { |
| 57 | + dep.Version = latestVersion |
| 58 | + } |
| 59 | + results = append(results, UpdateResult{ |
| 60 | + ModulePath: modPath, |
| 61 | + OldVersion: oldVersion, |
| 62 | + NewVersion: latestVersion, |
| 63 | + Updated: updated, |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + return results, nil |
| 68 | +} |
| 69 | + |
| 70 | +// FormatModuleFile formats a parsed module file back to CUE source bytes. |
| 71 | +func FormatModuleFile(f *modfile.File) ([]byte, error) { |
| 72 | + data, err := modfile.Format(f) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("failed to format module.cue: %w", err) |
| 75 | + } |
| 76 | + return data, nil |
| 77 | +} |
| 78 | + |
| 79 | +// WriteModuleFileAtomic atomically writes module.cue in the given cue.mod directory. |
| 80 | +// It writes to a temporary file first, then renames for atomicity. |
| 81 | +func WriteModuleFileAtomic(cueModDir string, data []byte) error { |
| 82 | + moduleCuePath := filepath.Join(cueModDir, "module.cue") |
| 83 | + tmpPath := moduleCuePath + ".tmp" |
| 84 | + |
| 85 | + if err := os.WriteFile(tmpPath, data, 0644); err != nil { |
| 86 | + return fmt.Errorf("failed to write temporary module.cue: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + if err := os.Rename(tmpPath, moduleCuePath); err != nil { |
| 90 | + os.Remove(tmpPath) |
| 91 | + return fmt.Errorf("failed to rename temporary module.cue: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// HasVendoredModules returns true if vendored tomei modules exist under the directory. |
| 98 | +func HasVendoredModules(dir string) bool { |
| 99 | + vendorPath := filepath.Join(dir, "cue.mod", "pkg", verify.FirstPartyPrefix) |
| 100 | + info, err := os.Stat(vendorPath) |
| 101 | + return err == nil && info.IsDir() |
| 102 | +} |
| 103 | + |
| 104 | +// AnyUpdated returns true if any dependency was actually updated. |
| 105 | +func AnyUpdated(results []UpdateResult) bool { |
| 106 | + return slices.ContainsFunc(results, func(r UpdateResult) bool { |
| 107 | + return r.Updated |
| 108 | + }) |
| 109 | +} |
0 commit comments