feat: add standardizer optimize makefile

pull/2159/head
Xinwei Xiong (cubxxw) 2 years ago
parent 48df76fb8b
commit 727850c85c

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# https://github.com/marketplace/actions/code-language-detector
directory: ./
file_types:
- .go

@ -0,0 +1,50 @@
# https://github.com/marketplace/actions/conformity-checker-for-project
baseConfig:
searchDirectory: "./"
ignoreCase: false
directoryNaming:
allowHyphens: true
allowUnderscores: false
mustBeLowercase: true
fileNaming:
allowHyphens: true
allowUnderscores: true
mustBeLowercase: true
ignoreFormats:
- "\\.log$"
- "\\.env$"
- "README\\.md$"
- "_test\\.go$"
- "\\.md$"
- _test\\.txt$
- LICENSE
- Dockerfile
- CODEOWNERS
- Makefile
ignoreDirectories:
- "vendor"
- ".git"
- "deployments"
- "node_modules"
- "logs"
- "CHANGELOG"
- "components"
- "_output"
- "tools/openim-web"
- "CHANGELOG"
- "examples/Test_directory"
- test/testdata
fileTypeSpecificNaming:
".yaml":
allowHyphens: true
allowUnderscores: false
mustBeLowercase: true
".go":
allowHyphens: false
allowUnderscores: true
mustBeLowercase: true

@ -73,6 +73,9 @@ jobs:
- name: Code Typecheck Detector
uses: kubecub/typecheck@main
- name: Conformity Checker for Project
uses: kubecub/standardizer@main
- name: Module Operations
run: |
sudo make tidy

@ -52,7 +52,7 @@
</p>
## 🟢 扫描微信进群交流
<img src="./docs/images/Wechat.jpg" width="300">
<img src="./docs/images/wechat.jpg" width="300">
## Ⓜ️ 关于 OpenIM

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Before

Width:  |  Height:  |  Size: 207 KiB

After

Width:  |  Height:  |  Size: 207 KiB

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 118 KiB

