Skip to content

Commit 530e671

Browse files
cmaglieper1234
andauthored
[breaking] legacy: refactoring of the old i18n.Logger (part 2) (#1625)
* legacy: refactored ErrorfWithLogger function Now is no longer needed, this function was basically made for arduino-builder to allow transfering the error to the IDE, BTW the correct fix must be made inside arduino-builder (to actually let him push the error in the logger). * Renamed legacy ctx fields ExecStdout/ExecStderr to Stdout/Stderr * Removed empty Lint() function * Removed dependency on i18n.Logger * Removed no more used i18n.Logger \o/ \o/ * Simplified i18n.Init function * legacy: builder default output on os.Stdout/os.Stderr * legacy: updated integration tests for slightly different output of builder * Use positional parameter for most translated string This will make easier to transfer translations from the Arduino IDE 1.8 * Added note to UPGRADING.md * Updated UPGRADING.md * Removed extra blank line in library detection recap * Update docs/UPGRADING.md Co-authored-by: per1234 <[email protected]>
1 parent 5beeba4 commit 530e671

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+316
-568
lines changed

Diff for: arduino/libraries/lint.go

-45
This file was deleted.

Diff for: commands/compile/compile.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/arduino/arduino-cli/configuration"
3434
"github.com/arduino/arduino-cli/i18n"
3535
"github.com/arduino/arduino-cli/legacy/builder"
36-
legacyi18n "github.com/arduino/arduino-cli/legacy/builder/i18n"
3736
"github.com/arduino/arduino-cli/legacy/builder/types"
3837
"github.com/arduino/arduino-cli/metrics"
3938
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -167,12 +166,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
167166
builderCtx.USBVidPid = req.GetVidPid()
168167
builderCtx.WarningsLevel = req.GetWarnings()
169168

170-
if debug {
171-
builderCtx.DebugLevel = 100
172-
} else {
173-
builderCtx.DebugLevel = 5
174-
}
175-
176169
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), "build.warn_data_percentage=75")
177170

178171
if req.GetBuildCachePath() != "" {
@@ -205,9 +198,8 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
205198
builderCtx.BuiltInLibrariesDirs = paths.NewPathList(ideLibrariesPath)
206199
}
207200

208-
builderCtx.ExecStdout = outStream
209-
builderCtx.ExecStderr = errStream
210-
builderCtx.SetLogger(&legacyi18n.LoggerToCustomStreams{Stdout: outStream, Stderr: errStream})
201+
builderCtx.Stdout = outStream
202+
builderCtx.Stderr = errStream
211203
builderCtx.Clean = req.GetClean()
212204
builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly()
213205

Diff for: docs/UPGRADING.md

+23
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ The `parseArch` parameter was removed since it was unused and was always true. T
3232
always parsed by the function. Furthermore the function now should also correctly interpret `packager:arch` spelled with
3333
the wrong casing.
3434
35+
### `github.com/arduino/arduino-cli/i18n.Init(...)` now requires an empty string to be passed for autodetection of locale
36+
37+
For automated detection of locale, change the call from:
38+
39+
```go
40+
i18n.Init()
41+
```
42+
43+
to
44+
45+
```go
46+
i18n.Init("")
47+
```
48+
49+
### `github.com/arduino/arduino-cli/legacy/i18n` module has been removed (in particular the `i18n.Logger`)
50+
51+
The `i18n.Logger` is no longer available. It was mainly used in the legacy builder struct field `Context.Logger`.
52+
53+
The `Context.Logger` field has been replaced with plain `io.Writer` fields `Contex.Stdout` and `Context.Stderr`. All
54+
existing logger functionality has been dropped, for example the Java-Style formatting with tags like `{0} {1}...` must
55+
be replaced with one of the equivalent golang printf-based alternatives and logging levels must be replaced with direct
56+
writes to `Stdout` or `Stderr`.
57+
3558
## 0.20.0
3659
3760
### `board details` arguments change

