code.gitea.io/gitea@v1.22.3/modules/packages/conan/conanfile_parser.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package conan
     5  
     6  import (
     7  	"io"
     8  	"regexp"
     9  	"strings"
    10  )
    11  
    12  var (
    13  	patternAuthor      = compilePattern("author")
    14  	patternHomepage    = compilePattern("homepage")
    15  	patternURL         = compilePattern("url")
    16  	patternLicense     = compilePattern("license")
    17  	patternDescription = compilePattern("description")
    18  	patternTopics      = regexp.MustCompile(`(?im)^\s*topics\s*=\s*\((.+)\)`)
    19  	patternTopicList   = regexp.MustCompile(`\s*['"](.+?)['"]\s*,?`)
    20  )
    21  
    22  func compilePattern(name string) *regexp.Regexp {
    23  	return regexp.MustCompile(`(?im)^\s*` + name + `\s*=\s*['"\(](.+)['"\)]`)
    24  }
    25  
    26  func ParseConanfile(r io.Reader) (*Metadata, error) {
    27  	buf, err := io.ReadAll(io.LimitReader(r, 1<<20))
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	metadata := &Metadata{}
    33  
    34  	m := patternAuthor.FindSubmatch(buf)
    35  	if len(m) > 1 && len(m[1]) > 0 {
    36  		metadata.Author = string(m[1])
    37  	}
    38  	m = patternHomepage.FindSubmatch(buf)
    39  	if len(m) > 1 && len(m[1]) > 0 {
    40  		metadata.ProjectURL = string(m[1])
    41  	}
    42  	m = patternURL.FindSubmatch(buf)
    43  	if len(m) > 1 && len(m[1]) > 0 {
    44  		metadata.RepositoryURL = string(m[1])
    45  	}
    46  	m = patternLicense.FindSubmatch(buf)
    47  	if len(m) > 1 && len(m[1]) > 0 {
    48  		metadata.License = strings.ReplaceAll(strings.ReplaceAll(string(m[1]), "'", ""), "\"", "")
    49  	}
    50  	m = patternDescription.FindSubmatch(buf)
    51  	if len(m) > 1 && len(m[1]) > 0 {
    52  		metadata.Description = string(m[1])
    53  	}
    54  	m = patternTopics.FindSubmatch(buf)
    55  	if len(m) > 1 && len(m[1]) > 0 {
    56  		m2 := patternTopicList.FindAllSubmatch(m[1], -1)
    57  		if len(m2) > 0 {
    58  			metadata.Keywords = make([]string, 0, len(m2))
    59  			for _, g := range m2 {
    60  				if len(g) > 1 {
    61  					metadata.Keywords = append(metadata.Keywords, string(g[1]))
    62  				}
    63  			}
    64  		}
    65  	}
    66  	return metadata, nil
    67  }