Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin changes for maven and gradle #11

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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