Skip to content

Commit

Permalink
add cheatsheet check script
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Jan 4, 2022
1 parent 8e66d27 commit e8a1a4f
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 81 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,25 @@ jobs:
- name: Build darwin binary
run: |
GOOS=darwin go build
check-cheatsheet:
runs-on: ubuntu-latest
env:
GOFLAGS: -mod=vendor
GOARCH: amd64
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.16.x
- name: Cache build
uses: actions/cache@v1
with:
path: ~/.cache/go-build
key: ${{runner.os}}-go-${{hashFiles('**/go.sum')}}-build
restore-keys: |
${{runner.os}}-go-
- name: Check Cheatsheet
run: |
go run scripts/cheatsheet/main.go check
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root.
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root.

# Lazygit Keybindings

Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_nl.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root.
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root.

# Lazygit Sneltoetsen

Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_pl.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root.
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root.

# Lazygit Keybindings

Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_zh.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root.
# This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root.

# Lazygit 按键绑定

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/mgutz/str v1.2.0
github.com/onsi/ginkgo v1.10.3 // indirect
github.com/onsi/gomega v1.7.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sahilm/fuzzy v0.1.0
github.com/sirupsen/logrus v1.4.2
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad
Expand Down
79 changes: 79 additions & 0 deletions pkg/cheatsheet/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cheatsheet

import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"

"github.com/pmezard/go-difflib/difflib"
)

func Check() {
dir := GetDir()
tmpDir := filepath.Join(os.TempDir(), "lazygit_cheatsheet")
err := os.RemoveAll(tmpDir)
if err != nil {
log.Fatalf("Error occured while checking if cheatsheets are up to date: %v", err)
}
err = os.Mkdir(tmpDir, 0700)
if err != nil {
log.Fatalf("Error occured 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 occured 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\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 := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Error occured 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 occured while checking if cheatsheets are up to date: %v", err)
}

return content
}
35 changes: 21 additions & 14 deletions scripts/generate_cheatsheet.go → pkg/cheatsheet/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,63 @@
// The content of this generated file is a keybindings cheatsheet.
//
// To generate cheatsheet in english run:
// LANG=en go run scripts/generate_cheatsheet.go
// go run scripts/generate_cheatsheet.go

package main
package cheatsheet

import (
"fmt"
"log"
"os"
"sort"
"strings"

"github.com/jesseduffield/lazygit/pkg/app"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/integration"
)

type bindingSection struct {
title string
bindings []*gui.Binding
}

func main() {
func CommandToRun() string {
return "go run scripts/cheatsheet/main.go generate"
}

func GetDir() string {
return integration.GetRootDirectory() + "/docs/keybindings"
}

func generateAtDir(cheatsheetDir string) {
os.Setenv("LANG", "en")

translationSetsByLang := i18n.GetTranslationSets()
mConfig := config.NewDummyAppConfig()

for lang := range translationSetsByLang {
os.Setenv("LC_ALL", lang)
mApp, _ := app.NewApp(mConfig, "")
file, err := os.Create(getProjectRoot() + "/docs/keybindings/Keybindings_" + lang + ".md")
path := cheatsheetDir + "/Keybindings_" + lang + ".md"
file, err := os.Create(path)
if err != nil {
panic(err)
}

bindingSections := getBindingSections(mApp)
content := formatSections(mApp.Tr, bindingSections)
content = fmt.Sprintf("# This file is auto-generated. To update, make the changes in the "+
"pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root.\n\n%s", content)
"pkg/i18n directory and then run `%s` from the project root.\n\n%s", CommandToRun(), content)
writeString(file, content)
}
}

func Generate() {
generateAtDir(GetDir())
}

func writeString(file *os.File, str string) {
_, err := file.WriteString(str)
if err != nil {
Expand Down Expand Up @@ -248,11 +263,3 @@ func formatSections(tr *i18n.TranslationSet, bindingSections []*bindingSection)

return content
}

func getProjectRoot() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
return strings.Split(dir, "lazygit")[0] + "lazygit"
}
27 changes: 27 additions & 0 deletions scripts/cheatsheet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"log"
"os"

"github.com/jesseduffield/lazygit/pkg/cheatsheet"
)

func main() {
if len(os.Args) < 2 {
log.Fatal("Please provide a command: one of 'generate', 'check'")
}

command := os.Args[1]

switch command {
case "generate":
cheatsheet.Generate()
fmt.Printf("\nGenerated cheatsheets in %s\n", cheatsheet.GetDir())
case "check":
cheatsheet.Check()
default:
log.Fatal("\nUnknown command. Expected one of 'generate', 'check'")
}
}
63 changes: 0 additions & 63 deletions scripts/push_new_patch/main.go

This file was deleted.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ github.com/mitchellh/go-homedir
# github.com/onsi/gomega v1.7.1
## explicit
# github.com/pmezard/go-difflib v1.0.0
## explicit
github.com/pmezard/go-difflib/difflib
# github.com/rivo/uniseg v0.2.0
github.com/rivo/uniseg
Expand Down

0 comments on commit e8a1a4f

Please sign in to comment.