This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
- Loading branch information
Showing
3 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. | ||
******************************************************************************/ | ||
package recognizer | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/redhat-developer/alizer/pkg/apis/language" | ||
) | ||
|
||
type DevFileType struct { | ||
Name string | ||
Language string | ||
ProjectType string | ||
Tags []string | ||
} | ||
|
||
func SelectDevFileFromTypes(path string, devFileTypes []DevFileType) (DevFileType, error) { | ||
languages, err := Analyze(path) | ||
if err != nil { | ||
return DevFileType{}, err | ||
} | ||
for _, language := range languages { | ||
devfile, err := selectDevFileByLanguage(language, devFileTypes) | ||
if err == nil { | ||
return devfile, nil | ||
} | ||
} | ||
return DevFileType{}, errors.New("No valid devfile found for project in " + path) | ||
} | ||
|
||
func selectDevFileByLanguage(language language.Language, devFileTypes []DevFileType) (DevFileType, error) { | ||
scoreTarget := 0 | ||
devfileTarget := DevFileType{} | ||
FRAMEWORK_WEIGHT := 10 | ||
TOOL_WEIGHT := 5 | ||
for _, devFile := range devFileTypes { | ||
score := 0 | ||
if strings.EqualFold(devFile.Language, language.Name) || matches(language.Aliases, devFile.Language) { | ||
score++ | ||
if matches(language.Frameworks, devFile.ProjectType) { | ||
score += FRAMEWORK_WEIGHT | ||
} | ||
for _, tag := range devFile.Tags { | ||
if matches(language.Frameworks, tag) { | ||
score += FRAMEWORK_WEIGHT | ||
} | ||
if matches(language.Tools, tag) { | ||
score += TOOL_WEIGHT | ||
} | ||
} | ||
} | ||
if score > scoreTarget { | ||
scoreTarget = score | ||
devfileTarget = devFile | ||
} | ||
} | ||
|
||
if scoreTarget == 0 { | ||
return devfileTarget, errors.New("No valid devfile found for current language " + language.Name) | ||
} | ||
return devfileTarget, nil | ||
} | ||
|
||
func matches(values []string, valueToFind string) bool { | ||
for _, value := range values { | ||
if strings.EqualFold(value, valueToFind) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package recognizer | ||
|
||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. | ||
******************************************************************************/ | ||
import ( | ||
"testing" | ||
|
||
"github.com/redhat-developer/alizer/pkg/apis/recognizer" | ||
) | ||
|
||
func TestDetectQuarkusDevfile(t *testing.T) { | ||
detectDevFile(t, "quarkus", "java-quarkus") | ||
} | ||
|
||
func TestDetectMicronautDevfile(t *testing.T) { | ||
detectDevFile(t, "micronaut", "java-maven") | ||
} | ||
|
||
func TestDetectNodeJSDevfile(t *testing.T) { | ||
detectDevFile(t, "nodejs-ex", "nodejs") | ||
} | ||
|
||
func TestDetectDjangoDevfile(t *testing.T) { | ||
detectDevFile(t, "django", "python-django") | ||
} | ||
|
||
func detectDevFile(t *testing.T, projectName string, devFileName string) { | ||
devFileTypes := getDevFileTypes() | ||
|
||
testingProjectPath := GetTestProjectPath(projectName) | ||
|
||
devFileType, err := recognizer.SelectDevFileFromTypes(testingProjectPath, devFileTypes) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if devFileType.Name != devFileName { | ||
t.Error("Expected value " + devFileName + " but it was" + devFileType.Name) | ||
} | ||
} | ||
|
||
func getDevFileTypes() []recognizer.DevFileType { | ||
return []recognizer.DevFileType{ | ||
{ | ||
Name: "java", | ||
Language: "java", | ||
ProjectType: "java", | ||
Tags: make([]string, 0), | ||
}, | ||
{ | ||
Name: "java-quarkus", | ||
Language: "java", | ||
ProjectType: "quarkus", | ||
Tags: []string{ | ||
"Java", | ||
"Quarkus", | ||
}, | ||
}, | ||
{ | ||
Name: "java-maven", | ||
Language: "java", | ||
ProjectType: "java", | ||
Tags: []string{ | ||
"Java", | ||
"Maven", | ||
}, | ||
}, | ||
{ | ||
Name: "java-spring", | ||
Language: "java", | ||
ProjectType: "spring", | ||
Tags: []string{ | ||
"Java", | ||
"Spring", | ||
}, | ||
}, | ||
{ | ||
Name: "java-vertx", | ||
Language: "java", | ||
ProjectType: "vertx", | ||
Tags: []string{ | ||
"Java", | ||
"Vert.x", | ||
}, | ||
}, | ||
{ | ||
Name: "java-wildfly", | ||
Language: "java", | ||
ProjectType: "wildfly", | ||
Tags: []string{ | ||
"Java", | ||
"Wildfly", | ||
}, | ||
}, | ||
{ | ||
Name: "nodejs", | ||
Language: "nodejs", | ||
ProjectType: "nodejs", | ||
Tags: []string{ | ||
"NodeJS", | ||
"Express", | ||
}, | ||
}, | ||
{ | ||
Name: "python-django", | ||
Language: "python", | ||
ProjectType: "django", | ||
Tags: []string{ | ||
"Python", | ||
"pip", | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters