gitea源码

conanfile_parser.go 1.8KB

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