diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 066322b58..aa6bfb1e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/docs/keybindings/Keybindings_en.md b/docs/keybindings/Keybindings_en.md index 302f5a82f..ac25c4ca8 100644 --- a/docs/keybindings/Keybindings_en.md +++ b/docs/keybindings/Keybindings_en.md @@ -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 diff --git a/docs/keybindings/Keybindings_nl.md b/docs/keybindings/Keybindings_nl.md index 4dcc1c586..64db119fd 100644 --- a/docs/keybindings/Keybindings_nl.md +++ b/docs/keybindings/Keybindings_nl.md @@ -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 diff --git a/docs/keybindings/Keybindings_pl.md b/docs/keybindings/Keybindings_pl.md index 6191d1516..c394e800d 100644 --- a/docs/keybindings/Keybindings_pl.md +++ b/docs/keybindings/Keybindings_pl.md @@ -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 diff --git a/docs/keybindings/Keybindings_zh.md b/docs/keybindings/Keybindings_zh.md index d0b297df3..13de0853b 100644 --- a/docs/keybindings/Keybindings_zh.md +++ b/docs/keybindings/Keybindings_zh.md @@ -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 按键绑定 diff --git a/go.mod b/go.mod index 7f7c8a369..1eecf4df6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/cheatsheet/check.go b/pkg/cheatsheet/check.go new file mode 100644 index 000000000..03f65d910 --- /dev/null +++ b/pkg/cheatsheet/check.go @@ -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 +} diff --git a/scripts/generate_cheatsheet.go b/pkg/cheatsheet/generate.go similarity index 92% rename from scripts/generate_cheatsheet.go rename to pkg/cheatsheet/generate.go index fc188b6f9..513049f23 100644 --- a/scripts/generate_cheatsheet.go +++ b/pkg/cheatsheet/generate.go @@ -4,21 +4,21 @@ // 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 { @@ -26,14 +26,25 @@ type bindingSection struct { 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) } @@ -41,11 +52,15 @@ func main() { 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 { @@ -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" -} diff --git a/scripts/cheatsheet/main.go b/scripts/cheatsheet/main.go new file mode 100644 index 000000000..bccf3324a --- /dev/null +++ b/scripts/cheatsheet/main.go @@ -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'") + } +} diff --git a/scripts/push_new_patch/main.go b/scripts/push_new_patch/main.go deleted file mode 100755 index a26f2b676..000000000 --- a/scripts/push_new_patch/main.go +++ /dev/null @@ -1,63 +0,0 @@ -// call from project root with -// go run scripts/push_new_patch/main.go - -// goreleaser expects a $GITHUB_TOKEN env variable to be defined -// in order to push the release got github - -package main - -import ( - "fmt" - "io/ioutil" - "log" - "os" - "strconv" - "strings" - - "github.com/jesseduffield/lazygit/pkg/secureexec" -) - -func main() { - version, err := ioutil.ReadFile("VERSION") - if err != nil { - log.Panicln(err.Error()) - } - stringVersion := string(version) - fmt.Println("VERSION was " + stringVersion) - - runCommand("git", "pull") - - splitVersion := strings.Split(stringVersion, ".") - patch := splitVersion[len(splitVersion)-1] - newPatch, _ := strconv.Atoi(patch) - splitVersion[len(splitVersion)-1] = strconv.FormatInt(int64(newPatch)+1, 10) - newVersion := strings.Join(splitVersion, ".") - - err = ioutil.WriteFile("VERSION", []byte(newVersion), 0644) - if err != nil { - log.Panicln(err.Error()) - } - - runCommand("git", "add", "VERSION") - runCommand("git", "commit", "-m", "bump version to "+newVersion, "--", "VERSION") - runCommand("git", "push") - runCommand("git", "tag", newVersion) - runCommand("git", "push", "origin", newVersion) - runCommand("goreleaser", "--rm-dist") - runCommand("rm", "-rf", "dist") -} - -func runCommand(args ...string) { - fmt.Println(strings.Join(args, " ")) - cmd := secureexec.Command(args[0], args[1:]...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Start() - if err != nil { - panic(err.Error()) - } - err = cmd.Wait() - if err != nil { - panic(err.Error()) - } -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 47c766c71..c2ad24796 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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