Skip to content

Commit

Permalink
Merge pull request #11 from wings-software/rahulk_cihack
Browse files Browse the repository at this point in the history
Kotlin changes for maven and gradle
  • Loading branch information
rahkumar56 authored Dec 20, 2023
2 parents 877e342 + 1a8f97d commit a9e2f45
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
4 changes: 3 additions & 1 deletion builder/rules/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func (h Harness) GetRules() []Rule {
harness.ConfigureRuby,
harness.ConfigureRust,
harness.ConfigureSwift,
harness.ConfigureDocker,
harness.ConfigureDefault,
harness.ConfigureKotlin,
harness.ConfigureKotlinwithMaven,
harness.ConfigureDocker,
harness.ConfigureScala,
harness.ConfigurePHP,
}
Expand Down
61 changes: 61 additions & 0 deletions builder/rules/harness/rule_kotlin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package harness

import (
"io/fs"

spec "github.com/drone/spec/dist/go"
"github.com/wings-software/autogen-go/utils"
)

// ConfigureGo configures a Go step.
func ConfigureKotlin(fsys fs.FS, pipeline *spec.Pipeline) error {
stage := pipeline.Stages[0].Spec.(*spec.StageCI)

// check for the go.mod file.
if !utils.Exists(fsys, "build.gradle.kts") {
return nil
}

// check if we should use a container-based
// execution environment.
useImage := utils.IsContainerRuntime(pipeline)

// add the go build step
{
script := new(spec.StepExec)
script.Run = "./gradlew build"

if useImage {
script.Image = "kotlin"
}

step := new(spec.Step)
step.Name = "kotlin_build"
step.Type = "script"
step.Spec = script

stage.Steps = append(stage.Steps, step)
}

// add the go test step
{
script := new(spec.StepExec)
script.Run = "./gradlew test"

if useImage {
script.Image = "kotlin"
}

step := new(spec.Step)
step.Name = "kotlin_test"
step.Type = "script"
step.Spec = script
stage.Steps = append(stage.Steps, step)
}

return nil
}
63 changes: 63 additions & 0 deletions builder/rules/harness/rule_kotlin_mvn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package harness

import (
"fmt"
"io/fs"

spec "github.com/drone/spec/dist/go"
"github.com/wings-software/autogen-go/utils"
)

// ConfigureGo configures a Go step.
func ConfigureKotlinwithMaven(fsys fs.FS, pipeline *spec.Pipeline) error {
stage := pipeline.Stages[0].Spec.(*spec.StageCI)

// check for the go.mod file.
if !(utils.Exists(fsys, "pom.xml") && utils.IsDirectoryPresent(fsys, "/src/test/kotlin")) {
fmt.Println("Error getting current kotlin directory:")
return nil
}

// check if we should use a container-based
// execution environment.
useImage := utils.IsContainerRuntime(pipeline)

// add the go build step
{
script := new(spec.StepExec)
script.Run = "mvn clean compile"

if useImage {
script.Image = "maven"
}

step := new(spec.Step)
step.Name = "kotlin_mvn_build"
step.Type = "script"
step.Spec = script

stage.Steps = append(stage.Steps, step)
}

// add the go test step
{
script := new(spec.StepExec)
script.Run = "mvn clean test"

if useImage {
script.Image = "maven"
}

step := new(spec.Step)
step.Name = "kotlin_mvn_test"
step.Type = "script"
step.Spec = script
stage.Steps = append(stage.Steps, step)
}

return nil
}
35 changes: 35 additions & 0 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package utils
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"

spec "github.com/drone/spec/dist/go"
)
Expand All @@ -31,6 +33,39 @@ func Exists(fsys fs.FS, name string) bool {
return err == nil
}

// // helper function returns true if the dir exist in base path
// at the base path.
func IsDirectoryPresent(fsys fs.FS, directoryPath string) bool {
// Use os.Stat to check if the directory exists
// Get the current working directory
currentDir, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current working directory:", err)
//return
}

fmt.Println("Current Working Directory:", currentDir)
fmt.Println("kotlin Directory:", currentDir+directoryPath)

_, err = os.Stat(directoryPath + directoryPath)

// Check if there is no error and the path exists and is a directory
if err == nil {
fmt.Println("kotlin Directory is present:", currentDir+directoryPath)
return true
}

// Check if the error is due to the directory not existing
if os.IsNotExist(err) {
fmt.Println("kotlin Directory is not present:", currentDir+directoryPath)
return false
}

// Handle other errors (e.g., permission issues)
fmt.Println("Error checking directory:", err)
return false
}

// helper function reads the named file at the base path.
func Read(fsys fs.FS, name string) ([]byte, error) {
return fsys.(fs.ReadFileFS).ReadFile(name)
Expand Down

0 comments on commit a9e2f45

Please sign in to comment.