Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 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 jar |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame^] | 22 | const ( |
| 23 | MetaDir = "META-INF/" |
| 24 | ManifestFile = MetaDir + "MANIFEST.MF" |
| 25 | ModuleInfoClass = "module-info.class" |
| 26 | ) |
| 27 | |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 28 | // EntryNamesLess tells whether <filepathA> should precede <filepathB> in |
| 29 | // the order of files with a .jar |
| 30 | func EntryNamesLess(filepathA string, filepathB string) (less bool) { |
| 31 | diff := index(filepathA) - index(filepathB) |
| 32 | if diff == 0 { |
| 33 | return filepathA < filepathB |
| 34 | } |
| 35 | return diff < 0 |
| 36 | } |
| 37 | |
| 38 | // Treats trailing * as a prefix match |
| 39 | func patternMatch(pattern, name string) bool { |
| 40 | if strings.HasSuffix(pattern, "*") { |
| 41 | return strings.HasPrefix(name, strings.TrimSuffix(pattern, "*")) |
| 42 | } else { |
| 43 | return name == pattern |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | var jarOrder = []string{ |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame^] | 48 | MetaDir, |
| 49 | ManifestFile, |
| 50 | MetaDir + "*", |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 51 | "*", |
| 52 | } |
| 53 | |
| 54 | func index(name string) int { |
| 55 | for i, pattern := range jarOrder { |
| 56 | if patternMatch(pattern, name) { |
| 57 | return i |
| 58 | } |
| 59 | } |
| 60 | panic(fmt.Errorf("file %q did not match any pattern", name)) |
| 61 | } |