/
pkgconv.go
146 lines (134 loc) · 3.79 KB
/
pkgconv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2021 Derivita Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pkgconv
import (
"fmt"
"log"
"path/filepath"
"strings"
"github.com/bazelbuild/bazel-watcher/third_party/bazel/master/src/main/protobuf/blaze_query"
"golang.org/x/tools/go/packages"
)
type target struct {
name string
folder string
srcs []string
deps []string
importpath string
rule string
embed []string
suffix string
compilers []string
actual string
}
// Load generates packages.Packages from bazel query results.
// Each go rule in the input is converted into a Package with ID, PkgPath, and Imports.
// Imports is generated from bazel deps, so it does not include any standard library packages.
// Name may also be set for test packages.
func Load(protoTargets []*blaze_query.Target) []*packages.Package {
var pkgs []*packages.Package
targets := make(map[string]*target)
for _, pt := range protoTargets {
if pt.GetType() != blaze_query.Target_RULE {
continue
}
rule := pt.GetRule()
t := &target{
name: rule.GetName(),
rule: rule.GetRuleClass(),
folder: filepath.Dir(rule.GetLocation()),
}
for _, a := range rule.GetAttribute() {
switch a.GetName() {
case "deps":
t.deps = a.GetStringListValue()
case "srcs":
t.srcs = a.GetStringListValue()
case "embed":
t.embed = a.GetStringListValue()
case "importpath":
t.importpath = a.GetStringValue()
case "suffix":
t.suffix = a.GetStringValue()
case "proto":
proto := a.GetStringValue()
if proto != "" {
t.srcs = append(t.srcs, proto)
}
case "protos":
t.srcs = append(t.srcs, a.GetStringListValue()...)
case "compilers":
t.compilers = a.GetStringListValue()
case "actual":
t.actual = a.GetStringValue()
case "library":
t.embed = []string{a.GetStringValue()}
}
}
targets[t.name] = t
}
for _, t := range targets {
pkg := t.toPackage(targets)
pkgs = append(pkgs, pkg...)
}
return pkgs
}
func (t target) toPackage(targets map[string]*target) []*packages.Package {
switch t.rule {
case "go_library":
return convertGoLibrary(t, targets)
case "go_proto_library":
return convertGoProtoLibrary(t, targets)
case "go_tool_library":
// ignore
case "go_test":
return convertGoTest(t, targets)
case "alias":
actual := targets[t.actual]
if actual != nil {
return actual.toPackage(targets)
}
default:
if t.importpath != "" {
log.Printf("importpath %#v for %v %v", t.importpath, t.rule, t.name)
}
}
return nil
}
func binpath(pkg string) string {
if index := strings.Index(pkg, ":"); index != -1 {
pkg = pkg[:index]
}
if pkg[0] == '@' {
parts := strings.Split(pkg[1:], "//")
return filepath.Join("bazel-bin", "external", parts[0], parts[1])
}
pkg = strings.TrimPrefix(pkg, "//")
return filepath.Join("bazel-bin", pkg)
}
func targetName(t *blaze_query.Target) string {
switch t.GetType() {
case blaze_query.Target_RULE:
return t.GetRule().GetName()
case blaze_query.Target_SOURCE_FILE:
return t.GetSourceFile().GetName()
case blaze_query.Target_GENERATED_FILE:
return t.GetGeneratedFile().GetName()
case blaze_query.Target_PACKAGE_GROUP:
return t.GetPackageGroup().GetName()
case blaze_query.Target_ENVIRONMENT_GROUP:
return t.GetEnvironmentGroup().GetName()
}
return fmt.Sprintf("%#v", t)
}