-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvegradlesubprojects.go
60 lines (46 loc) · 1.33 KB
/
resolvegradlesubprojects.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide a gradle subproject directory name")
os.Exit(1)
}
subproject := os.Args[1]
dir, err := os.Getwd()
if err != nil {
fmt.Println("Failed to get working directory", err)
os.Exit(1)
}
gradleFilesGlob := filepath.Join(dir, "*", "build.gradle*") // Get all build.gradle or build.gradle.kts files one level deep
gradleFilePaths, err := filepath.Glob(gradleFilesGlob)
if len(gradleFilePaths) == 0 {
fmt.Printf("No gradle files found 1 level deep of %s\n", dir)
os.Exit(1)
}
gradleFiles, err := readGradleFiles(gradleFilePaths)
if err != nil {
fmt.Println("Failed to read gradle files", err)
os.Exit(1)
}
allProjectsDependencies := CreateDependencyMap(gradleFiles)
allDependencies := ResolveDependencies(allProjectsDependencies, subproject)
fmt.Println(strings.Join(allDependencies, " "))
}
func readGradleFiles(filePaths []string) (map[string]string, error) {
gradleFiles := make(map[string]string)
for _, file := range filePaths {
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("file %v: %v", file, err)
}
subprojectDirname := filepath.Base(filepath.Dir(file))
gradleFiles[subprojectDirname] = string(content)
}
return gradleFiles, nil
}