@ -4,7 +4,6 @@ use (
.
./tools/changelog
./tools/component
./tools/formitychecker
./tools/imctl
./tools/infra
./tools/ncpu

@ -314,9 +314,6 @@ openim::golang::setup_platforms
readonly OPENIM_CLIENT_TARGETS=(
changelog
component
conversion-msg
conversion-mysql
formitychecker
imctl
infra
ncpu

@ -243,7 +243,7 @@ function openim::release::package_client_tarballs() {
local client_bins=("${OPENIM_CLIENT_BINARIES[@]}")
# client_bins: changelog component conversion-msg conversion-mysql formitychecker imctl infra ncpu openim-web up35 versionchecker yamlfmt
# client_bins: changelog component imctl infra ncpu openim-web up35 versionchecker yamlfmt
# Copy client binclient_bins:aries
openim::log::info " Copy client binaries: ${client_bins[@]/#/${LOCAL_OUTPUT_BINTOOLSPATH}/${platform}/}"
openim::log::info " Copy client binaries to: ${release_stage}/client/bin"

@ -0,0 +1,17 @@
# Copyright © 2023 OpenIMSDK.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# OpenIM Makefile Versions used
#

@ -78,6 +78,13 @@ VERSION := $(shell git describe --tags --always --match='v*')
# v2.3.3: git tag
endif
# Helper function to get dependency version from go.mod
get_gomod_version = $(shell go list -m $1 | awk '{print $$2}')
define go_install
$(info ===========> Installing $(1)@$(2))
$(GO) install $(1)@$(2)
endef
# Check if the tree is dirty. default to dirty(maybe u should commit?)
GIT_TREE_STATE:="dirty"
ifeq (, $(shell git status --porcelain 2>/dev/null))

@ -238,6 +238,7 @@ install.rts:
@$(GO) install github.com/galeone/rts/cmd/rts@latest
# ================= kubecub openim tools =========================================
# https://github.com/kubecub
## install.typecheck: install kubecub typecheck check for go code
.PHONY: install.typecheck
install.typecheck:
@ -248,6 +249,11 @@ install.typecheck:
install.comment-lang-detector:
@$(GO) install github.com/kubecub/comment-lang-detector/cmd/cld@latest
## install.standardizer: install kubecub standardizer check for go code standardize
.PHONY: install.standardizer
install.standardizer:
@$(GO) install github.com/kubecub/standardizer@latest
## tools.help: Display help information about the tools package
.PHONY: tools.help
tools.help: scripts/make-rules/tools.mk

@ -0,0 +1,33 @@
#!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script does a fast type check of script srnetes code for all platforms.
# Usage: `scripts/verify-standardizer.sh`.
OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${OPENIM_ROOT}/scripts/lib/init.sh"
openim::golang::verify_go_version
cd "${OPENIM_ROOT}"
ret=0
scripts/run-in-gopath.sh \
make tools.verify.standardizer
${OPENIM_ROOT}/_output/tools/standardizer || ret=$?
if [[ $ret -ne 0 ]]; then
openim::log::error "Failed to check the directory name or file name. Your name may not meet the specification. Please check the configuration file and the directory or file name." >&2
openim::log::error "Please see https://github.com/kubecub/standardizer for more information." >&2
exit 1
fi

@ -1,102 +0,0 @@
# Development of a Go-Based Conformity Checker for Project File and Directory Naming Standards
### 1. Project Overview
#### Project Name
- `GoConformityChecker`
#### Functionality Description
- Checks if the file and subdirectory names in a specified directory adhere to specific naming conventions.
- Supports specific file types (e.g., `.go`, `.yml`, `.yaml`, `.md`, `.sh`, etc.).
- Allows users to specify directories to be checked and directories to be ignored.
- More read https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/code-conventions.md
#### Naming Conventions
- Go files: Only underscores are allowed.
- YAML, YML, and Markdown files: Only hyphens are allowed.
- Directories: Only underscores are allowed.
### 2. File Structure
- `main.go`: Entry point of the program, handles command-line arguments.
- `checker/checker.go`: Contains the core logic.
- `config/config.go`: Parses and stores configuration information.
### 3. Core Code Design
#### main.go
- Parses command-line arguments, including the directory to be checked and directories to be ignored.
- Calls the `checker` module for checking.
#### config.go
- Defines a configuration structure, such as directories to check and ignore.
#### checker.go
- Iterates through the specified directory.
- Applies different naming rules based on file types and directory names.
- Records files or directories that do not conform to the standards.
### 4. Pseudocode Example
#### main.go
```go
package main
import (
"flag"
"fmt"
"GoConformityChecker/checker"
)
func main() {
// Parse command-line arguments
var targetDir string
var ignoreDirs string
flag.StringVar(&targetDir, "target", ".", "Directory to check")
flag.StringVar(&ignoreDirs, "ignore", "", "Directories to ignore")
flag.Parse()
// Call the checker
err := checker.CheckDirectory(targetDir, ignoreDirs)
if err != nil {
fmt.Println("Error:", err)
}
}
```
#### checker.go
```go
package checker
import (
// Import necessary packages
)
func CheckDirectory(targetDir, ignoreDirs string) error {
// Iterate through the directory, applying rules to check file and directory names
// Return any found errors or non-conformities
return nil
}
```
### 5. Implementation Details
- **File and Directory Traversal**: Use Go's `path/filepath` package to traverse directories and subdirectories.
- **Naming Rules Checking**: Apply different regex expressions for naming checks based on file extensions.
- **Error Handling and Reporting**: Record files or directories that do not conform and report to the user.
### 6. Future Development and Extensions
- Support more file types and naming rules.
- Provide more detailed error reports, such as showing line numbers and specific naming mistakes.
- Add a graphical or web interface for non-command-line users.
The above is an overview of the entire project's design. Following this design, specific coding implementation can begin. Note that the actual implementation may need adjustments based on real-world conditions.

@ -1,113 +0,0 @@
// Copyright © 2024 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package checker
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/OpenIMSDK/tools/errs"
"github.com/openimsdk/open-im-server/tools/formitychecker/config"
)
var (
underscoreRegex = regexp.MustCompile(`^[a-zA-Z0-9_]+\.[a-zA-Z0-9]+$`)
hyphenRegex = regexp.MustCompile(`^[a-zA-Z0-9\-]+\.[a-zA-Z0-9]+$`)
)
// CheckDirectory initiates the checking process for the specified directories using configuration from config.Config.
func CheckDirectory(cfg *config.Config) error {
ignoreMap := make(map[string]struct{})
for _, dir := range cfg.IgnoreDirs {
ignoreMap[dir] = struct{}{}
}
for _, targetDir := range cfg.TargetDirs {
err := filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errs.Wrap(err, fmt.Sprintf("error walking directory '%s'", targetDir))
}
// Skip if the directory is in the ignore list
dirName := filepath.Base(filepath.Dir(path))
if _, ok := ignoreMap[dirName]; ok && info.IsDir() {
return filepath.SkipDir
}
// Check the naming convention
if err := checkNamingConvention(path, info); err != nil {
fmt.Println(err)
}
return nil
})
if err != nil {
return fmt.Errorf("error checking directory '%s': %w", targetDir, err)
}
}
return nil
}
// checkNamingConvention checks if the file or directory name conforms to the standard naming conventions.
func checkNamingConvention(path string, info os.FileInfo) error {
fileName := info.Name()
// Handle special cases for directories like .git
if info.IsDir() && strings.HasPrefix(fileName, ".") {
return nil // Skip special directories
}
// Extract the main part of the name (without extension for files)
mainName := fileName
if !info.IsDir() {
mainName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
}
// Determine the type of file and apply corresponding naming rule
switch {
case info.IsDir():
if !isValidName(mainName, "_") { // Directory names must only contain underscores
return fmt.Errorf("!!! invalid directory name: %s", path)
}
case strings.HasSuffix(fileName, ".go"):
if !isValidName(mainName, "_") { // Go files must only contain underscores
return fmt.Errorf("!!! invalid Go file name: %s", path)
}
case strings.HasSuffix(fileName, ".yml"), strings.HasSuffix(fileName, ".yaml"), strings.HasSuffix(fileName, ".md"):
if !isValidName(mainName, "-") { // YML, YAML, and Markdown files must only contain hyphens
return fmt.Errorf("!!! invalid file name: %s", path)
}
}
return nil
}
// isValidName checks if the file name conforms to the specified rule (underscore or hyphen).
func isValidName(name, charType string) bool {
switch charType {
case "_":
return underscoreRegex.MatchString(name)
case "-":
return hyphenRegex.MatchString(name)
default:
return false
}
}

