Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to detect jre version from build.gradle #214

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9841f8d
changes to detect jre version from build.gradle
hsubramanianaks Jun 2, 2023
958b352
add logic for gradlew target comptability
hsubramanianaks Jun 5, 2023
0957af4
add extractors and python
davidgamero Jun 7, 2023
dd918aa
remove log statements for compile error
hsubramanianaks Jun 7, 2023
86dedad
UT for detect default method
hsubramanianaks Jun 8, 2023
a20780a
remove log level
hsubramanianaks Jun 8, 2023
6c0d174
func for handling multiple separators
hsubramanianaks Jun 8, 2023
597106e
add UT's for prompts
hsubramanianaks Jun 8, 2023
843328e
address review comments
hsubramanianaks Jun 9, 2023
70ec207
Merge branch 'main' into gradlew-changes
Tatsinnit Jun 9, 2023
fffb7c1
python fixes
davidgamero Jun 12, 2023
3f954cf
Merge branch 'main' into repo-reader-interface
davidgamero Jun 12, 2023
18f436f
tests for python extractor and reporeader
davidgamero Jun 12, 2023
2678d8e
changes to detect jre version from build.gradle
hsubramanianaks Jun 2, 2023
0c9fdd5
add logic for gradlew target comptability
hsubramanianaks Jun 5, 2023
9abe58c
remove log statements for compile error
hsubramanianaks Jun 7, 2023
b62793c
UT for detect default method
hsubramanianaks Jun 8, 2023
58d004d
remove log level
hsubramanianaks Jun 8, 2023
f1a3246
func for handling multiple separators
hsubramanianaks Jun 8, 2023
a0edbbe
add UT's for prompts
hsubramanianaks Jun 8, 2023
49ca712
address review comments
hsubramanianaks Jun 9, 2023
2bc3e34
fix test reporeader depth
davidgamero Jun 13, 2023
aa26f7c
Merge branch 'main' into repo-reader-interface
davidgamero Jun 14, 2023
4a0193c
move repo init to initConfig
davidgamero Jun 14, 2023
540762e
interface changes for gradle
hsubramanianaks Jun 15, 2023
3e378e8
Merge branch 'gradlew-changes' of github.com:hsubramanianaks/draft in…
hsubramanianaks Jun 15, 2023
655815f
Merge branch 'main' into gradlew-changes
hsubramanianaks Jun 15, 2023
0145ff9
Merge branch 'gradlew-changes' of github.com:hsubramanianaks/draft in…
hsubramanianaks Jun 15, 2023
eee8222
gracefully handle missing reporeader, and move it back to run method
davidgamero Jun 15, 2023
73280f2
changes for gradle with reporeader
hsubramanianaks Jun 16, 2023
7e81036
remove old method
hsubramanianaks Jun 16, 2023
1081281
Merge branch 'repo-reader-interface' of https://github.com/davidgamer…
hsubramanianaks Jun 16, 2023
bf06e5f
changes for extracting PORT from gradle
hsubramanianaks Jun 16, 2023
f2e0631
Merge branch 'main' into gradlew-changes
hsubramanianaks Jun 16, 2023
f7390fa
remove log level statement
hsubramanianaks Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func newCreateCmd() *cobra.Command {
}

