From a942ed5831a6894428fcd13a306da18524e61791 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Tue, 15 Oct 2024 20:45:08 +0200 Subject: [PATCH 01/27] Add Poe2ArbVersion to L10n --- flutter/flutter_config.go | 2 ++ flutter/flutter_config_test.go | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/flutter/flutter_config.go b/flutter/flutter_config.go index 4ec3921..31a0c66 100644 --- a/flutter/flutter_config.go +++ b/flutter/flutter_config.go @@ -24,6 +24,8 @@ type FlutterConfig struct { // // https://github.com/flutter/flutter/blob/61a0add2865c51bfee33939c1820709d1115c77d/packages/flutter_tools/lib/src/localizations/localizations_utils.dart#L291 type L10n struct { + Poe2ArbVersion string `yaml:"poe2arb-version"` + ARBDir string `yaml:"arb-dir"` TemplateArbFile string `yaml:"template-arb-file"` RequireResourceAttributes bool `yaml:"required-resource-attributes"` diff --git a/flutter/flutter_config_test.go b/flutter/flutter_config_test.go index a730c4e..b4abc6c 100644 --- a/flutter/flutter_config_test.go +++ b/flutter/flutter_config_test.go @@ -63,7 +63,7 @@ func TestNewFromDirectory(t *testing.T) { err = os.WriteFile(filepath.Join(dir, "pubspec.yaml"), []byte{}, 0o666) assert.NoError(t, err) - l10nContents := `{arb-dir: this-is/arb-dir/test, poeditor-project-id: 123123}` + l10nContents := `{arb-dir: this-is/arb-dir/test, poeditor-project-id: 123123, poe2arb-version: ">=0.5.0 <0.7"}` err = os.WriteFile(filepath.Join(dir, "l10n.yaml"), []byte(l10nContents), 0o666) assert.NoError(t, err) @@ -75,5 +75,6 @@ func TestNewFromDirectory(t *testing.T) { assert.Equal(t, "this-is/arb-dir/test", cfg.L10n.ARBDir) assert.Equal(t, "123123", cfg.L10n.POEditorProjectID) + assert.Equal(t, ">=0.5.0 <0.7", cfg.L10n.Poe2ArbVersion) }) } From 461cff0b810da074391c341740eda4d06cb6ca55 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Tue, 15 Oct 2024 20:46:12 +0200 Subject: [PATCH 02/27] Add go-version dependency --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index 5d54794..81fc26e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/TwiN/go-color v1.4.0 github.com/caarlos0/env/v6 v6.10.1 + github.com/hashicorp/go-version v1.7.0 github.com/spf13/cobra v1.6.1 github.com/wk8/go-ordered-map/v2 v2.1.1 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 3798500..ea400da 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= +github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= From 488cf1f7fa9161015d0e56e940f0a20b5c3fb31f Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Tue, 22 Oct 2024 20:24:45 +0200 Subject: [PATCH 03/27] Add version constraint guard --- cmd/guard.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/poe.go | 20 ++----------- 2 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 cmd/guard.go diff --git a/cmd/guard.go b/cmd/guard.go new file mode 100644 index 0000000..df748d9 --- /dev/null +++ b/cmd/guard.go @@ -0,0 +1,83 @@ +package cmd + +import ( + "context" + "fmt" + "os" + "strings" + + "github.com/hashicorp/go-version" + "github.com/leancodepl/poe2arb/flutter" + "github.com/spf13/cobra" +) + +type flutterConfigKey int + +const key flutterConfigKey = 1 + +func contextWithFlutterConfig(ctx context.Context, flutterConfig *flutter.FlutterConfig) context.Context { + return context.WithValue(ctx, key, flutterConfig) +} + +func flutterConfigFromContext(ctx context.Context) (*flutter.FlutterConfig, bool) { + flutterCfg, ok := ctx.Value(key).(*flutter.FlutterConfig) + return flutterCfg, ok +} + +func ensureSufficientVersion(versionConstraint string) error { + if versionConstraint == "" { + return nil + } + + constraint, err := newConstraintFromString(versionConstraint) + if err != nil { + return fmt.Errorf("invalid poe2arb-version format in l10n.yaml: %s", versionConstraint) + } + + version, _ := version.NewVersion(Version) + + if !constraint.Check(version) { + return fmt.Errorf("Poe2Arb version %s does not match constraint %s defined in l10n.yaml", version, versionConstraint) + } + + return nil +} + +func newConstraintFromString(versionConstraint string) (version.Constraints, error) { + return version.NewConstraint(strings.ReplaceAll(versionConstraint, " ", ", ")) +} + +func getFlutterConfig() (*flutter.FlutterConfig, error) { + workDir, err := os.Getwd() + if err != nil { + return nil, err + } + + flutterCfg, err := flutter.NewFromDirectory(workDir) + if err != nil { + return nil, err + } + + return flutterCfg, nil +} + +func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) { + log := getLogger(cmd) + + logSub := log.Info("loading Flutter config").Sub() + + flutterCfg, err := getFlutterConfig() + if err != nil { + logSub.Error("failed: " + err.Error()) + os.Exit(1) + } + + err = ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) + if err != nil { + logSub.Error("failed: " + err.Error()) + os.Exit(1) + } + + ctx := contextWithFlutterConfig(cmd.Context(), flutterCfg) + cmd.SetContext(ctx) +} diff --git a/cmd/poe.go b/cmd/poe.go index a7d7de8..ac1599b 100644 --- a/cmd/poe.go +++ b/cmd/poe.go @@ -24,6 +24,7 @@ var ( SilenceErrors: true, SilenceUsage: true, RunE: runPoe, + PreRun: getFlutterConfigAndEnsureSufficientVersion, } termPrefixRegexp = regexp.MustCompile("[a-zA-Z]*") ) @@ -103,10 +104,7 @@ func getOptionsSelector(cmd *cobra.Command) (*poeOptionsSelector, error) { return nil, err } - flutterCfg, err := getFlutterConfig() - if err != nil { - return nil, err - } + flutterCfg, _ := flutterConfigFromContext(cmd.Context()) return &poeOptionsSelector{ flags: cmd.Flags(), @@ -115,20 +113,6 @@ func getOptionsSelector(cmd *cobra.Command) (*poeOptionsSelector, error) { }, nil } -func getFlutterConfig() (*flutter.FlutterConfig, error) { - workDir, err := os.Getwd() - if err != nil { - return nil, err - } - - flutterCfg, err := flutter.NewFromDirectory(workDir) - if err != nil { - return nil, err - } - - return flutterCfg, nil -} - type poeCommand struct { options *poeOptions client *poeditor.Client From dc20ad081903da3e65267ebf257895dfade26f2f Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 15:19:34 +0200 Subject: [PATCH 04/27] Replace PreRun with PreRunE in poe command, change flutterConfigFromContext to flutterConfigFromCommand function, handle version.NewVersion error --- cmd/guard.go | 18 +++++++++++------- cmd/poe.go | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/guard.go b/cmd/guard.go index df748d9..1a883e2 100644 --- a/cmd/guard.go +++ b/cmd/guard.go @@ -19,9 +19,8 @@ func contextWithFlutterConfig(ctx context.Context, flutterConfig *flutter.Flutte return context.WithValue(ctx, key, flutterConfig) } -func flutterConfigFromContext(ctx context.Context) (*flutter.FlutterConfig, bool) { - flutterCfg, ok := ctx.Value(key).(*flutter.FlutterConfig) - return flutterCfg, ok +func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { + return cmd.Context().Value(key).(*flutter.FlutterConfig) } func ensureSufficientVersion(versionConstraint string) error { @@ -34,7 +33,10 @@ func ensureSufficientVersion(versionConstraint string) error { return fmt.Errorf("invalid poe2arb-version format in l10n.yaml: %s", versionConstraint) } - version, _ := version.NewVersion(Version) + version, err := version.NewVersion(Version) + if err != nil { + return fmt.Errorf("poe2arb version format is invalid: %s", err) + } if !constraint.Check(version) { return fmt.Errorf("Poe2Arb version %s does not match constraint %s defined in l10n.yaml", version, versionConstraint) @@ -61,7 +63,7 @@ func getFlutterConfig() (*flutter.FlutterConfig, error) { return flutterCfg, nil } -func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) { +func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { log := getLogger(cmd) logSub := log.Info("loading Flutter config").Sub() @@ -69,15 +71,17 @@ func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) flutterCfg, err := getFlutterConfig() if err != nil { logSub.Error("failed: " + err.Error()) - os.Exit(1) + return err } err = ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) if err != nil { logSub.Error("failed: " + err.Error()) - os.Exit(1) + return err } ctx := contextWithFlutterConfig(cmd.Context(), flutterCfg) cmd.SetContext(ctx) + + return nil } diff --git a/cmd/poe.go b/cmd/poe.go index ac1599b..fb60bbe 100644 --- a/cmd/poe.go +++ b/cmd/poe.go @@ -24,7 +24,7 @@ var ( SilenceErrors: true, SilenceUsage: true, RunE: runPoe, - PreRun: getFlutterConfigAndEnsureSufficientVersion, + PreRunE: getFlutterConfigAndEnsureSufficientVersion, } termPrefixRegexp = regexp.MustCompile("[a-zA-Z]*") ) @@ -104,7 +104,7 @@ func getOptionsSelector(cmd *cobra.Command) (*poeOptionsSelector, error) { return nil, err } - flutterCfg, _ := flutterConfigFromContext(cmd.Context()) + flutterCfg := flutterConfigFromCommand(cmd) return &poeOptionsSelector{ flags: cmd.Flags(), From ac9196d567707d5be5de1eb0fa8edbde103b81b3 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 15:20:45 +0200 Subject: [PATCH 05/27] Change constraint check error message --- cmd/guard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/guard.go b/cmd/guard.go index 1a883e2..3d6d765 100644 --- a/cmd/guard.go +++ b/cmd/guard.go @@ -39,7 +39,7 @@ func ensureSufficientVersion(versionConstraint string) error { } if !constraint.Check(version) { - return fmt.Errorf("Poe2Arb version %s does not match constraint %s defined in l10n.yaml", version, versionConstraint) + return fmt.Errorf("poe2arb version %s does not match constraint %s defined in l10n.yaml", version, versionConstraint) } return nil From 304d6b803b12ed0710729886e623814fab809c82 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 15:23:24 +0200 Subject: [PATCH 06/27] Change required constraint format, remove unnecessary newConstraintFromString function --- cmd/guard.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cmd/guard.go b/cmd/guard.go index 3d6d765..6f6ee4f 100644 --- a/cmd/guard.go +++ b/cmd/guard.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "strings" "github.com/hashicorp/go-version" "github.com/leancodepl/poe2arb/flutter" @@ -28,7 +27,7 @@ func ensureSufficientVersion(versionConstraint string) error { return nil } - constraint, err := newConstraintFromString(versionConstraint) + constraint, err := version.NewConstraint(versionConstraint) if err != nil { return fmt.Errorf("invalid poe2arb-version format in l10n.yaml: %s", versionConstraint) } @@ -45,10 +44,6 @@ func ensureSufficientVersion(versionConstraint string) error { return nil } -func newConstraintFromString(versionConstraint string) (version.Constraints, error) { - return version.NewConstraint(strings.ReplaceAll(versionConstraint, " ", ", ")) -} - func getFlutterConfig() (*flutter.FlutterConfig, error) { workDir, err := os.Getwd() if err != nil { From 2bc8bdd5ec69be34790e9517bb9af680fbf217df Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 15:28:17 +0200 Subject: [PATCH 07/27] Move Poe2ArbVersion field under custom options section --- flutter/flutter_config.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/flutter/flutter_config.go b/flutter/flutter_config.go index 31a0c66..4683efc 100644 --- a/flutter/flutter_config.go +++ b/flutter/flutter_config.go @@ -24,8 +24,6 @@ type FlutterConfig struct { // // https://github.com/flutter/flutter/blob/61a0add2865c51bfee33939c1820709d1115c77d/packages/flutter_tools/lib/src/localizations/localizations_utils.dart#L291 type L10n struct { - Poe2ArbVersion string `yaml:"poe2arb-version"` - ARBDir string `yaml:"arb-dir"` TemplateArbFile string `yaml:"template-arb-file"` RequireResourceAttributes bool `yaml:"required-resource-attributes"` @@ -35,6 +33,7 @@ type L10n struct { POEditorProjectID string `yaml:"poeditor-project-id"` POEditorLangs []string `yaml:"poeditor-langs"` POEditorTermPrefix string `yaml:"poeditor-term-prefix"` + Poe2ArbVersion string `yaml:"poe2arb-version"` } func newDefaultL10n() *L10n { From 11351259014b768065990e777e3de1dc80a43a3c Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 15:39:40 +0200 Subject: [PATCH 08/27] Remove flutterConfigKey type, replace key with flutterConfigKey of type ctkKey --- cmd/guard.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cmd/guard.go b/cmd/guard.go index 6f6ee4f..d1087f2 100644 --- a/cmd/guard.go +++ b/cmd/guard.go @@ -10,16 +10,14 @@ import ( "github.com/spf13/cobra" ) -type flutterConfigKey int - -const key flutterConfigKey = 1 +const flutterConfigKey = ctxKey(2) func contextWithFlutterConfig(ctx context.Context, flutterConfig *flutter.FlutterConfig) context.Context { - return context.WithValue(ctx, key, flutterConfig) + return context.WithValue(ctx, flutterConfigKey, flutterConfig) } func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { - return cmd.Context().Value(key).(*flutter.FlutterConfig) + return cmd.Context().Value(flutterConfigKey).(*flutter.FlutterConfig) } func ensureSufficientVersion(versionConstraint string) error { From cc525ac10d7095e93b7a3e9d5f1523d449969138 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 15:41:59 +0200 Subject: [PATCH 09/27] Remove unnecessary contextWithFlutterConfig function --- cmd/guard.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cmd/guard.go b/cmd/guard.go index d1087f2..9ea796b 100644 --- a/cmd/guard.go +++ b/cmd/guard.go @@ -12,10 +12,6 @@ import ( const flutterConfigKey = ctxKey(2) -func contextWithFlutterConfig(ctx context.Context, flutterConfig *flutter.FlutterConfig) context.Context { - return context.WithValue(ctx, flutterConfigKey, flutterConfig) -} - func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { return cmd.Context().Value(flutterConfigKey).(*flutter.FlutterConfig) } @@ -73,7 +69,7 @@ func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) return err } - ctx := contextWithFlutterConfig(cmd.Context(), flutterCfg) + ctx := context.WithValue(cmd.Context(), flutterConfigKey, flutterCfg) cmd.SetContext(ctx) return nil From c93b0bb2be35f2cd1d6b50e776ab8e8871b8cfd8 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 15:45:47 +0200 Subject: [PATCH 10/27] Rename file - guard.go -> flutter_config_version_guard.go --- cmd/{guard.go => flutter_config_version_guard.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cmd/{guard.go => flutter_config_version_guard.go} (100%) diff --git a/cmd/guard.go b/cmd/flutter_config_version_guard.go similarity index 100% rename from cmd/guard.go rename to cmd/flutter_config_version_guard.go From b459f48a936579091612d73064fd0430fb4074c7 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 17:19:31 +0200 Subject: [PATCH 11/27] Use color markdown in README --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7fb6117..8a54c92 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,8 @@ poe2arb convert io --lang en < Hello_World_English.json > lib/l10n/app_en.arb ### Seeding POEditor project -**EXPERIMENTAL FEATURE** +> [!WARNING] +> **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, @@ -96,8 +97,8 @@ with already populated translations is inadvisable. ## Syntax & supported features -Term name must be a valid Dart field name, additionaly, it must start with a -lowercase letter ([Flutter's constraint][term-name-constraint]). +> [!IMPORTANT] +> Term name must be a valid Dart field name, additionaly, it must start with a lowercase letter ([Flutter's constraint][term-name-constraint]). ### Term prefix filtering @@ -150,6 +151,7 @@ Available placeholder types: **Only template files can define placeholders with their type and format.** In non-template languages, placeholders' types and formats are ignored and no logical errors are reported. +> [!NOTE] > \*If you're using Flutter 3.5 or older, you need to specify format for numeric placeholders. > Otherwise `flutter gen-l10n` will fail. You can look at the legacy placeholder syntax diagrams > [for placeholders here][flutter35-placeholders-diagram] and for [plural's `count` placeholders here][flutter35-count-placeholders-diagram]. From 5abcd316673b190412126b2381884a5526ff7675 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 17:24:07 +0200 Subject: [PATCH 12/27] Fix formatting --- cmd/poe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/poe.go b/cmd/poe.go index fb60bbe..821ecc2 100644 --- a/cmd/poe.go +++ b/cmd/poe.go @@ -24,7 +24,7 @@ var ( SilenceErrors: true, SilenceUsage: true, RunE: runPoe, - PreRunE: getFlutterConfigAndEnsureSufficientVersion, + PreRunE: getFlutterConfigAndEnsureSufficientVersion, } termPrefixRegexp = regexp.MustCompile("[a-zA-Z]*") ) From babdad709c3c26ca22205e32ae42dd4dbd08e091 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 17:26:24 +0200 Subject: [PATCH 13/27] Use getFlutterConfigAndEnsureSufficientVersion as PreRunE in seedCmd --- cmd/seed.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/seed.go b/cmd/seed.go index 9cc46d7..50666b7 100644 --- a/cmd/seed.go +++ b/cmd/seed.go @@ -19,6 +19,7 @@ var seedCmd = &cobra.Command{ SilenceErrors: true, SilenceUsage: true, RunE: runSeed, + PreRunE: getFlutterConfigAndEnsureSufficientVersion, } func init() { From 40470a67d1b0705e40cf51d971971c6cc9c04b5c Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 17:34:21 +0200 Subject: [PATCH 14/27] Add getFlutterConfigAndEnsureSufficientVersion function doc --- cmd/flutter_config_version_guard.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 9ea796b..25c229a 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -52,6 +52,8 @@ func getFlutterConfig() (*flutter.FlutterConfig, error) { return flutterCfg, nil } +// getFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, +// puts it in the context and verifies if poe2arb version matches constraint. func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { log := getLogger(cmd) From 6e33b3a8e11407f0fb53b49591f86f70f4c75386 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 17:35:20 +0200 Subject: [PATCH 15/27] Change functions order --- cmd/flutter_config_version_guard.go | 50 ++++++++++++++--------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 25c229a..65fd65c 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -16,6 +16,31 @@ func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { return cmd.Context().Value(flutterConfigKey).(*flutter.FlutterConfig) } +// getFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, +// puts it in the context and verifies if poe2arb version matches constraint. +func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { + log := getLogger(cmd) + + logSub := log.Info("loading Flutter config").Sub() + + flutterCfg, err := getFlutterConfig() + if err != nil { + logSub.Error("failed: " + err.Error()) + return err + } + + err = ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) + if err != nil { + logSub.Error("failed: " + err.Error()) + return err + } + + ctx := context.WithValue(cmd.Context(), flutterConfigKey, flutterCfg) + cmd.SetContext(ctx) + + return nil +} + func ensureSufficientVersion(versionConstraint string) error { if versionConstraint == "" { return nil @@ -51,28 +76,3 @@ func getFlutterConfig() (*flutter.FlutterConfig, error) { return flutterCfg, nil } - -// getFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, -// puts it in the context and verifies if poe2arb version matches constraint. -func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { - log := getLogger(cmd) - - logSub := log.Info("loading Flutter config").Sub() - - flutterCfg, err := getFlutterConfig() - if err != nil { - logSub.Error("failed: " + err.Error()) - return err - } - - err = ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) - if err != nil { - logSub.Error("failed: " + err.Error()) - return err - } - - ctx := context.WithValue(cmd.Context(), flutterConfigKey, flutterCfg) - cmd.SetContext(ctx) - - return nil -} From a57e2d22077c362e88fbf4a92676e4743358dca6 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 18:01:37 +0200 Subject: [PATCH 16/27] Add poe2arb version constrain description to README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 8a54c92..4f9beef 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,15 @@ other: Andy has {count} kilograms of {fruit}. You must provide at least `other` plural category for your translations, otherwise it won't be converted. +## Constraining version for a Flutter project + +You can constrain poe2arb version by specifying `poe2arb-version` option in `l10n.yaml`. + +Available formats: +* specific version - `0.5.1` +* version range - `>=0.5.1, <0.7` +* min/max version - `>0.5.1`, `<=0.7` + ## Contributing ### Formatting From d8e4403a72aa4bd2e24441616f9986bb1ce9c424 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 18:03:30 +0200 Subject: [PATCH 17/27] Improve getFlutterConfigAndEnsureSufficientVersion function doc --- cmd/flutter_config_version_guard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 65fd65c..7b383b5 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -17,7 +17,7 @@ func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { } // getFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, -// puts it in the context and verifies if poe2arb version matches constraint. +// puts it in the command's context and verifies if poe2arb version matches constraint. func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { log := getLogger(cmd) From 4890dbeee608918031dcb37612e924e9f4c09fcd Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Sat, 26 Oct 2024 18:05:39 +0200 Subject: [PATCH 18/27] Change flutter config test parameters --- flutter/flutter_config_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flutter/flutter_config_test.go b/flutter/flutter_config_test.go index b4abc6c..e808d91 100644 --- a/flutter/flutter_config_test.go +++ b/flutter/flutter_config_test.go @@ -63,7 +63,7 @@ func TestNewFromDirectory(t *testing.T) { err = os.WriteFile(filepath.Join(dir, "pubspec.yaml"), []byte{}, 0o666) assert.NoError(t, err) - l10nContents := `{arb-dir: this-is/arb-dir/test, poeditor-project-id: 123123, poe2arb-version: ">=0.5.0 <0.7"}` + l10nContents := `{arb-dir: this-is/arb-dir/test, poeditor-project-id: 123123, poe2arb-version: ">=0.5.0, <0.7"}` err = os.WriteFile(filepath.Join(dir, "l10n.yaml"), []byte(l10nContents), 0o666) assert.NoError(t, err) @@ -75,6 +75,6 @@ func TestNewFromDirectory(t *testing.T) { assert.Equal(t, "this-is/arb-dir/test", cfg.L10n.ARBDir) assert.Equal(t, "123123", cfg.L10n.POEditorProjectID) - assert.Equal(t, ">=0.5.0 <0.7", cfg.L10n.Poe2ArbVersion) + assert.Equal(t, ">=0.5.0, <0.7", cfg.L10n.Poe2ArbVersion) }) } From f695d3aa921799dc01ec2a47f9e96034fdc151be Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Thu, 31 Oct 2024 10:23:45 +0100 Subject: [PATCH 19/27] Change placeholder type for error --- cmd/flutter_config_version_guard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 7b383b5..0dbd9c3 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -53,7 +53,7 @@ func ensureSufficientVersion(versionConstraint string) error { version, err := version.NewVersion(Version) if err != nil { - return fmt.Errorf("poe2arb version format is invalid: %s", err) + return fmt.Errorf("poe2arb version format is invalid: %w", err) } if !constraint.Check(version) { From fea33bbe7c5b5e9dac9dda56a90ac6300e29df32 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Thu, 31 Oct 2024 17:43:55 +0100 Subject: [PATCH 20/27] Add go-version link to README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 4f9beef..d3aa18e 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,8 @@ Available formats: * version range - `>=0.5.1, <0.7` * min/max version - `>0.5.1`, `<=0.7` +You can find more information about version constraints format [here][go-version]. + ## Contributing ### Formatting @@ -266,6 +268,7 @@ git push origin v0.1.1 [flutter35-count-placeholders-diagram]: https://github.com/leancodepl/poe2arb/blob/24be17d6721698526c879b3fada87183b359e8e8/art/count-placeholder-syntax.svg [placeholder-diagram-img]: art/placeholder-syntax.svg [count-placeholder-diagram-img]: art/count-placeholder-syntax.svg +[go-version]: https://github.com/hashicorp/go-version [gofumpt]: https://github.com/mvdan/gofumpt [gofmt]: https://pkg.go.dev/cmd/gofmt [staticcheck]: https://staticcheck.io From 57251d04657aa6792879c0e7d6c7735503d4f005 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Thu, 31 Oct 2024 18:13:30 +0100 Subject: [PATCH 21/27] Use empty structs for keys --- cmd/flutter_config_version_guard.go | 6 +++--- cmd/poe2arb.go | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 0dbd9c3..7fd0f18 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -10,10 +10,10 @@ import ( "github.com/spf13/cobra" ) -const flutterConfigKey = ctxKey(2) +type flutterConfigKey struct {} func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { - return cmd.Context().Value(flutterConfigKey).(*flutter.FlutterConfig) + return cmd.Context().Value(flutterConfigKey{}).(*flutter.FlutterConfig) } // getFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, @@ -35,7 +35,7 @@ func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) return err } - ctx := context.WithValue(cmd.Context(), flutterConfigKey, flutterCfg) + ctx := context.WithValue(cmd.Context(), flutterConfigKey{}, flutterCfg) cmd.SetContext(ctx) return nil diff --git a/cmd/poe2arb.go b/cmd/poe2arb.go index a231477..3ea4f89 100644 --- a/cmd/poe2arb.go +++ b/cmd/poe2arb.go @@ -14,9 +14,7 @@ var rootCmd = &cobra.Command{ Short: "POEditor JSON to Flutter ARB converter", } -type ctxKey int - -const loggerKey = ctxKey(1) +type loggerKey struct{} func Execute(logger *log.Logger) { rootCmd.AddCommand(convertCmd) @@ -24,7 +22,7 @@ func Execute(logger *log.Logger) { rootCmd.AddCommand(seedCmd) rootCmd.AddCommand(versionCmd) - ctx := context.WithValue(context.Background(), loggerKey, logger) + ctx := context.WithValue(context.Background(), loggerKey{}, logger) if err := rootCmd.ExecuteContext(ctx); err != nil { os.Exit(1) @@ -32,5 +30,5 @@ func Execute(logger *log.Logger) { } func getLogger(cmd *cobra.Command) *log.Logger { - return cmd.Context().Value(loggerKey).(*log.Logger) + return cmd.Context().Value(loggerKey{}).(*log.Logger) } From 89e11cc31b21d45b0d62bcd481a487f15afb71c8 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Thu, 31 Oct 2024 18:29:01 +0100 Subject: [PATCH 22/27] Put functions inside flutterConfigVersionGuard struct --- cmd/flutter_config_version_guard.go | 12 +++++++----- cmd/poe.go | 2 +- cmd/seed.go | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 7fd0f18..74ac3d5 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -12,24 +12,26 @@ import ( type flutterConfigKey struct {} +type flutterConfigVersionGuard struct {} + func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { return cmd.Context().Value(flutterConfigKey{}).(*flutter.FlutterConfig) } // getFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, // puts it in the command's context and verifies if poe2arb version matches constraint. -func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { +func (fvg flutterConfigVersionGuard) GetFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { log := getLogger(cmd) logSub := log.Info("loading Flutter config").Sub() - flutterCfg, err := getFlutterConfig() + flutterCfg, err := fvg.getFlutterConfig() if err != nil { logSub.Error("failed: " + err.Error()) return err } - err = ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) + err = fvg.ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) if err != nil { logSub.Error("failed: " + err.Error()) return err @@ -41,7 +43,7 @@ func getFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) return nil } -func ensureSufficientVersion(versionConstraint string) error { +func (flutterConfigVersionGuard) ensureSufficientVersion(versionConstraint string) error { if versionConstraint == "" { return nil } @@ -63,7 +65,7 @@ func ensureSufficientVersion(versionConstraint string) error { return nil } -func getFlutterConfig() (*flutter.FlutterConfig, error) { +func (flutterConfigVersionGuard) getFlutterConfig() (*flutter.FlutterConfig, error) { workDir, err := os.Getwd() if err != nil { return nil, err diff --git a/cmd/poe.go b/cmd/poe.go index 821ecc2..de2c99e 100644 --- a/cmd/poe.go +++ b/cmd/poe.go @@ -24,7 +24,7 @@ var ( SilenceErrors: true, SilenceUsage: true, RunE: runPoe, - PreRunE: getFlutterConfigAndEnsureSufficientVersion, + PreRunE: flutterConfigVersionGuard{}.GetFlutterConfigAndEnsureSufficientVersion, } termPrefixRegexp = regexp.MustCompile("[a-zA-Z]*") ) diff --git a/cmd/seed.go b/cmd/seed.go index 50666b7..5c1f841 100644 --- a/cmd/seed.go +++ b/cmd/seed.go @@ -19,7 +19,7 @@ var seedCmd = &cobra.Command{ SilenceErrors: true, SilenceUsage: true, RunE: runSeed, - PreRunE: getFlutterConfigAndEnsureSufficientVersion, + PreRunE: flutterConfigVersionGuard{}.GetFlutterConfigAndEnsureSufficientVersion, } func init() { From 91153ca73bf18217df1d0c5c17d790e173bbdaef Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Mon, 4 Nov 2024 18:36:34 +0100 Subject: [PATCH 23/27] Fix GetFlutterConfigAndEnsureSufficientVersion doc --- cmd/flutter_config_version_guard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 74ac3d5..494cf4a 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -18,7 +18,7 @@ func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { return cmd.Context().Value(flutterConfigKey{}).(*flutter.FlutterConfig) } -// getFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, +// GetFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, // puts it in the command's context and verifies if poe2arb version matches constraint. func (fvg flutterConfigVersionGuard) GetFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { log := getLogger(cmd) From db377d7f57bef44bd7acefd676874a6e0a757143 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Tue, 5 Nov 2024 14:02:31 +0100 Subject: [PATCH 24/27] Fix formatting --- cmd/flutter_config_version_guard.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index 494cf4a..a996f05 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -10,9 +10,9 @@ import ( "github.com/spf13/cobra" ) -type flutterConfigKey struct {} +type flutterConfigKey struct{} -type flutterConfigVersionGuard struct {} +type flutterConfigVersionGuard struct{} func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { return cmd.Context().Value(flutterConfigKey{}).(*flutter.FlutterConfig) From bfec7fcffc2ae3ae0f7ac371777470145f1c4f23 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Tue, 5 Nov 2024 19:27:17 +0100 Subject: [PATCH 25/27] Fix receiver name --- cmd/flutter_config_version_guard.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/flutter_config_version_guard.go b/cmd/flutter_config_version_guard.go index a996f05..d354850 100644 --- a/cmd/flutter_config_version_guard.go +++ b/cmd/flutter_config_version_guard.go @@ -20,18 +20,18 @@ func flutterConfigFromCommand(cmd *cobra.Command) *flutter.FlutterConfig { // GetFlutterConfigAndEnsureSufficientVersion gets Flutter project configuration, // puts it in the command's context and verifies if poe2arb version matches constraint. -func (fvg flutterConfigVersionGuard) GetFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { +func (fcvg flutterConfigVersionGuard) GetFlutterConfigAndEnsureSufficientVersion(cmd *cobra.Command, _ []string) error { log := getLogger(cmd) logSub := log.Info("loading Flutter config").Sub() - flutterCfg, err := fvg.getFlutterConfig() + flutterCfg, err := fcvg.getFlutterConfig() if err != nil { logSub.Error("failed: " + err.Error()) return err } - err = fvg.ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) + err = fcvg.ensureSufficientVersion(flutterCfg.L10n.Poe2ArbVersion) if err != nil { logSub.Error("failed: " + err.Error()) return err From b993fe46689b89a994fddfffd8d83910c41aea6e Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Tue, 5 Nov 2024 19:42:23 +0100 Subject: [PATCH 26/27] Create global versionGuard variable --- cmd/poe.go | 2 +- cmd/poe2arb.go | 11 +++++++---- cmd/seed.go | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/poe.go b/cmd/poe.go index de2c99e..70ea13c 100644 --- a/cmd/poe.go +++ b/cmd/poe.go @@ -24,7 +24,7 @@ var ( SilenceErrors: true, SilenceUsage: true, RunE: runPoe, - PreRunE: flutterConfigVersionGuard{}.GetFlutterConfigAndEnsureSufficientVersion, + PreRunE: versionGuard.GetFlutterConfigAndEnsureSufficientVersion, } termPrefixRegexp = regexp.MustCompile("[a-zA-Z]*") ) diff --git a/cmd/poe2arb.go b/cmd/poe2arb.go index 3ea4f89..da27ecd 100644 --- a/cmd/poe2arb.go +++ b/cmd/poe2arb.go @@ -9,10 +9,13 @@ import ( "github.com/spf13/cobra" ) -var rootCmd = &cobra.Command{ - Use: "poe2arb", - Short: "POEditor JSON to Flutter ARB converter", -} +var ( + rootCmd = &cobra.Command{ + Use: "poe2arb", + Short: "POEditor JSON to Flutter ARB converter", + } + versionGuard = flutterConfigVersionGuard{} +) type loggerKey struct{} diff --git a/cmd/seed.go b/cmd/seed.go index 5c1f841..0aa4174 100644 --- a/cmd/seed.go +++ b/cmd/seed.go @@ -19,7 +19,7 @@ var seedCmd = &cobra.Command{ SilenceErrors: true, SilenceUsage: true, RunE: runSeed, - PreRunE: flutterConfigVersionGuard{}.GetFlutterConfigAndEnsureSufficientVersion, + PreRunE: versionGuard.GetFlutterConfigAndEnsureSufficientVersion, } func init() { From 9e9b91615d6432482824d0e07c1d6c6746fc8312 Mon Sep 17 00:00:00 2001 From: Krzysztof Mamak Date: Tue, 5 Nov 2024 20:02:20 +0100 Subject: [PATCH 27/27] Replace formats list with code snippet --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d3aa18e..1dc19bf 100644 --- a/README.md +++ b/README.md @@ -204,10 +204,13 @@ You must provide at least `other` plural category for your translations, otherwi You can constrain poe2arb version by specifying `poe2arb-version` option in `l10n.yaml`. -Available formats: -* specific version - `0.5.1` -* version range - `>=0.5.1, <0.7` -* min/max version - `>0.5.1`, `<=0.7` +```yaml +# Available formats: +poe2arb-version: "0.5.1" # Specific version +poe2arb-version: ">=0.5.1, <0.7" # Version range +poe2arb-version: ">0.5.1" # Minimum version +poe2arb-version: "<=0.7" # Maximum version +``` You can find more information about version constraints format [here][go-version].