Skip to content

Commit 9ceccbc

Browse files
facchinmcmaglie
authored andcommitted
Make package_index library aware of installed cores
The coreDependency resolution will search for most recent installed core. The test has been moved to allow populating the context
1 parent 137b456 commit 9ceccbc

File tree

4 files changed

+83
-33
lines changed

4 files changed

+83
-33
lines changed

add_missing_build_properties_from_parent_platform_txt_files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (s *OverridePropertiesWithJsonInfo) Run(ctx *types.Context) error {
5757

5858
if ctx.JsonFolders != nil {
5959

60-
jsonProperties, err := json_package_index.PackageIndexFoldersToPropertiesMap(ctx.JsonFolders)
60+
jsonProperties, err := json_package_index.PackageIndexFoldersToPropertiesMap(ctx.Hardware, ctx.JsonFolders)
6161

6262
if err != nil {
6363
// doesn't matter, log the broken package in verbose mode

json_package_index/package_index.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ package json_package_index
3232
import (
3333
"encoding/json"
3434
"errors"
35-
"github.com/blang/semver"
3635
"io/ioutil"
3736
"net/http"
3837
"os"
3938
"path/filepath"
4039
"strings"
4140

41+
"github.com/blang/semver"
42+
4243
"github.com/arduino/arduino-builder/constants"
4344
_ "github.com/arduino/arduino-builder/i18n"
45+
"github.com/arduino/arduino-builder/types"
4446
properties "github.com/arduino/go-properties-map"
4547
)
4648

@@ -52,14 +54,14 @@ type core struct {
5254
Name string `json:"archiveFileName"`
5355
Checksum string `json:"checksum"`
5456
destination string
57+
installed bool
5558
Dependencies []struct {
5659
Packager string `json:"packager"`
5760
Name string `json:"name"`
5861
Version string `json:"version"`
5962
} `json:"toolsDependencies"`
6063
CoreDependencies []struct {
6164
Packager string `json:"packager"`
62-
Name string `json:"name"`
6365
} `json:"coreDependencies"`
6466
}
6567

@@ -99,9 +101,10 @@ var systems = map[string]string{
99101

100102
var globalProperties map[string]properties.Map
101103

102-
func PackageIndexFoldersToPropertiesMap(folders []string) (map[string]properties.Map, error) {
104+
func PackageIndexFoldersToPropertiesMap(packages *types.Packages, folders []string) (map[string]properties.Map, error) {
103105

104106
var paths []string
107+
105108
for _, folder := range folders {
106109
folder, err := filepath.Abs(folder)
107110
if err != nil {
@@ -114,15 +117,15 @@ func PackageIndexFoldersToPropertiesMap(folders []string) (map[string]properties
114117
}
115118
}
116119
}
117-
return PackageIndexesToPropertiesMap(paths)
120+
return PackageIndexesToPropertiesMap(packages, paths)
118121
}
119122

120-
func PackageIndexesToPropertiesMap(urls []string) (map[string]properties.Map, error) {
123+
func PackageIndexesToPropertiesMap(packages *types.Packages, urls []string) (map[string]properties.Map, error) {
121124

122125
globalProperties = make(map[string]properties.Map)
123126
coreDependencyMap := make(map[string]string)
124127

125-
data, err := PackageIndexesToGlobalIndex(urls)
128+
data, err := PackageIndexesToGlobalIndex(packages, urls)
126129

127130
for _, p := range data.Packages {
128131
for _, a := range p.Platforms {
@@ -137,32 +140,32 @@ func PackageIndexesToPropertiesMap(urls []string) (map[string]properties.Map, er
137140
}
138141
for _, coredep := range a.CoreDependencies {
139142
// inherit all the tools from latest coredep
140-
version, err := findLatestCore(data, coredep.Packager, coredep.Name)
141143
if err == nil {
142144
coreDependencyMap[p.Name+":"+a.Architecture+":"+a.Version] =
143-
coredep.Packager + ":" + coredep.Name + ":" + version
145+
coredep.Packager + ":" + a.Architecture
144146
}
145147
}
146148
globalProperties[p.Name+":"+a.Architecture+":"+a.Version] = localProperties.Clone()
147149
}
148150
}
149151

150152
for idx, parentCore := range coreDependencyMap {
151-
if (globalProperties[parentCore]) != nil {
152-
globalProperties[idx] = globalProperties[parentCore].Clone()
153+
version, err := findLatestInstalledCore(data, strings.Split(parentCore, ":")[0], strings.Split(parentCore, ":")[1])
154+
if err == nil {
155+
globalProperties[idx] = globalProperties[parentCore+":"+version].Clone()
153156
}
154157
}
155158

156159
return globalProperties, err
157160
}
158161

159-
func findLatestCore(data index, Packager string, Name string) (string, error) {
162+
func findLatestInstalledCore(data index, Packager string, Name string) (string, error) {
160163
latest, _ := semver.Make("0.0.0")
161164
for _, p := range data.Packages {
162165
for _, a := range p.Platforms {
163166
if p.Name == Packager && a.Architecture == Name {
164167
test, _ := semver.Make(a.Version)
165-
if test.GT(latest) {
168+
if test.GT(latest) && a.installed {
166169
latest = test
167170
}
168171
}
@@ -178,9 +181,9 @@ func findLatestCore(data index, Packager string, Name string) (string, error) {
178181
return latest.String(), err
179182
}
180183

181-
func PackageIndexesToGlobalIndex(urls []string) (index, error) {
184+
func PackageIndexesToGlobalIndex(packages *types.Packages, urls []string) (index, error) {
182185

183-
// firststub of arduino-pdpm
186+
// first stub of arduino-pdpm
184187
var data index
185188
var err error
186189

@@ -211,5 +214,29 @@ func PackageIndexesToGlobalIndex(urls []string) (index, error) {
211214
data.Packages = append(data.Packages, entry)
212215
}
213216
}
217+
218+
for i, p := range data.Packages {
219+
for j, a := range p.Platforms {
220+
if packages != nil && packages.Packages[p.Name] != nil &&
221+
packages.Packages[p.Name].Platforms[a.Architecture] != nil &&
222+
packages.Packages[p.Name].Platforms[a.Architecture].Properties["version"] == a.Version {
223+
data.Packages[i].Platforms[j].installed = true
224+
}
225+
}
226+
}
227+
214228
return data, err
215229
}
230+
231+
func CompareVersions(fv string, sv string) int {
232+
v1, _ := semver.Make(fv)
233+
v2, _ := semver.Make(sv)
234+
if v1.EQ(v2) {
235+
return 0
236+
}
237+
if v1.GT(v2) {
238+
return 1
239+
} else {
240+
return -1
241+
}
242+
}
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package json_package_index
22

33
import (
4+
"github.com/stretchr/testify/require"
45
"path/filepath"
56
"testing"
6-
7-
"github.com/stretchr/testify/require"
87
)
98

109
func TestPropertiesPackageIndex(t *testing.T) {
1110

1211
var paths []string
1312
paths = append(paths, filepath.Join("testdata", "package_index.json"))
1413

15-
p, err := PackageIndexesToPropertiesMap(paths)
14+
p, err := PackageIndexesToPropertiesMap(nil, paths)
1615

1716
require.NoError(t, err)
1817

@@ -25,7 +24,7 @@ func TestPropertiesPackageIndexRemote(t *testing.T) {
2524
paths = append(paths, filepath.Join("testdata", "package_index.json"))
2625
paths = append(paths, "http://downloads.arduino.cc/packages/package_arduino.cc_linux_index.json")
2726

28-
p, err := PackageIndexesToPropertiesMap(paths)
27+
p, err := PackageIndexesToPropertiesMap(nil, paths)
2928

3029
require.NoError(t, err)
3130

@@ -38,7 +37,7 @@ func TestPackageIndexToGlobalIndex(t *testing.T) {
3837
var paths []string
3938
paths = append(paths, filepath.Join("testdata", "package_index.json"))
4039

41-
p, err := PackageIndexesToGlobalIndex(paths)
40+
p, err := PackageIndexesToGlobalIndex(nil, paths)
4241
require.NoError(t, err)
4342

4443
require.Equal(t, "Arduino", p.Packages[0].Maintainer)
@@ -48,18 +47,8 @@ func TestPackageIndexFoldersToPropertiesMap(t *testing.T) {
4847
var paths []string
4948
paths = append(paths, "testdata")
5049

51-
p, err := PackageIndexFoldersToPropertiesMap(paths)
50+
p, err := PackageIndexFoldersToPropertiesMap(nil, paths)
5251
require.NoError(t, err)
5352

5453
require.Equal(t, "{runtime.tools.avr-gcc-4.9.2-atmel3.5.3-arduino2.path}", p["arduino:avr:1.6.12"]["runtime.tools.avr-gcc.path"])
5554
}
56-
57-
func TestCoreDependencyProperty(t *testing.T) {
58-
var paths []string
59-
paths = append(paths, "testdata")
60-
61-
p, err := PackageIndexFoldersToPropertiesMap(paths)
62-
require.NoError(t, err)
63-
64-
require.Equal(t, "{runtime.tools.avr-gcc-4.9.2-atmel3.5.3-arduino2.path}", p["attiny:avr:1.0.2"]["runtime.tools.avr-gcc.path"])
65-
}

test/add_build_board_property_if_missing_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
package test
3131

3232
import (
33+
"path/filepath"
34+
"testing"
35+
3336
"github.com/arduino/arduino-builder"
3437
"github.com/arduino/arduino-builder/constants"
38+
"github.com/arduino/arduino-builder/json_package_index"
3539
"github.com/arduino/arduino-builder/types"
3640
"github.com/stretchr/testify/require"
37-
"path/filepath"
38-
"testing"
3941
)
4042

4143
func TestAddBuildBoardPropertyIfMissing(t *testing.T) {
@@ -95,3 +97,35 @@ func TestAddBuildBoardPropertyIfMissingNotMissing(t *testing.T) {
9597
require.Equal(t, "atmega2560", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_MCU])
9698
require.Equal(t, "AVR_MEGA2560", targetBoard.Properties[constants.BUILD_PROPERTIES_BUILD_BOARD])
9799
}
100+
101+
func TestCoreDependencyProperty(t *testing.T) {
102+
var paths []string
103+
path, _ := filepath.Abs(filepath.Join("..", "..", "json_package_index", "testdata"))
104+
paths = append(paths, path)
105+
106+
DownloadCoresAndToolsAndLibraries(t)
107+
108+
ctx := &types.Context{
109+
HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"},
110+
}
111+
112+
commands := []types.Command{
113+
&builder.HardwareLoader{},
114+
}
115+
116+
for _, command := range commands {
117+
err := command.Run(ctx)
118+
NoError(t, err)
119+
}
120+
121+
p, err := json_package_index.PackageIndexFoldersToPropertiesMap(ctx.Hardware, paths)
122+
require.NoError(t, err)
123+
124+
version := ctx.Hardware.Packages["arduino"].Platforms["avr"].Properties["version"]
125+
126+
if json_package_index.CompareVersions(version, "1.6.12") >= 0 {
127+
require.Equal(t, "{runtime.tools.avr-gcc-4.9.2-atmel3.5.3-arduino2.path}", p["attiny:avr:1.0.2"]["runtime.tools.avr-gcc.path"])
128+
} else {
129+
require.Equal(t, "{runtime.tools.avr-gcc-4.8.1-arduino5.path}", p["attiny:avr:1.0.2"]["runtime.tools.avr-gcc.path"])
130+
}
131+
}

0 commit comments

Comments
 (0)