...

Source file src/github.com/bazelbuild/rules_go/go/tools/gopackagesdriver/json_packages_driver.go

Documentation: github.com/bazelbuild/rules_go/go/tools/gopackagesdriver

     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  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"runtime"
    20  )
    21  
    22  type JSONPackagesDriver struct {
    23  	registry *PackageRegistry
    24  }
    25  
    26  func NewJSONPackagesDriver(jsonFiles []string, prf PathResolverFunc, bazelVersion string) (*JSONPackagesDriver, error) {
    27  	jpd := &JSONPackagesDriver{
    28  		registry: NewPackageRegistry(bazelVersion),
    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  
    50  func (b *JSONPackagesDriver) GetResponse(labels []string) *driverResponse {
    51  	rootPkgs, packages := b.registry.Match(labels)
    52  
    53  	return &driverResponse{
    54  		NotHandled: false,
    55  		Compiler:   "gc",
    56  		Arch:       runtime.GOARCH,
    57  		Roots:      rootPkgs,
    58  		Packages:   packages,
    59  	}
    60  }
    61  

View as plain text