mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
Add functions to infer build tool (#1210)
This commit is contained in:
parent
79475732f9
commit
c15f6a03d2
36
pkg/piperutils/projectStructure.go
Normal file
36
pkg/piperutils/projectStructure.go
Normal file
@ -0,0 +1,36 @@
|
||||
package piperutils
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
// ProjectStructure describes a directory containing source code
|
||||
type ProjectStructure struct {
|
||||
directory string
|
||||
}
|
||||
|
||||
// UsesMta returns `true` if the project structure directory contains typical files for mta projects (mta.yaml, mta.yml), `false` otherwise
|
||||
func (projectStructure *ProjectStructure) UsesMta() bool {
|
||||
return projectStructure.anyFileExists("mta.yaml", "mta.yml")
|
||||
}
|
||||
|
||||
// UsesMaven returns `true` if the project structure directory contains a pom.xml file, false otherwise
|
||||
func (projectStructure *ProjectStructure) UsesMaven() bool {
|
||||
return projectStructure.anyFileExists("pom.xml")
|
||||
}
|
||||
|
||||
// UsesNpm returns `true` if the project structure directory contains a package.json file, false otherwise
|
||||
func (projectStructure *ProjectStructure) UsesNpm() bool {
|
||||
return projectStructure.anyFileExists("package.json")
|
||||
}
|
||||
|
||||
func (projectStructure *ProjectStructure) anyFileExists(candidates ...string) bool {
|
||||
for i := 0; i < len(candidates); i++ {
|
||||
exists, err := FileExists(filepath.Join(projectStructure.directory, candidates[i]))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if exists {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
47
pkg/piperutils/projectStructure_test.go
Normal file
47
pkg/piperutils/projectStructure_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package piperutils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestProjectWithOnlyMtaFile(t *testing.T) {
|
||||
projectStructure := ProjectStructure{directory: "testdata/mta"}
|
||||
resultMta := projectStructure.UsesMta()
|
||||
assert.True(t, resultMta)
|
||||
resultPom := projectStructure.UsesMaven()
|
||||
assert.False(t, resultPom)
|
||||
resultNpm := projectStructure.UsesNpm()
|
||||
assert.False(t, resultNpm)
|
||||
}
|
||||
|
||||
func TestProjectWithOnlyPomFile(t *testing.T) {
|
||||
projectStructure := ProjectStructure{directory: "testdata/maven"}
|
||||
resultMta := projectStructure.UsesMta()
|
||||
assert.False(t, resultMta)
|
||||
resultPom := projectStructure.UsesMaven()
|
||||
assert.True(t, resultPom)
|
||||
resultNpm := projectStructure.UsesNpm()
|
||||
assert.False(t, resultNpm)
|
||||
}
|
||||
|
||||
func TestProjectWithOnlyNpmFile(t *testing.T) {
|
||||
projectStructure := ProjectStructure{directory: "testdata/npm"}
|
||||
resultMta := projectStructure.UsesMta()
|
||||
assert.False(t, resultMta)
|
||||
resultPom := projectStructure.UsesMaven()
|
||||
assert.False(t, resultPom)
|
||||
resultNpm := projectStructure.UsesNpm()
|
||||
assert.True(t, resultNpm)
|
||||
}
|
||||
|
||||
func TestDirectryParameterIsEmptyAndNoProjectFilesAreInIt(t *testing.T) {
|
||||
projectStructure := ProjectStructure{}
|
||||
resultMta := projectStructure.UsesMta()
|
||||
assert.False(t, resultMta)
|
||||
resultPom := projectStructure.UsesMaven()
|
||||
assert.False(t, resultPom)
|
||||
resultNpm := projectStructure.UsesNpm()
|
||||
assert.False(t, resultNpm)
|
||||
}
|
0
pkg/piperutils/testdata/maven/pom.xml
vendored
Normal file
0
pkg/piperutils/testdata/maven/pom.xml
vendored
Normal file
0
pkg/piperutils/testdata/mta/mta.yaml
vendored
Normal file
0
pkg/piperutils/testdata/mta/mta.yaml
vendored
Normal file
0
pkg/piperutils/testdata/npm/package.json
vendored
Normal file
0
pkg/piperutils/testdata/npm/package.json
vendored
Normal file
Loading…
Reference in New Issue
Block a user