func (cc *createCmd) initConfig() error {
log.SetLevel(log.DebugLevel)
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
if cc.createConfigPath != "" {
log.Debug("loading config")
configBytes, err := os.ReadFile(cc.createConfigPath)
Expand Down Expand Up @@ -198,8 +199,8 @@ func (cc *createCmd) detectLanguage() (*config.DraftConfig, string, error) {
if lang.Language == "Java" {

selection := &promptui.Select{
Label: "Linguist detected Java, are you using maven or gradle?",
Items: []string{"gradle", "maven"},
Label: "Linguist detected Java, are you using maven or gradle or gradle wrapper?",
Items: []string{"gradle", "maven", "gradlew"},
Tatsinnit marked this conversation as resolved.
Show resolved Hide resolved
}

_, selectResponse, err := selection.Run()
Expand All @@ -209,6 +210,8 @@ func (cc *createCmd) detectLanguage() (*config.DraftConfig, string, error) {

if selectResponse == "gradle" {
lang.Language = "Gradle"
} else if selectResponse == "gradlew" {
lang.Language = "Gradlew"
}
}
}
Expand Down Expand Up @@ -264,6 +267,7 @@ func (cc *createCmd) generateDockerfile(langConfig *config.DraftConfig, lowerLan
}
for _, d := range extractedDefaults {
langConfig.VariableDefaults = append(langConfig.VariableDefaults, d)
log.Debugf("adding default %s=%s", d.Name, d.Value)
}

var inputs map[string]string
Expand Down
67 changes: 67 additions & 0 deletions pkg/languages/defaults/gradle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package defaults

import (
"fmt"
"strings"

"github.com/Azure/draft/pkg/reporeader"
log "github.com/sirupsen/logrus"
)

type GradleExtractor struct {
}

// GetName implements reporeader.VariableExtractor
func (*GradleExtractor) GetName() string {
return "gradle"
}

// MatchesLanguage implements reporeader.VariableExtractor
func (*GradleExtractor) MatchesLanguage(lowerlang string) bool {
return lowerlang == "gradle" || lowerlang == "gradlew"
}

// ReadDefaults implements reporeader.VariableExtractor
func (*GradleExtractor) ReadDefaults(r reporeader.RepoReader) (map[string]string, error) {
extractedValues := make(map[string]string)
files, err := r.FindFiles(".", []string{"*.gradle"}, 2)
if err != nil {
return nil, fmt.Errorf("error finding gradle files: %v", err)
}
if len(files) > 0 {
f, err := r.ReadFile(files[0])
if err != nil {
log.Warn("Unable to read build.gradle, skipping detection")
return nil, nil
}
content := string(f)
// this separator is used to split the line from build.gradle ex: sourceCompatibility = '1.8'
// output will be ['sourceCompatibility', '1.8'] or ["sourceCompatibility", "1.8"]
separator := func(c rune) bool {
return c == ' ' || c == '=' || c == '\n' || c == '\r' || c == '\t' || c == '{' || c == '}' || c == '[' || c == ']' || c == '-'
}
// this func takes care of removing the single or double quotes from split array output
cutset := func(c rune) bool { return c == '\'' || c == '"' }
if strings.Contains(content, "sourceCompatibility") || strings.Contains(content, "targetCompatibility") || strings.Contains(content, "server.port") {
stringAfterSplit := strings.FieldsFunc(content, separator)
for i := 0; i < len(stringAfterSplit); i++ {
if stringAfterSplit[i] == "sourceCompatibility" {
detectedVersion := strings.TrimFunc(stringAfterSplit[i+1], cutset)
detectedVersion = detectedVersion + "-jre"
extractedValues["VERSION"] = detectedVersion
} else if stringAfterSplit[i] == "targetCompatibility" {
detectedBuilderVersion := strings.TrimFunc(stringAfterSplit[i+1], cutset)
detectedBuilderVersion = "jdk" + detectedBuilderVersion
extractedValues["BUILDERVERSION"] = detectedBuilderVersion
} else if stringAfterSplit[i] == "server.port" {
detectedPort := strings.TrimFunc(stringAfterSplit[i+1], cutset)
extractedValues["PORT"] = detectedPort
}
}
}
}

return extractedValues, nil
}

var _ reporeader.VariableExtractor = &GradleExtractor{}
116 changes: 116 additions & 0 deletions pkg/languages/defaults/gradle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package defaults

import (
"io/ioutil"
"reflect"
"testing"

"github.com/Azure/draft/pkg/reporeader"
)

func TestGradleExtractor_ReadDefaults(t *testing.T) {
content, err := ioutil.ReadFile("testdata/sample.gradle")
if err != nil {
t.Errorf("error reading sample_build.gradle: %v", err)
}
type args struct {
r reporeader.RepoReader
}
tests := []struct {
name string
args args
want map[string]string
wantErr bool
}{
{
name: "extract gradle jre version with spaces",
args: args{
r: reporeader.TestRepoReader{
Files: map[string][]byte{
"build.gradle": []byte("group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility=\"11\" targetCompatibility='11'"),
},
},
},
want: map[string]string{
"VERSION": "11-jre",
"BUILDERVERSION": "jdk11",
},
wantErr: false,
},
{
name: "extract gradle jre version with new lines",
args: args{
r: reporeader.TestRepoReader{
Files: map[string][]byte{
"build.gradle": []byte("group = 'com.example'\nversion = '0.0.1-SNAPSHOT'\nsourceCompatibility=\"11\"\ntargetCompatibility='11'"),
},
},
},
want: map[string]string{
"VERSION": "11-jre",
"BUILDERVERSION": "jdk11",
},
wantErr: false,
},
{
name: "extract gradle jre version with tabs",
args: args{
r: reporeader.TestRepoReader{
Files: map[string][]byte{
"build.gradle": []byte("group = 'com.example'\tversion = '0.0.1-SNAPSHOT'\tsourceCompatibility= \"12\" \ntargetCompatibility='11'"),
},
},
},
want: map[string]string{
"VERSION": "12-jre",
"BUILDERVERSION": "jdk11",
},
wantErr: false,
},
{
name: "extract gradle jre version with double spaces",
args: args{
r: reporeader.TestRepoReader{
Files: map[string][]byte{
"build.gradle": []byte("group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility=\"12\"\ntargetCompatibility='11'"),
},
},
},
want: map[string]string{
"VERSION": "12-jre",
"BUILDERVERSION": "jdk11",
},
wantErr: false,
},
{
name: "extract gradle jre version reading from a file",
args: args{
r: reporeader.TestRepoReader{
Files: map[string][]byte{
"build.gradle": content,
},
},
},
want: map[string]string{
"VERSION": "11-jre",
"BUILDERVERSION": "jdk11",
"PORT": "8081",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := GradleExtractor{}
got, err := p.ReadDefaults(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("ReadDefaults() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadDefaults() got = %v, want %v", got, tt.want)
}
})
}

}
39 changes: 39 additions & 0 deletions pkg/languages/defaults/testdata/sample.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
plugins {
id 'org.springframework.boot' version '2.6.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = "11"
targetCompatibility = '11'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}

bootRun {
args = ['--server.port=8081']
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
implementation 'com.networknt:json-schema-validator:1.0.66'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}
1 change: 1 addition & 0 deletions pkg/languages/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func CreateLanguagesFromEmbedFS(dockerfileTemplates embed.FS, dest string) *Lang
func (l *Languages) ExtractDefaults(lowerLang string, r reporeader.RepoReader) ([]config.BuilderVarDefault, error) {
extractors := []reporeader.VariableExtractor{
&defaults.PythonExtractor{},
&defaults.GradleExtractor{},
}
extractedValues := make(map[string]string)
var extractedDefaults []config.BuilderVarDefault
Expand Down
2 changes: 2 additions & 0 deletions template/dockerfiles/gradlew/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Dockerfile
charts/
17 changes: 17 additions & 0 deletions template/dockerfiles/gradlew/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM gradle:{{BUILDERVERSION}} as BUILD

COPY --chown=gradle:gradle . /project
COPY gradlew gradlew
COPY gradle/wrapper gradle/wrapper
RUN chmod +x gradle/wrapper
RUN chmod +x gradlew
RUN ./gradlew -i -s -b /project/build.gradle clean build

FROM eclipse-temurin:{{VERSION}}
ENV PORT {{PORT}}
EXPOSE {{PORT}}

COPY --from=BUILD /project/build/libs/* /opt/
WORKDIR /opt/
RUN ls -l
CMD ["/bin/bash", "-c", "find -type f -name '*SNAPSHOT.jar' | xargs java -jar"]
22 changes: 22 additions & 0 deletions template/dockerfiles/gradlew/draft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: gradlew
displayName: Gradlew
nameOverrides:
- path: "dockerignore"
prefix: "."
variables:
- name: "PORT"
description: "the port exposed in the application"
type: int
- name: "BUILDERVERSION"
description: "the version of gradle used during the builder stage to generate the executable"
exampleValues: ["jdk8","jdk11","jdk17","jdk19"]
- name: "VERSION"
description: "the version of openjdk used by the application"
exampleValues: ["8-jre","11-jre","17-jre","19-jre"]
variableDefaults:
- name: "BUILDERVERSION"
value: "jdk11"
- name: "VERSION"
value: "11-jre"
- name: "PORT"
value: "80"
2 changes: 1 addition & 1 deletion test/integration/rust/helm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ deployVariables:
value: "host.minikube.internal:5001/testapp"
languageVariables:
- name: "VERSION"
value: "1.70.0"
value: "1.59.0"
- name: "BUILDERVERSION"
value: "null"
- name: "PORT"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/rust/kustomize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ deployVariables:
value: "host.minikube.internal:5001/testapp"
languageVariables:
- name: "VERSION"
value: "1.70.0"
value: "1.59.0"
- name: "BUILDERVERSION"
value: "null"
- name: "PORT"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/rust/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ deployVariables:
value: "host.minikube.internal:5001/testapp"
languageVariables:
- name: "VERSION"
value: "1.70.0"
value: "1.59.0"
- name: "BUILDERVERSION"
value: "null"
- name: "PORT"
Expand Down