|
| 1 | +// Copyright 2025 The CUE Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "cuelang.org/go/cue/ast" |
| 24 | + "cuelang.org/go/cue/build" |
| 25 | + "cuelang.org/go/cue/format" |
| 26 | + "cuelang.org/go/cue/literal" |
| 27 | + "cuelang.org/go/cue/load" |
| 28 | + "cuelang.org/go/cue/parser" |
| 29 | + "cuelang.org/go/internal/mod/semver" |
| 30 | + "cuelang.org/go/mod/module" |
| 31 | + "github.com/spf13/cobra" |
| 32 | +) |
| 33 | + |
| 34 | +func newModRenameCmd(c *Command) *cobra.Command { |
| 35 | + cmd := &cobra.Command{ |
| 36 | + Use: "rename <newModulePath>", |
| 37 | + Short: "rename the current module", |
| 38 | + Long: `Rename changes the name of the current module, |
| 39 | +updating import statements in source files as required. |
| 40 | +
|
| 41 | +Note that this command is not yet stable and may be changed. |
| 42 | +`, |
| 43 | + RunE: mkRunE(c, runModRename), |
| 44 | + Args: cobra.ExactArgs(1), |
| 45 | + } |
| 46 | + |
| 47 | + return cmd |
| 48 | +} |
| 49 | + |
| 50 | +type modRenamer struct { |
| 51 | + oldModulePath string |
| 52 | + oldModuleMajor string |
| 53 | + oldModuleQualifier string |
| 54 | + newModulePath string |
| 55 | + newModuleMajor string |
| 56 | + newModuleQualifier string |
| 57 | +} |
| 58 | + |
| 59 | +func runModRename(cmd *Command, args []string) error { |
| 60 | + modFilePath, mf, _, err := readModuleFile() |
| 61 | + if err != nil { |
| 62 | + return nil |
| 63 | + } |
| 64 | + if mf.Module == args[0] { |
| 65 | + // Nothing to do |
| 66 | + return nil |
| 67 | + } |
| 68 | + var mr modRenamer |
| 69 | + mr.oldModulePath, mr.oldModuleMajor, err = splitModulePath(mf.Module) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + mr.oldModuleQualifier = module.ParseImportPath(mr.oldModulePath).Qualifier |
| 74 | + mf.Module = args[0] |
| 75 | + mr.newModulePath, mr.newModuleMajor, err = splitModulePath(mf.Module) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + mr.newModuleQualifier = module.ParseImportPath(mr.newModulePath).Qualifier |
| 80 | + |
| 81 | + // TODO if we're renaming to a module that we currently depend on, |
| 82 | + // perhaps we should detect that and give an error. |
| 83 | + newModFileData, err := mf.Format() |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("invalid resulting module.cue file after edits: %v", err) |
| 86 | + } |
| 87 | + if err := os.WriteFile(modFilePath, newModFileData, 0o666); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + modRoot, err := findModuleRoot() |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + binst := load.Instances([]string{"./..."}, &load.Config{ |
| 96 | + Dir: modRoot, |
| 97 | + ModuleRoot: modRoot, |
| 98 | + Tests: true, |
| 99 | + Tools: true, |
| 100 | + AllCUEFiles: true, |
| 101 | + Package: "*", |
| 102 | + // Note: the mod renaming can work even when |
| 103 | + // some external imports don't. |
| 104 | + SkipImports: true, |
| 105 | + }) |
| 106 | + if len(binst) == 0 { |
| 107 | + // No packages to rename. |
| 108 | + return nil |
| 109 | + } |
| 110 | + if binst[0].Module == "" { |
| 111 | + return fmt.Errorf("no current module to rename") |
| 112 | + } |
| 113 | + for _, inst := range binst { |
| 114 | + if err := inst.Err; err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + for _, file := range inst.BuildFiles { |
| 118 | + if filepath.Dir(file.Filename) != inst.Dir { |
| 119 | + // Avoid processing files which are inherited from parent directories. |
| 120 | + continue |
| 121 | + } |
| 122 | + if err := mr.renameFile(file); err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func (mr *modRenamer) renameFile(file *build.File) error { |
| 131 | + syntax, err := parser.ParseFile(file.Filename, file.Source, parser.ParseComments) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + changed := false |
| 137 | + for _, decl := range syntax.Preamble() { |
| 138 | + if decl, ok := decl.(*ast.ImportDecl); ok { |
| 139 | + for _, spec := range decl.Specs { |
| 140 | + ch, err := mr.rewriteImport(spec) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + changed = changed || ch |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + if !changed { |
| 149 | + return nil |
| 150 | + } |
| 151 | + data, err := format.Node(syntax) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + if err := os.WriteFile(file.Filename, data, 0o666); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func (mr *modRenamer) rewriteImport(spec *ast.ImportSpec) (changed bool, err error) { |
| 162 | + importPath, err := literal.Unquote(spec.Path.Value) |
| 163 | + if err != nil { |
| 164 | + return false, fmt.Errorf("malformed import path in AST: %v", err) |
| 165 | + } |
| 166 | + ip := module.ParseImportPath(importPath) |
| 167 | + if !pkgIsUnderneath(ip.Path, mr.oldModulePath) { |
| 168 | + return false, nil |
| 169 | + } |
| 170 | + |
| 171 | + // TODO it's possible that we've got a import of a package in a nested module |
| 172 | + // rather than a reference to a package in this module. We can only |
| 173 | + // tell that by actually importing the dependencies and looking up the |
| 174 | + // package in those dependencies, which seems like overkill for now at least. |
| 175 | + if ip.Qualifier == "" { |
| 176 | + return false, fmt.Errorf("import path %q has no implied package qualifier", importPath) |
| 177 | + } |
| 178 | + if ip.Version != "" && ip.Version != mr.oldModuleMajor { |
| 179 | + // Same module, different major version. Don't change it. |
| 180 | + return false, nil |
| 181 | + } |
| 182 | + ip.Path = mr.newModulePath + strings.TrimPrefix(ip.Path, mr.oldModulePath) |
| 183 | + if ip.Version != "" { |
| 184 | + // Keep the major version if it was there already; the main |
| 185 | + // module is always the default. |
| 186 | + ip.Version = mr.newModuleMajor |
| 187 | + } |
| 188 | + // Note: ip.Qualifier remains the same as before, which means |
| 189 | + // that regardless of the new import path, the implied identifier |
| 190 | + // will remain the same, so no change is needed to spec.Ident. |
| 191 | + ip.ExplicitQualifier = false // Only include if needed. |
| 192 | + spec.Path.Value = literal.String.Quote(ip.String()) |
| 193 | + return true, nil |
| 194 | +} |
| 195 | + |
| 196 | +// pkgIsUnderneath reports whether pkg2 is a package |
| 197 | +// underneath (or the same as) pkg1. |
| 198 | +func pkgIsUnderneath(pkg1, pkg2 string) bool { |
| 199 | + if len(pkg1) < len(pkg2) { |
| 200 | + return false |
| 201 | + } |
| 202 | + if !strings.HasPrefix(pkg1, pkg2) { |
| 203 | + return false |
| 204 | + } |
| 205 | + return len(pkg1) == len(pkg2) || pkg1[len(pkg2)] == '/' |
| 206 | +} |
| 207 | + |
| 208 | +func splitModulePath(path string) (mpath string, mvers string, err error) { |
| 209 | + mpath, mvers, ok := module.SplitPathVersion(path) |
| 210 | + if ok { |
| 211 | + if semver.Major(mvers) != mvers { |
| 212 | + return "", "", fmt.Errorf("module path %q should contain the major version only", path) |
| 213 | + } |
| 214 | + if err := module.CheckPath(mpath); err != nil { |
| 215 | + return "", "", fmt.Errorf("invalid module path %q: %v", path, err) |
| 216 | + } |
| 217 | + return mpath, mvers, nil |
| 218 | + } |
| 219 | + mpath = path |
| 220 | + if err := module.CheckPathWithoutVersion(mpath); err != nil { |
| 221 | + return "", "", fmt.Errorf("invalid module path %q: %v", path, err) |
| 222 | + } |
| 223 | + return mpath, "v0", nil |
| 224 | +} |
0 commit comments