Skip to content

Commit

Permalink
Support csproj files for identifying .NET Core projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Minter committed Nov 21, 2016
1 parent 9c4f9ee commit 4c3626f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pkg/generate/app/sourcelookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func (e SourceRepositoryEnumerator) Detect(dir string, dockerStrategy bool) (*So
// is docker
if !dockerStrategy {
for _, d := range e.Detectors {
if detected, ok := d(dir); ok {
if detected := d(dir); detected != nil {
info.Types = append(info.Types, SourceLanguageType{
Platform: detected.Platform,
Version: detected.Version,
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/app/strategyref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func (d dfile) GetDirective(name string) ([]string, bool) {
return result, ok
}

func fakeDetector(dir string) (*source.Info, bool) {
func fakeDetector(dir string) *source.Info {
return &source.Info{
Platform: "JEE",
Version: "1.0",
}, true
}
}
51 changes: 19 additions & 32 deletions pkg/generate/source/detector.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package source

import (
"os"
"path/filepath"
)
import "path/filepath"

// Info is detected platform information from a source directory
type Info struct {
Expand All @@ -13,7 +10,7 @@ type Info struct {

// DetectorFunc is a function that returns source Info from a given directory.
// It returns true if it was able to detect the code in the given directory.
type DetectorFunc func(dir string) (*Info, bool)
type DetectorFunc func(dir string) *Info

// Detectors is a set of DetectorFunc that is used to detect the
// language/platform for a given source directory
Expand All @@ -33,66 +30,56 @@ var DefaultDetectors = Detectors{
}

// DetectRuby detects Ruby source
func DetectRuby(dir string) (*Info, bool) {
func DetectRuby(dir string) *Info {
return detect("ruby", dir, "Gemfile", "Rakefile", "config.ru")
}

// DetectJava detects Java source
func DetectJava(dir string) (*Info, bool) {
func DetectJava(dir string) *Info {
return detect("jee", dir, "pom.xml")
}

// DetectNodeJS detects NodeJS source
func DetectNodeJS(dir string) (*Info, bool) {
func DetectNodeJS(dir string) *Info {
return detect("nodejs", dir, "app.json", "package.json")
}

// DetectPHP detects PHP source
func DetectPHP(dir string) (*Info, bool) {
func DetectPHP(dir string) *Info {
return detect("php", dir, "index.php", "composer.json")
}

// DetectPython detects Python source
func DetectPython(dir string) (*Info, bool) {
func DetectPython(dir string) *Info {
return detect("python", dir, "requirements.txt", "setup.py")
}

// DetectPerl detects Perl source
func DetectPerl(dir string) (*Info, bool) {
func DetectPerl(dir string) *Info {
return detect("perl", dir, "index.pl", "cpanfile")
}

// DetectScala detects Scala source
func DetectScala(dir string) (*Info, bool) {
func DetectScala(dir string) *Info {
return detect("scala", dir, "build.sbt")
}

// DetectDotNet detects .NET source and matches it to a dotnet supported annotatin or dotnet imagestream name
func DetectDotNet(dir string) (*Info, bool) {
return detect("dotnet", dir, "project.json")
// DetectDotNet detects .NET source and matches it to a dotnet supported annotation or dotnet imagestream name
func DetectDotNet(dir string) *Info {
return detect("dotnet", dir, "project.json", "*.csproj")
}

// DetectLiteralDotNet detects .NET source and matches it to a .net supported annotation
func DetectLiteralDotNet(dir string) (*Info, bool) {
return detect(".net", dir, "project.json")
func DetectLiteralDotNet(dir string) *Info {
return detect(".net", dir, "project.json", "*.csproj")
}

// detect returns an Info object with the given platform if the source at dir contains any of the argument files
func detect(platform string, dir string, files ...string) (*Info, bool) {
if filesPresent(dir, files) {
return &Info{
Platform: platform,
}, true
}
return nil, false
}

func filesPresent(dir string, files []string) bool {
for _, f := range files {
_, err := os.Stat(filepath.Join(dir, f))
if err == nil {
return true
func detect(platform string, dir string, globs ...string) *Info {
for _, g := range globs {
if matches, _ := filepath.Glob(filepath.Join(dir, g)); len(matches) > 0 {
return &Info{Platform: platform}
}
}
return false
return nil
}

0 comments on commit 4c3626f

Please sign in to comment.