14 Star 126 Fork 35

GVP京东开源 / sbom-tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ruby_preprocessor.go 2.52 KB
一键复制 编辑 原始数据 按行查看 历史
jdtdevops 提交于 2024-01-22 15:42 . fix: typo
// Copyright (c) 2023 Jingdong Technology Information Technology Co., Ltd.
// SBOM-TOOL is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package ruby
import (
"bytes"
"io"
"regexp"
"strings"
"gitee.com/JD-opensource/sbom-tool/pkg/fingerprint/preprocessor"
"gitee.com/JD-opensource/sbom-tool/pkg/util/pattern_set"
)
var (
remove_block_comment_re *regexp.Regexp
remove_out_line_comment_re *regexp.Regexp
remove_in_line_comment_re *regexp.Regexp
prefixSet *pattern_set.PatternSet
)
type Preprocessor struct{}
func NewRubyPreprocessor() preprocessor.PreProcessor {
return &Preprocessor{}
}
func (g Preprocessor) Name() string {
return "ruby"
}
func (g Preprocessor) ProcessContent(content string) string {
processFns := []func(content string) string{
removeComments,
removeCommonKeywordLines,
}
code := content
for _, processFn := range processFns {
code = processFn(code)
}
return code
}
func (g Preprocessor) SupportedFileTypes() []string {
return []string{".rb"}
}
func removeComments(content string) string {
content = remove_block_comment_re.ReplaceAllString(content, "")
content = remove_out_line_comment_re.ReplaceAllString(content, "")
content = remove_in_line_comment_re.ReplaceAllString(content, "")
return content
}
func removeCommonKeywordLines(content string) string {
buf := bytes.NewBuffer([]byte(content))
var sb strings.Builder
for {
line, err := buf.ReadString('\n')
if err == io.EOF && line == "" {
break
}
if err != nil && err != io.EOF {
break
}
trimmed := strings.TrimSpace(line)
if trimmed == "" {
continue
}
if prefixSet.Match(trimmed) {
continue
}
sb.WriteString(trimmed)
sb.WriteByte('\n')
if err == io.EOF {
break
}
}
return strings.TrimRight(sb.String(), "\n")
}
func init() {
remove_block_comment_re = regexp.MustCompile(`(?s)(=begin.*=end)`)
remove_out_line_comment_re = regexp.MustCompile(`(?m)^\s*#.*$`)
remove_in_line_comment_re = regexp.MustCompile(`(?m)#.*$`)
prefixSet = pattern_set.NewPrefixPatternMatchSet(
"}",
"{",
"require",
"end",
"begin",
"next",
"redo",
"rescue",
"retry",
)
}
Go
1
https://gitee.com/JD-opensource/sbom-tool.git
git@gitee.com:JD-opensource/sbom-tool.git
JD-opensource
sbom-tool
sbom-tool
v0.1.0

搜索帮助