Skip to content

Commit

Permalink
fix module
Browse files Browse the repository at this point in the history
fix module
  • Loading branch information
giacomozanatta committed Jan 31, 2022
1 parent 9dbd340 commit 61fdaa0
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 130 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module sfcc-properties
module github.com/giacomozanatta/sfcc-properties

go 1.17

Expand Down
279 changes: 150 additions & 129 deletions sfcc-properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"os"
Expand All @@ -13,6 +15,29 @@ import (
"github.com/xuri/excelize/v2"
)

type Config struct {
Cartridges []string `json:"cartridges"` // cartridges: from the less important to the most
Locales []string `json:"locales"` // define locales: you must include all the locales
}

type ExcelEntry struct {
cartridge string
label string
}

var config Config

func getConfig(fileName string) {
configFile, err := os.Open(fileName)
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
}

// getExcelCol: convert an integer x to an excel column index.
func getExcelCol(x int) string {
chars := [...]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
if x%26 == 0 { // Z
Expand All @@ -32,134 +57,39 @@ func getExcelCol(x int) string {
}

}
func main() {

properties := map[string]map[string]map[string]map[string]string{}
// cartridges: from the less important to the most
cartridges := [...]string{}
// define locales: you must include all the locales
locales := [...]string{"br", "es", "it", "ja_JP", "kr", "pt", "ru", "zh", "it_IT", "de", "fr", "fr_FR", "it_IT", "zh_CN", "en_GB", "jp", "en", "ja", "en_SE"}
for _, cartridge := range cartridges {
var processedFiles []string
path := ""
dir, err := os.Open(path)
if err != nil {
fmt.Println("Fail to open folder: " + path)
continue
}
defer dir.Close()
fileInfos, err := dir.Readdir(-1)
if err != nil {
fmt.Println(err)
return
}
_, exists := properties[cartridge]
if !exists {
properties[cartridge] = map[string]map[string]map[string]string{}
}
for _, locale := range locales {
for _, fi := range fileInfos {
if strings.HasSuffix(fi.Name(), "_"+locale+".properties") {
defaultFile := strings.TrimSuffix(fi.Name(), "_"+locale+".properties")
_, exists := properties[cartridge][defaultFile]
if !exists {
properties[cartridge][defaultFile] = map[string]map[string]string{}
//fmt.Println(fi.Name())
// process defaultFile
filename := filepath.Join(path, defaultFile+".properties")
file, _ := os.Open(filename)
if err != nil {
log.Fatal(err)
}
if file != nil {
properties[cartridge][defaultFile]["default"] = map[string]string{}
defer file.Close()
//fmt.Println(file.Name())
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if !strings.HasPrefix(scanner.Text(), "#") && len(scanner.Text()) > 0 {
property := strings.SplitN(scanner.Text(), "=", 2)
if len(property) < 2 {
fmt.Println("Error parsing for property: " + scanner.Text() + " // file:" + filename)
continue
}
properties[cartridge][defaultFile]["default"][property[0]] = property[1]
}
}
processedFiles = append(processedFiles, defaultFile+".properties")
}
}
_, exists = properties[cartridge][defaultFile][locale]
if !exists {
properties[cartridge][defaultFile][locale] = map[string]string{}
}
// start processing file
filename := filepath.Join(path, fi.Name())
//fmt.Println(fi.Name())
file, _ := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if !(strings.HasPrefix(scanner.Text(), "#")) && len(scanner.Text()) > 0 {
text := scanner.Text()
property := strings.SplitN(text, "=", 2)
if len(property) < 2 {
fmt.Println("Error parsing for property: " + scanner.Text() + " // file:" + filename)
continue
}
properties[cartridge][defaultFile][locale][property[0]] = property[1]
}
}
processedFiles = append(processedFiles, fi.Name())
}
func processFile(file *os.File, fileMap map[string]string) {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if !strings.HasPrefix(scanner.Text(), "#") && len(scanner.Text()) > 0 { // throw away comments and empty lines
property := strings.SplitN(scanner.Text(), "=", 2)
if len(property) < 2 { // throw away any other lines that does not represents a property (e.g. it does not have at least an = sign)
log.Println("Error parsing for property: " + scanner.Text())
continue
}
fileMap[property[0]] = property[1]
}
// process file not in locales (files that have only default)
for _, fi := range fileInfos {
if strings.HasSuffix(fi.Name(), ".properties") {
//fmt.Println(fi.Name())
alreadyProcessed := false
for _, b := range processedFiles {
if b == fi.Name() {
//fmt.Println("Already in file")
alreadyProcessed = true
break
}
}
if !alreadyProcessed {
defaultFile := strings.TrimSuffix(fi.Name(), ".properties")
properties[cartridge][defaultFile] = map[string]map[string]string{}
properties[cartridge][defaultFile]["default"] = map[string]string{}
filename := filepath.Join(path, fi.Name())
}
}

//fmt.Println(fi.Name())
file, _ := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if !strings.HasPrefix(scanner.Text(), "#") && len(scanner.Text()) > 0 {
property := strings.SplitN(scanner.Text(), "=", 2)
if len(property) < 2 {
fmt.Println("Error parsing for property: " + scanner.Text() + " // file:" + filename)
continue
}
properties[cartridge][defaultFile]["default"][property[0]] = property[1]
}
}
processedFiles = append(processedFiles, fi.Name())
}
}
func getAllPropertiesDefaultName(properties map[string]map[string]map[string]map[string]string) []string {
keys := map[string]bool{}
for cartridge := range properties {
for k := range properties[cartridge] {
keys[k] = true
}
}
names := make([]string, 0, len(keys))
for k := range keys {
names = append(names, k)
}
sort.Strings(names)
return names

}
func excelizeProperties(properties map[string]map[string]map[string]map[string]string) {
keys := map[string]bool{}
for _, cartridge := range cartridges {
for _, cartridge := range config.Cartridges {
for k := range properties[cartridge] {
keys[k] = true
}
Expand Down Expand Up @@ -188,7 +118,7 @@ func main() {
f.SetCellValue(sheetName, "C2", "label")
f.SetCellStyle(sheetName, "C2", "C2", boldStyle)
i := 4
for _, locale := range locales {
for _, locale := range config.Locales {
f.SetCellValue(sheetName, getExcelCol(i)+"1", locale)
f.SetCellStyle(sheetName, getExcelCol(i)+"1", getExcelCol(i)+"1", boldStyle)
f.MergeCell(sheetName, getExcelCol(i)+"1", getExcelCol(i+1)+"1")
Expand All @@ -199,12 +129,8 @@ func main() {
i += 2
}

type ExcelEntry struct {
cartridge string
label string
}
entries := map[string]map[string]ExcelEntry{} // map id locale Excel Entry
for _, cartridge := range cartridges {
for _, cartridge := range config.Cartridges {
saveLocale := func(locale string) {
_, exists := properties[cartridge][sheetName]
if exists {
Expand All @@ -222,8 +148,7 @@ func main() {
}
}
saveLocale("default")
// TODO: default
for _, locale := range locales {
for _, locale := range config.Locales {
saveLocale(locale)
}
}
Expand All @@ -239,7 +164,7 @@ func main() {
f.SetCellValue(sheetName, "C"+strconv.Itoa(j), labels["default"].label)
}
i := 0
for _, locale := range locales {
for _, locale := range config.Locales {
_, exists := labels[locale]
if exists {
f.SetCellValue(sheetName, getExcelCol(i+4)+strconv.Itoa(j), labels[locale].cartridge)
Expand All @@ -258,3 +183,99 @@ func main() {
fmt.Println(err)
}
}
func main() {
configFile := flag.String("config", "config.json", "file name")
getConfig(*configFile)
properties := map[string]map[string]map[string]map[string]string{}

for _, cartridge := range config.Cartridges {
var processedFiles []string
path := "cartridges/" + cartridge + "/cartridge/templates/resources"
dir, err := os.Open(path)
if err != nil {
fmt.Println("Fail to open folder: " + path)
continue
}
defer dir.Close()
fileInfos, err := dir.Readdir(-1)
if err != nil {
fmt.Println(err)
return
}
_, exists := properties[cartridge]
// create map, cartridge level
if !exists {
properties[cartridge] = map[string]map[string]map[string]string{}
}
for _, locale := range config.Locales {
for _, fi := range fileInfos {
if strings.HasSuffix(fi.Name(), "_"+locale+".properties") {
// get default property file
defaultFile := strings.TrimSuffix(fi.Name(), "_"+locale+".properties")
// create map for file
_, exists := properties[cartridge][defaultFile]
if !exists {
// process defaultFile
properties[cartridge][defaultFile] = map[string]map[string]string{}
filename := filepath.Join(path, defaultFile+".properties")
file, _ := os.Open(filename)
if err != nil {
log.Fatal(err)
}
if file != nil {
defer file.Close()
properties[cartridge][defaultFile]["default"] = map[string]string{}
processFile(file, properties[cartridge][defaultFile]["default"])
processedFiles = append(processedFiles, defaultFile+".properties")
}
}

_, exists = properties[cartridge][defaultFile][locale]
if !exists {
properties[cartridge][defaultFile][locale] = map[string]string{}
}

// process file
filename := filepath.Join(path, fi.Name())
file, _ := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
processFile(file, properties[cartridge][defaultFile][locale])
processedFiles = append(processedFiles, fi.Name())
}
}
}
// process file not in locales (files that have only default)
for _, fi := range fileInfos {
if strings.HasSuffix(fi.Name(), ".properties") {
alreadyProcessed := false
// check if file was already processed on the previous pass
for _, b := range processedFiles {
if b == fi.Name() {
alreadyProcessed = true
break
}
}
if !alreadyProcessed {
defaultFile := strings.TrimSuffix(fi.Name(), ".properties")
properties[cartridge][defaultFile] = map[string]map[string]string{}
properties[cartridge][defaultFile]["default"] = map[string]string{}
filename := filepath.Join(path, fi.Name())

file, _ := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
processFile(file, properties[cartridge][defaultFile]["default"])
processedFiles = append(processedFiles, fi.Name())
}
}
}
}
// prepare to "excelize" the properties.
excelizeProperties(properties)

}

0 comments on commit 61fdaa0

Please sign in to comment.