Skip to content

Commit

Permalink
changes to detect jre version from build.gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubramanianaks committed Jun 2, 2023
1 parent 580337c commit d5586d3
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
38 changes: 36 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bufio"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -59,6 +60,7 @@ type createCmd struct {
}

func newCreateCmd() *cobra.Command {
log.SetLevel(log.TraceLevel)
cc := &createCmd{}

cmd := &cobra.Command{
Expand Down Expand Up @@ -194,8 +196,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"},
}

_, selectResponse, err := selection.Run()
Expand All @@ -205,6 +207,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 @@ -349,6 +353,8 @@ func (cc *createCmd) createFiles(detectedLang *config.DraftConfig, lowerLang str
return errors.New("can only pass in one of --dockerfile-only and --deployment-only")
}

cc.detectDefaults(detectedLang, lowerLang)

if cc.skipFileDetection {
if !cc.deploymentOnly {
err := cc.generateDockerfile(detectedLang, lowerLang)
Expand Down Expand Up @@ -429,6 +435,34 @@ func (cc *createCmd) createFiles(detectedLang *config.DraftConfig, lowerLang str
return nil
}

func (cc *createCmd) detectDefaults(detectedLang *config.DraftConfig, lowerLang string) {
if lowerLang == "gradlew" || lowerLang == "gradle" {
// read from build.gradle and detect version
f, err := os.Open("build.gradle")
if err != nil {
log.Warn("Unable to read build.gradle, skipping detection")
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "sourceCompatibility") {
detectedVersion := strings.Split(line, " = ")[1] // sourceCompatibility = '1.8'
detectedVersion = strings.Trim(detectedVersion, "'")
detectedVersion = "jdk" + detectedVersion
detectedDefaults := []config.BuilderVarDefault{
{Name: "VERSION", Value: detectedVersion},
}
log.Info("detected version %s", detectedVersion)
log.Info("Detected %s from build.gradle for %s project", detectedVersion, lowerLang)
detectedLang.DetectedDefaults = detectedDefaults
return
}
}
}
}

func init() {
rootCmd.AddCommand(newCreateCmd())
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/draftconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type DraftConfig struct {
NameOverrides []FileNameOverride `yaml:"nameOverrides"`
Variables []BuilderVar `yaml:"variables"`
VariableDefaults []BuilderVarDefault `yaml:"variableDefaults"`
DetectedDefaults []BuilderVarDefault `yaml:"detectedDefaults"`

nameOverrideMap map[string]string
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/prompts/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ func RunPromptsFromConfigWithSkipsIO(config *config.DraftConfig, varsToSkip []st
}
inputs[promptVariableName] = input
} else {
defaultValue := GetVariableDefaultValue(promptVariableName, config.VariableDefaults, inputs)
defaultValue := ""
detectedDefaultValue := GetDetectedDefaultValue(promptVariableName, config.DetectedDefaults, inputs)
if detectedDefaultValue != "" {
defaultValue = detectedDefaultValue
} else {
defaultValue = GetVariableDefaultValue(promptVariableName, config.VariableDefaults, inputs)
}

stringInput, err := RunDefaultableStringPrompt(customPrompt, defaultValue, nil, Stdin, Stdout)
if err != nil {
Expand All @@ -74,6 +80,21 @@ func RunPromptsFromConfigWithSkipsIO(config *config.DraftConfig, varsToSkip []st
return inputs, nil
}

func GetDetectedDefaultValue(variableName string, detectedDefaults []config.BuilderVarDefault, inputs map[string]string) string {
defaultValue := ""
for _, detectedDefault := range detectedDefaults {
if detectedDefault.Name == variableName {
defaultValue = detectedDefault.Value
log.Debugf("setting default value for %s to %s from detected default rule", variableName, defaultValue)
if detectedDefault.ReferenceVar != "" && inputs[detectedDefault.ReferenceVar] != "" {
defaultValue = inputs[detectedDefault.ReferenceVar]
log.Debugf("setting default value for %s to %s from referenceVar %s", variableName, defaultValue, detectedDefault.ReferenceVar)
}
}
}
return defaultValue
}

// GetVariableDefaultValue returns the default value for a variable, if one is set in variableDefaults from a ReferenceVar or literal VariableDefault.Value in that order.
func GetVariableDefaultValue(variableName string, variableDefaults []config.BuilderVarDefault, inputs map[string]string) string {
defaultValue := ""
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"

0 comments on commit d5586d3

Please sign in to comment.