|
| 1 | +//go:build ignore |
| 2 | +// +build ignore |
| 3 | + |
| 4 | +/* |
| 5 | + * Copyright 2021-2024 JetBrains s.r.o. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +// Script to generate CLI documentation in README.md using cobra/doc. |
| 21 | +// Documents init and scan first, then all other non-hidden commands. |
| 22 | +package main |
| 23 | + |
| 24 | +import ( |
| 25 | + "bytes" |
| 26 | + "os" |
| 27 | + "regexp" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/JetBrains/qodana-cli/internal/cmd" |
| 31 | + "github.com/spf13/cobra" |
| 32 | + "github.com/spf13/cobra/doc" |
| 33 | +) |
| 34 | + |
| 35 | +func main() { |
| 36 | + cmd.InitCli() |
| 37 | + root := cmd.GetRootCommand() |
| 38 | + |
| 39 | + cmdMap := make(map[string]*cobra.Command) |
| 40 | + for _, c := range root.Commands() { |
| 41 | + cmdMap[c.Name()] = c |
| 42 | + } |
| 43 | + |
| 44 | + var docs bytes.Buffer |
| 45 | + writeDoc := func(c *cobra.Command) { |
| 46 | + doc.GenMarkdownCustom(c, &docs, func(string) string { return "" }) |
| 47 | + docs.WriteString("\n") |
| 48 | + } |
| 49 | + |
| 50 | + if c := cmdMap["init"]; c != nil && !c.Hidden { |
| 51 | + writeDoc(c) |
| 52 | + } |
| 53 | + if c := cmdMap["scan"]; c != nil && !c.Hidden { |
| 54 | + writeDoc(c) |
| 55 | + } |
| 56 | + for _, c := range root.Commands() { |
| 57 | + if !c.Hidden && c.Name() != "init" && c.Name() != "scan" { |
| 58 | + writeDoc(c) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + result := cleanup(docs.String()) |
| 63 | + content, _ := os.ReadFile("README.md") |
| 64 | + newContent := regexp.MustCompile(`(?s)## qodana init\n.*?(## Why)`).ReplaceAllString(string(content), result+"$1") |
| 65 | + |
| 66 | + if len(os.Args) > 1 && os.Args[1] == "--check" { |
| 67 | + if string(content) != newContent { |
| 68 | + println("README.md is out of date. Run 'go run scripts/generate-docs.go' to update it.") |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | + println("README.md is up to date.") |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + os.WriteFile("README.md", []byte(newContent), 0644) |
| 76 | + println("README.md updated.") |
| 77 | +} |
| 78 | + |
| 79 | +// cleanup removes ANSI codes, UUID defaults, and replaces home dir with ~ |
| 80 | +func cleanup(s string) string { |
| 81 | + s = regexp.MustCompile(`\x1b\[[0-9;]*m`).ReplaceAllString(s, "") |
| 82 | + s = regexp.MustCompile(` \(default "[0-9a-f-]{36}"\)`).ReplaceAllString(s, "") |
| 83 | + if h, _ := os.UserHomeDir(); h != "" { |
| 84 | + s = strings.ReplaceAll(s, h, "~") |
| 85 | + } |
| 86 | + return s |
| 87 | +} |
0 commit comments