|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package result |
| 17 | + |
| 18 | +import ( |
| 19 | + "slices" |
| 20 | + |
| 21 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 22 | + "github.com/iancoleman/orderedmap" |
| 23 | + semver "go.bug.st/relaxed-semver" |
| 24 | +) |
| 25 | + |
| 26 | +// NewPlatformResult creates a new result.Platform from rpc.PlatformSummary |
| 27 | +func NewPlatformResult(in *rpc.PlatformSummary) *Platform { |
| 28 | + meta := in.Metadata |
| 29 | + res := &Platform{ |
| 30 | + Id: meta.Id, |
| 31 | + Maintainer: meta.Maintainer, |
| 32 | + Website: meta.Website, |
| 33 | + Email: meta.Email, |
| 34 | + ManuallyInstalled: meta.ManuallyInstalled, |
| 35 | + Deprecated: meta.Deprecated, |
| 36 | + Indexed: meta.Indexed, |
| 37 | + |
| 38 | + Releases: orderedmap.New(), |
| 39 | + InstalledVersion: in.InstalledVersion, |
| 40 | + LatestVersion: in.LatestVersion, |
| 41 | + } |
| 42 | + |
| 43 | + for k, v := range in.Releases { |
| 44 | + res.Releases.Set(k, NewPlatformReleaseResult(v)) |
| 45 | + } |
| 46 | + res.Releases.SortKeys(func(keys []string) { |
| 47 | + slices.SortFunc(keys, func(x, y string) int { |
| 48 | + return semver.ParseRelaxed(x).CompareTo(semver.ParseRelaxed(y)) |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + versions := []*semver.RelaxedVersion{} |
| 53 | + for version := range in.Releases { |
| 54 | + versions = append(versions, semver.ParseRelaxed(version)) |
| 55 | + } |
| 56 | + slices.SortFunc(versions, (*semver.RelaxedVersion).CompareTo) |
| 57 | + for _, version := range versions { |
| 58 | + res.Releases.Set(version.String(), NewPlatformReleaseResult(in.Releases[version.String()])) |
| 59 | + } |
| 60 | + return res |
| 61 | +} |
| 62 | + |
| 63 | +// Platform maps a rpc.Platform |
| 64 | +type Platform struct { |
| 65 | + Id string `json:"id,omitempty"` |
| 66 | + Maintainer string `json:"maintainer,omitempty"` |
| 67 | + Website string `json:"website,omitempty"` |
| 68 | + Email string `json:"email,omitempty"` |
| 69 | + ManuallyInstalled bool `json:"manually_installed,omitempty"` |
| 70 | + Deprecated bool `json:"deprecated,omitempty"` |
| 71 | + Indexed bool `json:"indexed,omitempty"` |
| 72 | + |
| 73 | + Releases *orderedmap.OrderedMap `json:"releases,omitempty"` |
| 74 | + |
| 75 | + InstalledVersion string `json:"installed_version,omitempty"` |
| 76 | + LatestVersion string `json:"latest_version,omitempty"` |
| 77 | +} |
| 78 | + |
| 79 | +// NewPlatformReleaseResult creates a new result.PlatformRelease from rpc.PlatformRelease |
| 80 | +func NewPlatformReleaseResult(in *rpc.PlatformRelease) *PlatformRelease { |
| 81 | + var boards []*Board |
| 82 | + for _, board := range in.Boards { |
| 83 | + boards = append(boards, &Board{ |
| 84 | + Name: board.Name, |
| 85 | + Fqbn: board.Fqbn, |
| 86 | + }) |
| 87 | + } |
| 88 | + var help *HelpResource |
| 89 | + if in.Help != nil { |
| 90 | + help = &HelpResource{ |
| 91 | + Online: in.Help.Online, |
| 92 | + } |
| 93 | + } |
| 94 | + res := &PlatformRelease{ |
| 95 | + Name: in.Name, |
| 96 | + Version: in.Version, |
| 97 | + Type: in.Type, |
| 98 | + Installed: in.Installed, |
| 99 | + Boards: boards, |
| 100 | + Help: help, |
| 101 | + MissingMetadata: in.MissingMetadata, |
| 102 | + Deprecated: in.Deprecated, |
| 103 | + } |
| 104 | + return res |
| 105 | +} |
| 106 | + |
| 107 | +// PlatformRelease maps a rpc.PlatformRelease |
| 108 | +type PlatformRelease struct { |
| 109 | + Name string `json:"name,omitempty"` |
| 110 | + Version string `json:"version,omitempty"` |
| 111 | + Type []string `json:"type,omitempty"` |
| 112 | + Installed bool `json:"installed,omitempty"` |
| 113 | + Boards []*Board `json:"boards,omitempty"` |
| 114 | + Help *HelpResource `json:"help,omitempty"` |
| 115 | + MissingMetadata bool `json:"missing_metadata,omitempty"` |
| 116 | + Deprecated bool `json:"deprecated,omitempty"` |
| 117 | +} |
| 118 | + |
| 119 | +// Board maps a rpc.Board |
| 120 | +type Board struct { |
| 121 | + Name string `json:"name,omitempty"` |
| 122 | + Fqbn string `json:"fqbn,omitempty"` |
| 123 | +} |
| 124 | + |
| 125 | +// HelpResource maps a rpc.HelpResource |
| 126 | +type HelpResource struct { |
| 127 | + Online string `json:"online,omitempty"` |
| 128 | +} |
0 commit comments