From 805c48bf973ca1fdd902d90403bbc927a3e6cefe Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Sat, 19 Mar 2022 09:42:08 +0800 Subject: [PATCH 01/12] config: support config license header comment style. --- commands/header_fix.go | 2 +- pkg/comments/config.go | 5 +++++ pkg/config/config.go | 6 ++++-- pkg/header/fix.go | 37 +++++++++++++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/commands/header_fix.go b/commands/header_fix.go index 3b0b828..54848c3 100644 --- a/commands/header_fix.go +++ b/commands/header_fix.go @@ -45,7 +45,7 @@ var FixCommand = &cobra.Command{ } for _, file := range files { - if err := header.Fix(file, &Config.Header, &result); err != nil { + if err := header.Fix(file, &Config.Header, Config.Languages, &result); err != nil { errors = append(errors, err.Error()) } } diff --git a/pkg/comments/config.go b/pkg/comments/config.go index 8639b3e..5dc471a 100644 --- a/pkg/comments/config.go +++ b/pkg/comments/config.go @@ -114,3 +114,8 @@ func FileCommentStyle(filename string) *CommentStyle { } return nil } + +func FileCommentStyleById(styleId string) *CommentStyle { + result := comments[styleId] + return &result +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 1d552a7..bc7f6df 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,6 +18,7 @@ package config import ( + "github.com/apache/skywalking-eyes/pkg/comments" "os" "github.com/apache/skywalking-eyes/assets" @@ -29,8 +30,9 @@ import ( ) type Config struct { - Header header.ConfigHeader `yaml:"header"` - Deps deps.ConfigDeps `yaml:"dependency"` + Header header.ConfigHeader `yaml:"header"` + Deps deps.ConfigDeps `yaml:"dependency"` + Languages map[string]comments.Language `yaml:"language"` } // Parse reads and parses the header check configurations in config file. diff --git a/pkg/header/fix.go b/pkg/header/fix.go index 55936b1..6dfeee5 100644 --- a/pkg/header/fix.go +++ b/pkg/header/fix.go @@ -29,14 +29,14 @@ import ( ) // Fix adds the configured license header to the given file. -func Fix(file string, config *ConfigHeader, result *Result) error { +func Fix(file string, config *ConfigHeader, languages map[string]comments.Language, result *Result) error { var r Result if err := CheckFile(file, config, &r); err != nil || !r.HasFailure() { logger.Log.Warnln("Try to fix a valid file, do nothing:", file) return err } - style := comments.FileCommentStyle(file) + style := getCommentStyle(file, languages) if style == nil { return fmt.Errorf("unsupported file: %v", file) @@ -45,6 +45,39 @@ func Fix(file string, config *ConfigHeader, result *Result) error { return InsertComment(file, style, config, result) } +func getCommentStyle(filename string, languages map[string]comments.Language) *comments.CommentStyle { + result := comments.FileCommentStyle(filename) + configCommentStyles := configCommentStyle(languages) + for extension, styleId := range configCommentStyles { + if strings.HasSuffix(filename, extension) { + result = comments.FileCommentStyleById(styleId) + } + } + return result +} + +func configCommentStyle(languages map[string]comments.Language) map[string]string { + result := make(map[string]string) + if len(languages) == 0 { + return result + } + for _, lang := range languages { + for _, extension := range lang.Extensions { + if lang.CommentStyleID == "" { + continue + } + result[extension] = lang.CommentStyleID + } + for _, filename := range lang.Filenames { + if lang.CommentStyleID == "" { + continue + } + result[filename] = lang.CommentStyleID + } + } + return result +} + func InsertComment(file string, style *comments.CommentStyle, config *ConfigHeader, result *Result) error { stat, err := os.Stat(file) if err != nil { From d7b293d3874f9fe265fe7d4339315979d3a24373 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Sat, 19 Mar 2022 09:42:59 +0800 Subject: [PATCH 02/12] config: add unit test for support config license header comment style. --- .licenserc.yaml | 43 +++++++++++ pkg/comments/config_test.go | 9 +++ pkg/config/.licenserc_test.yaml | 128 ++++++++++++++++++++++++++++++++ pkg/config/config_test.go | 28 +++++++ 4 files changed, 208 insertions(+) create mode 100644 pkg/config/.licenserc_test.yaml create mode 100644 pkg/config/config_test.go diff --git a/.licenserc.yaml b/.licenserc.yaml index e93e885..b28a0d6 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -83,3 +83,46 @@ header: # `header` section is configurations for source codes license header. dependency: files: - go.mod + +language: + Go: + type: programming + color: "#00ADD8" + aliases: + - golang + extensions: + - ".go" + tm_scope: source.go + ace_mode: golang + codemirror_mode: go + codemirror_mime_type: text/x-go + language_id: 132 + comment_style_id: SlashAsterisk + YAML: + type: data + color: "#cb171e" + tm_scope: source.yaml + aliases: + - yml + extensions: + - ".yml" + - ".mir" + - ".reek" + - ".rviz" + - ".sublime-syntax" + - ".syntax" + - ".yaml" + - ".yaml-tmlanguage" + - ".yaml.sed" + - ".yml.mysql" + filenames: + - ".clang-format" + - ".clang-tidy" + - ".gemrc" + - glide.lock + - yarn.lock + ace_mode: yaml + codemirror_mode: yaml + codemirror_mime_type: text/x-yaml + language_id: 407 + comment_style_id: Hashtag diff --git a/pkg/comments/config_test.go b/pkg/comments/config_test.go index 9bfea88..10455ca 100644 --- a/pkg/comments/config_test.go +++ b/pkg/comments/config_test.go @@ -63,3 +63,12 @@ func TestCommentStyle(t *testing.T) { }) } } + +func TestFileCommentStyleById(t *testing.T) { + styleId := "SlashAsterisk" + style := FileCommentStyleById(styleId) + if len(style.ID) == 0 { + t.Logf("Comment style not exist, ID = %v", styleId) + t.Fail() + } +} diff --git a/pkg/config/.licenserc_test.yaml b/pkg/config/.licenserc_test.yaml new file mode 100644 index 0000000..b28a0d6 --- /dev/null +++ b/pkg/config/.licenserc_test.yaml @@ -0,0 +1,128 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +header: # `header` section is configurations for source codes license header. + license: + spdx-id: Apache-2.0 # the spdx id of the license, it's convenient when your license is standard SPDX license. + copyright-owner: Apache Software Foundation # the copyright owner to replace the [owner] in the `spdx-id` template. + content: | # `license` will be used as the content when `fix` command needs to insert a license header. + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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. + + # `pattern` is optional regexp if all the file headers are the same as `license` or the license of `spdx-id` and `copyright-owner`. + pattern: | + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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. + + paths: # `paths` are the path list that will be checked (and fixed) by license-eye, default is ['**']. + - '**' + + paths-ignore: # `paths-ignore` are the path list that will be ignored by license-eye. + - 'dist' + - 'licenses' + - '**/*.md' + - '**/testdata/**' + - '**/go.mod' + - '**/go.sum' + - 'LICENSE' + - 'NOTICE' + - '**/assets/header-templates/**' + - '**/assets/lcs-templates/**' + - '**/assets/languages.yaml' + - '**/assets/assets.gen.go' + - 'docs/**.svg' + + comment: on-failure # on what condition license-eye will comment on the pull request, `on-failure`, `always`, `never`. + + # license-location-threshold specifies the index threshold where the license header can be located, + # after all, a "header" cannot be TOO far from the file start. + license-location-threshold: 80 + +dependency: + files: + - go.mod + +language: + Go: + type: programming + color: "#00ADD8" + aliases: + - golang + extensions: + - ".go" + tm_scope: source.go + ace_mode: golang + codemirror_mode: go + codemirror_mime_type: text/x-go + language_id: 132 + comment_style_id: SlashAsterisk + YAML: + type: data + color: "#cb171e" + tm_scope: source.yaml + aliases: + - yml + extensions: + - ".yml" + - ".mir" + - ".reek" + - ".rviz" + - ".sublime-syntax" + - ".syntax" + - ".yaml" + - ".yaml-tmlanguage" + - ".yaml.sed" + - ".yml.mysql" + filenames: + - ".clang-format" + - ".clang-tidy" + - ".gemrc" + - glide.lock + - yarn.lock + ace_mode: yaml + codemirror_mode: yaml + codemirror_mime_type: text/x-yaml + language_id: 407 + comment_style_id: Hashtag diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 0000000..da995a2 --- /dev/null +++ b/pkg/config/config_test.go @@ -0,0 +1,28 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 "testing" + +func TestParseConfig(t *testing.T) { + config := &Config{ + + } + config.Parse(".licenserc_test.yaml") + t.Logf("Config header license = %v", config.Header.License) +} From 1374ea27789c22f8a3b9ba2f0afae49e711d4439 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Sat, 19 Mar 2022 11:01:59 +0800 Subject: [PATCH 03/12] config: add config parse unit test. --- pkg/config/config_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index da995a2..66d4dc2 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -23,6 +23,17 @@ func TestParseConfig(t *testing.T) { config := &Config{ } - config.Parse(".licenserc_test.yaml") - t.Logf("Config header license = %v", config.Header.License) + configFile := ".licenserc_test.yaml" + config.Parse(configFile) + if len(config.Header.GetLicenseContent()) == 0 { + t.Fail() + } + t.Logf("Parse config dependency files size = %v", len(config.Deps.Files)) + if len(config.Deps.Files) == 0 { + t.Fail() + } + t.Logf("Parse config languages size = %v", len(config.Languages)) + if len(config.Languages) == 0 { + t.Fail() + } } From ac4c5c697e777efab58f5b2d2c83bbae18227cf7 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Sat, 19 Mar 2022 11:03:53 +0800 Subject: [PATCH 04/12] config: add config example for language comment style. --- .licenserc.yaml | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/.licenserc.yaml b/.licenserc.yaml index b28a0d6..17e7dd9 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -98,31 +98,3 @@ language: codemirror_mime_type: text/x-go language_id: 132 comment_style_id: SlashAsterisk - YAML: - type: data - color: "#cb171e" - tm_scope: source.yaml - aliases: - - yml - extensions: - - ".yml" - - ".mir" - - ".reek" - - ".rviz" - - ".sublime-syntax" - - ".syntax" - - ".yaml" - - ".yaml-tmlanguage" - - ".yaml.sed" - - ".yml.mysql" - filenames: - - ".clang-format" - - ".clang-tidy" - - ".gemrc" - - glide.lock - - yarn.lock - ace_mode: yaml - codemirror_mode: yaml - codemirror_mime_type: text/x-yaml - language_id: 407 - comment_style_id: Hashtag From 44c1b5fef83fc5ba911fc05e49e75f18274fbffc Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Sat, 19 Mar 2022 11:25:20 +0800 Subject: [PATCH 05/12] Fixes code style check problem. --- pkg/comments/config.go | 2 +- pkg/comments/config_test.go | 2 +- pkg/header/fix.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/comments/config.go b/pkg/comments/config.go index 5dc471a..86f6608 100644 --- a/pkg/comments/config.go +++ b/pkg/comments/config.go @@ -115,7 +115,7 @@ func FileCommentStyle(filename string) *CommentStyle { return nil } -func FileCommentStyleById(styleId string) *CommentStyle { +func FileCommentStyleByID(styleId string) *CommentStyle { result := comments[styleId] return &result } diff --git a/pkg/comments/config_test.go b/pkg/comments/config_test.go index 10455ca..6da998f 100644 --- a/pkg/comments/config_test.go +++ b/pkg/comments/config_test.go @@ -66,7 +66,7 @@ func TestCommentStyle(t *testing.T) { func TestFileCommentStyleById(t *testing.T) { styleId := "SlashAsterisk" - style := FileCommentStyleById(styleId) + style := FileCommentStyleByID(styleId) if len(style.ID) == 0 { t.Logf("Comment style not exist, ID = %v", styleId) t.Fail() diff --git a/pkg/header/fix.go b/pkg/header/fix.go index 6dfeee5..18217ee 100644 --- a/pkg/header/fix.go +++ b/pkg/header/fix.go @@ -48,9 +48,9 @@ func Fix(file string, config *ConfigHeader, languages map[string]comments.Langua func getCommentStyle(filename string, languages map[string]comments.Language) *comments.CommentStyle { result := comments.FileCommentStyle(filename) configCommentStyles := configCommentStyle(languages) - for extension, styleId := range configCommentStyles { + for extension, styleID := range configCommentStyles { if strings.HasSuffix(filename, extension) { - result = comments.FileCommentStyleById(styleId) + result = comments.FileCommentStyleByID(styleID) } } return result From b1432653794c02194f222468a87a646a977547b3 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Sat, 19 Mar 2022 11:28:43 +0800 Subject: [PATCH 06/12] Fixes code style check problem. --- pkg/comments/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/comments/config.go b/pkg/comments/config.go index 86f6608..fcd2069 100644 --- a/pkg/comments/config.go +++ b/pkg/comments/config.go @@ -115,7 +115,7 @@ func FileCommentStyle(filename string) *CommentStyle { return nil } -func FileCommentStyleByID(styleId string) *CommentStyle { - result := comments[styleId] +func FileCommentStyleByID(styleID string) *CommentStyle { + result := comments[styleID] return &result } From 7c98f16f2cb6724af7760c9d6f506705450d6c47 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Thu, 31 Mar 2022 09:35:40 +0800 Subject: [PATCH 07/12] Fixes code style check problem. --- pkg/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index bc7f6df..71df04e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,11 +18,11 @@ package config import ( - "github.com/apache/skywalking-eyes/pkg/comments" "os" "github.com/apache/skywalking-eyes/assets" "github.com/apache/skywalking-eyes/internal/logger" + "github.com/apache/skywalking-eyes/pkg/comments" "github.com/apache/skywalking-eyes/pkg/deps" "github.com/apache/skywalking-eyes/pkg/header" From 8d99904b6117d02904e8953d013be96f1e50842c Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Mon, 4 Apr 2022 10:18:10 +0800 Subject: [PATCH 08/12] config: support config license header comment style, fix code logic. --- commands/header_fix.go | 2 +- pkg/comments/config.go | 42 +++++++++++--------- pkg/comments/config_test.go | 9 ----- pkg/config/.licenserc_test.yaml | 68 ++++++++++++--------------------- pkg/config/config.go | 6 +-- pkg/header/config.go | 6 ++- pkg/header/fix.go | 37 +----------------- 7 files changed, 59 insertions(+), 111 deletions(-) diff --git a/commands/header_fix.go b/commands/header_fix.go index 54848c3..3b0b828 100644 --- a/commands/header_fix.go +++ b/commands/header_fix.go @@ -45,7 +45,7 @@ var FixCommand = &cobra.Command{ } for _, file := range files { - if err := header.Fix(file, &Config.Header, Config.Languages, &result); err != nil { + if err := header.Fix(file, &Config.Header, &result); err != nil { errors = append(errors, err.Error()) } } diff --git a/pkg/comments/config.go b/pkg/comments/config.go index fcd2069..5c5e3b9 100644 --- a/pkg/comments/config.go +++ b/pkg/comments/config.go @@ -59,20 +59,7 @@ func init() { initCommentStyles() - for _, lang := range languages { - for _, extension := range lang.Extensions { - if lang.CommentStyleID == "" { - continue - } - commentStyles[extension] = comments[lang.CommentStyleID] - } - for _, filename := range lang.Filenames { - if lang.CommentStyleID == "" { - continue - } - commentStyles[filename] = comments[lang.CommentStyleID] - } - } + initLanguageCommentStyles(languages) } func initLanguages() { @@ -106,6 +93,26 @@ func initCommentStyles() { } } +func initLanguageCommentStyles(languages map[string]Language) { + if len(languages) == 0 { + return + } + for _, lang := range languages { + for _, extension := range lang.Extensions { + if lang.CommentStyleID == "" { + continue + } + commentStyles[extension] = comments[lang.CommentStyleID] + } + for _, filename := range lang.Filenames { + if lang.CommentStyleID == "" { + continue + } + commentStyles[filename] = comments[lang.CommentStyleID] + } + } +} + func FileCommentStyle(filename string) *CommentStyle { for extension, style := range commentStyles { if strings.HasSuffix(filename, extension) { @@ -115,7 +122,6 @@ func FileCommentStyle(filename string) *CommentStyle { return nil } -func FileCommentStyleByID(styleID string) *CommentStyle { - result := comments[styleID] - return &result -} +func OverrideLanguageCommentStyle(languages map[string]Language) { + initLanguageCommentStyles(languages) +} \ No newline at end of file diff --git a/pkg/comments/config_test.go b/pkg/comments/config_test.go index 6da998f..9bfea88 100644 --- a/pkg/comments/config_test.go +++ b/pkg/comments/config_test.go @@ -63,12 +63,3 @@ func TestCommentStyle(t *testing.T) { }) } } - -func TestFileCommentStyleById(t *testing.T) { - styleId := "SlashAsterisk" - style := FileCommentStyleByID(styleId) - if len(style.ID) == 0 { - t.Logf("Comment style not exist, ID = %v", styleId) - t.Fail() - } -} diff --git a/pkg/config/.licenserc_test.yaml b/pkg/config/.licenserc_test.yaml index b28a0d6..2ad0dee 100644 --- a/pkg/config/.licenserc_test.yaml +++ b/pkg/config/.licenserc_test.yaml @@ -80,49 +80,31 @@ header: # `header` section is configurations for source codes license header. # after all, a "header" cannot be TOO far from the file start. license-location-threshold: 80 + language: + Go: + extensions: + - ".go" + comment_style_id: SlashAsterisk + YAML: + extensions: + - ".yml" + - ".mir" + - ".reek" + - ".rviz" + - ".sublime-syntax" + - ".syntax" + - ".yaml" + - ".yaml-tmlanguage" + - ".yaml.sed" + - ".yml.mysql" + filenames: + - ".clang-format" + - ".clang-tidy" + - ".gemrc" + - glide.lock + - yarn.lock + comment_style_id: Hashtag + dependency: files: - go.mod - -language: - Go: - type: programming - color: "#00ADD8" - aliases: - - golang - extensions: - - ".go" - tm_scope: source.go - ace_mode: golang - codemirror_mode: go - codemirror_mime_type: text/x-go - language_id: 132 - comment_style_id: SlashAsterisk - YAML: - type: data - color: "#cb171e" - tm_scope: source.yaml - aliases: - - yml - extensions: - - ".yml" - - ".mir" - - ".reek" - - ".rviz" - - ".sublime-syntax" - - ".syntax" - - ".yaml" - - ".yaml-tmlanguage" - - ".yaml.sed" - - ".yml.mysql" - filenames: - - ".clang-format" - - ".clang-tidy" - - ".gemrc" - - glide.lock - - yarn.lock - ace_mode: yaml - codemirror_mode: yaml - codemirror_mime_type: text/x-yaml - language_id: 407 - comment_style_id: Hashtag diff --git a/pkg/config/config.go b/pkg/config/config.go index 71df04e..1d552a7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -22,7 +22,6 @@ import ( "github.com/apache/skywalking-eyes/assets" "github.com/apache/skywalking-eyes/internal/logger" - "github.com/apache/skywalking-eyes/pkg/comments" "github.com/apache/skywalking-eyes/pkg/deps" "github.com/apache/skywalking-eyes/pkg/header" @@ -30,9 +29,8 @@ import ( ) type Config struct { - Header header.ConfigHeader `yaml:"header"` - Deps deps.ConfigDeps `yaml:"dependency"` - Languages map[string]comments.Language `yaml:"language"` + Header header.ConfigHeader `yaml:"header"` + Deps deps.ConfigDeps `yaml:"dependency"` } // Parse reads and parses the header check configurations in config file. diff --git a/pkg/header/config.go b/pkg/header/config.go index 73f95d7..b2f98e3 100644 --- a/pkg/header/config.go +++ b/pkg/header/config.go @@ -28,6 +28,7 @@ import ( "github.com/apache/skywalking-eyes/assets" "github.com/apache/skywalking-eyes/internal/logger" + "github.com/apache/skywalking-eyes/pkg/comments" "github.com/apache/skywalking-eyes/pkg/license" "github.com/bmatcuk/doublestar/v2" @@ -59,7 +60,8 @@ type ConfigHeader struct { // LicenseLocationThreshold specifies the index threshold where the license header can be located, // after all, a "header" cannot be TOO far from the file start. - LicenseLocationThreshold int `yaml:"license-location-threshold"` + LicenseLocationThreshold int `yaml:"license-location-threshold"` + Languages map[string]comments.Language `yaml:"language"` } // NormalizedLicense returns the normalized string of the license content, @@ -104,6 +106,8 @@ func (config *ConfigHeader) Finalize() error { config.Paths = []string{"**"} } + comments.OverrideLanguageCommentStyle(config.Languages) + config.PathsIgnore = append(config.PathsIgnore, ".git", "**/*.txt") if file, err := os.Open(".gitignore"); err == nil { diff --git a/pkg/header/fix.go b/pkg/header/fix.go index 18217ee..55936b1 100644 --- a/pkg/header/fix.go +++ b/pkg/header/fix.go @@ -29,14 +29,14 @@ import ( ) // Fix adds the configured license header to the given file. -func Fix(file string, config *ConfigHeader, languages map[string]comments.Language, result *Result) error { +func Fix(file string, config *ConfigHeader, result *Result) error { var r Result if err := CheckFile(file, config, &r); err != nil || !r.HasFailure() { logger.Log.Warnln("Try to fix a valid file, do nothing:", file) return err } - style := getCommentStyle(file, languages) + style := comments.FileCommentStyle(file) if style == nil { return fmt.Errorf("unsupported file: %v", file) @@ -45,39 +45,6 @@ func Fix(file string, config *ConfigHeader, languages map[string]comments.Langua return InsertComment(file, style, config, result) } -func getCommentStyle(filename string, languages map[string]comments.Language) *comments.CommentStyle { - result := comments.FileCommentStyle(filename) - configCommentStyles := configCommentStyle(languages) - for extension, styleID := range configCommentStyles { - if strings.HasSuffix(filename, extension) { - result = comments.FileCommentStyleByID(styleID) - } - } - return result -} - -func configCommentStyle(languages map[string]comments.Language) map[string]string { - result := make(map[string]string) - if len(languages) == 0 { - return result - } - for _, lang := range languages { - for _, extension := range lang.Extensions { - if lang.CommentStyleID == "" { - continue - } - result[extension] = lang.CommentStyleID - } - for _, filename := range lang.Filenames { - if lang.CommentStyleID == "" { - continue - } - result[filename] = lang.CommentStyleID - } - } - return result -} - func InsertComment(file string, style *comments.CommentStyle, config *ConfigHeader, result *Result) error { stat, err := os.Stat(file) if err != nil { From 3ca930f6a134310ddbe0e39d6dd8dd4a40a7a9fe Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Mon, 4 Apr 2022 10:24:09 +0800 Subject: [PATCH 09/12] Fixes test config file directory. --- .../testdata/.licenserc_language_config_test.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pkg/config/.licenserc_test.yaml => test/testdata/.licenserc_language_config_test.yaml (100%) diff --git a/pkg/config/.licenserc_test.yaml b/test/testdata/.licenserc_language_config_test.yaml similarity index 100% rename from pkg/config/.licenserc_test.yaml rename to test/testdata/.licenserc_language_config_test.yaml From e916383b62fc8e8f0a24cac3d396ebd0c3cbebc8 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Mon, 4 Apr 2022 10:32:19 +0800 Subject: [PATCH 10/12] Fixes checkstyle problem. --- pkg/comments/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/comments/config.go b/pkg/comments/config.go index 5c5e3b9..3bc7ece 100644 --- a/pkg/comments/config.go +++ b/pkg/comments/config.go @@ -93,7 +93,7 @@ func initCommentStyles() { } } -func initLanguageCommentStyles(languages map[string]Language) { +func initLanguageCommentStyles(languages map[string]Language) { if len(languages) == 0 { return } @@ -124,4 +124,4 @@ func FileCommentStyle(filename string) *CommentStyle { func OverrideLanguageCommentStyle(languages map[string]Language) { initLanguageCommentStyles(languages) -} \ No newline at end of file +} From badd0f685a86a34d47411ffd9a86607239ec1262 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Mon, 4 Apr 2022 10:39:00 +0800 Subject: [PATCH 11/12] Remove unused test file. --- pkg/config/config_test.go | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 pkg/config/config_test.go diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go deleted file mode 100644 index 66d4dc2..0000000 --- a/pkg/config/config_test.go +++ /dev/null @@ -1,39 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you 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 "testing" - -func TestParseConfig(t *testing.T) { - config := &Config{ - - } - configFile := ".licenserc_test.yaml" - config.Parse(configFile) - if len(config.Header.GetLicenseContent()) == 0 { - t.Fail() - } - t.Logf("Parse config dependency files size = %v", len(config.Deps.Files)) - if len(config.Deps.Files) == 0 { - t.Fail() - } - t.Logf("Parse config languages size = %v", len(config.Languages)) - if len(config.Languages) == 0 { - t.Fail() - } -} From 672c344293e0a1726ca6eb015de07b613224b24f Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Mon, 4 Apr 2022 11:07:53 +0800 Subject: [PATCH 12/12] Fixes project config. --- .licenserc.yaml | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/.licenserc.yaml b/.licenserc.yaml index 17e7dd9..c2248d5 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -80,21 +80,12 @@ header: # `header` section is configurations for source codes license header. # after all, a "header" cannot be TOO far from the file start. license-location-threshold: 80 + language: + Go: + extensions: + - ".go" + comment_style_id: DoubleSlash + dependency: files: - go.mod - -language: - Go: - type: programming - color: "#00ADD8" - aliases: - - golang - extensions: - - ".go" - tm_scope: source.go - ace_mode: golang - codemirror_mode: go - codemirror_mime_type: text/x-go - language_id: 132 - comment_style_id: SlashAsterisk