Skip to content

Commit

Permalink
big changes v2
Browse files Browse the repository at this point in the history
  • Loading branch information
zakaria-chahboun committed Aug 25, 2024
1 parent 61d4c15 commit ee1f30c
Show file tree
Hide file tree
Showing 19 changed files with 629 additions and 858 deletions.
107 changes: 107 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/zakaria-chahboun/cute"
)

var (
rootCmd = &cobra.Command{
Use: "tarjem",
Short: "CLI tool for translation management",
}

initCmd = &cobra.Command{
Use: "init",
Short: "Initialize translations.yaml files",
Run: func(cmd *cobra.Command, args []string) {
force, _ := cmd.Flags().GetBool("force")
// Forced
if force {
err := createInitTranslationFile(DEFAULT_TRANSLATIONS_FILE_PATH, DEFAULT_TRANSLATIONS_FILE_DATA)
cute.Check("Error", err)
cute.Println(DEFAULT_TRANSLATIONS_FILE_PATH, "was created successfully.")
os.Exit(1)
}
// Non-Forced: Check if the file exists
if _, err := os.Stat(DEFAULT_TRANSLATIONS_FILE_PATH); !os.IsNotExist(err) {
cute.Println(DEFAULT_TRANSLATIONS_FILE_PATH, "already exists. Use --force to overwrite.")
} else {
err := createInitTranslationFile(DEFAULT_TRANSLATIONS_FILE_PATH, DEFAULT_TRANSLATIONS_FILE_DATA)
cute.Check("Error", err)
cute.Println(DEFAULT_TRANSLATIONS_FILE_PATH, "was created successfully.")
}
os.Exit(1)
},
}

exportCmd = &cobra.Command{
Use: "export",
Short: "Export generated Go files",
Run: func(cmd *cobra.Command, args []string) {
lang, _ := cmd.Flags().GetString("lang")
pkg, _ := cmd.Flags().GetString("package")

if lang == "" {
cute.Check("Error", fmt.Errorf("language must be specified using --lang"))
cmd.Help()
os.Exit(1)
}

// translations.yaml exists?
_, err := os.Stat(DEFAULT_TRANSLATIONS_FILE_PATH)
if err != nil {
alert()
os.Exit(1)
}

// Export logic
if pkg != "" {
EXPORTED_PACKAGE_NAME = pkg
}
ExportForProgrammingLanguage(lang)
os.Exit(1)
},
}

clearCmd = &cobra.Command{
Use: "clear",
Short: "Remove the exported translations.go file",
Run: func(cmd *cobra.Command, args []string) {
// Check if the file exists
if _, err := os.Stat(EXPORTED_TRANSLATIONS_FILE); os.IsNotExist(err) {
cute.Println("No exported files to remove.")
os.Exit(1)
}
// Remove the file
err := os.Remove(EXPORTED_TRANSLATIONS_FILE)
if err != nil {
cute.Check("Error removing file", err)
os.Exit(1)
}
cute.Println("Successfully removed", EXPORTED_TRANSLATIONS_FILE)
os.Exit(1)
},
}

helpCmd = &cobra.Command{
Use: "help",
Short: "Show help information",
Run: func(cmd *cobra.Command, args []string) {
rootCmd.Help()
os.Exit(1)
},
}

versionCmd = &cobra.Command{
Use: "version",
Short: "Display version information",
Run: func(cmd *cobra.Command, args []string) {
cute.Println("tarjem version", version)
os.Exit(1)
},
}
)
78 changes: 78 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"bytes"
"fmt"
"go/format"
"strings"
"text/template"

"github.com/zakaria-chahboun/cute"
)

var SupportedProgrammingLanguages = []string{"go"}

func ExportForProgrammingLanguage(pl string) {

if !contains(SupportedProgrammingLanguages, strings.ToLower(pl)) {
cute.Println("Unsupported language", "Only:", strings.Join(SupportedProgrammingLanguages, ","))
return
}

// functions to be used inside the template files
templateFuncs := template.FuncMap{
"snakeCaseToCamelCase": snakeCaseToCamelCase,
"convertToGoType": convertToGoType,
"titleCase": strings.Title,
"join": strings.Join,
"trim": strings.TrimSpace,
"isBlank": isBlank,
"containsDateType": containsDateType,
"correctPlaceholders": correctPlaceholders,
"replacePlaceholdersWithFormat": replacePlaceholdersWithFormat,
}

// parse template files
translationsTemplate := template.Must(template.New("translations.go.tmpl").Funcs(templateFuncs).Parse(string(TRANSLATIONS_TEMPLATE_DATA)))

// load translations.yaml file
translationsData, err := loadTranslationsFromFile(DEFAULT_TRANSLATIONS_FILE_PATH)
cute.Check("load translations.yaml file", err)

// parse messages
err = parseMessages(translationsData)
cute.Check("parsing translations.yaml file", err)

// Bind data into translations template
var compiledOutput bytes.Buffer
err = translationsTemplate.Execute(&compiledOutput, struct {
PackageName string
Messages Messages
UniqueLangs []string
UniqueVariableTypes []string
DateFormat string
TimeFormat string
DateTimeFormat string
}{
PackageName: EXPORTED_PACKAGE_NAME,
Messages: translationsData,
UniqueLangs: getUniqueLangs(translationsData), // all_messages not messages!
UniqueVariableTypes: getUniqueVariableTypes(translationsData),
DateFormat: DATE_FORMAT,
TimeFormat: TIME_FORMAT,
DateTimeFormat: DATETIME_FORMAT,
})
cute.Check("bind data in translations.go.tmpl file", err)

// go format
formattedOutput, err := format.Source(compiledOutput.Bytes())
cute.Check("go format", err)

// save template with values as a go file (.go)
err = saveToFile(EXPORTED_TRANSLATIONS_FILE, formattedOutput)
cute.Check(fmt.Sprintf("exporting %s file", EXPORTED_TRANSLATIONS_FILE), err)

// done
cute.SetTitleColor(cute.BrightGreen)
cute.Println("translations generated successfully 🎉")
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ module github.com/zakaria-chahboun/tarjem
go 1.18

require (
github.com/BurntSushi/toml v1.2.0
github.com/spf13/cobra v1.8.1
github.com/zakaria-chahboun/cute v1.2.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
16 changes: 12 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/zakaria-chahboun/cute v1.0.4 h1:KcaM6eQjWYoJNrBQe+8PvQ+ftRCX+UdSMDxYxc9jeOs=
github.com/zakaria-chahboun/cute v1.0.4/go.mod h1:RAmXt97oqG8Hdfnz1lWq8D3XNGEwgijeM0H5ubREFIc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/zakaria-chahboun/cute v1.2.0 h1:fSwn7FbBjMejxbCCkxTP8v/XI3oia/74P9P+zqi/UMI=
github.com/zakaria-chahboun/cute v1.2.0/go.mod h1:RAmXt97oqG8Hdfnz1lWq8D3XNGEwgijeM0H5ubREFIc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit ee1f30c

Please sign in to comment.