Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code format during build, remove code-batch-process #21471

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -264,7 +264,7 @@ clean:

.PHONY: fmt
fmt:
GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}'
@GO=$(GO) ./build/gitea-fmt.sh -w
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@GO=$(GO) ./build/gitea-fmt.sh -w
@GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) GO=$(GO) ./build/gitea-fmt.sh -w

$(eval TEMPLATES := $(shell find templates -type f -name '*.tmpl'))
@# strip whitespace after '{{' and before `}}` unless there is only whitespace before it
@$(SED_INPLACE) -e 's/{{[ ]\{1,\}/{{/g' -e '/^[ ]\{1,\}}}/! s/[ ]\{1,\}}}/}}/g' $(TEMPLATES)
Expand Down
15 changes: 4 additions & 11 deletions build.go
Expand Up @@ -10,15 +10,8 @@ package main
// These libraries will not be included in a normal compilation.

import (
// for embed
_ "github.com/shurcooL/vfsgen"

// for cover merge
_ "golang.org/x/tools/cover"

// for vet
_ "code.gitea.io/gitea-vet"

// for swagger
_ "github.com/go-swagger/go-swagger/cmd/swagger"
_ "code.gitea.io/gitea-vet" // for vet
_ "github.com/go-swagger/go-swagger/cmd/swagger" // for swagger
_ "github.com/shurcooL/vfsgen" // for embed
_ "golang.org/x/tools/cover" // for cover merge
)
292 changes: 0 additions & 292 deletions build/code-batch-process.go

This file was deleted.

73 changes: 73 additions & 0 deletions build/codeformat.go
@@ -0,0 +1,73 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

//go:build ignore

// Gitea's code formatter:
// * Sort imports with 3 groups: std, gitea, other

package main

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"code.gitea.io/gitea/build/codeformat"
)

func showUsage() {
fmt.Printf("Usage: codeformat {-l|-w} directory\n")
}

var ignoreList = []string{
"_bindata.go",
"tests/gitea-repositories-meta",
"tests/integration/migration-test",
"modules/git/tests",
"models/fixtures",
"models/migrations/fixtures",
"services/gitdiff/testdata",
}

func main() {
if len(os.Args) != 3 {
showUsage()
os.Exit(1)
}
doList := os.Args[1] == "-l"
doWrite := os.Args[1] == "-w"
dir := os.Args[2]
if !doList && !doWrite {
showUsage()
fmt.Printf("You should set either '-l' or '-w'\n")
os.Exit(1)
}

err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
path = strings.ReplaceAll(path, "\\", "/")
for _, ignore := range ignoreList {
if strings.Contains(path, ignore) {
return filepath.SkipDir
}
}
if d.IsDir() {
return nil // walk into
}
if !strings.HasSuffix(path, ".go") {
return nil
}
if err := codeformat.FormatGoImports(path, doList, doWrite); err != nil {
log.Printf("Failed to format go imports: %s, err=%v", path, err)
return err
}
return nil
})
if err != nil {
log.Printf("Failed to format code by walking directory: %s, err=%v", dir, err)
os.Exit(1)
}
}