1// Copyright 2021 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 main 16 17import ( 18 "fmt" 19 "go/types" 20) 21 22type JSONPackagesDriver struct { 23 registry *PackageRegistry 24} 25 26func NewJSONPackagesDriver(jsonFiles []string, prf PathResolverFunc) (*JSONPackagesDriver, error) { 27 jpd := &JSONPackagesDriver{ 28 registry: NewPackageRegistry(), 29 } 30 31 for _, f := range jsonFiles { 32 if err := WalkFlatPackagesFromJSON(f, func(pkg *FlatPackage) { 33 jpd.registry.Add(pkg) 34 }); err != nil { 35 return nil, fmt.Errorf("unable to walk json: %w", err) 36 } 37 } 38 39 if err := jpd.registry.ResolvePaths(prf); err != nil { 40 return nil, fmt.Errorf("unable to resolve paths: %w", err) 41 } 42 43 if err := jpd.registry.ResolveImports(); err != nil { 44 return nil, fmt.Errorf("unable to resolve paths: %w", err) 45 } 46 47 return jpd, nil 48} 49 50func (b *JSONPackagesDriver) GetResponse(labels []string) *driverResponse { 51 rootPkgs, packages := b.registry.Match(labels) 52 53 return &driverResponse{ 54 NotHandled: false, 55 Sizes: types.SizesFor("gc", "amd64").(*types.StdSizes), 56 Roots: rootPkgs, 57 Packages: packages, 58 } 59} 60