Skip to content

Commit be4ce51

Browse files
committed
adding support for dockerfile components
Signed-off-by: Michael Hoang <mhoang@redhat.com>
1 parent e55ee31 commit be4ce51

File tree

16 files changed

+336
-54
lines changed

16 files changed

+336
-54
lines changed

docs/public/alizer-spec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Component detection is only enabled for a subset of programming languages
156156
- Python
157157
- Rust
158158
- PHP
159+
- Dockerfile
159160

160161
To perform component detection Alizer splits the languages in two sets: `languages with a configuration file` (like Java
161162
which can have a pom.xml or a build.gradle) and `languages without a configuration file` (such as Python which does not have a
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Copyright 2023 Red Hat, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package enricher
17+
18+
import (
19+
"context"
20+
"github.com/devfile/alizer/pkg/apis/model"
21+
)
22+
23+
type DockerEnricher struct{}
24+
25+
type DockerFrameworkDetector interface {
26+
DoPortsDetection(component *model.Component, ctx *context.Context)
27+
}
28+
29+
func (d DockerEnricher) GetSupportedLanguages() []string {
30+
return []string{"dockerfile"}
31+
}
32+
33+
func (d DockerEnricher) DoEnrichLanguage(*model.Language, *[]string) {
34+
// There are no frameworks for the Dockerfile language
35+
return
36+
}
37+
38+
func (d DockerEnricher) DoEnrichComponent(component *model.Component, _ model.DetectionSettings, _ *context.Context) {
39+
projectName := GetDefaultProjectName(component.Path)
40+
component.Name = projectName
41+
42+
var ports []int
43+
ports = GetPortsFromDockerFile(component.Path)
44+
if len(ports) > 0 {
45+
component.Ports = ports
46+
}
47+
return
48+
}
49+
50+
func (d DockerEnricher) IsConfigValidForComponentDetection(language string, config string) bool {
51+
return IsConfigurationValidForLanguage(language, config)
52+
}

pkg/apis/enricher/enricher.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func getEnrichers() []Enricher {
9393
&DotNetEnricher{},
9494
&GoEnricher{},
9595
&PHPEnricher{},
96+
&DockerEnricher{},
9697
}
9798
}
9899

pkg/apis/recognizer/component_recognizer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ func appendIfMissing(components []model.Component, component model.Component) []
226226
}
227227

