Skip to content

Commit

Permalink
add java package lint
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwei7 committed Sep 5, 2019
1 parent 8736015 commit cb7e039
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
1 change: 0 additions & 1 deletion internal/lint/check_go_package_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (v *fileOptionsGoPackagePrefixVisitor) VisitOption(element *proto.Option) {

func (v *fileOptionsGoPackagePrefixVisitor) Finally() error {
if v.option == nil {
v.AddFailuref(v.option.Position, `Option "go_package not exists"`)
return nil
}
value := v.option.Constant.Source
Expand Down
67 changes: 67 additions & 0 deletions internal/lint/check_java_package_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package lint

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

"github.com/emicklei/proto"
"github.com/uber/prototool/internal/text"
)

var defaultJavaPackagePrefix = "com.bilibili.bapis"

var fileOptionsJavaPackagePrefixLinter = NewLinter(
"FILE_OPTIONS_JAVA_PACKAGE_PREFIX",
fmt.Sprintf(`Verifies that the file option "go_package" has prefix "%s", prefix value can set by environment PROTO_JAVA_PACKAGE_PREFIX`, defaultJavaPackagePrefix),
checkFileOptionsJavePackagePrefix,
)

func checkFileOptionsJavePackagePrefix(add func(*text.Failure), dirPath string, descriptors []*FileDescriptor) error {
return runVisitor(&fileOptionsJavaPackagePrefixVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
}

type fileOptionsJavaPackagePrefixVisitor struct {
baseAddVisitor

option *proto.Option
fileName string
}

func (v *fileOptionsJavaPackagePrefixVisitor) OnStart(descriptor *FileDescriptor) error {
v.fileName = descriptor.Filename
v.option = nil
return nil
}

func (v *fileOptionsJavaPackagePrefixVisitor) VisitOption(element *proto.Option) {
if element.Name == "java_package" {
v.option = element
}
}

func (v *fileOptionsJavaPackagePrefixVisitor) Finally() error {
if v.option == nil {
return nil
}
value := v.option.Constant.Source
prefix := defaultJavaPackagePrefix
if v := os.Getenv("PROTO_JAVA_PACKAGE_PREFIX"); v != "" {
prefix = v
}
ignoredDirs := os.Getenv("PROTO_JAVA_PACKAGE_PREFIX_IGNORED")
if ignoredDirs == "" {
ignoredDirs = "bilibili,extension,third_party"
}
for _, ignored := range strings.Split(ignoredDirs, ",") {
if ignored == strings.Split(v.fileName, "/")[0] {
return nil
}
}
expect_package := prefix + "." + filepath.Dir(v.fileName)
if expect_package != value {
v.AddFailuref(v.option.Position, `Expect option "java_package" as: "%s" actual: "%s"`, expect_package, value)
}
return nil
}
1 change: 1 addition & 0 deletions internal/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ var (
commentsAllLinter,
fileOptionsGoPackagePrefixLinter,
importsScopeLinter,
fileOptionsJavaPackagePrefixLinter,
}

// DefaultLinters is the slice of default Linters.
Expand Down
2 changes: 1 addition & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -ex
GOOS=windows go build -o prototool-windows-amd64.exe ./cmd/prototool
GOOS=linux go build -o prototool-linux-amd64 ./cmd/prototool
GOOS=darwin go build -o prototool-darwin-amd64 cmd/prototool
GOOS=darwin go build -o prototool-darwin-amd64 ./cmd/prototool
for f in $(ls | grep prototool-)
do
tar -Jcf ${f}.tar.xz ${f}
Expand Down

0 comments on commit cb7e039

Please sign in to comment.