-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplease.go
163 lines (140 loc) · 3.72 KB
/
please.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/please-build/gcfg"
)
type PleaseGraph struct {
Packages map[string]*PleasePackage `json:"packages,omitempty"`
}
func (g *PleaseGraph) String() string {
var sb strings.Builder
for key, value := range g.Packages {
sb.WriteString("Package{name=")
sb.WriteString(key)
sb.WriteString(", targets=[")
sb.WriteString(value.String())
sb.WriteString("]}")
}
return sb.String()
}
type PleasePackage struct {
Targets map[string]*PleaseTarget `json:"targets,omitempty"`
}
func (p *PleasePackage) String() string {
var sb strings.Builder
for key, target := range p.Targets {
sb.WriteString("Target{name=")
sb.WriteString(key)
sb.WriteString(", {")
sb.WriteString(fmt.Sprintf("%+v", target))
sb.WriteString("} }")
}
return sb.String()
}
// PleaseTarget maps one-to-one to the target definition provided by Please.
type PleaseTarget struct {
Inputs []string `json:"inputs,omitempty"`
Outs []string `json:"outs,omitempty"`
Deps []string `json:"deps,omitempty"`
Data []string `json:"data,omitempty"`
Labels []string `json:"labels,omitempty"`
Requires []string `json:"requires,omitempty"`
Command string `json:"command,omitempty"`
Sources interface{} `json:"srcs,omitempty"`
Tools interface{} `json:"tools,omitempty"`
Hash string `json:"hash,omitempty"`
Binary bool `json:"binary,omitempty"`
}
// ToolForName returns the concatenated list of tools in a single string.
func (t *PleaseTarget) ToolForName(name string) string {
tools, ok := t.Tools.(map[string]interface{})
if !ok {
return ""
}
listOfTools, ok := tools[name].([]interface{})
if !ok {
return ""
}
strTools := []string{}
for _, tool := range listOfTools {
val, ok := tool.(string)
if ok {
strTools = append(strTools, val)
}
}
return strings.Join(strTools, " ")
}
// AllSources returns a list of sources concatenated by a whitespace.
func (t *PleaseTarget) AllSources() string {
tools, ok := t.Sources.(map[string]interface{})
if !ok {
return ""
}
listOfTools, ok := tools["srcs"].([]interface{})
if !ok {
return ""
}
strTools := []string{}
for _, tool := range listOfTools {
val, ok := tool.(string)
if ok {
strTools = append(strTools, val)
}
}
return strings.Join(strTools, " ")
}
// PleaseConfig contains the configuration for C/C++ rules.
type PleaseConfig struct {
Cpp struct {
CcTool string
CppTool string
LdTool string
ArTool string
LinkWithLdtool string
DefaultOptCflags string
DefaultDbgCflags string
DefaultOptCppFlags string
DefaultDbgCppFlags string
DefaltLdFlags string
PkgconfigPath string
Coverage bool
Testmain string
Clangmodules bool
Dsymtool string
}
}
// PleaseRunner runs the Please commands
type PleaseRunner struct{}
// Graph returns a dependency graph for the target
func (PleaseRunner) Graph(target string) (*PleaseGraph, error) {
stdout := bytes.NewBuffer(nil)
cmd := exec.Command("plz", "query", "graph", target)
cmd.Stdout = stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
graph := &PleaseGraph{}
if err := json.Unmarshal(stdout.Bytes(), graph); err != nil {
return nil, err
}
return graph, nil
}
// Config returns the C/C++ configuration from .plzconfig
func (PleaseRunner) Config() (*PleaseConfig, error) {
stdout := bytes.NewBuffer(nil)
cmd := exec.Command("plz", "query", "config")
cmd.Stdout = stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, err
}
config := &PleaseConfig{}
err := gcfg.ReadInto(config, stdout)
return config, gcfg.FatalOnly(err)
}