|
| 1 | +package v3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + semver "github.com/Masterminds/semver/v3" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "github.com/gofiber/cli/cmd/internal" |
| 15 | +) |
| 16 | + |
| 17 | +var reTemplateImport = regexp.MustCompile(`"github\.com/gofiber/template/([a-zA-Z0-9_-]+)(?:/v(\d+))?([^\"]*)"`) |
| 18 | + |
| 19 | +type templateTarget struct { |
| 20 | + modulePath string |
| 21 | + version string |
| 22 | +} |
| 23 | + |
| 24 | +type templateGoModPatterns struct { |
| 25 | + require *regexp.Regexp |
| 26 | + replace *regexp.Regexp |
| 27 | +} |
| 28 | + |
| 29 | +// templateTargets maps template packages to their minimum module path and version |
| 30 | +// required for Fiber v3 core compatibility. Source: |
| 31 | +// https://github.com/gofiber/template/pull/437 |
| 32 | +var templateTargets = map[string]templateTarget{ |
| 33 | + "ace": {modulePath: "github.com/gofiber/template/ace/v3", version: "v3.0.0"}, |
| 34 | + "amber": {modulePath: "github.com/gofiber/template/amber/v3", version: "v3.0.0"}, |
| 35 | + "django": {modulePath: "github.com/gofiber/template/django/v4", version: "v4.0.0"}, |
| 36 | + "handlebars": {modulePath: "github.com/gofiber/template/handlebars/v3", version: "v3.0.0"}, |
| 37 | + "html": {modulePath: "github.com/gofiber/template/html/v3", version: "v3.0.0"}, |
| 38 | + "jet": {modulePath: "github.com/gofiber/template/jet/v3", version: "v3.0.0"}, |
| 39 | + "mustache": {modulePath: "github.com/gofiber/template/mustache/v3", version: "v3.0.0"}, |
| 40 | + "pug": {modulePath: "github.com/gofiber/template/pug/v3", version: "v3.0.0"}, |
| 41 | + "slim": {modulePath: "github.com/gofiber/template/slim/v3", version: "v3.0.0"}, |
| 42 | +} |
| 43 | + |
| 44 | +// MigrateTemplateVersions updates template package imports and go.mod entries |
| 45 | +// to the minimum versions required for Fiber v3 core compatibility. |
| 46 | +func MigrateTemplateVersions(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { |
| 47 | + changedImports, err := internal.ChangeFileContent(cwd, func(content string) string { |
| 48 | + return reTemplateImport.ReplaceAllStringFunc(content, func(match string) string { |
| 49 | + sub := reTemplateImport.FindStringSubmatch(match) |
| 50 | + if len(sub) != 4 { |
| 51 | + return match |
| 52 | + } |
| 53 | + |
| 54 | + target, ok := templateTargets[sub[1]] |
| 55 | + if !ok { |
| 56 | + return match |
| 57 | + } |
| 58 | + |
| 59 | + return fmt.Sprintf(`"%s%s"`, target.modulePath, sub[3]) |
| 60 | + }) |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to migrate template imports: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + modChanged, err := migrateTemplateModules(cwd) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + if !changedImports && !modChanged { |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + cmd.Println("Migrated template package versions") |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func migrateTemplateModules(cwd string) (bool, error) { |
| 80 | + modChanged := false |
| 81 | + patterns := compileTemplateGoModPatterns() |
| 82 | + |
| 83 | + walkErr := filepath.WalkDir(cwd, func(path string, d fs.DirEntry, walkErr error) error { |
| 84 | + if walkErr != nil { |
| 85 | + return walkErr |
| 86 | + } |
| 87 | + if d.IsDir() { |
| 88 | + if d.Name() == vendorDirName { |
| 89 | + return filepath.SkipDir |
| 90 | + } |
| 91 | + return nil |
| 92 | + } |
| 93 | + if d.Name() != goModFileName { |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + info, err := d.Info() |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("stat %s: %w", path, err) |
| 100 | + } |
| 101 | + |
| 102 | + b, err := os.ReadFile(path) // #nosec G304 -- reading module file |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("read %s: %w", path, err) |
| 105 | + } |
| 106 | + content := string(b) |
| 107 | + updated := content |
| 108 | + |
| 109 | + for pkg, target := range templateTargets { |
| 110 | + updated = updateTemplateGoModModule(updated, target.modulePath, target.version, patterns[pkg]) |
| 111 | + } |
| 112 | + |
| 113 | + if updated == content { |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + if err := os.WriteFile(path, []byte(updated), info.Mode().Perm()); err != nil { |
| 118 | + return fmt.Errorf("write %s: %w", path, err) |
| 119 | + } |
| 120 | + modChanged = true |
| 121 | + return nil |
| 122 | + }) |
| 123 | + if walkErr != nil { |
| 124 | + return false, fmt.Errorf("failed to migrate template modules: %w", walkErr) |
| 125 | + } |
| 126 | + |
| 127 | + return modChanged, nil |
| 128 | +} |
| 129 | + |
| 130 | +func compileTemplateGoModPatterns() map[string]templateGoModPatterns { |
| 131 | + patterns := make(map[string]templateGoModPatterns, len(templateTargets)) |
| 132 | + for pkg := range templateTargets { |
| 133 | + rePath := fmt.Sprintf(`github\.com/gofiber/template/%s(?:/v\d+)?`, regexp.QuoteMeta(pkg)) |
| 134 | + patterns[pkg] = templateGoModPatterns{ |
| 135 | + require: regexp.MustCompile(fmt.Sprintf(`(?m)^(\s*(?:require\s+)?)%s\s+%s`, rePath, goModVersionPattern)), |
| 136 | + replace: regexp.MustCompile(fmt.Sprintf(`(?m)^(\s*replace\s+)%s(\s+%s)?(\s+=>\s+)`, rePath, goModVersionPattern)), |
| 137 | + } |
| 138 | + } |
| 139 | + return patterns |
| 140 | +} |
| 141 | + |
| 142 | +func updateTemplateGoModModule(content, newPath, version string, patterns templateGoModPatterns) string { |
| 143 | + content = patterns.require.ReplaceAllString(content, fmt.Sprintf(`${1}%s %s`, newPath, version)) |
| 144 | + content = patterns.replace.ReplaceAllStringFunc(content, func(s string) string { |
| 145 | + sub := patterns.replace.FindStringSubmatch(s) |
| 146 | + if len(sub) != 4 { |
| 147 | + return s |
| 148 | + } |
| 149 | + |
| 150 | + if strings.TrimSpace(sub[2]) == "" { |
| 151 | + return fmt.Sprintf("%s%s%s", sub[1], newPath, sub[3]) |
| 152 | + } |
| 153 | + return fmt.Sprintf("%s%s %s%s", sub[1], newPath, version, sub[3]) |
| 154 | + }) |
| 155 | + |
| 156 | + return content |
| 157 | +} |
0 commit comments