github.com/apache/skywalking-eyes@v0.6.0/pkg/comments/config.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package comments
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/apache/skywalking-eyes/assets"
    25  
    26  	"gopkg.in/yaml.v3"
    27  )
    28  
    29  type CommentStyle struct {
    30  	ID           string `yaml:"id"`
    31  	After        string `yaml:"after"`
    32  	Start        string `yaml:"start"`
    33  	Middle       string `yaml:"middle"`
    34  	End          string `yaml:"end"`
    35  	EnsureAfter  string `yaml:"ensure_after"`
    36  	EnsureBefore string `yaml:"ensure_before"`
    37  }
    38  
    39  func (style *CommentStyle) Validate() error {
    40  	if style.Start == "" || strings.TrimSpace(style.Start) == "" {
    41  		return fmt.Errorf("comment style 'start' cannot be empty")
    42  	}
    43  	return nil
    44  }
    45  
    46  type Language struct {
    47  	Type           string   `yaml:"type"`
    48  	Extensions     []string `yaml:"extensions"`
    49  	Filenames      []string `yaml:"filenames"`
    50  	CommentStyleID string   `yaml:"comment_style_id"`
    51  }
    52  
    53  var languages map[string]Language
    54  var comments = make(map[string]CommentStyle)
    55  var commentStyles = make(map[string]CommentStyle)
    56  
    57  func init() {
    58  	initLanguages()
    59  
    60  	initCommentStyles()
    61  
    62  	initLanguageCommentStyles(languages)
    63  }
    64  
    65  func initLanguages() {
    66  	content, err := assets.Asset("languages.yaml")
    67  	if err != nil {
    68  		panic(fmt.Errorf("should never happen: %w", err))
    69  	}
    70  
    71  	if err := yaml.Unmarshal(content, &languages); err != nil {
    72  		panic(err)
    73  	}
    74  
    75  	for s, language := range languages {
    76  		languages[s] = language
    77  	}
    78  }
    79  
    80  func initCommentStyles() {
    81  	content, err := assets.Asset("styles.yaml")
    82  	if err != nil {
    83  		panic(fmt.Errorf("should never happen: %w", err))
    84  	}
    85  
    86  	var styles []CommentStyle
    87  	if err = yaml.Unmarshal(content, &styles); err != nil {
    88  		panic(err)
    89  	}
    90  
    91  	for _, style := range styles {
    92  		comments[style.ID] = style
    93  	}
    94  }
    95  
    96  func initLanguageCommentStyles(languages map[string]Language) {
    97  	if len(languages) == 0 {
    98  		return
    99  	}
   100  	for _, lang := range languages {
   101  		for _, extension := range lang.Extensions {
   102  			if lang.CommentStyleID == "" {
   103  				continue
   104  			}
   105  			commentStyles[extension] = comments[lang.CommentStyleID]
   106  		}
   107  		for _, filename := range lang.Filenames {
   108  			if lang.CommentStyleID == "" {
   109  				continue
   110  			}
   111  			commentStyles[filename] = comments[lang.CommentStyleID]
   112  		}
   113  	}
   114  }
   115  
   116  func FileCommentStyle(filename string) *CommentStyle {
   117  	for extension, style := range commentStyles {
   118  		if strings.HasSuffix(filename, extension) {
   119  			return &style
   120  		}
   121  	}
   122  	return nil
   123  }
   124  
   125  func OverrideLanguageCommentStyle(languages map[string]Language) {
   126  	initLanguageCommentStyles(languages)
   127  }