1// Copyright 2020 CUE Authors 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 fix 16 17import ( 18 "os" 19 20 "cuelang.org/go/cue/ast" 21 "cuelang.org/go/cue/build" 22 "cuelang.org/go/cue/errors" 23) 24 25// Instances modifies all files contained in the given build instances at once. 26// 27// It also applies fix.File. 28func Instances(a []*build.Instance, o ...Option) errors.Error { 29 cwd, _ := os.Getwd() 30 31 // Collect all 32 p := processor{ 33 instances: a, 34 cwd: cwd, 35 } 36 37 p.visitAll(func(f *ast.File) { File(f, o...) }) 38 39 return p.err 40} 41 42type processor struct { 43 instances []*build.Instance 44 cwd string 45 46 err errors.Error 47} 48 49func (p *processor) visitAll(fn func(f *ast.File)) { 50 if p.err != nil { 51 return 52 } 53 54 done := map[*ast.File]bool{} 55 56 for _, b := range p.instances { 57 for _, f := range b.Files { 58 if done[f] { 59 continue 60 } 61 done[f] = true 62 fn(f) 63 } 64 } 65} 66