Diff for: i18n/convert.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package i18n
17+
18+
import (
19+
"regexp"
20+
"strconv"
21+
"strings"
22+
)
23+
24+
var javaFormatPlaceholderRegexp = regexp.MustCompile(`{(\d)}`)
25+
26+
// FromJavaToGoSyntax convert a translation string made for Java to a one suitable for golang (printf-style).
27+
// The conversion transforms java placeholders like "{0}","{1}","{2}",etc... with the equivalent for golang
28+
// "%[1]v","%[2]v","%[3]v",etc...
29+
// The double single-quote "''" is translated into a single single-quote "'".
30+
func FromJavaToGoSyntax(s string) string {
31+
// Replace "{x}" => "%[x+1]v"
32+
for _, submatch := range javaFormatPlaceholderRegexp.FindAllStringSubmatch(s, -1) {
33+
idx, err := strconv.Atoi(submatch[1])
34+
if err != nil {
35+
panic(err)
36+
}
37+
s = strings.Replace(s, submatch[0], "%["+strconv.Itoa(idx+1)+"]v", -1)
38+
}
39+
40+
// Replace "''" => "'"
41+
s = strings.Replace(s, "''", "'", -1)
42+
43+
return s
44+
}

Diff for: i18n/convert_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package i18n
17+
18+
import (
19+
"fmt"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func format(format string, a ...interface{}) string {
26+
format = FromJavaToGoSyntax(format)
27+
message := fmt.Sprintf(format, a...)
28+
return message
29+
}
30+
31+
func TestI18NSyntax(t *testing.T) {
32+
require.Equal(t, "Do you want to remove %[1]v?\nIf you do so you won't be able to use %[1]v any more.", FromJavaToGoSyntax("Do you want to remove {0}?\nIf you do so you won't be able to use {0} any more."))
33+
require.Equal(t, "A file named \"%[1]v\" already exists in \"%[2]v\"", FromJavaToGoSyntax("A file named \"{0}\" already exists in \"{1}\""))
34+
require.Equal(t, "Board %[1]v:%[2]v:%[3]v doesn't define a 'build.board' preference. Auto-set to: %[4]v", FromJavaToGoSyntax("Board {0}:{1}:{2} doesn''t define a ''build.board'' preference. Auto-set to: {3}"))
35+
36+
require.Equal(t, "22 11\n", fmt.Sprintf("%[2]d %[1]d\n", 11, 22))
37+
require.Equal(t, "d c b a", format("{3} {2} {1} {0}", "a", "b", "c", "d"))
38+
39+
require.Equal(t, "e d b a", format("{4} {3} {1} {0}", "a", "b", "c", "d", "e"))
40+
41+
require.Equal(t, "a b", format("{0} {1}", "a", "b", "c", "d", "e"))
42+
43+
require.Equal(t, "%!v(BADINDEX) c b a", format("{3} {2} {1} {0}", "a", "b", "c"))
44+
require.Equal(t, "%!v(BADINDEX) %!v(BADINDEX) %!v(BADINDEX) %!v(BADINDEX)", format("{3} {2} {1} {0}"))
45+
46+
require.Equal(t, "I'm %[1]v%% sure", FromJavaToGoSyntax("I'm {0}%% sure"))
47+
require.Equal(t, "I'm 100% sure", format("I'm {0}%% sure", 100))
48+
49+
require.Equal(t, "Either in [1] or in [2]", format("Either in [{0}] or in [{1}]", 1, 2))
50+
51+
require.Equal(t, "Using library a at version b in folder: c ", format("Using library {0} at version {1} in folder: {2} {3}", "a", "b", "c", ""))
52+
53+
require.Equal(t, "Missing 'a' from library in b", format("Missing '{0}' from library in {1}", "a", "b"))
54+
}

Diff for: i18n/i18n.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ package i18n
1919
// 1. Locale specified via the function call
2020
// 2. OS Locale
2121
// 3. en (default)
22-
func Init(configLocale ...string) {
22+
func Init(configLocale string) {
2323
locales := supportedLocales()
24-
if len(configLocale) > 1 {
25-
panic("Multiple arguments not supported")
26-
}
27-
28-
if len(configLocale) > 0 && configLocale[0] != "" {
29-
if locale := findMatchingLocale(configLocale[0], locales); locale != "" {
24+
if configLocale != "" {
25+
if locale := findMatchingLocale(configLocale, locales); locale != "" {
3026
setLocale(locale)
3127
return
3228
}

Diff for: legacy/builder/add_build_board_property_if_missing.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package builder
1717

1818
import (
19-
"os"
2019
"strings"
2120

2221
"github.com/arduino/arduino-cli/legacy/builder/constants"
@@ -27,22 +26,17 @@ type AddBuildBoardPropertyIfMissing struct{}
2726

2827
func (*AddBuildBoardPropertyIfMissing) Run(ctx *types.Context) error {
2928
packages := ctx.Hardware
30-
logger := ctx.GetLogger()
3129

3230
for _, aPackage := range packages {
3331
for _, platform := range aPackage.Platforms {
3432
for _, platformRelease := range platform.Releases {
3533
for _, board := range platformRelease.Boards {
3634
if board.Properties.Get("build.board") == "" {
3735
board.Properties.Set("build.board", strings.ToUpper(platform.Architecture+"_"+board.BoardID))
38-
logger.Fprintln(
39-
os.Stdout,
40-
constants.LOG_LEVEL_WARN,
41-
tr("Warning: Board {0}:{1}:{2} doesn''t define a %s preference. Auto-set to: {3}", "''build.board''"),
42-
aPackage.Name,
43-
platform.Architecture,
44-
board.BoardID,
45-
board.Properties.Get(constants.BUILD_PROPERTIES_BUILD_BOARD))
36+
ctx.Info(tr("Warning: Board %[1]s doesn't define a %[2]s preference. Auto-set to: %[3]s",
37+
aPackage.Name+":"+platform.Architecture+":"+board.BoardID,
38+
"'build.board'",
39+
board.Properties.Get(constants.BUILD_PROPERTIES_BUILD_BOARD)))
4640
}
4741
}
4842
}

Diff for: legacy/builder/builder.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package builder
1717

1818
import (
19-
"os"
2019
"reflect"
21-
"strconv"
2220
"time"
2321

2422
"github.com/arduino/arduino-cli/arduino/sketch"
@@ -27,6 +25,7 @@ import (
2725
"github.com/arduino/arduino-cli/legacy/builder/types"
2826
"github.com/arduino/arduino-cli/legacy/builder/utils"
2927
"github.com/pkg/errors"
28+
"github.com/sirupsen/logrus"
3029
)
3130

3231
var tr = i18n.Tr
@@ -57,31 +56,31 @@ func (s *Builder) Run(ctx *types.Context) error {
5756

5857
&ContainerMergeCopySketchFiles{},
5958

60-
utils.LogIfVerbose("info", tr("Detecting libraries used...")),
59+
utils.LogIfVerbose(false, tr("Detecting libraries used...")),
6160
&ContainerFindIncludes{},
6261

6362
&WarnAboutArchIncompatibleLibraries{},
6463

65-
utils.LogIfVerbose("info", tr("Generating function prototypes...")),
64+
utils.LogIfVerbose(false, tr("Generating function prototypes...")),
6665
&PreprocessSketch{},
6766

68-
utils.LogIfVerbose("info", tr("Compiling sketch...")),
67+
utils.LogIfVerbose(false, tr("Compiling sketch...")),
6968
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.sketch.prebuild", Suffix: ".pattern"},
7069
&phases.SketchBuilder{},
7170
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.sketch.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
7271

73-
utils.LogIfVerbose("info", tr("Compiling libraries...")),
72+
utils.LogIfVerbose(false, tr("Compiling libraries...")),
7473
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.libraries.prebuild", Suffix: ".pattern"},
7574
&UnusedCompiledLibrariesRemover{},
7675
&phases.LibrariesBuilder{},
7776
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.libraries.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
7877

79-
utils.LogIfVerbose("info", tr("Compiling core...")),
78+
utils.LogIfVerbose(false, tr("Compiling core...")),
8079
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.prebuild", Suffix: ".pattern"},
8180
&phases.CoreBuilder{},
8281
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
8382

84-
utils.LogIfVerbose("info", tr("Linking everything together...")),
83+
utils.LogIfVerbose(false, tr("Linking everything together...")),
8584
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.linking.prelink", Suffix: ".pattern"},
8685
&phases.Linker{},
8786
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.linking.postlink", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
@@ -163,7 +162,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
163162
}
164163

165164
// Output arduino-preprocessed source
166-
ctx.ExecStdout.Write([]byte(ctx.Source))
165+
ctx.Stdout.Write([]byte(ctx.Source))
167166
return nil
168167
}
169168

@@ -200,9 +199,7 @@ func runCommands(ctx *types.Context, commands []types.Command) error {
200199
}
201200

202201
func PrintRingNameIfDebug(ctx *types.Context, command types.Command) {
203-
if ctx.DebugLevel >= 10 {
204-
ctx.GetLogger().Fprintln(os.Stdout, "debug", "Ts: {0} - Running: {1}", strconv.FormatInt(time.Now().Unix(), 10), reflect.Indirect(reflect.ValueOf(command)).Type().Name())
205-
}
202+
logrus.Debugf("Ts: %d - Running: %s", time.Now().Unix(), reflect.Indirect(reflect.ValueOf(command)).Type().Name())
206203
}
207204

208205
func RunBuilder(ctx *types.Context) error {

0 commit comments

Comments
 (0)