diff --git a/.licenserc.yaml b/.licenserc.yaml index e93e885..c2248d5 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -80,6 +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 diff --git a/pkg/comments/config.go b/pkg/comments/config.go index 8639b3e..3bc7ece 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) { @@ -114,3 +121,7 @@ func FileCommentStyle(filename string) *CommentStyle { } return nil } + +func OverrideLanguageCommentStyle(languages map[string]Language) { + initLanguageCommentStyles(languages) +} 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/test/testdata/.licenserc_language_config_test.yaml b/test/testdata/.licenserc_language_config_test.yaml new file mode 100644 index 0000000..2ad0dee --- /dev/null +++ b/test/testdata/.licenserc_language_config_test.yaml @@ -0,0 +1,110 @@ +# 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 + + 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