|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "text/template" |
| 8 | + |
| 9 | + "github.com/SierraSoftworks/roadmap" |
| 10 | + "github.com/urfave/cli/v2" |
| 11 | + yaml "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +//go:embed templates/roadmap.basic.frontmatter.md |
| 15 | +var frontmatterMdRoadmapBasicTemplate string |
| 16 | + |
| 17 | +//go:embed templates/roadmap.advanced.frontmatter.md |
| 18 | +var frontmatterMdRoadmapAdvancedTemplate string |
| 19 | + |
| 20 | +var frontmatterMdRenderCommand = cli.Command{ |
| 21 | + Name: "frontmatter-markdown", |
| 22 | + Aliases: []string{"fmd"}, |
| 23 | + Usage: "Generate a Markdown file which can be rendered to visualize your roadmap specification, including a frontmatter preamble.", |
| 24 | + Flags: []cli.Flag{ |
| 25 | + &cli.BoolFlag{ |
| 26 | + Name: "simple", |
| 27 | + Usage: "Emits simplified Markdown for maximum compatibility.", |
| 28 | + }, |
| 29 | + &cli.StringFlag{ |
| 30 | + Name: "frontmatter", |
| 31 | + Aliases: []string{"m"}, |
| 32 | + Usage: "Additional frontmatter fields to include in the frontmatter, should be provided in YAML format.", |
| 33 | + Value: "{}", |
| 34 | + }, |
| 35 | + &cli.StringFlag{ |
| 36 | + Name: "input", |
| 37 | + Aliases: []string{"in"}, |
| 38 | + Usage: "The input roadmap.yml `FILE`.", |
| 39 | + Value: "roadmap.yml", |
| 40 | + TakesFile: true, |
| 41 | + Required: true, |
| 42 | + }, |
| 43 | + &cli.StringFlag{ |
| 44 | + Name: "output", |
| 45 | + Aliases: []string{"out"}, |
| 46 | + Usage: "The output `FILE` (defaults to stdout).", |
| 47 | + TakesFile: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + Action: func(c *cli.Context) error { |
| 51 | + f, err := os.ReadFile(c.String("input")) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + r, err := roadmap.Parse(f) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + tmpl := frontmatterMdRoadmapAdvancedTemplate |
| 62 | + if c.Bool("simple") { |
| 63 | + tmpl = frontmatterMdRoadmapBasicTemplate |
| 64 | + } |
| 65 | + |
| 66 | + dot, err := renderTextTemplate(r, tmpl, template.FuncMap{ |
| 67 | + "stateColor": func(state string) string { |
| 68 | + switch state { |
| 69 | + case "TODO": |
| 70 | + return "#aaa" |
| 71 | + case "DOING": |
| 72 | + return "#63B2EB" |
| 73 | + case "DONE": |
| 74 | + return "#3EAF7C" |
| 75 | + case "SKIP": |
| 76 | + return "#F65BD2" |
| 77 | + default: |
| 78 | + return "#aaa" |
| 79 | + } |
| 80 | + }, |
| 81 | + "requirementColor": func(requirement string) string { |
| 82 | + switch requirement { |
| 83 | + case "MUST": |
| 84 | + return "#E06446" |
| 85 | + case "SHOULD": |
| 86 | + return "#E0AF2F" |
| 87 | + case "MAY": |
| 88 | + return "#3ABDE0" |
| 89 | + default: |
| 90 | + return "#888" |
| 91 | + } |
| 92 | + }, |
| 93 | + "metadata": func() map[string]interface{} { |
| 94 | + m := map[string]interface{}{} |
| 95 | + if err := yaml.Unmarshal([]byte(c.String("frontmatter")), &m); err != nil { |
| 96 | + return map[string]interface{}{ |
| 97 | + "__parse_error": err.Error(), |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if m["title"] == nil { |
| 102 | + m["title"] = r.Title |
| 103 | + } |
| 104 | + |
| 105 | + if m["description"] == nil { |
| 106 | + m["description"] = r.Description |
| 107 | + } |
| 108 | + |
| 109 | + return m |
| 110 | + }, |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + if c.String("output") != "" { |
| 117 | + return os.WriteFile(c.String("output"), []byte(dot), os.ModePerm) |
| 118 | + } else { |
| 119 | + _, err := fmt.Println(dot) |
| 120 | + return err |
| 121 | + } |
| 122 | + }, |
| 123 | +} |
0 commit comments