mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-26 20:23:49 +00:00
feat(build/lint-locale-usage): detect missing msgids noted in masked usages files
This commit is contained in:
parent
2533ddfc18
commit
557133b790
2 changed files with 56 additions and 33 deletions
2
Makefile
2
Makefile
|
@ -460,7 +460,7 @@ lint-locale:
|
||||||
|
|
||||||
.PHONY: lint-locale-usage
|
.PHONY: lint-locale-usage
|
||||||
lint-locale-usage:
|
lint-locale-usage:
|
||||||
$(GO) run build/lint-locale-usage/lint-locale-usage.go --allow-masked-usages-from=build/lint-locale-usage/allowed-masked-usage.txt
|
$(GO) run ./build/lint-locale-usage --allow-masked-usages-from=build/lint-locale-usage/allowed-masked-usage.txt
|
||||||
|
|
||||||
.PHONY: lint-md
|
.PHONY: lint-md
|
||||||
lint-md: node_modules
|
lint-md: node_modules
|
||||||
|
|
|
@ -6,6 +6,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/token"
|
"go/token"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
@ -118,15 +119,21 @@ func DecodeKeyForStm(key string) []string {
|
||||||
return ret[:i]
|
return ret[:i]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseAllowedMaskedUsages(fname string, usedMsgids *container.Set[string], allowedMaskedPrefixes *StringTrieMap) error {
|
func ParseAllowedMaskedUsages(fname string, usedMsgids *container.Set[string], allowedMaskedPrefixes *StringTrieMap, chkMsgid func(msgid string) bool) error {
|
||||||
file, err := os.Open(fname)
|
file, err := os.Open(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return LocatedError{
|
||||||
|
Location: fname,
|
||||||
|
Kind: "Open",
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
lno := 0
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
|
lno++
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
if line == "" || strings.HasPrefix(line, "#") {
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
continue
|
continue
|
||||||
|
@ -134,10 +141,24 @@ func ParseAllowedMaskedUsages(fname string, usedMsgids *container.Set[string], a
|
||||||
if strings.HasSuffix(line, ".") {
|
if strings.HasSuffix(line, ".") {
|
||||||
allowedMaskedPrefixes.Insert(DecodeKeyForStm(line))
|
allowedMaskedPrefixes.Insert(DecodeKeyForStm(line))
|
||||||
} else {
|
} else {
|
||||||
|
if !chkMsgid(line) {
|
||||||
|
return LocatedError{
|
||||||
|
Location: fmt.Sprintf("%s: line %d", fname, lno),
|
||||||
|
Kind: "undefined msgid",
|
||||||
|
Err: errors.New(line),
|
||||||
|
}
|
||||||
|
}
|
||||||
(*usedMsgids)[line] = struct{}{}
|
(*usedMsgids)[line] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return scanner.Err()
|
if err := scanner.Err(); err != nil {
|
||||||
|
return LocatedError{
|
||||||
|
Location: fname,
|
||||||
|
Kind: "Scanner",
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// This command assumes that we get started from the project root directory
|
// This command assumes that we get started from the project root directory
|
||||||
|
@ -175,35 +196,6 @@ func main() {
|
||||||
usedMsgids := make(container.Set[string])
|
usedMsgids := make(container.Set[string])
|
||||||
allowedMaskedPrefixes := make(StringTrieMap)
|
allowedMaskedPrefixes := make(StringTrieMap)
|
||||||
|
|
||||||
for _, arg := range os.Args[1:] {
|
|
||||||
switch arg {
|
|
||||||
case "--allow-missing-msgids":
|
|
||||||
allowMissingMsgids = true
|
|
||||||
|
|
||||||
case "--allow-unused-msgids":
|
|
||||||
allowUnusedMsgids = true
|
|
||||||
|
|
||||||
default:
|
|
||||||
if argval, found := strings.CutPrefix(arg, "--allow-masked-usages-from="); found {
|
|
||||||
if err := ParseAllowedMaskedUsages(argval, &usedMsgids, &allowedMaskedPrefixes); err != nil {
|
|
||||||
fmt.Printf("%s:\tERROR: unable to parse masked usages: %s\n", argval, err.Error())
|
|
||||||
os.Exit(7)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Printf("<command line>:\tERROR: unknown argument: %s\n", arg)
|
|
||||||
os.Exit(6)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onError := func(err error) {
|
|
||||||
if err == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
os.Exit(3)
|
|
||||||
}
|
|
||||||
|
|
||||||
msgids := make(container.Set[string])
|
msgids := make(container.Set[string])
|
||||||
|
|
||||||
localeFile := filepath.Join(filepath.Join("options", "locale"), "locale_en-US.ini")
|
localeFile := filepath.Join(filepath.Join("options", "locale"), "locale_en-US.ini")
|
||||||
|
@ -239,6 +231,37 @@ func main() {
|
||||||
|
|
||||||
gotAnyMsgidError := false
|
gotAnyMsgidError := false
|
||||||
|
|
||||||
|
for _, arg := range os.Args[1:] {
|
||||||
|
switch arg {
|
||||||
|
case "--allow-missing-msgids":
|
||||||
|
allowMissingMsgids = true
|
||||||
|
|
||||||
|
case "--allow-unused-msgids":
|
||||||
|
allowUnusedMsgids = true
|
||||||
|
|
||||||
|
default:
|
||||||
|
if argval, found := strings.CutPrefix(arg, "--allow-masked-usages-from="); found {
|
||||||
|
if err := ParseAllowedMaskedUsages(argval, &usedMsgids, &allowedMaskedPrefixes, func(msgid string) bool {
|
||||||
|
return msgids.Contains(msgid)
|
||||||
|
}); err != nil {
|
||||||
|
fmt.Printf("%s\n", err.Error())
|
||||||
|
os.Exit(7)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Printf("<command line>:\tERROR: unknown argument: %s\n", arg)
|
||||||
|
os.Exit(6)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onError := func(err error) {
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
os.Exit(3)
|
||||||
|
}
|
||||||
|
|
||||||
handler := Handler{
|
handler := Handler{
|
||||||
OnMsgidPrefix: func(fset *token.FileSet, pos token.Pos, msgidPrefix string) {
|
OnMsgidPrefix: func(fset *token.FileSet, pos token.Pos, msgidPrefix string) {
|
||||||
// TODO: perhaps we should check if we have any strings with such a prefix, but that's slow...
|
// TODO: perhaps we should check if we have any strings with such a prefix, but that's slow...
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue