Skip to content

Commit b0c42df

Browse files
marcuscaiseypeterebden
authored andcommitted
Output default values for plugin config fields from plz query config
1 parent 8a33019 commit b0c42df

7 files changed

Lines changed: 96 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
__pycache__
1010

1111
.idea
12+
.vscode
1213
*.iml
1314
/venv
1415
/out

src/query/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"//src/core",
1515
"//src/fs",
1616
"//src/parse",
17+
"//src/plz",
1718
],
1819
)
1920

src/query/config.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/please-build/gcfg"
1010

1111
"github.com/thought-machine/please/src/core"
12+
"github.com/thought-machine/please/src/plz"
1213
)
1314

1415
// Config prints configuration settings in human-readable format.
@@ -29,7 +30,15 @@ func Config(config *core.Configuration, options []string) {
2930

3031
values, err := gcfg.Get(config, section, subsection, name)
3132
if err != nil {
32-
log.Fatalf("Failed to get %s: %s", option, err)
33+
if section == "plugin" {
34+
if defaultValues, ok := pluginConfigFieldDefaultValues(config, subsection, name); ok {
35+
values = defaultValues
36+
err = nil
37+
}
38+
}
39+
if err != nil {
40+
log.Fatalf("Failed to get %s: %s", option, err)
41+
}
3342
}
3443

3544
for _, value := range values {
@@ -39,6 +48,53 @@ func Config(config *core.Configuration, options []string) {
3948
}
4049
}
4150

51+
// pluginConfigFieldDefaultValues returns the default values of a plugin config field and a boolean indicating whether
52+
// the plugin field actually exists.
53+
func pluginConfigFieldDefaultValues(config *core.Configuration, pluginName string, name string) ([]string, bool) {
54+
plugin, ok := config.Plugin[pluginName]
55+
if !ok {
56+
return nil, false
57+
}
58+
59+
state := core.NewBuildState(config)
60+
plz.Run([]core.BuildLabel{plugin.Target}, nil, state, state.Config, state.TargetArch)
61+
subrepo := state.Graph.SubrepoOrDie(pluginName)
62+
subrepo.State.Initialise(subrepo)
63+
64+
for key, field := range subrepo.State.RepoConfig.PluginConfig {
65+
configKey := field.ConfigKey
66+
if configKey == "" {
67+
configKey = strings.ReplaceAll(key, "_", "")
68+
}
69+
if strings.EqualFold(configKey, name) {
70+
values := normaliseBuildLabels(field.DefaultValue, subrepo.Name)
71+
return values, true
72+
}
73+
}
74+
75+
return nil, false
76+
}
77+
78+
// normaliseBuildLabels returns a copy of values with each build label made absolute.
79+
// For example, //tools/bar in subrepo foo is replaced by ///foo//tools/bar.
80+
func normaliseBuildLabels(values []string, subrepo string) []string {
81+
valuesCopy := make([]string, len(values))
82+
for i, value := range values {
83+
if core.LooksLikeABuildLabel(value) {
84+
target, annotation := core.SplitLabelAnnotation(value)
85+
if label, err := core.TryParseBuildLabel(target, "", subrepo); err == nil {
86+
annotatedLabel := core.AnnotatedOutputLabel{
87+
BuildLabel: label,
88+
Annotation: annotation,
89+
}
90+
value = annotatedLabel.String()
91+
}
92+
}
93+
valuesCopy[i] = value
94+
}
95+
return valuesCopy
96+
}
97+
4298
// ConfigJSON prints the configuration settings as JSON.
4399
func ConfigJSON(config *core.Configuration) {
44100
data, err := gcfg.RawJSON(config)

test/plz_query/config/BUILD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ BUILD.plz
1717
60000000000""",
1818
)
1919

20+
please_repo_e2e_test(
21+
name = "plugin_config_field_default_value",
22+
repo = "test_repo",
23+
data = {"plugin": [":foo_plugin"]},
24+
plz_command = "plz -o please.pluginrepo:file://$TMP_DIR/$DATA_PLUGIN query config plugin.foo.fooctool plugin.foo.bartools > output.txt",
25+
expected_output = {"output.txt": """fooc
26+
///foo//tools/bar:bar
27+
///baz//tools/bar:bar
28+
"""},
29+
)
30+
31+
genrule(
32+
name = "foo_plugin",
33+
srcs = {
34+
"plugin": ["foo_plugin"],
35+
"tool": ["//test/plugins/tools:fooc"],
36+
},
37+
outs = ["foo_plugin.tar.gz"],
38+
cmd = "mv $SRCS_PLUGIN foo_plugin && mv $SRCS_TOOL foo_plugin/tools && tar -czf $OUT foo_plugin",
39+
)
40+
2041
plz_e2e_test(
2142
name = "json_config",
2243
cmd = "plz query config --json",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[PluginDefinition]
2+
name = foo
3+
4+
[PluginConfig "fooc_tool"]
5+
DefaultValue = fooc
6+
7+
[PluginConfig "bar_tools"]
8+
Repeatable = true
9+
DefaultValue = //tools/bar
10+
DefaultValue = ///baz//tools/bar
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Plugin "foo"]
2+
Target = //:foo
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugin_repo(
2+
name = "foo",
3+
revision = "v1.0.0",
4+
)

0 commit comments

Comments
 (0)