• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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			"visibility": true,
42		},
43		SubstituteAttrs: map[string]bool{},
44		MergeableAttrs: map[string]bool{
45			"srcs": true,
46		},
47		ResolveAttrs: map[string]bool{
48			"deps": true,
49		},
50	},
51	pyLibraryKind: {
52		MatchAny: true,
53		NonEmptyAttrs: map[string]bool{
54			"deps":       true,
55			"srcs":       true,
56			"imports":    true,
57			"visibility": true,
58		},
59		SubstituteAttrs: map[string]bool{},
60		MergeableAttrs: map[string]bool{
61			"srcs": true,
62		},
63		ResolveAttrs: map[string]bool{
64			"deps": true,
65		},
66	},
67	pyTestKind: {
68		MatchAny: false,
69		NonEmptyAttrs: map[string]bool{
70			"deps":       true,
71			"main":       true,
72			"srcs":       true,
73			"imports":    true,
74			"visibility": true,
75		},
76		SubstituteAttrs: map[string]bool{},
77		MergeableAttrs: map[string]bool{
78			"srcs": true,
79		},
80		ResolveAttrs: map[string]bool{
81			"deps": true,
82		},
83	},
84}
85
86// Loads returns .bzl files and symbols they define. Every rule generated by
87// GenerateRules, now or in the past, should be loadable from one of these
88// files.
89func (py *Python) Loads() []rule.LoadInfo {
90	return pyLoads
91}
92
93var pyLoads = []rule.LoadInfo{
94	{
95		Name: "@rules_python//python:defs.bzl",
96		Symbols: []string{
97			pyBinaryKind,
98			pyLibraryKind,
99			pyTestKind,
100		},
101	},
102}
103