generate docs and completion concurrently for a faster "make"

Michael Muré created

Change summary

doc/gen_docs.go                   | 83 +++++++++++++++++++++++++++++++++
doc/gen_manpage.go                | 46 ------------------
doc/gen_markdown.go               | 36 --------------
git-bug.go                        |  8 --
misc/gen_bash_completion.go       | 24 ---------
misc/gen_completion.go            | 61 ++++++++++++++++++++++++
misc/gen_fish_completion.go       | 24 ---------
misc/gen_powershell_completion.go | 24 ---------
misc/gen_zsh_completion.go        | 24 ---------
9 files changed, 146 insertions(+), 184 deletions(-)

Detailed changes

doc/gen_docs.go 🔗

@@ -0,0 +1,83 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"path"
+	"path/filepath"
+	"time"
+
+	"github.com/spf13/cobra/doc"
+
+	"github.com/MichaelMure/git-bug/commands"
+)
+
+func main() {
+	fmt.Println("Generating documentation ...")
+
+	tasks := map[string]func() error{
+		"ManPage":  genManPage,
+		"Markdown": genMarkdown,
+	}
+
+	// Due to concurrency issues in cobra, the following can't be concurrent :(
+
+	// var wg sync.WaitGroup
+	for name, f := range tasks {
+		// wg.Add(1)
+		// go func(name string, f func() error) {
+		// 	defer wg.Done()
+		err := f()
+		if err != nil {
+			fmt.Printf("  - %s: %v\n", name, err)
+			return
+		}
+		fmt.Printf("  - %s: ok\n", name)
+		// }(name, f)
+	}
+
+	// wg.Wait()
+}
+
+func genManPage() error {
+	cwd, _ := os.Getwd()
+	dir := path.Join(cwd, "doc", "man")
+
+	date := time.Date(2019, 4, 1, 12, 0, 0, 0, time.UTC)
+
+	header := &doc.GenManHeader{
+		Title:   "GIT-BUG",
+		Section: "1",
+		Date:    &date,
+		Source:  "Generated from git-bug's source code",
+	}
+
+	files, err := filepath.Glob(dir + "/*.1")
+	if err != nil {
+		return err
+	}
+	for _, f := range files {
+		if err := os.Remove(f); err != nil {
+			return err
+		}
+	}
+
+	return doc.GenManTree(commands.RootCmd, header, dir)
+}
+
+func genMarkdown() error {
+	cwd, _ := os.Getwd()
+	dir := path.Join(cwd, "doc", "md")
+
+	files, err := filepath.Glob(dir + "/*.md")
+	if err != nil {
+		return err
+	}
+	for _, f := range files {
+		if err := os.Remove(f); err != nil {
+			return err
+		}
+	}
+
+	return doc.GenMarkdownTree(commands.RootCmd, dir)
+}

doc/gen_manpage.go 🔗

@@ -1,46 +0,0 @@
-// +build ignore
-
-package main
-
-import (
-	"fmt"
-	"log"
-	"os"
-	"path"
-	"path/filepath"
-	"time"
-
-	"github.com/MichaelMure/git-bug/commands"
-	"github.com/spf13/cobra/doc"
-)
-
-func main() {
-	cwd, _ := os.Getwd()
-	dir := path.Join(cwd, "doc", "man")
-
-	date := time.Date(2019, 4, 1, 12, 0, 0, 0, time.UTC)
-
-	header := &doc.GenManHeader{
-		Title:   "GIT-BUG",
-		Section: "1",
-		Date:    &date,
-		Source:  "Generated from git-bug's source code",
-	}
-
-	fmt.Println("Generating manpage ...")
-
-	files, err := filepath.Glob(dir + "/*.1")
-	if err != nil {
-		log.Fatal(err)
-	}
-	for _, f := range files {
-		if err := os.Remove(f); err != nil {
-			log.Fatal(err)
-		}
-	}
-
-	err = doc.GenManTree(commands.RootCmd, header, dir)
-	if err != nil {
-		log.Fatal(err)
-	}
-}

doc/gen_markdown.go 🔗

@@ -1,36 +0,0 @@
-// +build ignore
-
-package main
-
-import (
-	"fmt"
-	"log"
-	"os"
-	"path"
-	"path/filepath"
-
-	"github.com/MichaelMure/git-bug/commands"
-	"github.com/spf13/cobra/doc"
-)
-
-func main() {
-	cwd, _ := os.Getwd()
-	dir := path.Join(cwd, "doc", "md")
-
-	fmt.Println("Generating Markdown documentation ...")
-
-	files, err := filepath.Glob(dir + "/*.md")
-	if err != nil {
-		log.Fatal(err)
-	}
-	for _, f := range files {
-		if err := os.Remove(f); err != nil {
-			log.Fatal(err)
-		}
-	}
-
-	err = doc.GenMarkdownTree(commands.RootCmd, dir)
-	if err != nil {
-		log.Fatal(err)
-	}
-}

