Skip to content

Commit

Permalink
[breaking] gRPC Compile request now returns expanded build properties…
Browse files Browse the repository at this point in the history
… by default (#2184)
  • Loading branch information
cmaglie committed May 26, 2023
1 parent f328ecd commit ca79383
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 296 deletions.
16 changes: 16 additions & 0 deletions arduino/utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package utils
import (
"net/url"
"runtime"

"github.com/arduino/go-properties-orderedmap"
)

// URLParse parses a raw URL string and handles local files URLs depending on the platform
Expand All @@ -33,3 +35,17 @@ func URLParse(rawURL string) (*url.URL, error) {
}
return URL, nil
}

// ExpandBuildProperties expands the build properties placeholders in the slice of properties.
func ExpandBuildProperties(props []string) ([]string, error) {
expanded, err := properties.LoadFromSlice(props)
if err != nil {
return nil, err
}
expandedProps := []string{}
for _, k := range expanded.Keys() {
v := expanded.Get(k)
expandedProps = append(expandedProps, k+"="+expanded.ExpandPropsInString(v))
}
return expandedProps, nil
}
4 changes: 4 additions & 0 deletions commands/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
)
Expand Down Expand Up @@ -59,6 +60,9 @@ func Details(ctx context.Context, req *rpc.BoardDetailsRequest) (*rpc.BoardDetai
v := boardProperties.Get(k)
details.BuildProperties = append(details.BuildProperties, k+"="+v)
}
if !req.GetDoNotExpandBuildProperties() {
details.BuildProperties, _ = utils.ExpandBuildProperties(details.BuildProperties)
}

details.DebuggingSupported = boardProperties.ContainsKey("debug.executable") ||
boardPlatform.Properties.ContainsKey("debug.executable") ||
Expand Down
4 changes: 4 additions & 0 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
bldr "github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/buildcache"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/configuration"
Expand Down Expand Up @@ -233,6 +234,9 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
for _, key := range keys {
r.BuildProperties = append(r.BuildProperties, key+"="+buildProperties.Get(key))
}
if !req.GetDoNotExpandBuildProperties() {
r.BuildProperties, _ = utils.ExpandBuildProperties(r.BuildProperties)
}
}()

if req.GetShowProperties() {
Expand Down
30 changes: 30 additions & 0 deletions docs/UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

Here you can find a list of migration guides to handle breaking changes between releases of the CLI.

## 0.33.0

### gRPC `cc.arduino.cli.commands.v1.Compile` command now return expanded build_properties by default.

The gRPC `cc.arduino.cli.commands.v1.Compile` command now return expanded `build_properties` by default. If you want the
**un**expanded `build_properties` you must set to `true` the field `do_not_expand_build_properties` in the
`CompileRequest`.

### `compile --show-properties` now return the expanded build properties.

The command `compile --show-properties` now returns the **expanded** build properties, with the variable placeholders
replaced with their current value. If you need the **un**expanded build properties you must change the command line to
`compile --show-properties=unexpanded`.

Before:

```
$ arduino-cli board details -b arduino:avr:uno --show-properties | grep ^tools.avrdude.path
tools.avrdude.path={runtime.tools.avrdude.path}
```

Now:

```
$ arduino-cli board details -b arduino:avr:uno --show-properties | grep ^tools.avrdude.path
tools.avrdude.path=/home/megabug/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17
$ arduino-cli board details -b arduino:avr:uno --show-properties=unexpanded | grep ^tools.avrdude.path
tools.avrdude.path={runtime.tools.avrdude.path}
```

## 0.32.2

### golang API: method `github.com/arduino/arduino-cli/arduino/cores/Board.GetBuildProperties` changed signature
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/arguments/show_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *ShowProperties) Get() (ShowPropertiesMode, error) {
func (p *ShowProperties) AddToCommand(command *cobra.Command) {
command.Flags().StringVar(&p.arg,
"show-properties", "disabled",
tr(`Show build properties. The properties are returned exactly as they are defined. Use "--show-properties=expanded" to replace placeholders with context values.`),
tr(`Show build properties. The properties are expanded, use "--show-properties=unexpanded" if you want them exactly as they are defined.`),
)
command.Flags().Lookup("show-properties").NoOptDefVal = "unexpanded" // default if the flag is present with no value
command.Flags().Lookup("show-properties").NoOptDefVal = "expanded" // default if the flag is present with no value
}
10 changes: 3 additions & 7 deletions internal/cli/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/compile"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/cli/instance"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand Down Expand Up @@ -66,17 +65,14 @@ func runDetailsCommand(fqbn string, showFullDetails, listProgrammers bool, showP
feedback.Fatal(err.Error(), feedback.ErrBadArgument)
}
res, err := board.Details(context.Background(), &rpc.BoardDetailsRequest{
Instance: inst,
Fqbn: fqbn,
Instance: inst,
Fqbn: fqbn,
DoNotExpandBuildProperties: showPropertiesMode == arguments.ShowPropertiesUnexpanded,
})
if err != nil {
feedback.Fatal(tr("Error getting board details: %v", err), feedback.ErrGeneric)
}

if showPropertiesMode == arguments.ShowPropertiesExpanded {
res.BuildProperties, _ = compile.ExpandBuildProperties(res.GetBuildProperties())
}

feedback.PrintResult(detailsResult{
details: res,
listProgrammers: listProgrammers,
Expand Down
20 changes: 1 addition & 19 deletions internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"github.com/arduino/arduino-cli/table"
"github.com/arduino/arduino-cli/version"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -231,6 +230,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
SignKey: signKey,
EncryptKey: encryptKey,
SkipLibrariesDiscovery: skipLibrariesDiscovery,
DoNotExpandBuildProperties: showProperties == arguments.ShowPropertiesUnexpanded,
}
compileRes, compileError := compile.Compile(context.Background(), compileRequest, stdOut, stdErr, nil)
if compileError == nil && uploadAfterCompile {
Expand Down Expand Up @@ -365,27 +365,9 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
}
feedback.FatalResult(res, feedback.ErrGeneric)
}
if showProperties == arguments.ShowPropertiesExpanded {
res.BuilderResult.BuildProperties, _ = ExpandBuildProperties(res.BuilderResult.GetBuildProperties())
// ignore error, it should never fail
}
feedback.PrintResult(res)
}

