Skip to content

Commit

Permalink
Merge pull request #28 from Shresht7:refactor-docs
Browse files Browse the repository at this point in the history
Refactors the docs and docs-generator
  • Loading branch information
Shresht7 authored Feb 12, 2024
2 parents 1d8c3f1 + b03bf92 commit e2d4772
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: 1.22

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{
"label": "doc",
"type": "shell",
"command": "go run docs/generator/main.go docs/generator/toTemplateData.go docs/generator/generateREADME.go docs/generator/generateDocumentation.go",
"command": "go run ./docs/generator/...",
"group": {
"kind": "build",
"isDefault": true
Expand Down
File renamed without changes
12 changes: 7 additions & 5 deletions docs/generator/generateDocumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"
)

// Generates the documentation for the given cobra command. If recurse is true,
// Generates the documentation for the given cobra command in the given directory. If recurse is `true`,
// it will recurse through the subcommands and generate documentation for them as well
func generateDocumentation(cmd *cobra.Command, dir string, recurse bool) error {

Expand All @@ -22,10 +22,11 @@ func generateDocumentation(cmd *cobra.Command, dir string, recurse bool) error {

// Create and parse the template definitions
tmpl := template.Must(template.ParseFiles(
"docs/templates/command.md",
"docs/templates/_back-to-top.md",
"docs/generator/templates/command.md",
"docs/generator/templates/_back-to-top.md",
))

// Execute and write the template to the file
err = tmpl.Execute(w, toTemplateData(cmd))
if err != nil {
return err
Expand Down Expand Up @@ -53,10 +54,11 @@ func generateDocumentation(cmd *cobra.Command, dir string, recurse bool) error {

// Create and parse the template definitions
tmpl = template.Must(template.ParseFiles(
"docs/templates/index.md",
"docs/templates/_back-to-top.md",
"docs/generator/templates/index.md",
"docs/generator/templates/_back-to-top.md",
))

// Execute and write the template to the README file
err = tmpl.Execute(i, toTemplateData(cmd))
if err != nil {
return err
Expand Down
14 changes: 5 additions & 9 deletions docs/generator/generateREADME.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import (
"os"

"github.com/spf13/cobra"

"github.com/Shresht7/goutils/slice"
)

// Generates the main README.md file for the project.
// It uses templates from the docs/templates directory.
func generateREADME(cmd *cobra.Command) error {

// Create the README.md file
Expand All @@ -23,14 +20,13 @@ func generateREADME(cmd *cobra.Command) error {

// Create and parse the template definitions
tmpl := template.Must(template.ParseFiles(
"docs/templates/README.md",
"docs/templates/_back-to-top.md",
"docs/templates/_command-readme.md",
"docs/generator/templates/README.md",
"docs/generator/templates/_back-to-top.md",
"docs/generator/templates/_command-readme.md",
))

err = tmpl.Execute(w, slice.Map(cmd.Commands(), func(cmd *cobra.Command, idx int) map[string]any {
return toTemplateData(cmd)
}))
// Execute the template and write the README.md file
err = tmpl.Execute(w, toTemplateData(cmd))
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions docs/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"github.com/Shresht7/gh-license/cmd"
)

// Generate generates the documentation for this project
// Generates documentation for this project
func main() {

// Generate the documentation for the root command and all subcommands
// The documentation will be generated in the docs directory
// Generate the documentation for the root command and all subcommands in the "docs" folder
err := generateDocumentation(cmd.RootCmd, "docs", true)
if err != nil {
log.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ gh license create mit

## ⌨️ Commands

{{ range . }}
{{ range .Commands }}
{{- template "Command" . -}}
{{ end }}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions docs/generator/toTemplateData.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/Shresht7/Scribe/markdown"
"github.com/Shresht7/goutils/slice"
str "github.com/Shresht7/goutils/strings"
)

// HELPER FUNCTIONS
Expand All @@ -27,7 +26,7 @@ func toTemplateData(cmd *cobra.Command) map[string]any {

"Aliases": strings.Join(
slice.Map(cmd.Aliases, func(x string, i int) string {
return str.Wrap("`", x)
return markdown.Code(x)
}),
", ",
),
Expand Down Expand Up @@ -68,7 +67,7 @@ func toTemplateData(cmd *cobra.Command) map[string]any {
// Append the row
rows = append(rows, []string{
"`--" + flag.Name + ", -" + flag.Shorthand + "`",
str.Wrap("`", flag.Value.Type()),
markdown.Code(flag.Value.Type()),
flag.Usage,
flag.DefValue,
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Shresht7/gh-license

go 1.21
go 1.22

require (
github.com/Shresht7/Scribe v0.0.0-20230331052318-69ae28065919
Expand Down

0 comments on commit e2d4772

Please sign in to comment.