Browse code

Support csproj files for identifying .NET Core projects

Jim Minter authored on 2016/11/12 23:47:17
Showing 2 changed files
... ...
@@ -502,7 +502,7 @@ func (e SourceRepositoryEnumerator) Detect(dir string, noSourceDetection bool) (
502 502
 	// is docker or pipeline
503 503
 	if !noSourceDetection {
504 504
 		for _, d := range e.Detectors {
505
-			if detected, ok := d(dir); ok {
505
+			if detected := d(dir); detected != nil {
506 506
 				info.Types = append(info.Types, SourceLanguageType{
507 507
 					Platform: detected.Platform,
508 508
 					Version:  detected.Version,
... ...
@@ -1,9 +1,6 @@
1 1
 package source
2 2
 
3
-import (
4
-	"os"
5
-	"path/filepath"
6
-)
3
+import "path/filepath"
7 4
 
8 5
 // Info is detected platform information from a source directory
9 6
 type Info struct {
... ...
@@ -13,7 +10,7 @@ type Info struct {
13 13
 
14 14
 // DetectorFunc is a function that returns source Info from a given directory.
15 15
 // It returns true if it was able to detect the code in the given directory.
16
-type DetectorFunc func(dir string) (*Info, bool)
16
+type DetectorFunc func(dir string) *Info
17 17
 
18 18
 // Detectors is a set of DetectorFunc that is used to detect the
19 19
 // language/platform for a given source directory
... ...
@@ -33,66 +30,56 @@ var DefaultDetectors = Detectors{
33 33
 }
34 34
 
35 35
 // DetectRuby detects Ruby source
36
-func DetectRuby(dir string) (*Info, bool) {
36
+func DetectRuby(dir string) *Info {
37 37
 	return detect("ruby", dir, "Gemfile", "Rakefile", "config.ru")
38 38
 }
39 39
 
40 40
 // DetectJava detects Java source
41
-func DetectJava(dir string) (*Info, bool) {
41
+func DetectJava(dir string) *Info {
42 42
 	return detect("jee", dir, "pom.xml")
43 43
 }
44 44
 
45 45
 // DetectNodeJS detects NodeJS source
46
-func DetectNodeJS(dir string) (*Info, bool) {
46
+func DetectNodeJS(dir string) *Info {
47 47
 	return detect("nodejs", dir, "app.json", "package.json")
48 48
 }
49 49
 
50 50
 // DetectPHP detects PHP source
51
-func DetectPHP(dir string) (*Info, bool) {
51
+func DetectPHP(dir string) *Info {
52 52
 	return detect("php", dir, "index.php", "composer.json")
53 53
 }
54 54
 
55 55
 // DetectPython detects Python source
56
-func DetectPython(dir string) (*Info, bool) {
56
+func DetectPython(dir string) *Info {
57 57
 	return detect("python", dir, "requirements.txt", "setup.py")
58 58
 }
59 59
 
60 60
 // DetectPerl detects Perl source
61
-func DetectPerl(dir string) (*Info, bool) {
61
+func DetectPerl(dir string) *Info {
62 62
 	return detect("perl", dir, "index.pl", "cpanfile")
63 63
 }
64 64
 
65 65
 // DetectScala detects Scala source
66
-func DetectScala(dir string) (*Info, bool) {
66
+func DetectScala(dir string) *Info {
67 67
 	return detect("scala", dir, "build.sbt")
68 68
 }
69 69
 
70
-// DetectDotNet detects .NET source and matches it to a dotnet supported annotatin or dotnet imagestream name
71
-func DetectDotNet(dir string) (*Info, bool) {
72
-	return detect("dotnet", dir, "project.json")
70
+// DetectDotNet detects .NET source and matches it to a dotnet supported annotation or dotnet imagestream name
71
+func DetectDotNet(dir string) *Info {
72
+	return detect("dotnet", dir, "project.json", "*.csproj")
73 73
 }
74 74
 
75 75
 // DetectLiteralDotNet detects .NET source and matches it to a .net supported annotation
76
-func DetectLiteralDotNet(dir string) (*Info, bool) {
77
-	return detect(".net", dir, "project.json")
76
+func DetectLiteralDotNet(dir string) *Info {
77
+	return detect(".net", dir, "project.json", "*.csproj")
78 78
 }
79 79
 
80 80
 // detect returns an Info object with the given platform if the source at dir contains any of the argument files
81
-func detect(platform string, dir string, files ...string) (*Info, bool) {
82
-	if filesPresent(dir, files) {
83
-		return &Info{
84
-			Platform: platform,
85
-		}, true
86
-	}
87
-	return nil, false
88
-}
89
-
90
-func filesPresent(dir string, files []string) bool {
91
-	for _, f := range files {
92
-		_, err := os.Stat(filepath.Join(dir, f))
93
-		if err == nil {
94
-			return true
81
+func detect(platform string, dir string, globs ...string) *Info {
82
+	for _, g := range globs {
83
+		if matches, _ := filepath.Glob(filepath.Join(dir, g)); len(matches) > 0 {
84
+			return &Info{Platform: platform}
95 85
 		}
96 86
 	}
97
-	return false
87
+	return nil
98 88
 }