diff --git a/builder/rules/harness.go b/builder/rules/harness.go index d2fb13a..f4d4a45 100644 --- a/builder/rules/harness.go +++ b/builder/rules/harness.go @@ -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, } diff --git a/builder/rules/harness/rule_kotlin.go b/builder/rules/harness/rule_kotlin.go new file mode 100644 index 0000000..d118b4e --- /dev/null +++ b/builder/rules/harness/rule_kotlin.go @@ -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 +} diff --git a/builder/rules/harness/rule_kotlin_mvn.go b/builder/rules/harness/rule_kotlin_mvn.go new file mode 100644 index 0000000..a58ff38 --- /dev/null +++ b/builder/rules/harness/rule_kotlin_mvn.go @@ -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 +} diff --git a/utils/util.go b/utils/util.go index 4493fc1..38048b8 100644 --- a/utils/util.go +++ b/utils/util.go @@ -7,7 +7,9 @@ package utils import ( "encoding/json" "errors" + "fmt" "io/fs" + "os" spec "github.com/drone/spec/dist/go" ) @@ -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)