Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add seed command #79

Merged
merged 9 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*.dll
*.so
*.dylib

poe2arb
!poe2arb/

# Test binary, built with `go test -c`
*.test
Expand All @@ -24,4 +26,4 @@ go.work
# Goreleaser
dist/

.vscode
.vscode
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ Currently, only an stdin/stdout is supported for the `poe2arb convert` command.
poe2arb convert io --lang en < Hello_World_English.json > lib/l10n/app_en.arb
```

### Seeding POEditor project

**EXPERIMENTAL FEATURE**

If you're setting up a project from some template code, you probably already have some ARB files that need
to be imported into the POEditor project. Using the POEditor's built-in tool won't give a satisfying result,
as it will completely ignore placeholders along with their types and other parameters, as well as it won't
"understand" the plural ICU message format. This is where `poe2arb seed` command comes into place.

`poe2arb seed` command uses the same configuration as the `poe2arb poe`, but **it needs API access token with a write
access**, to create language in the project if needed, and to upload the translations and terms.

This command is meant only for seeding the project, i.e. setting its first contents. It won't override your existing
translations and won't delete anything. That said, it should still be run with caution and running this on projects
with already populated translations is inadvisable.

## Syntax & supported features

Term name must be a valid Dart field name, additionaly, it must start with a
Expand Down
4 changes: 2 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"os"

"github.com/leancodepl/poe2arb/converter"
"github.com/leancodepl/poe2arb/convert/poe2arb"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -40,7 +40,7 @@ func runConvertIo(cmd *cobra.Command, args []string) error {
noTemplate, _ := cmd.Flags().GetBool(noTemplateFlag)
termPrefix, _ := cmd.Flags().GetString(termPrefixFlag)

conv := converter.NewConverter(os.Stdin, &converter.ConverterOptions{
conv := poe2arb.NewConverter(os.Stdin, &poe2arb.ConverterOptions{
Lang: lang,
Template: !noTemplate,
RequireResourceAttributes: true,
Expand Down
4 changes: 2 additions & 2 deletions cmd/poe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"regexp"
"strings"

"github.com/leancodepl/poe2arb/converter"
"github.com/leancodepl/poe2arb/convert/poe2arb"
"github.com/leancodepl/poe2arb/flutter"
"github.com/leancodepl/poe2arb/log"
"github.com/leancodepl/poe2arb/poeditor"
Expand Down Expand Up @@ -242,7 +242,7 @@ func (c *poeCommand) ExportLanguage(lang poeditor.Language, template bool) error

convertLogSub := logSub.Info("converting JSON to ARB").Sub()

conv := converter.NewConverter(resp.Body, &converter.ConverterOptions{
conv := poe2arb.NewConverter(resp.Body, &poe2arb.ConverterOptions{
Lang: lang.Code,
Template: template,
RequireResourceAttributes: c.options.RequireResourceAttributes,
Expand Down
1 change: 1 addition & 0 deletions cmd/poe2arb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const loggerKey = ctxKey(1)
func Execute(logger *log.Logger) {
rootCmd.AddCommand(convertCmd)
rootCmd.AddCommand(poeCmd)
rootCmd.AddCommand(seedCmd)
rootCmd.AddCommand(versionCmd)

ctx := context.WithValue(context.Background(), loggerKey, logger)
Expand Down
162 changes: 162 additions & 0 deletions cmd/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package cmd

import (
"bytes"
"errors"
"os"
"path/filepath"
"strings"
"time"

"github.com/leancodepl/poe2arb/convert/arb2poe"
"github.com/leancodepl/poe2arb/poeditor"
"github.com/spf13/cobra"
)

var seedCmd = &cobra.Command{
Use: "seed",
Short: "EXPERIMENTAL! Seeds POEditor with data from ARBs. To be used only on empty projects.",
SilenceErrors: true,
SilenceUsage: true,
RunE: runSeed,
}

func init() {
seedCmd.Flags().StringP(projectIDFlag, "p", "", "POEditor project ID")
seedCmd.Flags().StringP(tokenFlag, "t", "", "POEditor API token")
seedCmd.Flags().StringP(termPrefixFlag, "", "", "POEditor term prefix")
seedCmd.Flags().StringP(outputDirFlag, "o", "", `Output directory [default: "."]`)
seedCmd.Flags().StringSliceP(overrideLangsFlag, "", []string{}, "Override downloaded languages")
}

func runSeed(cmd *cobra.Command, args []string) error {
log := getLogger(cmd)

fileLog := log.Info("loading options").Sub()

sel, err := getOptionsSelector(cmd)
if err != nil {
fileLog.Error("failed: " + err.Error())
return err
}

options, err := sel.SelectOptions()
if err != nil {
fileLog.Error("failed: " + err.Error())
return err
}

fileLog = log.Info("reading ARB files in %s", options.OutputDir).Sub()

var files []string
rawFiles, err := os.ReadDir(options.OutputDir)
if err != nil {
fileLog.Error("failed: " + err.Error())
return err
}
for _, file := range rawFiles {
if file.IsDir() {
continue
}

fileName := file.Name()
if !strings.HasPrefix(fileName, options.ARBPrefix) || filepath.Ext(fileName) != ".arb" {
continue
}

files = append(files, filepath.Join(options.OutputDir, fileName))
}

if len(files) == 0 {
fileLog.Error("no ARB files found")
return err
} else {
fileLog.Info("found %d ARB files", len(files))
}

poeClient := poeditor.NewClient(options.Token)

availableLangs, err := poeClient.GetProjectLanguages(options.ProjectID)
if err != nil {
log.Error("failed fetching languages: " + err.Error())
return err
}

first := true
for _, filePath := range files {
fileLog = log.Info("seeding %s", filepath.Base(filePath)).Sub()
fileLog.Info("converting ARB to JSON")

file, err := os.Open(filePath)
if err != nil {
fileLog.Error("failed: " + err.Error())
return err
}

converter := arb2poe.NewConverter(file, options.TemplateLocale, options.TermPrefix)

var b bytes.Buffer
lang, err := converter.Convert(&b)
if err != nil {
if errors.Is(err, arb2poe.NoTermsError) {
fileLog.Info("no terms to convert")
continue
}

fileLog.Error("failed: " + err.Error())
return err
}

if len(options.OverrideLangs) > 0 {
langFound := false
for _, overridenLang := range options.OverrideLangs {
if lang == overridenLang {
langFound = true
break
}
}

if !langFound {
fileLog.Info("skipping language %s", lang)
continue
}
}

availableLangFound := false
for _, availableLang := range availableLangs {
if lang == availableLang.Code {
availableLangFound = true
break
}
}

if !availableLangFound {
langLog := fileLog.Info("adding language %s to project", lang).Sub()

err = poeClient.AddLanguage(options.ProjectID, lang)
if err != nil {
langLog.Error("failed: " + err.Error())
return err
}
}

if !first {
fileLog.Info("waiting 30 seconds to avoid rate limiting")
time.Sleep(30 * time.Second)
}

uploadLog := fileLog.Info("uploading JSON to POEditor").Sub()

err = poeClient.Upload(options.ProjectID, lang, &b)
if err != nil {
uploadLog.Error("failed: " + err.Error())
return err
} else {
fileLog.Success("done")
}

first = false
}

return nil
}
29 changes: 29 additions & 0 deletions convert/arb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package convert holds structures common to both directions of conversion.
package convert

import (
orderedmap "github.com/wk8/go-ordered-map/v2"
)

const LocaleKey = "@@locale"

type ARBMessage struct {
Name string
Translation string
Attributes *ARBMessageAttributes
}

type ARBMessageAttributes struct {
Description string `json:"description,omitempty"`
Placeholders *orderedmap.OrderedMap[string, *ARBPlaceholder] `json:"placeholders,omitempty"`
}

func (a *ARBMessageAttributes) IsEmpty() bool {
return a.Description == "" && (a.Placeholders == nil || a.Placeholders.Len() == 0)
}

type ARBPlaceholder struct {
Name string `json:"-"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
}
Loading