From 56b7b39c327f7e0f577ebde7a30cfbaa0d89dfb9 Mon Sep 17 00:00:00 2001 From: rahkumar56 <139110109+rahkumar56@users.noreply.github.com> Date: Thu, 30 Nov 2023 11:03:29 +0530 Subject: [PATCH] Added gradle and maven rules --- builder/rules/harness.go | 4 +- builder/rules/harness/rule_docker.go | 8 +-- builder/rules/harness/rule_kotlin.go | 61 +++++++++++++++++++++++ builder/rules/harness/rule_kotlin_mvn.go | 63 ++++++++++++++++++++++++ utils/util.go | 35 +++++++++++++ 5 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 builder/rules/harness/rule_kotlin.go create mode 100644 builder/rules/harness/rule_kotlin_mvn.go diff --git a/builder/rules/harness.go b/builder/rules/harness.go index 7fdb11e..e4999b5 100644 --- a/builder/rules/harness.go +++ b/builder/rules/harness.go @@ -21,7 +21,9 @@ func (h Harness) GetRules() []Rule { harness.ConfigureRuby, harness.ConfigureRust, harness.ConfigureSwift, - harness.ConfigureDocker, harness.ConfigureDefault, + harness.ConfigureKotlin, + harness.ConfigureKotlinwithMaven, + harness.ConfigureDocker, } } diff --git a/builder/rules/harness/rule_docker.go b/builder/rules/harness/rule_docker.go index b43db11..fe2739c 100644 --- a/builder/rules/harness/rule_docker.go +++ b/builder/rules/harness/rule_docker.go @@ -35,9 +35,11 @@ func ConfigureDocker(fsys fs.FS, pipeline *spec.Pipeline) error { script := new(spec.StepPlugin) script.Image = "plugins/docker" script.With = map[string]interface{}{ - "tags": "latest", - "repo": repo, - "dry_run": true, + "tags": "latest", + "repo": repo, + "dry_run": true, + "username": "<+input>", + "password": "<+input>", } if useImage { 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 78349c9..938e2b2 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)