Skip to content

Commit

Permalink
func for handling multiple separators
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubramanianaks committed Jun 8, 2023
1 parent a20780a commit 6c0d174
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 6 additions & 6 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,30 +445,30 @@ func (cc *createCmd) detectDefaults(detectedLang *config.DraftConfig, lowerLang
defer f.Close()
detectedDefaults := make([]config.BuilderVarDefault, 0)
scanner := bufio.NewScanner(f)
separator := func(c rune) bool { return c == ' ' || c == '=' }
cutset := func(c rune) bool { return c == '\'' || c == '"' }
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "sourceCompatibility") {
detectedVersion := strings.Split(line, " = ")[1] // sourceCompatibility = '1.8'
detectedVersion = strings.Trim(detectedVersion, "'")
detectedVersion := strings.FieldsFunc(line, separator)[1] // sourceCompatibility = '1.8'
detectedVersion = strings.TrimFunc(detectedVersion, cutset)
detectedVersion = detectedVersion + "-jre"
builderVarDefault := config.BuilderVarDefault{
Name: "VERSION",
Value: detectedVersion,
}
detectedDefaults = append(detectedDefaults, builderVarDefault)
//log.Info("Detected VERSION is %q from build.gradle for %s project", detectedVersion, lowerLang)
}

if strings.Contains(line, "targetCompatibility") {
detectedBuilderVersion := strings.Split(line, " = ")[1] // targetCompatibility = '1.8'
detectedBuilderVersion = strings.Trim(detectedBuilderVersion, "'")
detectedBuilderVersion := strings.FieldsFunc(line, separator)[1] // targetCompatibility = '1.8'
detectedBuilderVersion = strings.TrimFunc(detectedBuilderVersion, cutset)
detectedBuilderVersion = "jdk" + detectedBuilderVersion
detectedBuilderVar := config.BuilderVarDefault{
Name: "BUILDERVERSION",
Value: detectedBuilderVersion,
}
detectedDefaults = append(detectedDefaults, detectedBuilderVar)
//log.Info("Detected BUILDER VERSION is %s from build.gradle for %s project", detectedBuilderVersion[0], lowerLang)
}
detectedLang.DetectedDefaults = detectedDefaults
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,16 @@ func TestDetectDefaultsNoTargetComptability(t *testing.T) {
assert.Equal(t, "11-jre", mockDraftConfig.DetectedDefaults[0].Value)
os.Remove("build.gradle")
}

func TestDetectDefaultsWithoutSpacesInDoubleQuotes(t *testing.T) {
ioutil.WriteFile("build.gradle", []byte("sourceCompatibility=\"17\"\ntargetCompatibility = '12'"), 0644)
mockCC := &createCmd{}
mockDraftConfig := &config.DraftConfig{}
mockCC.detectDefaults(mockDraftConfig, "gradle")
assert.Equal(t, 2, len(mockDraftConfig.DetectedDefaults))
assert.Equal(t, "VERSION", mockDraftConfig.DetectedDefaults[0].Name)
assert.Equal(t, "17-jre", mockDraftConfig.DetectedDefaults[0].Value)
assert.Equal(t, "BUILDERVERSION", mockDraftConfig.DetectedDefaults[1].Name)
assert.Equal(t, "jdk12", mockDraftConfig.DetectedDefaults[1].Value)
os.Remove("build.gradle")
}

0 comments on commit 6c0d174

Please sign in to comment.