From d5586d37d2dfd01e4ef22e16c36f1b942f849f98 Mon Sep 17 00:00:00 2001 From: Hariharan Subramanian Date: Fri, 2 Jun 2023 20:35:57 +0000 Subject: [PATCH] changes to detect jre version from build.gradle --- cmd/create.go | 38 ++++++++++++++++++++-- pkg/config/draftconfig.go | 1 + pkg/prompts/prompts.go | 23 ++++++++++++- template/dockerfiles/gradlew/.dockerignore | 2 ++ template/dockerfiles/gradlew/Dockerfile | 17 ++++++++++ template/dockerfiles/gradlew/draft.yaml | 22 +++++++++++++ 6 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 template/dockerfiles/gradlew/.dockerignore create mode 100644 template/dockerfiles/gradlew/Dockerfile create mode 100644 template/dockerfiles/gradlew/draft.yaml diff --git a/cmd/create.go b/cmd/create.go index 3e8db48b1..c8378c799 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -1,6 +1,7 @@ package cmd import ( + "bufio" "encoding/json" "errors" "fmt" @@ -59,6 +60,7 @@ type createCmd struct { } func newCreateCmd() *cobra.Command { + log.SetLevel(log.TraceLevel) cc := &createCmd{} cmd := &cobra.Command{ @@ -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() @@ -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" } } } @@ -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) @@ -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()) } diff --git a/pkg/config/draftconfig.go b/pkg/config/draftconfig.go index 62e92869c..b2f381dc8 100644 --- a/pkg/config/draftconfig.go +++ b/pkg/config/draftconfig.go @@ -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 } diff --git a/pkg/prompts/prompts.go b/pkg/prompts/prompts.go index 80e68f974..ccd87292a 100644 --- a/pkg/prompts/prompts.go +++ b/pkg/prompts/prompts.go @@ -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 { @@ -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 := "" diff --git a/template/dockerfiles/gradlew/.dockerignore b/template/dockerfiles/gradlew/.dockerignore new file mode 100644 index 000000000..843dec4fd --- /dev/null +++ b/template/dockerfiles/gradlew/.dockerignore @@ -0,0 +1,2 @@ +Dockerfile +charts/ diff --git a/template/dockerfiles/gradlew/Dockerfile b/template/dockerfiles/gradlew/Dockerfile new file mode 100644 index 000000000..8b1be74d0 --- /dev/null +++ b/template/dockerfiles/gradlew/Dockerfile @@ -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"] diff --git a/template/dockerfiles/gradlew/draft.yaml b/template/dockerfiles/gradlew/draft.yaml new file mode 100644 index 000000000..40911be71 --- /dev/null +++ b/template/dockerfiles/gradlew/draft.yaml @@ -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" \ No newline at end of file