|
| 1 | +package sbt |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "regexp" |
| 7 | +) |
| 8 | + |
| 9 | +type IBuildService interface { |
| 10 | + ParseBuildModules(path string) ([]string, error) |
| 11 | + FindPomFile(dir string) (string, error) |
| 12 | + RenamePomToXml(pomFile, destDir string) (string, error) |
| 13 | +} |
| 14 | + |
| 15 | +type BuildService struct{} |
| 16 | + |
| 17 | +func (b BuildService) ParseBuildModules(path string) ([]string, error) { |
| 18 | + content, err := os.ReadFile(path) |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + |
| 23 | + moduleRegex := regexp.MustCompile(`project\s*\(\s*"([^"]+)"\s*\)`) |
| 24 | + matches := moduleRegex.FindAllStringSubmatch(string(content), -1) |
| 25 | + |
| 26 | + modules := make([]string, 0, len(matches)) |
| 27 | + for _, match := range matches { |
| 28 | + if len(match) > 1 { |
| 29 | + modules = append(modules, match[1]) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return modules, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (b BuildService) FindPomFile(dir string) (string, error) { |
| 37 | + targetDir := filepath.Join(dir, "target") |
| 38 | + |
| 39 | + scalaVersionDirs, err := filepath.Glob(filepath.Join(targetDir, "scala-*")) |
| 40 | + if err != nil || len(scalaVersionDirs) == 0 { |
| 41 | + return "", err |
| 42 | + } |
| 43 | + |
| 44 | + for _, scalaDir := range scalaVersionDirs { |
| 45 | + pomFiles, err := filepath.Glob(filepath.Join(scalaDir, "*.pom")) |
| 46 | + if err == nil && len(pomFiles) > 0 { |
| 47 | + return pomFiles[0], nil |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return "", nil |
| 52 | +} |
| 53 | + |
| 54 | +func (b BuildService) RenamePomToXml(pomFile, destDir string) (string, error) { |
| 55 | + content, err := os.ReadFile(pomFile) |
| 56 | + if err != nil { |
| 57 | + return "", err |
| 58 | + } |
| 59 | + |
| 60 | + pomXmlPath := filepath.Join(destDir, "pom.xml") |
| 61 | + err = os.WriteFile(pomXmlPath, content, 0600) |
| 62 | + if err != nil { |
| 63 | + return "", err |
| 64 | + } |
| 65 | + |
| 66 | + return pomXmlPath, nil |
| 67 | +} |
0 commit comments