Skip to content

Commit 8b1c1eb

Browse files
facchinmcmaglie
authored andcommitted
Add initial package to handle json board index files
Works with folders, filenames and urls
1 parent 0d042f0 commit 8b1c1eb

File tree

3 files changed

+2597
-0
lines changed

3 files changed

+2597
-0
lines changed

json_package_index/package_index.go

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* This file is part of Arduino Builder.
3+
*
4+
* Copyright 2016 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino Builder is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package json_package_index
31+
32+
import (
33+
"encoding/json"
34+
"io/ioutil"
35+
"net/http"
36+
"os"
37+
"path/filepath"
38+
"strings"
39+
40+
"github.com/arduino/arduino-builder/constants"
41+
_ "github.com/arduino/arduino-builder/i18n"
42+
properties "github.com/arduino/go-properties-map"
43+
)
44+
45+
type core struct {
46+
Architecture string `json:"architecture"`
47+
Version string `json:"version"`
48+
URL string `json:"url"`
49+
Maintainer string `json:"maintainer"`
50+
Name string `json:"archiveFileName"`
51+
Checksum string `json:"checksum"`
52+
destination string
53+
Dependencies []struct {
54+
Packager string `json:"packager"`
55+
Name string `json:"name"`
56+
Version string `json:"version"`
57+
} `json:"toolsDependencies"`
58+
}
59+
60+
type tool struct {
61+
Name string `json:"name"`
62+
Version string `json:"version"`
63+
Systems []struct {
64+
Host string `json:"host"`
65+
URL string `json:"url"`
66+
Name string `json:"archiveFileName"`
67+
Checksum string `json:"checksum"`
68+
} `json:"systems"`
69+
url string
70+
destination string
71+
}
72+
73+
type index struct {
74+
Packages []struct {
75+
Name string `json:"name"`
76+
Maintainer string `json:"maintainer"`
77+
Platforms []core `json:"platforms"`
78+
Tools []tool `json:"tools"`
79+
} `json:"packages"`
80+
}
81+
82+
var systems = map[string]string{
83+
"linuxamd64": "x86_64-linux-gnu",
84+
"linux386": "i686-linux-gnu",
85+
"darwinamd64": "apple-darwin",
86+
"windows386": "i686-mingw32",
87+
}
88+
89+
// globalProperties is a big map of properties maps in the form
90+
// globalProperties["arduino:avr:1.6.12"] = usual properties Map
91+
// at compile time, when de board is well defined, the relevant map
92+
// should be merged with the "classic" map overriding its values
93+
94+
var globalProperties map[string]properties.Map
95+
96+
func PackageIndexFoldersToPropertiesMap(folders []string) (map[string]properties.Map, error) {
97+
98+
var paths []string
99+
for _, folder := range folders {
100+
folder, err := filepath.Abs(folder)
101+
if err != nil {
102+
break
103+
}
104+
files, _ := ioutil.ReadDir(folder)
105+
for _, f := range files {
106+
if strings.HasPrefix(f.Name(), "package") && strings.HasSuffix(f.Name(), "index.json") {
107+
paths = append(paths, filepath.Join(folder, f.Name()))
108+
}
109+
}
110+
}
111+
return PackageIndexesToPropertiesMap(paths)
112+
}
113+
114+
func PackageIndexesToPropertiesMap(urls []string) (map[string]properties.Map, error) {
115+
116+
globalProperties = make(map[string]properties.Map)
117+
118+
data, err := PackageIndexesToGlobalIndex(urls)
119+
120+
for _, p := range data.Packages {
121+
for _, a := range p.Platforms {
122+
localProperties := make(properties.Map)
123+
for _, dep := range a.Dependencies {
124+
localProperties[constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX+dep.Name+constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX] =
125+
"{" + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX + dep.Name + "-" + dep.Version + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX + "}"
126+
if dep.Packager != p.Name {
127+
localProperties[constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX+dep.Name+"-"+dep.Version+constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX] =
128+
"{" + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX + dep.Name + "-" + dep.Packager + "-" + dep.Version + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX + "}"
129+
}
130+
}
131+
globalProperties[p.Name+":"+a.Architecture+":"+a.Version] = localProperties.Clone()
132+
}
133+
}
134+
return globalProperties, err
135+
}
136+
137+
func PackageIndexesToGlobalIndex(urls []string) (index, error) {
138+
139+
// firststub of arduino-pdpm
140+
var data index
141+
var err error
142+
143+
for _, url := range urls {
144+
145+
var body []byte
146+
var localdata index
147+
localpath, _ := filepath.Abs(url)
148+
_, err := os.Stat(localpath)
149+
150+
if err != nil {
151+
resp, err := http.Get(url)
152+
if err == nil {
153+
defer resp.Body.Close()
154+
body, err = ioutil.ReadAll(resp.Body)
155+
if err != nil {
156+
break
157+
}
158+
}
159+
} else {
160+
body, err = ioutil.ReadFile(localpath)
161+
if err != nil {
162+
break
163+
}
164+
}
165+
json.Unmarshal(body, &localdata)
166+
for _, entry := range localdata.Packages {
167+
data.Packages = append(data.Packages, entry)
168+
}
169+
}
170+
return data, err
171+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package json_package_index
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestPropertiesPackageIndex(t *testing.T) {
11+
12+
var paths []string
13+
paths = append(paths, filepath.Join("testdata", "package_index.json"))
14+
15+
p, err := PackageIndexesToPropertiesMap(paths)
16+
17+
require.NoError(t, err)
18+
19+
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"])
20+
}
21+
22+
func TestPropertiesPackageIndexRemote(t *testing.T) {
23+
24+
var paths []string
25+
paths = append(paths, filepath.Join("testdata", "package_index.json"))
26+
paths = append(paths, "http://downloads.arduino.cc/packages/package_arduino.cc_linux_index.json")
27+
28+
p, err := PackageIndexesToPropertiesMap(paths)
29+
30+
require.NoError(t, err)
31+
32+
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"])
33+
require.Equal(t, "{runtime.tools.linuxuploader-1.2.path}", p["arduino:arm_cortexA:0.4.0"]["runtime.tools.linuxuploader.path"])
34+
}
35+
36+
func TestPackageIndexToGlobalIndex(t *testing.T) {
37+
38+
var paths []string
39+
paths = append(paths, filepath.Join("testdata", "package_index.json"))
40+
41+
p, err := PackageIndexesToGlobalIndex(paths)
42+
require.NoError(t, err)
43+
44+
require.Equal(t, "Arduino", p.Packages[0].Maintainer)
45+
}
46+
47+
func TestPackageIndexFoldersToPropertiesMap(t *testing.T) {
48+
var paths []string
49+
paths = append(paths, "testdata")
50+
51+
p, err := PackageIndexFoldersToPropertiesMap(paths)
52+
require.NoError(t, err)
53+
54+
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"])
55+
}

0 commit comments

Comments
 (0)