Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[config]Support config license header comment style. #97

Merged
merged 12 commits into from
Apr 6, 2022
6 changes: 6 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 25 additions & 14 deletions pkg/comments/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -114,3 +121,7 @@ func FileCommentStyle(filename string) *CommentStyle {
}
return nil
}

func OverrideLanguageCommentStyle(languages map[string]Language) {
initLanguageCommentStyles(languages)
}
6 changes: 5 additions & 1 deletion pkg/header/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -104,6 +106,8 @@ func (config *ConfigHeader) Finalize() error {
config.Paths = []string{"**"}
}

comments.OverrideLanguageCommentStyle(config.Languages)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


config.PathsIgnore = append(config.PathsIgnore, ".git", "**/*.txt")

if file, err := os.Open(".gitignore"); err == nil {
Expand Down
110 changes: 110 additions & 0 deletions test/testdata/.licenserc_language_config_test.yaml
Original file line number Diff line number Diff line change
@@ -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