From d91f5fdb529d5902cc578cce36351ff022c8fb27 Mon Sep 17 00:00:00 2001 From: Bernd Ahlers Date: Tue, 13 Mar 2018 13:58:51 +0100 Subject: [PATCH] Add PomFiles function to project modules --- pomparse/pomparse.go | 29 +++++++++++++++++++++++++++++ project/project.go | 15 +++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/pomparse/pomparse.go b/pomparse/pomparse.go index dd4f913..ac5ccfc 100644 --- a/pomparse/pomparse.go +++ b/pomparse/pomparse.go @@ -7,6 +7,7 @@ import ( "github.com/Graylog2/graylog-project-cli/utils" "io/ioutil" "os" + "path/filepath" "strings" ) @@ -135,3 +136,31 @@ func ParseEffectivePom(moduleName string, modulePath string) MavenPom { return ParsePom(file.Name()) } + +// Return pom.xml files for the given module directory and all its submodules. If the given path is empty, the current +// directory is assumed and the pom.xml file paths are relative. +func FindPomFiles(path string) []string { + var files []string + + pomFile := "pom.xml" + + if path != "" { + pomFile = filepath.Join(path, pomFile) + } + + if !utils.FileExists(pomFile) { + return files + } + + // First add this pom.xml + files = append(files, pomFile) + + // Then check if there are modules + for _, module := range ParsePom(pomFile).Modules { + modulePath := filepath.Join(path, module) + + files = append(files, FindPomFiles(modulePath)...) + } + + return files +} diff --git a/project/project.go b/project/project.go index 33e6655..37c765d 100644 --- a/project/project.go +++ b/project/project.go @@ -49,6 +49,21 @@ func (module *Module) HasSubmodules() bool { return len(module.Submodules) > 0 } +// Returns a list of pom.xml files for this modules. If the relative parameter +// is set to "true", the path to the pom.xml files will be relative to the +// module root. +func (module *Module) PomFiles(relative bool) []string { + var list []string + if relative { + utils.InDirectory(module.Path, func() { + list = pomparse.FindPomFiles("") + }) + } else { + list = pomparse.FindPomFiles(module.Path) + } + return list +} + func (module *Module) RelativePath() string { // The path in the "" tag needs to be relative to make maven happy! // Using the GetRelativePathEvalSymlinks function here to make sure the relative path is as short as possible.