|
| 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 | +} |
0 commit comments