Skip to content

Commit c6d35eb

Browse files
committed
chore: new generator
1 parent 1b9e357 commit c6d35eb

File tree

6 files changed

+145
-81
lines changed

6 files changed

+145
-81
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ linters:
166166
linters: [gosec]
167167
text: "G115: integer overflow conversion uint64 -> int"
168168

169+
# The files created during the tests don't need to be secured.
170+
- path: scripts/website/expand_templates/linters_test.go
171+
linters: [gosec]
172+
text: "G306: Expect WriteFile permissions to be 0600 or less"
173+
169174
formatters:
170175
enable:
171176
- gofmt
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ConfigurationFile.md
2+
FormattersSettings.md
3+
LintersSettings.md

scripts/website/expand_templates/linters.go

Lines changed: 100 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,14 @@ import (
2020

2121
const listItemPrefix = "list-item-"
2222

23-
func getExampleSnippets() (*SettingSnippets, error) {
24-
reference, err := os.ReadFile(".golangci.reference.yml")
25-
if err != nil {
26-
return nil, fmt.Errorf("can't read .golangci.reference.yml: %w", err)
27-
}
28-
29-
snippets, err := extractExampleSnippets(reference)
30-
if err != nil {
31-
return nil, fmt.Errorf("can't extract example snippets from .golangci.reference.yml: %w", err)
32-
}
33-
34-
return snippets, nil
35-
}
23+
const (
24+
keyLinters = "linters"
25+
keyFormatters = "formatters"
26+
keySettings = "settings"
27+
)
3628

37-
func getLintersListMarkdown(enabled bool) string {
38-
linters, err := readJSONFile[[]*types.LinterWrapper](filepath.Join("assets", "linters-info.json"))
29+
func getLintersListMarkdown(enabled bool, src string) string {
30+
linters, err := readJSONFile[[]*types.LinterWrapper](src)
3931
if err != nil {
4032
panic(err)
4133
}
@@ -172,14 +164,63 @@ func spanWithID(id, title, icon string) string {
172164
}
173165

174166
type SettingSnippets struct {
175-
ConfigurationFile string
176-
LintersSettings string
167+
ConfigurationFile string
168+
LintersSettings string
169+
FormattersSettings string
177170
}
178171

179-
func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
180-
var data yaml.Node
181-
err := yaml.Unmarshal(example, &data)
172+
func marshallSnippet(node *yaml.Node) (string, error) {
173+
builder := &strings.Builder{}
174+
175+
if node.Value != "" {
176+
_, _ = fmt.Fprintf(builder, "### %s\n\n", node.Value)
177+
}
178+
_, _ = fmt.Fprintln(builder, "```yaml")
179+
180+
encoder := yaml.NewEncoder(builder)
181+
encoder.SetIndent(2)
182+
183+
err := encoder.Encode(node)
184+
if err != nil {
185+
return "", err
186+
}
187+
188+
_, _ = fmt.Fprintln(builder, "```")
189+
_, _ = fmt.Fprintln(builder)
190+
191+
return builder.String(), nil
192+
}
193+
194+
type ExampleSnippetsExtractor struct {
195+
referencePath string
196+
assetsPath string
197+
}
198+
199+
func NewExampleSnippetsExtractor() *ExampleSnippetsExtractor {
200+
return &ExampleSnippetsExtractor{
201+
// TODO(ldez) replace .golangci.next.reference.yml by .golangci.reference.yml
202+
referencePath: ".golangci.next.reference.yml",
203+
assetsPath: "assets",
204+
}
205+
}
206+
207+
func (e *ExampleSnippetsExtractor) GetExampleSnippets() (*SettingSnippets, error) {
208+
reference, err := os.ReadFile(e.referencePath)
209+
if err != nil {
210+
return nil, fmt.Errorf("can't read .golangci.reference.yml: %w", err)
211+
}
212+
213+
snippets, err := e.extractExampleSnippets(reference)
182214
if err != nil {
215+
return nil, fmt.Errorf("can't extract example snippets from .golangci.reference.yml: %w", err)
216+
}
217+
218+
return snippets, nil
219+
}
220+
221+
func (e *ExampleSnippetsExtractor) extractExampleSnippets(example []byte) (*SettingSnippets, error) {
222+
var data yaml.Node
223+
if err := yaml.Unmarshal(example, &data); err != nil {
183224
return nil, err
184225
}
185226

@@ -205,7 +246,7 @@ func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
205246

206247
for j, node := range root.Content {
207248
switch node.Value {
208-
case "run", "output", "linters", "linters-settings", "issues", "severity": // TODO(ldez) documentation
249+
case "run", "output", keyLinters, keyFormatters, "issues", "severity":
209250
default:
210251
continue
211252
}
@@ -233,20 +274,43 @@ func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
233274

234275
globalNode.Content = append(globalNode.Content, node, newNode)
235276

236-
if node.Value == "linters-settings" { // TODO(ldez) documentation
237-
snippets.LintersSettings, err = getLintersSettingSections(node, nextNode)
238-
if err != nil {
239-
return nil, err
277+
if node.Value == keyLinters || node.Value == keyFormatters {
278+
for i := 0; i < len(nextNode.Content); i++ {
279+
if nextNode.Content[i].Value != keySettings {
280+
continue
281+
}
282+
283+
settingSections, err := e.getSettingSections(node, nextNode.Content[i+1])
284+
if err != nil {
285+
return nil, err
286+
}
287+
288+
switch node.Value {
289+
case keyLinters:
290+
snippets.LintersSettings = settingSections
291+
292+
case keyFormatters:
293+
snippets.FormattersSettings = settingSections
294+
}
295+
296+
nextNode.Content[i+1].Content = []*yaml.Node{
297+
{
298+
HeadComment: fmt.Sprintf(`See the dedicated "%s.%s" documentation section.`, node.Value, nextNode.Content[i].Value),
299+
Kind: node.Kind,
300+
Style: node.Style,
301+
Tag: node.Tag,
302+
Value: "option",
303+
},
304+
{
305+
Kind: node.Kind,
306+
Style: node.Style,
307+
Tag: node.Tag,
308+
Value: "value",
309+
},
310+
}
311+
312+
i++
240313
}
241-
242-
_, _ = builder.WriteString(
243-
// TODO(ldez) documentation
244-
fmt.Sprintf(
245-
"### `%s` configuration\n\nSee the dedicated [linters-settings](/usage/linters) documentation section.\n\n",
246-
node.Value,
247-
),
248-
)
249-
continue
250314
}
251315

252316
nodeSection := &yaml.Node{
@@ -275,8 +339,8 @@ func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
275339
return &snippets, nil
276340
}
277341

278-
func getLintersSettingSections(node, nextNode *yaml.Node) (string, error) {
279-
linters, err := readJSONFile[[]*types.LinterWrapper](filepath.Join("assets", "linters-info.json"))
342+
func (e *ExampleSnippetsExtractor) getSettingSections(node, nextNode *yaml.Node) (string, error) {
343+
linters, err := readJSONFile[[]*types.LinterWrapper](filepath.Join(e.assetsPath, fmt.Sprintf("%s-info.json", node.Value)))
280344
if err != nil {
281345
return "", err
282346
}
@@ -331,25 +395,3 @@ func getLintersSettingSections(node, nextNode *yaml.Node) (string, error) {
331395

332396
return builder.String(), nil
333397
}
334-
335-
func marshallSnippet(node *yaml.Node) (string, error) {
336-
builder := &strings.Builder{}
337-
338-
if node.Value != "" {
339-
_, _ = fmt.Fprintf(builder, "### %s\n\n", node.Value)
340-
}
341-
_, _ = fmt.Fprintln(builder, "```yaml")
342-
343-
encoder := yaml.NewEncoder(builder)
344-
encoder.SetIndent(2)
345-
346-
err := encoder.Encode(node)
347-
if err != nil {
348-
return "", err
349-
}
350-
351-
_, _ = fmt.Fprintln(builder, "```")
352-
_, _ = fmt.Fprintln(builder)
353-
354-
return builder.String(), nil
355-
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_ExampleSnippetsExtractor_GetExampleSnippets(t *testing.T) {
12+
t.Skip("only for debugging purpose")
13+
14+
e := &ExampleSnippetsExtractor{
15+
referencePath: "../../../.golangci.next.reference.yml",
16+
assetsPath: filepath.Join("..", "..", "..", "assets"),
17+
}
18+
19+
m, err := e.GetExampleSnippets()
20+
require.NoError(t, err)
21+
22+
t.Log(m)
23+
24+
err = os.WriteFile("./ConfigurationFile.md", []byte(m.ConfigurationFile), 0644)
25+
require.NoError(t, err)
26+
27+
err = os.WriteFile("./LintersSettings.md", []byte(m.LintersSettings), 0644)
28+
require.NoError(t, err)
29+
30+
err = os.WriteFile("./FormattersSettings.md", []byte(m.FormattersSettings), 0644)
31+
require.NoError(t, err)
32+
}

scripts/website/expand_templates/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func processDoc(path string, replacements map[string]string, madeReplacements ma
9090
}
9191

9292
func buildTemplateContext() (map[string]string, error) {
93-
snippets, err := getExampleSnippets()
93+
snippets, err := NewExampleSnippetsExtractor().GetExampleSnippets()
9494
if err != nil {
9595
return nil, err
9696
}
@@ -123,10 +123,12 @@ func buildTemplateContext() (map[string]string, error) {
123123
return map[string]string{
124124
"CustomGCLReference": pluginReference,
125125
"LintersExample": snippets.LintersSettings,
126+
"FormattersExample": snippets.FormattersSettings,
126127
"ConfigurationExample": snippets.ConfigurationFile,
127128
"LintersCommandOutputEnabledOnly": helps.Enable,
128-
"EnabledByDefaultLinters": getLintersListMarkdown(true),
129-
"DisabledByDefaultLinters": getLintersListMarkdown(false),
129+
"EnabledByDefaultLinters": getLintersListMarkdown(true, filepath.Join("assets", "linters-info.json")),
130+
"DisabledByDefaultLinters": getLintersListMarkdown(false, filepath.Join("assets", "linters-info.json")),
131+
"Formatters": getLintersListMarkdown(false, filepath.Join("assets", "formatters-info.json")),
130132
"ExclusionPresets": exclusions,
131133
"ThanksList": getThanksList(),
132134
"RunHelpText": helps.Help,

scripts/website/expand_templates/main_test.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)