Skip to content

Commit 659c758

Browse files
committed
Add initial package to handle json board index files
Works with folders, filenames and urls
1 parent bdf33ac commit 659c758

File tree

3 files changed

+2596
-0
lines changed

3 files changed

+2596
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
"arduino.cc/builder/constants"
34+
_ "arduino.cc/builder/i18n"
35+
"arduino.cc/properties"
36+
"encoding/json"
37+
"io/ioutil"
38+
"net/http"
39+
"os"
40+
"path/filepath"
41+
"strings"
42+
)
43+
44+
type core struct {
45+
Architecture string `json:"architecture"`
46+
Version string `json:"version"`
47+
URL string `json:"url"`
48+
Maintainer string `json:"maintainer"`
49+
Name string `json:"archiveFileName"`
50+
Checksum string `json:"checksum"`
51+
destination string
52+
Dependencies []struct {
53+
Packager string `json:"packager"`
54+
Name string `json:"name"`
55+
Version string `json:"version"`
56+
} `json:"toolsDependencies"`
57+
}
58+
59+
type tool struct {
60+
Name string `json:"name"`
61+
Version string `json:"version"`
62+
Systems []struct {
63+
Host string `json:"host"`
64+
URL string `json:"url"`
65+
Name string `json:"archiveFileName"`
66+
Checksum string `json:"checksum"`
67+
} `json:"systems"`
68+
url string
69+
destination string
70+
}
71+
72+
type index struct {
73+
Packages []struct {
74+
Name string `json:"name"`
75+
Maintainer string `json:"maintainer"`
76+
Platforms []core `json:"platforms"`
77+
Tools []tool `json:"tools"`
78+
} `json:"packages"`
79+
}
80+
81+
var systems = map[string]string{
82+
"linuxamd64": "x86_64-linux-gnu",
83+
"linux386": "i686-linux-gnu",
84+
"darwinamd64": "apple-darwin",
85+
"windows386": "i686-mingw32",
86+
}
87+
88+
// globalProperties is a big map of properties maps in the form
89+
// globalProperties["arduino:avr:1.6.12"] = usual properties Map
90+
// at compile time, when de board is well defined, the relevant map
91+
// should be merged with the "classic" map overriding its values
92+
93+
var globalProperties map[string]properties.Map
94+
95+
func PackageIndexFoldersToPropertiesMap(folders []string) (map[string]properties.Map, error) {
96+
97+
var paths []string
98+
for _, folder := range folders {
99+
folder, err := filepath.Abs(folder)
100+
if err != nil {
101+
break
102+
}
103+
files, _ := ioutil.ReadDir(folder)
104+
for _, f := range files {
105+
if strings.HasPrefix(f.Name(), "package") && strings.HasSuffix(f.Name(), "index.json") {
106+
paths = append(paths, filepath.Join(folder, f.Name()))
107+
}
108+
}
109+
}
110+
return PackageIndexesToPropertiesMap(paths)
111+
}
112+
113+
func PackageIndexesToPropertiesMap(urls []string) (map[string]properties.Map, error) {
114+
115+
globalProperties = make(map[string]properties.Map)
116+
117+
data, err := PackageIndexesToGlobalIndex(urls)
118+
119+
for _, p := range data.Packages {
120+
for _, a := range p.Platforms {
121+
localProperties := make(properties.Map)
122+
for _, dep := range a.Dependencies {
123+
localProperties[constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX+dep.Name+constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX] =
124+
"{" + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX + dep.Name + "-" + dep.Version + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX + "}"
125+
if dep.Packager != p.Name {
126+
localProperties[constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX+dep.Name+"-"+dep.Version+constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX] =
127+
"{" + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_PREFIX + dep.Name + "-" + dep.Packager + "-" + dep.Version + constants.BUILD_PROPERTIES_RUNTIME_TOOLS_SUFFIX + "}"
128+
}
129+
}
130+
globalProperties[p.Name+":"+a.Architecture+":"+a.Version] = localProperties.Clone()
131+
}
132+
}
133+
return globalProperties, err
134+
}
135+
136+
func PackageIndexesToGlobalIndex(urls []string) (index, error) {
137+
138+
// firststub of arduino-pdpm
139+
var data index
140+
var err error
141+
142+
for _, url := range urls {
143+
144+
var body []byte
145+
var localdata index
146+
localpath, _ := filepath.Abs(url)
147+
_, err := os.Stat(localpath)
148+
149+
if err != nil {
150+
resp, err := http.Get(url)
151+
if err == nil {
152+
defer resp.Body.Close()
153+
body, err = ioutil.ReadAll(resp.Body)
154+
if err != nil {
155+
break
156+
}
157+
}
158+
} else {
159+
body, err = ioutil.ReadFile(localpath)
160+
if err != nil {
161+
break
162+
}
163+
}
164+
json.Unmarshal(body, &localdata)
165+
for _, entry := range localdata.Packages {
166+
data.Packages = append(data.Packages, entry)
167+
}
168+
}
169+
return data, err
170+
}
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)