From cb7e0391a77a863ab63da7d9ab0557579fe44a52 Mon Sep 17 00:00:00 2001 From: wei cheng Date: Thu, 5 Sep 2019 11:42:11 +0800 Subject: [PATCH] add java package lint --- internal/lint/check_go_package_prefix.go | 1 - internal/lint/check_java_package_prefix.go | 67 ++++++++++++++++++++++ internal/lint/lint.go | 1 + release.sh | 2 +- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 internal/lint/check_java_package_prefix.go diff --git a/internal/lint/check_go_package_prefix.go b/internal/lint/check_go_package_prefix.go index 9ae4148d..c007a769 100644 --- a/internal/lint/check_go_package_prefix.go +++ b/internal/lint/check_go_package_prefix.go @@ -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 diff --git a/internal/lint/check_java_package_prefix.go b/internal/lint/check_java_package_prefix.go new file mode 100644 index 00000000..8fbf0f94 --- /dev/null +++ b/internal/lint/check_java_package_prefix.go @@ -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 +} diff --git a/internal/lint/lint.go b/internal/lint/lint.go index 058f4eaa..ce55fcb5 100644 --- a/internal/lint/lint.go +++ b/internal/lint/lint.go @@ -137,6 +137,7 @@ var ( commentsAllLinter, fileOptionsGoPackagePrefixLinter, importsScopeLinter, + fileOptionsJavaPackagePrefixLinter, } // DefaultLinters is the slice of default Linters. diff --git a/release.sh b/release.sh index 4e45d28e..5692e065 100755 --- a/release.sh +++ b/release.sh @@ -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}