1// Copyright 2022 The Bazel Authors. 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 15package extractaar 16 17import ( 18 "fmt" 19 "path/filepath" 20 "strings" 21) 22 23func boolToString(b bool) string { 24 return strings.Title(fmt.Sprintf("%t", b)) 25} 26 27type validator interface { 28 validate(files []*aarFile) ([]*toCopy, *BuildozerError) 29} 30 31type manifestValidator struct { 32 dest string 33} 34 35func (v manifestValidator) validate(files []*aarFile) ([]*toCopy, *BuildozerError) { 36 var filesToCopy []*toCopy 37 seen := false 38 for _, file := range files { 39 if seen { 40 return nil, &BuildozerError{Msg: "More than one manifest was found"} 41 } 42 seen = true 43 filesToCopy = append(filesToCopy, &toCopy{src: file.path, dest: v.dest}) 44 } 45 if !seen { 46 return nil, &BuildozerError{Msg: "No manifest was found"} 47 } 48 return filesToCopy, nil 49} 50 51type resourceValidator struct { 52 dest string 53 ruleAttr string 54 hasRes tristate 55} 56 57func (v resourceValidator) validate(files []*aarFile) ([]*toCopy, *BuildozerError) { 58 var filesToCopy []*toCopy 59 seen := false 60 for _, file := range files { 61 seen = true 62 filesToCopy = append(filesToCopy, 63 &toCopy{src: file.path, dest: filepath.Join(v.dest, file.relPath)}, 64 ) 65 } 66 if v.hasRes.isSet() { 67 if seen != v.hasRes.value() { 68 var not string 69 if !seen { 70 not = "not " 71 } 72 msg := fmt.Sprintf("%s attribute is %s, but files were %sfound", v.ruleAttr, boolToString(v.hasRes.value()), not) 73 return nil, &BuildozerError{Msg: msg, RuleAttr: v.ruleAttr, NewValue: boolToString(seen)} 74 } 75 } 76 return filesToCopy, nil 77} 78