228228
func getLanguagesByConfigurationFile(configurationPerLanguage map[string][]string, file string) ([]string, error) {
229+
filename := filepath.Base(file)
229230
for regex, languages := range configurationPerLanguage {
230-
if match, _ := regexp.MatchString(regex, file); match {
231+
if match, _ := regexp.MatchString(regex, filename); match {
231232
return languages, nil
232233
}
233234
}

pkg/schema/languages.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type LanguagesProperties map[string]LanguageProperties
2929
type LanguageCustomization struct {
3030
ConfigurationFiles []string `yaml:"configuration_files"`
3131
Component bool `yaml:"component"`
32+
ContainerComponent bool `yaml:"container_component"`
3233
ExcludeFolders []string `yaml:"exclude_folders,omitempty"`
3334
Aliases []string `yaml:"aliases"`
3435
Disabled bool `default:"false" yaml:"disable_detection"`

pkg/utils/langfiles/languages_file_handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type LanguageItem struct {
2828
ConfigurationFiles []string
2929
ExcludeFolders []string
3030
Component bool
31+
ContainerComponent bool
3132
disabled bool
3233
}
3334

@@ -87,6 +88,7 @@ func customizeLanguage(languageItem *LanguageItem) {
8788
(*languageItem).ConfigurationFiles = customization.ConfigurationFiles
8889
(*languageItem).ExcludeFolders = customization.ExcludeFolders
8990
(*languageItem).Component = customization.Component
91+
(*languageItem).ContainerComponent = customization.ContainerComponent
9092
(*languageItem).Aliases = appendSlice((*languageItem).Aliases, customization.Aliases)
9193
(*languageItem).disabled = customization.Disabled
9294
}

pkg/utils/langfiles/languages_file_handler_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestGetLanguageByName(t *testing.T) {
118118
},
119119
{
120120
name: "JavaScript",
121-
expectedItem: LanguageItem{Name: "JavaScript", Aliases: []string{"js", "node", "nodejs", "TypeScript"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"[^-]package.json"}, ExcludeFolders: []string{"node_modules"}, Component: true, disabled: false},
121+
expectedItem: LanguageItem{Name: "JavaScript", Aliases: []string{"js", "node", "nodejs", "TypeScript"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"package.json"}, ExcludeFolders: []string{"node_modules"}, Component: true, disabled: false},
122122
expectedErr: nil,
123123
},
124124
{
@@ -133,7 +133,12 @@ func TestGetLanguageByName(t *testing.T) {
133133
},
134134
{
135135
name: "PHP",
136-
expectedItem: LanguageItem{Name: "PHP", Aliases: []string{"inc"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"composer.json", "[^-]package.json"}, ExcludeFolders: []string(nil), Component: true, disabled: false},
136+
expectedItem: LanguageItem{Name: "PHP", Aliases: []string{"inc"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"composer.json", "package.json"}, ExcludeFolders: []string(nil), Component: true, disabled: false},
137+
expectedErr: nil,
138+
},
139+
{
140+
name: "Dockerfile",
141+
expectedItem: LanguageItem{Name: "Dockerfile", Aliases: []string{"Containerfile"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"[Dd]ockerfile(\\.\\w+)?$", "[Cc]ontainerfile(\\.\\w+)?$"}, ExcludeFolders: []string(nil), Component: false, ContainerComponent: true, disabled: false},
137142
expectedErr: nil,
138143
},
139144
}
@@ -194,7 +199,7 @@ func TestGetLanguageByAlias(t *testing.T) {
194199
{
195200
name: "JavaScript",
196201
alias: "TypeScript",
197-
expectedItem: LanguageItem{Name: "JavaScript", Aliases: []string{"js", "node", "nodejs", "TypeScript"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"[^-]package.json"}, ExcludeFolders: []string{"node_modules"}, Component: true, disabled: false},
202+
expectedItem: LanguageItem{Name: "JavaScript", Aliases: []string{"js", "node", "nodejs", "TypeScript"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"package.json"}, ExcludeFolders: []string{"node_modules"}, Component: true, disabled: false},
198203
expectedErr: nil,
199204
},
200205
{
@@ -206,7 +211,13 @@ func TestGetLanguageByAlias(t *testing.T) {
206211
{
207212
name: "PHP",
208213
alias: "inc",
209-
expectedItem: LanguageItem{Name: "PHP", Aliases: []string{"inc"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"composer.json", "[^-]package.json"}, ExcludeFolders: []string(nil), Component: true, disabled: false},
214+
expectedItem: LanguageItem{Name: "PHP", Aliases: []string{"inc"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"composer.json", "package.json"}, ExcludeFolders: []string(nil), Component: true, disabled: false},
215+
expectedErr: nil,
216+
},
217+
{
218+
name: "Dockerfile",
219+
alias: "Containerfile",
220+
expectedItem: LanguageItem{Name: "Dockerfile", Aliases: []string{"Containerfile"}, Kind: "programming", Group: "", ConfigurationFiles: []string{"[Dd]ockerfile(\\.\\w+)?$", "[Cc]ontainerfile(\\.\\w+)?$"}, ExcludeFolders: []string(nil), Component: false, ContainerComponent: true, disabled: false},
210221
expectedErr: nil,
211222
},
212223
}

pkg/utils/langfiles/resources/languages-customization.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ C#:
66
- ".*\\.\\w+proj"
77
- "appsettings.json"
88
component: true
9+
Dockerfile:
10+
aliases:
11+
- "Containerfile"
12+
configuration_files:
13+
- "[Dd]ockerfile(\\.\\w+)?$"
14+
- "[Cc]ontainerfile(\\.\\w+)?$"
15+
container_component: true
916
F#:
1017
aliases:
1118
- "dotnet"
@@ -33,12 +40,12 @@ JavaScript:
3340
exclude_folders:
3441
- "node_modules"
3542
configuration_files:
36-
- "[^-]package.json"
43+
- "package.json"
3744
component: true
3845
PHP:
3946
configuration_files:
4047
- "composer.json"
41-
- "[^-]package.json"
48+
- "package.json"
4249
component: true
4350
Python:
4451
configuration_files:
@@ -55,7 +62,7 @@ TypeScript:
5562
exclude_folders:
5663
- "node_modules"
5764
configuration_files:
58-
- "[^-]package.json"
65+
- "package.json"
5966
component: true
6067
Visual Basic .NET:
6168
aliases:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM node:16
2+
3+
# Create app directory
4+
WORKDIR /usr/src/app
5+
6+
# Install app dependencies
7+
# A wildcard is used to ensure both package.json AND package-lock.json are copied
8+
COPY package*.json ./
9+
10+
RUN npm install
11+
12+
# Bundle app source
13+
COPY . .
14+
15+
EXPOSE 8090
16+
CMD [ "node", "server.js" ]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM node:16
2+
3+
# Create app directory
4+
WORKDIR /usr/src/app
5+
6+
# Install app dependencies
7+
# A wildcard is used to ensure both package.json AND package-lock.json are copied
8+
COPY package*.json ./
9+
10+
RUN npm install
11+
12+
# Bundle app source
13+
COPY .. .
14+
15+
EXPOSE 8085
16+
CMD [ "node", "server.js" ]

0 commit comments

Comments
 (0)