@ -1,41 +0,0 @@
// Copyright © 2024 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"strings"
)
// Config holds all the configuration parameters for the checker.
type Config struct {
TargetDirs []string // Directories to check
IgnoreDirs []string // Directories to ignore
}
// NewConfig creates and returns a new Config instance.
func NewConfig(targetDirs, ignoreDirs string) *Config {
return &Config{
TargetDirs: parseDirs(targetDirs),
IgnoreDirs: parseDirs(ignoreDirs),
}
}
// parseDirs splits a comma-separated string into a slice of directory names.
func parseDirs(dirs string) []string {
if dirs == "" {
return nil
}
return strings.Split(dirs, ",")
}

@ -1,41 +0,0 @@
// Copyright © 2024 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"github.com/openimsdk/open-im-server/tools/formitychecker/checker"
"github.com/openimsdk/open-im-server/tools/formitychecker/config"
)
func main() {
defaultTargetDirs := "."
defaultIgnoreDirs := "components,.git"
var targetDirs string
var ignoreDirs string
flag.StringVar(&targetDirs, "target", defaultTargetDirs, "Directories to check (default: current directory)")
flag.StringVar(&ignoreDirs, "ignore", defaultIgnoreDirs, "Directories to ignore (default: A/, B/)")
flag.Parse()
conf := config.NewConfig(targetDirs, ignoreDirs)
err := checker.CheckDirectory(conf)
if err != nil {
fmt.Println("Error:", err)
}
}

@ -1,3 +0,0 @@
module github.com/openimsdk/open-im-server/tools/formitychecker
go 1.19
Loading…
Cancel
Save