summaryrefslogtreecommitdiffstats
path: root/pkg/cheatsheet
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2023-09-29 18:42:53 +0200
committerStefan Haller <stefan@haller-berlin.de>2023-09-29 20:38:29 +0200
commit7af371701d44dbde052235522876c1bf75f05c91 (patch)
treeebdebeee596b4202cf50abc0e2d42b10f3bea680 /pkg/cheatsheet
parent5ccc95b76fbaa8d268818159d6ff4d64d23b153c (diff)
Use go:generate for generating cheatsheets
This has several benefits: - it's less code - we're using the same mechanism to generate all our auto-generated files, so if someone wants to add a new one, it's clear which pattern to follow - we can re-generate all generated files with a single command ("go generate ./...", or "make generate") - we only need a single check on CI to check that all files are up to date (see previous commit)
Diffstat (limited to 'pkg/cheatsheet')
-rw-r--r--pkg/cheatsheet/check.go77
-rw-r--r--pkg/cheatsheet/generate.go14
-rw-r--r--pkg/cheatsheet/generator.go14
3 files changed, 22 insertions, 83 deletions
diff --git a/pkg/cheatsheet/check.go b/pkg/cheatsheet/check.go
deleted file mode 100644
index 5c4df97ef..000000000
--- a/pkg/cheatsheet/check.go
+++ /dev/null
@@ -1,77 +0,0 @@
-package cheatsheet
-
-import (
- "fmt"
- "io/fs"
- "log"
- "os"
- "path/filepath"
- "regexp"
-
- "github.com/pmezard/go-difflib/difflib"
-)
-
-func Check() {
- dir := GetKeybindingsDir()
- tmpDir := filepath.Join(os.TempDir(), "lazygit_cheatsheet")
- err := os.RemoveAll(tmpDir)
- if err != nil {
- log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
- }
- err = os.Mkdir(tmpDir, 0o700)
- if err != nil {
- log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
- }
-
- generateAtDir(tmpDir)
- defer os.RemoveAll(tmpDir)
-
- actualContent := obtainContent(dir)
- expectedContent := obtainContent(tmpDir)
-
- if expectedContent == "" {
- log.Fatal("empty expected content")
- }
-
- if actualContent != expectedContent {
- err := difflib.WriteUnifiedDiff(os.Stdout, difflib.UnifiedDiff{
- A: difflib.SplitLines(expectedContent),
- B: difflib.SplitLines(actualContent),
- FromFile: "Expected",
- FromDate: "",
- ToFile: "Actual",
- ToDate: "",
- Context: 1,
- })
- if err != nil {
- log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
- }
- fmt.Printf("\nCheatsheets are out of date. Please run `%s` at the project root and commit the changes. If you run the script and no keybindings files are updated as a result, try rebasing onto master and trying again.\n", CommandToRun())
- os.Exit(1)
- }
-
- fmt.Println("\nCheatsheets are up to date")
-}
-
-func obtainContent(dir string) string {
- re := regexp.MustCompile(`Keybindings_\w+\.md$`)
-
- content := ""
- err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
- if re.MatchString(path) {
- bytes, err := os.ReadFile(path)
- if err != nil {
- log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
- }
- content += fmt.Sprintf("\n%s\n\n", filepath.Base(path))
- content += string(bytes)
- }
-
- return nil
- })
- if err != nil {
- log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
- }
-
- return content
-}
diff --git a/pkg/cheatsheet/generate.go b/pkg/cheatsheet/generate.go
index 184e84a04..392e9d64d 100644
--- a/pkg/cheatsheet/generate.go
+++ b/pkg/cheatsheet/generate.go
@@ -1,10 +1,12 @@
-// This "script" generates a file called Keybindings_{{.LANG}}.md
-// in current working directory.
+//go:generate go run generator.go
+
+// This "script" generates files called Keybindings_{{.LANG}}.md
+// in the docs/keybindings directory.
//
-// The content of this generated file is a keybindings cheatsheet.
+// The content of these generated files is a keybindings cheatsheet.
//
-// To generate cheatsheet in english run:
-// go run scripts/generate_cheatsheet.go
+// To generate the cheatsheets, run:
+// go generate pkg/cheatsheet/generate.go
package cheatsheet
@@ -42,7 +44,7 @@ type headerWithBindings struct {
}
func CommandToRun() string {
- return "go run scripts/cheatsheet/main.go generate"
+ return "go generate ./..."
}
func GetKeybindingsDir() string {
diff --git a/pkg/cheatsheet/generator.go b/pkg/cheatsheet/generator.go
new file mode 100644
index 000000000..1708a28e8
--- /dev/null
+++ b/pkg/cheatsheet/generator.go
@@ -0,0 +1,14 @@
+//go:build ignore
+
+package main
+
+import (
+ "fmt"
+
+ "github.com/jesseduffield/lazygit/pkg/cheatsheet"
+)
+
+func main() {
+ fmt.Printf("Generating cheatsheets in %s...\n", cheatsheet.GetKeybindingsDir())
+ cheatsheet.Generate()
+}