github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/label/labeler.go (about)

     1  /* Copyright 2017 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 label
    17  
    18  import (
    19  	"github.com/bazelbuild/bazel-gazelle/internal/config"
    20  	"github.com/bazelbuild/bazel-gazelle/internal/pathtools"
    21  )
    22  
    23  // Labeler generates Bazel labels for rules, based on their locations
    24  // within the repository.
    25  type Labeler struct {
    26  	c *config.Config
    27  }
    28  
    29  func NewLabeler(c *config.Config) *Labeler {
    30  	return &Labeler{c}
    31  }
    32  
    33  func (l *Labeler) LibraryLabel(rel string) Label {
    34  	return Label{Pkg: rel, Name: config.DefaultLibName}
    35  }
    36  
    37  func (l *Labeler) TestLabel(rel string, isXTest bool) Label {
    38  	var name string
    39  	if isXTest {
    40  		name = config.DefaultXTestName
    41  	} else {
    42  		name = config.DefaultTestName
    43  	}
    44  	return Label{Pkg: rel, Name: name}
    45  }
    46  
    47  func (l *Labeler) BinaryLabel(rel string) Label {
    48  	name := pathtools.RelBaseName(rel, l.c.GoPrefix, l.c.RepoRoot)
    49  	return Label{Pkg: rel, Name: name}
    50  }
    51  
    52  func (l *Labeler) ProtoLabel(rel, name string) Label {
    53  	return Label{Pkg: rel, Name: name + "_proto"}
    54  }
    55  
    56  func (l *Labeler) GoProtoLabel(rel, name string) Label {
    57  	return Label{Pkg: rel, Name: name + "_go_proto"}
    58  }