Skip to content

Commit

Permalink
feat: skip generate lang if no changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 5, 2022
1 parent 868a4fd commit 534d8d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
45 changes: 43 additions & 2 deletions cmd/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ package cmd

import (
"fmt"
"io"
"os"
"reflect"
"strings"

_ "github.com/alist-org/alist/v3/drivers"
"github.com/alist-org/alist/v3/internal/bootstrap/data"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -34,6 +37,43 @@ func convert(s string) string {
return firstUpper(ans)
}

func writeFile(name string, data interface{}) {
f, err := os.Open(fmt.Sprintf("../alist-web/src/lang/en/%s.json", name))
if err != nil {
log.Errorf("failed to open %s.json: %+v", name, err)
return
}
content, err := io.ReadAll(f)
if err != nil {
log.Errorf("failed to read %s.json: %+v", name, err)
return
}
oldData := make(map[string]interface{})
newData := make(map[string]interface{})
err = utils.Json.Unmarshal(content, &oldData)
if err != nil {
log.Errorf("failed to unmarshal %s.json: %+v", name, err)
return
}
content, err = utils.Json.Marshal(data)
if err != nil {
log.Errorf("failed to marshal json: %+v", err)
return
}
err = utils.Json.Unmarshal(content, &newData)
if err != nil {
log.Errorf("failed to unmarshal json: %+v", err)
return
}
if reflect.DeepEqual(oldData, newData) {
log.Infof("%s.json no changed, skip", name)
} else {
log.Infof("%s.json changed, update file", name)
//log.Infof("old: %+v\nnew:%+v", oldData, data)
utils.WriteJsonToFile(fmt.Sprintf("lang/%s.json", name), data)
}
}

func generateDriversJson() {
drivers := make(Drivers)
drivers["drivers"] = make(KV[interface{}])
Expand All @@ -58,7 +98,7 @@ func generateDriversJson() {
}
drivers[k] = items
}
utils.WriteJsonToFile("lang/drivers.json", drivers)
writeFile("drivers", drivers)
}

func generateSettingsJson() {
Expand All @@ -78,7 +118,8 @@ func generateSettingsJson() {
settingsLang[fmt.Sprintf("%ss", setting.Key)] = options
}
}
utils.WriteJsonToFile("lang/settings.json", settingsLang)
writeFile("settings", settingsLang)
//utils.WriteJsonToFile("lang/settings.json", settingsLang)
}

// langCmd represents the lang command
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/json.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package utils

import (
"io/ioutil"
"os"

json "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
Expand All @@ -16,7 +16,7 @@ func WriteJsonToFile(dst string, data interface{}) bool {
log.Errorf("failed convert Conf to []byte:%s", err.Error())
return false
}
err = ioutil.WriteFile(dst, str, 0777)
err = os.WriteFile(dst, str, 0777)
if err != nil {
log.Errorf("failed to write json file:%s", err.Error())
return false
Expand Down

0 comments on commit 534d8d3

Please sign in to comment.