github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/resolve/resolve_external.go (about) 1 /* Copyright 2016 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 16 package resolve 17 18 import ( 19 "github.com/bazelbuild/bazel-gazelle/internal/label" 20 "github.com/bazelbuild/bazel-gazelle/internal/pathtools" 21 "github.com/bazelbuild/bazel-gazelle/internal/repos" 22 ) 23 24 // externalResolver resolves import paths to external repositories. It uses 25 // vcs to determine the prefix of the import path that corresponds to the root 26 // of the repository (this will perform a network fetch for unqualified paths). 27 // The prefix is converted to a Bazel external name repo according to the 28 // guidelines in http://bazel.io/docs/be/functions.html#workspace. The remaining 29 // portion of the import path is treated as the package name. 30 type externalResolver struct { 31 l *label.Labeler 32 rc *repos.RemoteCache 33 } 34 35 var _ nonlocalResolver = (*externalResolver)(nil) 36 37 func newExternalResolver(l *label.Labeler, rc *repos.RemoteCache) *externalResolver { 38 return &externalResolver{l: l, rc: rc} 39 } 40 41 // Resolve resolves "importPath" into a label, assuming that it is a label in an 42 // external repository. It also assumes that the external repository follows the 43 // recommended reverse-DNS form of workspace name as described in 44 // http://bazel.io/docs/be/functions.html#workspace. 45 func (r *externalResolver) resolve(importPath string) (label.Label, error) { 46 prefix, repo, err := r.rc.Root(importPath) 47 if err != nil { 48 return label.NoLabel, err 49 } 50 51 var pkg string 52 if importPath != prefix { 53 pkg = pathtools.TrimPrefix(importPath, prefix) 54 } 55 56 l := r.l.LibraryLabel(pkg) 57 l.Repo = repo 58 return l, nil 59 }