1// Copyright 2023 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 python 16 17import ( 18 "github.com/bazelbuild/bazel-gazelle/rule" 19) 20 21const ( 22 pyBinaryKind = "py_binary" 23 pyLibraryKind = "py_library" 24 pyTestKind = "py_test" 25) 26 27// Kinds returns a map that maps rule names (kinds) and information on how to 28// match and merge attributes that may be found in rules of those kinds. 29func (*Python) Kinds() map[string]rule.KindInfo { 30 return pyKinds 31} 32 33var pyKinds = map[string]rule.KindInfo{ 34 pyBinaryKind: { 35 MatchAny: true, 36 NonEmptyAttrs: map[string]bool{ 37 "deps": true, 38 "main": true, 39 "srcs": true, 40 "imports": true, 41 }, 42 SubstituteAttrs: map[string]bool{}, 43 MergeableAttrs: map[string]bool{ 44 "srcs": true, 45 }, 46 ResolveAttrs: map[string]bool{ 47 "deps": true, 48 }, 49 }, 50 pyLibraryKind: { 51 MatchAny: false, 52 MatchAttrs: []string{"srcs"}, 53 NonEmptyAttrs: map[string]bool{ 54 "deps": true, 55 "srcs": true, 56 "imports": true, 57 }, 58 SubstituteAttrs: map[string]bool{}, 59 MergeableAttrs: map[string]bool{ 60 "srcs": true, 61 }, 62 ResolveAttrs: map[string]bool{ 63 "deps": true, 64 }, 65 }, 66 pyTestKind: { 67 MatchAny: false, 68 NonEmptyAttrs: map[string]bool{ 69 "deps": true, 70 "main": true, 71 "srcs": true, 72 "imports": true, 73 }, 74 SubstituteAttrs: map[string]bool{}, 75 MergeableAttrs: map[string]bool{ 76 "srcs": true, 77 }, 78 ResolveAttrs: map[string]bool{ 79 "deps": true, 80 }, 81 }, 82} 83 84// Loads returns .bzl files and symbols they define. Every rule generated by 85// GenerateRules, now or in the past, should be loadable from one of these 86// files. 87func (py *Python) Loads() []rule.LoadInfo { 88 return pyLoads 89} 90 91var pyLoads = []rule.LoadInfo{ 92 { 93 Name: "@rules_python//python:defs.bzl", 94 Symbols: []string{ 95 pyBinaryKind, 96 pyLibraryKind, 97 pyTestKind, 98 }, 99 }, 100} 101