github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/conan/conanfile_parser.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package conan 7 8 import ( 9 "io" 10 "regexp" 11 "strings" 12 ) 13 14 var ( 15 patternAuthor = compilePattern("author") 16 patternHomepage = compilePattern("homepage") 17 patternURL = compilePattern("url") 18 patternLicense = compilePattern("license") 19 patternDescription = compilePattern("description") 20 patternTopics = regexp.MustCompile(`(?im)^\s*topics\s*=\s*\((.+)\)`) 21 patternTopicList = regexp.MustCompile(`\s*['"](.+?)['"]\s*,?`) 22 ) 23 24 func compilePattern(name string) *regexp.Regexp { 25 return regexp.MustCompile(`(?im)^\s*` + name + `\s*=\s*['"\(](.+)['"\)]`) 26 } 27 28 func ParseConanfile(r io.Reader) (*Metadata, error) { 29 buf, err := io.ReadAll(io.LimitReader(r, 1<<20)) 30 if err != nil { 31 return nil, err 32 } 33 34 metadata := &Metadata{} 35 36 m := patternAuthor.FindSubmatch(buf) 37 if len(m) > 1 && len(m[1]) > 0 { 38 metadata.Author = string(m[1]) 39 } 40 m = patternHomepage.FindSubmatch(buf) 41 if len(m) > 1 && len(m[1]) > 0 { 42 metadata.ProjectURL = string(m[1]) 43 } 44 m = patternURL.FindSubmatch(buf) 45 if len(m) > 1 && len(m[1]) > 0 { 46 metadata.RepositoryURL = string(m[1]) 47 } 48 m = patternLicense.FindSubmatch(buf) 49 if len(m) > 1 && len(m[1]) > 0 { 50 metadata.License = strings.ReplaceAll(strings.ReplaceAll(string(m[1]), "'", ""), "\"", "") 51 } 52 m = patternDescription.FindSubmatch(buf) 53 if len(m) > 1 && len(m[1]) > 0 { 54 metadata.Description = string(m[1]) 55 } 56 m = patternTopics.FindSubmatch(buf) 57 if len(m) > 1 && len(m[1]) > 0 { 58 m2 := patternTopicList.FindAllSubmatch(m[1], -1) 59 if len(m2) > 0 { 60 metadata.Keywords = make([]string, 0, len(m2)) 61 for _, g := range m2 { 62 if len(g) > 1 { 63 metadata.Keywords = append(metadata.Keywords, string(g[1])) 64 } 65 } 66 } 67 } 68 return metadata, nil 69 }