Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
feat: retrieve project name when doing component detection (#96) (#101)
Browse files Browse the repository at this point in the history
* feat: retrieve project name when doing component detection (#96)

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>

* add tests

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>

* add gradle proj name detection

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>

* update doc

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
  • Loading branch information
lstocchi authored Jul 14, 2022
1 parent 4057f5a commit d5c8528
Show file tree
Hide file tree
Showing 35 changed files with 266 additions and 133 deletions.
2 changes: 2 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Alizer scans all files in the source tree to find out a configuration file. If f
Once the first step ends up, if there are no other free subfolders (free = folders that do not belong to any component) the second step is skipped otherwise Alizer tries to search for a component written with a language which may not have a configuration file. In that subfolder a simple Language detection is performed and the first language is taken into account for further calculations.

The result is a list of components where each component consists of:
- *name*: name of the component
- *path*: root of the component
- *languages*: list of languages belonging to the component ordered by their relevance.

Expand All @@ -63,6 +64,7 @@ import "github.com/redhat-developer/alizer/pkg/apis/recognizer"
components, err := recognizer.DetectComponents("./")
```

For more info about name detection plase check the [name detection](docs/name_detection.md) doc.
### Devfile Detection

It selects a devfile from a list of devfiles (from a devfile registry or other storage) based on the information found in the source tree.
Expand Down
23 changes: 23 additions & 0 deletions go/docs/name_detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Component Name Detection in Alizer

Name detection is one of the step included during component detection and it refers to the name of the app/project.

The process consists of two steps:
1) Some languages (Java, Javascript) have a specific place where the project name is set. If Alizer discovers one of those languages it checks for their configuration files to find out the name; if it fails or a language with no custom detection is detected, it proceeds with (2)
2) The directory name is used as name of the component

Below a list of the languages with a custom detection

### Java

#### Maven

Alizer searches for the `pom.xml` file in the root folder and takes the value defined by the `artifactId` field.

#### Gradle

Alizer searches for the `settings.gradle` file in the root folder and takes the value defined by the `rootProject.name` field.

### Javascript

Alizer searches for the `package.json` file in the root folder and takes the value defined by the `name` field
11 changes: 8 additions & 3 deletions go/pkg/apis/enricher/dotnet_enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package recognizer

import (
framework "github.com/redhat-developer/alizer/go/pkg/apis/enricher/framework/dotnet"
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
utils "github.com/redhat-developer/alizer/go/pkg/utils"
)

Expand All @@ -28,18 +28,23 @@ func getDotNetFrameworkDetectors() []FrameworkDetectorWithConfigFile {
}
}

func (j DotNetEnricher) DoEnrichLanguage(language *language.Language, files *[]string) {
func (j DotNetEnricher) DoEnrichLanguage(language *model.Language, files *[]string) {
configFiles := utils.GetFilesByRegex(files, ".*\\.\\w+proj")
for _, configFile := range configFiles {
getDotNetFrameworks(language, configFile)
}
}

func (j DotNetEnricher) DoEnrichComponent(component *model.Component) {
projectName := GetDefaultProjectName(component.Path)
component.Name = projectName
}

func (j DotNetEnricher) IsConfigValidForComponentDetection(language string, config string) bool {
return IsConfigurationValidForLanguage(language, config)
}

func getDotNetFrameworks(language *language.Language, configFile string) {
func getDotNetFrameworks(language *model.Language, configFile string) {
for _, detector := range getDotNetFrameworkDetectors() {
detector.DoFrameworkDetection(language, configFile)
}
Expand Down
13 changes: 9 additions & 4 deletions go/pkg/apis/enricher/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ import (
"path/filepath"
"strings"

"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"github.com/redhat-developer/alizer/go/pkg/utils/langfiles"
)

type Enricher interface {
GetSupportedLanguages() []string
DoEnrichLanguage(language *language.Language, files *[]string)
DoEnrichLanguage(language *model.Language, files *[]string)
DoEnrichComponent(component *model.Component)
IsConfigValidForComponentDetection(language string, configFile string) bool
}

type FrameworkDetectorWithConfigFile interface {
DoFrameworkDetection(language *language.Language, config string)
DoFrameworkDetection(language *model.Language, config string)
}

type FrameworkDetectorWithoutConfigFile interface {
DoFrameworkDetection(language *language.Language, files *[]string)
DoFrameworkDetection(language *model.Language, files *[]string)
}

/*
Expand Down Expand Up @@ -107,3 +108,7 @@ func isLanguageSupportedByEnricher(nameLanguage string, enricher Enricher) bool
}
return false
}

func GetDefaultProjectName(path string) string {
return filepath.Base(path)
}
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/dotnet/dotnet_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
"os"
"strings"

"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"github.com/redhat-developer/alizer/go/pkg/schema"
"github.com/redhat-developer/alizer/go/pkg/utils"
)

type DotNetDetector struct{}

func (m DotNetDetector) DoFrameworkDetection(language *language.Language, configFilePath string) {
func (m DotNetDetector) DoFrameworkDetection(language *model.Language, configFilePath string) {
framework := getFrameworks(configFilePath)
if framework == "" {
return
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/go/beego_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"golang.org/x/mod/modfile"
)

type BeegoDetector struct{}

func (e BeegoDetector) DoFrameworkDetection(language *language.Language, goMod *modfile.File) {
func (e BeegoDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/beego/beego") {
language.Frameworks = append(language.Frameworks, "Beego")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/go/echo_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"golang.org/x/mod/modfile"
)

type EchoDetector struct{}

func (e EchoDetector) DoFrameworkDetection(language *language.Language, goMod *modfile.File) {
func (e EchoDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/labstack/echo") {
language.Frameworks = append(language.Frameworks, "Echo")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/go/fasthttp_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"golang.org/x/mod/modfile"
)

type FastHttpDetector struct{}

func (e FastHttpDetector) DoFrameworkDetection(language *language.Language, goMod *modfile.File) {
func (e FastHttpDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/valyala/fasthttp") {
language.Frameworks = append(language.Frameworks, "FastHttp")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/go/gin_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"golang.org/x/mod/modfile"
)

type GinDetector struct{}

func (e GinDetector) DoFrameworkDetection(language *language.Language, goMod *modfile.File) {
func (e GinDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/gin-gonic/gin") {
language.Frameworks = append(language.Frameworks, "Gin")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/go/gofiber_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"golang.org/x/mod/modfile"
)

type GoFiberDetector struct{}

func (e GoFiberDetector) DoFrameworkDetection(language *language.Language, goMod *modfile.File) {
func (e GoFiberDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/gofiber/fiber") {
language.Frameworks = append(language.Frameworks, "GoFiber")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/go/mux_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"golang.org/x/mod/modfile"
)

type MuxDetector struct{}

func (e MuxDetector) DoFrameworkDetection(language *language.Language, goMod *modfile.File) {
func (e MuxDetector) DoFrameworkDetection(language *model.Language, goMod *modfile.File) {
if hasFramework(goMod.Require, "github.com/gorilla/mux") {
language.Frameworks = append(language.Frameworks, "Mux")
}
Expand Down
6 changes: 2 additions & 4 deletions go/pkg/apis/enricher/framework/java/micronaut_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
******************************************************************************/
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
)
import "github.com/redhat-developer/alizer/go/pkg/apis/model"

type MicronautDetector struct{}

func (m MicronautDetector) DoFrameworkDetection(language *language.Language, config string) {
func (m MicronautDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFwk, _ := hasFramework(config, "io.micronaut"); hasFwk {
language.Frameworks = append(language.Frameworks, "Micronaut")
}
Expand Down
6 changes: 2 additions & 4 deletions go/pkg/apis/enricher/framework/java/openliberty_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
******************************************************************************/
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
)
import "github.com/redhat-developer/alizer/go/pkg/apis/model"

type OpenLibertyDetector struct{}

func (o OpenLibertyDetector) DoFrameworkDetection(language *language.Language, config string) {
func (o OpenLibertyDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFwk, _ := hasFramework(config, "io.openliberty"); hasFwk {
language.Frameworks = append(language.Frameworks, "OpenLiberty")
}
Expand Down
6 changes: 2 additions & 4 deletions go/pkg/apis/enricher/framework/java/quarkus_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
******************************************************************************/
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
)
import "github.com/redhat-developer/alizer/go/pkg/apis/model"

type QuarkusDetector struct{}

func (q QuarkusDetector) DoFrameworkDetection(language *language.Language, config string) {
func (q QuarkusDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFwk, _ := hasFramework(config, "io.quarkus"); hasFwk {
language.Frameworks = append(language.Frameworks, "Quarkus")
}
Expand Down
6 changes: 2 additions & 4 deletions go/pkg/apis/enricher/framework/java/spring_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
******************************************************************************/
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
)
import "github.com/redhat-developer/alizer/go/pkg/apis/model"

type SpringDetector struct{}

func (s SpringDetector) DoFrameworkDetection(language *language.Language, config string) {
func (s SpringDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFwk, _ := hasFramework(config, "org.springframework"); hasFwk {
language.Frameworks = append(language.Frameworks, "Spring")
}
Expand Down
6 changes: 2 additions & 4 deletions go/pkg/apis/enricher/framework/java/vertx_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
******************************************************************************/
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
)
import "github.com/redhat-developer/alizer/go/pkg/apis/model"

type VertxDetector struct{}

func (v VertxDetector) DoFrameworkDetection(language *language.Language, config string) {
func (v VertxDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFwk, _ := hasFramework(config, "io.vertx"); hasFwk {
language.Frameworks = append(language.Frameworks, "Vertx")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
******************************************************************************/
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
)
import "github.com/redhat-developer/alizer/go/pkg/apis/model"

type ExpressDetector struct{}

func (e ExpressDetector) DoFrameworkDetection(language *language.Language, config string) {
func (e ExpressDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFramework(config, "express") {
language.Frameworks = append(language.Frameworks, "Express")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
******************************************************************************/
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
)
import "github.com/redhat-developer/alizer/go/pkg/apis/model"

type ReactJsDetector struct{}

func (r ReactJsDetector) DoFrameworkDetection(language *language.Language, config string) {
func (r ReactJsDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFramework(config, "react") {
language.Frameworks = append(language.Frameworks, "React")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/apis/enricher/framework/python/django_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package recognizer

import (
"github.com/redhat-developer/alizer/go/pkg/apis/language"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"github.com/redhat-developer/alizer/go/pkg/utils"
)

type DjangoDetector struct{}

func (d DjangoDetector) DoFrameworkDetection(language *language.Language, files *[]string) {
func (d DjangoDetector) DoFrameworkDetection(language *model.Language, files *[]string) {
managePy := utils.GetFile(files, "manage.py")
urlsPy := utils.GetFile(files, "urls.py")
wsgiPy := utils.GetFile(files, "wsgi.py")
Expand Down
Loading

0 comments on commit d5c8528

Please sign in to comment.