|
| 1 | +// Copyright 2022 The Forgejo Authors c/o Codeberg e.V.. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +//go:build ignore |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "bufio" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "regexp" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + trimPrefix = "gitea_" |
| 19 | + sourceFolder = "options/locales/" |
| 20 | +) |
| 21 | + |
| 22 | +// returns list of locales, still containing the file extension! |
| 23 | +func generate_locale_list() []string { |
| 24 | + localeFiles, _ := os.ReadDir(sourceFolder) |
| 25 | + locales := []string{} |
| 26 | + for _, localeFile := range localeFiles { |
| 27 | + if !localeFile.IsDir() && strings.HasPrefix(localeFile.Name(), trimPrefix) { |
| 28 | + locales = append(locales, strings.TrimPrefix(localeFile.Name(), trimPrefix)) |
| 29 | + } |
| 30 | + } |
| 31 | + return locales |
| 32 | +} |
| 33 | + |
| 34 | +// replace all occurrences of Gitea with Forgejo |
| 35 | +func renameGiteaForgejo(filename string) []byte { |
| 36 | + file, err := os.Open(filename) |
| 37 | + if err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + |
| 41 | + replacements := []string{ |
| 42 | + "Gitea", "Forgejo", |
| 43 | + "https://docs.gitea.com/installation/install-from-binary", "https://forgejo.org/download/#installation-from-binary", |
| 44 | + "https://github.com/go-gitea/gitea/tree/master/docker", "https://forgejo.org/download/#container-image", |
| 45 | + "https://docs.gitea.com/installation/install-from-package", "https://forgejo.org/download", |
| 46 | + "https://code.gitea.io/gitea", "https://forgejo.org/download", |
| 47 | + "code.gitea.io/gitea", "Forgejo", |
| 48 | + `<a href="https://github.com/go-gitea/gitea/issues" target="_blank">GitHub</a>`, `<a href="https://codeberg.org/forgejo/forgejo/issues" target="_blank">Codeberg</a>`, |
| 49 | + "https://github.com/go-gitea/gitea", "https://codeberg.org/forgejo/forgejo", |
| 50 | + "https://blog.gitea.io", "https://forgejo.org/news", |
| 51 | + "https://docs.gitea.com/usage/protected-tags", "https://forgejo.org/docs/latest/user/protection/#protected-tags", |
| 52 | + "https://docs.gitea.com/usage/webhooks", "https://forgejo.org/docs/latest/user/webhooks/", |
| 53 | + } |
| 54 | + replacer := strings.NewReplacer(replacements...) |
| 55 | + replaced := make(map[string]bool, len(replacements)/2) |
| 56 | + count_replaced := func(original string) { |
| 57 | + for i := 0; i < len(replacements); i += 2 { |
| 58 | + if strings.Contains(original, replacements[i]) { |
| 59 | + replaced[replacements[i]] = true |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + out := make([]byte, 0, 1024) |
| 65 | + scanner := bufio.NewScanner(file) |
| 66 | + scanner.Split(bufio.ScanLines) |
| 67 | + for scanner.Scan() { |
| 68 | + line := scanner.Text() |
| 69 | + |
| 70 | + if strings.HasPrefix(line, "license_desc=") { |
| 71 | + line = strings.Replace(line, "GitHub", "Forgejo", 1) |
| 72 | + } |
| 73 | + |
| 74 | + if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { |
| 75 | + out = append(out, []byte(line+"\n")...) |
| 76 | + } else if strings.HasPrefix(line, "settings.web_hook_name_gitea") { |
| 77 | + out = append(out, []byte(line+"\n")...) |
| 78 | + out = append(out, []byte("settings.web_hook_name_forgejo = Forgejo\n")...) |
| 79 | + } else if strings.HasPrefix(line, "migrate.gitea.description") { |
| 80 | + re := regexp.MustCompile(`(.*Gitea)`) |
| 81 | + out = append(out, []byte(re.ReplaceAllString(line, "${1}/Forgejo")+"\n")...) |
| 82 | + } else { |
| 83 | + count_replaced(line) |
| 84 | + out = append(out, []byte(replacer.Replace(line)+"\n")...) |
| 85 | + } |
| 86 | + } |
| 87 | + file.Close() |
| 88 | + if strings.HasSuffix(filename, "gitea_en-US.ini") { |
| 89 | + for i := 0; i < len(replacements); i += 2 { |
| 90 | + if replaced[replacements[i]] == false { |
| 91 | + log.Fatalf("%s was never used to replace something in %s, it is obsolete and must be updated", replacements[i], filename) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return out |
| 96 | +} |
| 97 | + |
| 98 | +func main() { |
| 99 | + d := os.Args[1] |
| 100 | + files, err := os.ReadDir(d) |
| 101 | + if err != nil { |
| 102 | + log.Fatal(err) |
| 103 | + } |
| 104 | + |
| 105 | + for _, f := range files { |
| 106 | + p := d + "/" + f.Name() |
| 107 | + os.WriteFile(p, renameGiteaForgejo(p), 0o644) |
| 108 | + } |
| 109 | +} |
0 commit comments