// ExpandBuildProperties expands the build properties placeholders in the slice of properties.
func ExpandBuildProperties(props []string) ([]string, error) {
expanded, err := properties.LoadFromSlice(props)
if err != nil {
return nil, err
}
expandedProps := []string{}
for _, k := range expanded.Keys() {
v := expanded.Get(k)
expandedProps = append(expandedProps, k+"="+expanded.ExpandPropsInString(v))
}
return expandedProps, nil
}

type compileResult struct {
CompilerOut string `json:"compiler_out"`
CompilerErr string `json:"compiler_err"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ func TestCompileShowProperties(t *testing.T) {
bareMinimum := cli.CopySketch("bare_minimum")

// Test --show-properties output is clean
// properties are not expanded
// properties are expanded
stdout, stderr, err := cli.Run("compile", "--fqbn", "arduino:avr:uno", "-v", "--show-properties", bareMinimum.String())
require.NoError(t, err)
props, err := properties.LoadFromBytes(stdout)
require.NoError(t, err, "Output must be a clean property list")
require.Empty(t, stderr)
require.True(t, props.ContainsKey("archive_file_path"))
require.Contains(t, props.Get("archive_file_path"), "{build.path}")
require.NotContains(t, props.Get("archive_file_path"), "{build.path}")

// Test --show-properties --format JSON output is clean
// properties are not expanded
// properties are expanded
stdout, stderr, err = cli.Run("compile", "--fqbn", "arduino:avr:uno", "-v", "--show-properties", "--format", "json", bareMinimum.String())
require.NoError(t, err)
require.Empty(t, stderr)
props, err = properties.LoadFromSlice(
requireCompileResponseJson(t, stdout).BuilderResult.GetBuildProperties())
require.NoError(t, err)
require.True(t, props.ContainsKey("archive_file_path"))
require.Contains(t, props.Get("archive_file_path"), "{build.path}")
require.NotContains(t, props.Get("archive_file_path"), "{build.path}")

// Test --show-properties output is clean, with a wrong FQBN
stdout, stderr, err = cli.Run("compile", "--fqbn", "arduino:avr:unoa", "-v", "--show-properties", bareMinimum.String())
Expand Down
Loading

0 comments on commit ca79383

Please sign in to comment.