git-bug.go 🔗

@@ -1,9 +1,5 @@
-//go:generate go run doc/gen_markdown.go
-//go:generate go run doc/gen_manpage.go
-//go:generate go run misc/gen_bash_completion.go
-//go:generate go run misc/gen_fish_completion.go
-//go:generate go run misc/gen_powershell_completion.go
-//go:generate go run misc/gen_zsh_completion.go
+//go:generate go run doc/gen_docs.go
+//go:generate go run misc/gen_completion.go
 
 package main
 

misc/gen_bash_completion.go 🔗

@@ -1,24 +0,0 @@
-// +build ignore
-
-package main
-
-import (
-	"fmt"
-	"log"
-	"os"
-	"path"
-
-	"github.com/MichaelMure/git-bug/commands"
-)
-
-func main() {
-	cwd, _ := os.Getwd()
-	dir := path.Join(cwd, "misc", "bash_completion", "git-bug")
-
-	fmt.Println("Generating Bash completion file ...")
-
-	err := commands.RootCmd.GenBashCompletionFile(dir)
-	if err != nil {
-		log.Fatal(err)
-	}
-}

misc/gen_completion.go 🔗

@@ -0,0 +1,61 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"path"
+	"sync"
+
+	"github.com/MichaelMure/git-bug/commands"
+)
+
+func main() {
+	fmt.Println("Generating completion files ...")
+
+	tasks := map[string]func() error{
+		"Bash":       genBash,
+		"Fish":       genFish,
+		"PowerShell": genPowerShell,
+		"ZSH":        genZsh,
+	}
+
+	var wg sync.WaitGroup
+	for name, f := range tasks {
+		wg.Add(1)
+		go func(name string, f func() error) {
+			defer wg.Done()
+			err := f()
+			if err != nil {
+				fmt.Printf("  - %s: %v\n", name, err)
+				return
+			}
+			fmt.Printf("  - %s: ok\n", name)
+		}(name, f)
+	}
+
+	wg.Wait()
+}
+
+func genBash() error {
+	cwd, _ := os.Getwd()
+	dir := path.Join(cwd, "misc", "bash_completion", "git-bug")
+	return commands.RootCmd.GenBashCompletionFile(dir)
+}
+
+func genFish() error {
+	cwd, _ := os.Getwd()
+	dir := path.Join(cwd, "misc", "fish_completion", "git-bug")
+	return commands.RootCmd.GenFishCompletionFile(dir, true)
+}
+
+func genPowerShell() error {
+	cwd, _ := os.Getwd()
+	filepath := path.Join(cwd, "misc", "powershell_completion", "git-bug")
+	return commands.RootCmd.GenPowerShellCompletionFile(filepath)
+}
+
+func genZsh() error {
+	cwd, _ := os.Getwd()
+	filepath := path.Join(cwd, "misc", "zsh_completion", "git-bug")
+	return commands.RootCmd.GenZshCompletionFile(filepath)
+}

misc/gen_fish_completion.go 🔗

@@ -1,24 +0,0 @@
-// +build ignore
-
-package main
-
-import (
-	"fmt"
-	"log"
-	"os"
-	"path"
-
-	"github.com/MichaelMure/git-bug/commands"
-)
-
-func main() {
-	cwd, _ := os.Getwd()
-	dir := path.Join(cwd, "misc", "fish_completion", "git-bug")
-
-	fmt.Println("Generating Fish completion file ...")
-
-	err := commands.RootCmd.GenFishCompletionFile(dir, true)
-	if err != nil {
-		log.Fatal(err)
-	}
-}

misc/gen_powershell_completion.go 🔗

@@ -1,24 +0,0 @@
-// +build ignore
-
-package main
-
-import (
-	"fmt"
-	"log"
-	"os"
-	"path"
-
-	"github.com/MichaelMure/git-bug/commands"
-)
-
-func main() {
-	cwd, _ := os.Getwd()
-	filepath := path.Join(cwd, "misc", "powershell_completion", "git-bug")
-
-	fmt.Println("Generating PowerShell completion file ...")
-
-	err := commands.RootCmd.GenPowerShellCompletionFile(filepath)
-	if err != nil {
-		log.Fatal(err)
-	}
-}

misc/gen_zsh_completion.go 🔗

@@ -1,24 +0,0 @@
-// +build ignore
-
-package main
-
-import (
-	"fmt"
-	"log"
-	"os"
-	"path"
-
-	"github.com/MichaelMure/git-bug/commands"
-)
-
-func main() {
-	cwd, _ := os.Getwd()
-	filepath := path.Join(cwd, "misc", "zsh_completion", "git-bug")
-
-	fmt.Println("Generating ZSH completion file ...")
-
-	err := commands.RootCmd.GenZshCompletionFile(filepath)
-	if err != nil {
-		log.Fatal(err)
-	}
-}