Paul Duffin | 1812294 | 2021-08-24 19:01:25 +0100 | [diff] [blame^] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
| 22 | // Supports customizing sdk snapshot output based on target build release. |
| 23 | |
| 24 | // buildRelease represents the version of a build system used to create a specific release. |
| 25 | // |
| 26 | // The name of the release, is the same as the code for the dessert release, e.g. S, T, etc. |
| 27 | type buildRelease struct { |
| 28 | // The name of the release, e.g. S, T, etc. |
| 29 | name string |
| 30 | |
| 31 | // The index of this structure within the buildReleases list. |
| 32 | ordinal int |
| 33 | } |
| 34 | |
| 35 | // String returns the name of the build release. |
| 36 | func (s *buildRelease) String() string { |
| 37 | return s.name |
| 38 | } |
| 39 | |
| 40 | // buildReleaseSet represents a set of buildRelease objects. |
| 41 | type buildReleaseSet struct { |
| 42 | // Set of *buildRelease represented as a map from *buildRelease to struct{}. |
| 43 | contents map[*buildRelease]struct{} |
| 44 | } |
| 45 | |
| 46 | // addItem adds a build release to the set. |
| 47 | func (s *buildReleaseSet) addItem(release *buildRelease) { |
| 48 | s.contents[release] = struct{}{} |
| 49 | } |
| 50 | |
| 51 | // addRange adds all the build releases from start (inclusive) to end (inclusive). |
| 52 | func (s *buildReleaseSet) addRange(start *buildRelease, end *buildRelease) { |
| 53 | for i := start.ordinal; i <= end.ordinal; i += 1 { |
| 54 | s.addItem(buildReleases[i]) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // contains returns true if the set contains the specified build release. |
| 59 | func (s *buildReleaseSet) contains(release *buildRelease) bool { |
| 60 | _, ok := s.contents[release] |
| 61 | return ok |
| 62 | } |
| 63 | |
| 64 | // String returns a string representation of the set, sorted from earliest to latest release. |
| 65 | func (s *buildReleaseSet) String() string { |
| 66 | list := []string{} |
| 67 | for _, release := range buildReleases { |
| 68 | if _, ok := s.contents[release]; ok { |
| 69 | list = append(list, release.name) |
| 70 | } |
| 71 | } |
| 72 | return fmt.Sprintf("[%s]", strings.Join(list, ",")) |
| 73 | } |
| 74 | |
| 75 | var ( |
| 76 | // nameToBuildRelease contains a map from name to build release. |
| 77 | nameToBuildRelease = map[string]*buildRelease{} |
| 78 | |
| 79 | // buildReleases lists all the available build releases. |
| 80 | buildReleases = []*buildRelease{} |
| 81 | |
| 82 | // allBuildReleaseSet is the set of all build releases. |
| 83 | allBuildReleaseSet = &buildReleaseSet{contents: map[*buildRelease]struct{}{}} |
| 84 | |
| 85 | // Add the build releases from oldest to newest. |
| 86 | buildReleaseS = initBuildRelease("S") |
| 87 | buildReleaseT = initBuildRelease("T") |
| 88 | ) |
| 89 | |
| 90 | // initBuildRelease creates a new build release with the specified name. |
| 91 | func initBuildRelease(name string) *buildRelease { |
| 92 | ordinal := len(nameToBuildRelease) |
| 93 | release := &buildRelease{name: name, ordinal: ordinal} |
| 94 | nameToBuildRelease[name] = release |
| 95 | buildReleases = append(buildReleases, release) |
| 96 | allBuildReleaseSet.addItem(release) |
| 97 | return release |
| 98 | } |
| 99 | |
| 100 | // latestBuildRelease returns the latest build release, i.e. the last one added. |
| 101 | func latestBuildRelease() *buildRelease { |
| 102 | return buildReleases[len(buildReleases)-1] |
| 103 | } |
| 104 | |
| 105 | // nameToRelease maps from build release name to the corresponding build release (if it exists) or |
| 106 | // the error if it does not. |
| 107 | func nameToRelease(name string) (*buildRelease, error) { |
| 108 | if r, ok := nameToBuildRelease[name]; ok { |
| 109 | return r, nil |
| 110 | } |
| 111 | |
| 112 | return nil, fmt.Errorf("unknown release %q, expected one of %s", name, allBuildReleaseSet) |
| 113 | } |
| 114 | |
| 115 | // parseBuildReleaseSet parses a build release set string specification into a build release set. |
| 116 | // |
| 117 | // The specification consists of one of the following: |
| 118 | // * a single build release name, e.g. S, T, etc. |
| 119 | // * a closed range (inclusive to inclusive), e.g. S-T |
| 120 | // * an open range, e.g. T+. |
| 121 | // |
| 122 | // This returns the set if the specification was valid or an error. |
| 123 | func parseBuildReleaseSet(specification string) (*buildReleaseSet, error) { |
| 124 | set := &buildReleaseSet{contents: map[*buildRelease]struct{}{}} |
| 125 | |
| 126 | if strings.HasSuffix(specification, "+") { |
| 127 | rangeStart := strings.TrimSuffix(specification, "+") |
| 128 | start, err := nameToRelease(rangeStart) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | end := latestBuildRelease() |
| 133 | set.addRange(start, end) |
| 134 | } else if strings.Contains(specification, "-") { |
| 135 | limits := strings.SplitN(specification, "-", 2) |
| 136 | start, err := nameToRelease(limits[0]) |
| 137 | if err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | |
| 141 | end, err := nameToRelease(limits[1]) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | if start.ordinal > end.ordinal { |
| 147 | return nil, fmt.Errorf("invalid closed range, start release %q is later than end release %q", start.name, end.name) |
| 148 | } |
| 149 | |
| 150 | set.addRange(start, end) |
| 151 | } else { |
| 152 | release, err := nameToRelease(specification) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | set.addItem(release) |
| 157 | } |
| 158 | |
| 159 | return set, nil |
| 160 | } |