github.com/SAP/jenkins-library@v1.362.0/pkg/versioning/gradle.go (about)

     1  package versioning
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/command"
    10  	"github.com/SAP/jenkins-library/pkg/gradle"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  )
    13  
    14  type gradleExecRunner interface {
    15  	Stdout(out io.Writer)
    16  	Stderr(err io.Writer)
    17  	RunExecutable(e string, p ...string) error
    18  }
    19  
    20  // Gradle defines a maven artifact used for versioning
    21  type Gradle struct {
    22  	execRunner     gradleExecRunner
    23  	utils          gradle.Utils
    24  	gradlePropsOut []byte
    25  	path           string
    26  	propertiesFile *PropertiesFile
    27  	versionField   string
    28  	writeFile      func(string, []byte, os.FileMode) error
    29  }
    30  
    31  func (g *Gradle) init() error {
    32  	if g.writeFile == nil {
    33  		g.writeFile = os.WriteFile
    34  	}
    35  
    36  	if g.propertiesFile == nil {
    37  		g.propertiesFile = &PropertiesFile{
    38  			path:             g.path,
    39  			versioningScheme: g.VersioningScheme(),
    40  			versionField:     g.versionField,
    41  			writeFile:        g.writeFile,
    42  		}
    43  		f, err := os.Open(g.path)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		fi, err := f.Stat()
    48  		if err != nil {
    49  			return err
    50  		}
    51  		if fi.IsDir() {
    52  			g.propertiesFile.path += "build.gradle"
    53  		}
    54  		err = g.propertiesFile.init()
    55  		if err != nil {
    56  			return err
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func (g *Gradle) initGetArtifact() error {
    63  	if g.execRunner == nil {
    64  		g.execRunner = &command.Command{}
    65  	}
    66  
    67  	if g.gradlePropsOut == nil {
    68  		gradleOptions := &gradle.ExecuteOptions{
    69  			Task:       "properties",
    70  			UseWrapper: true,
    71  		}
    72  		stdOut, err := gradle.Execute(gradleOptions, g.utils)
    73  		if err != nil {
    74  			log.Entry().WithError(err).Errorf("failed to retrieve properties of the gradle project: %v", err)
    75  			return err
    76  		}
    77  		g.gradlePropsOut = []byte(stdOut)
    78  	}
    79  	return nil
    80  }
    81  
    82  // VersioningScheme returns the relevant versioning scheme
    83  func (g *Gradle) VersioningScheme() string {
    84  	return "semver2"
    85  }
    86  
    87  // GetCoordinates reads the coordinates from the maven pom.xml descriptor file
    88  func (g *Gradle) GetCoordinates() (Coordinates, error) {
    89  	result := Coordinates{}
    90  	var err error
    91  	result.GroupID, err = g.GetGroupID()
    92  	if err != nil {
    93  		return result, err
    94  	}
    95  	result.ArtifactID, err = g.GetArtifactID()
    96  	if err != nil {
    97  		return result, err
    98  	}
    99  	result.Version, err = g.GetVersion()
   100  	if err != nil {
   101  		return result, err
   102  	}
   103  	// result.Packaging, err = g.GetPackaging()
   104  	// if err != nil {
   105  	// 	return nil, err
   106  	// }
   107  	return result, nil
   108  }
   109  
   110  // GetPackaging returns the current ID of the Group
   111  // func (g *Gradle) GetPackaging() (string, error) {
   112  // 	g.init()
   113  
   114  // 	packaging, err := g.runner.Evaluate(&g.options, "project.packaging", g.execRunner)
   115  // 	if err != nil {
   116  // 		return "", errors.Wrap(err, "Gradle - getting packaging failed")
   117  // 	}
   118  // 	return packaging, nil
   119  // }
   120  
   121  // GetGroupID returns the current ID of the Group
   122  func (g *Gradle) GetGroupID() (string, error) {
   123  	err := g.initGetArtifact()
   124  	if err != nil {
   125  		return "", err
   126  	}
   127  
   128  	regex := regexp.MustCompile(`(?m:^group: (.*))`)
   129  	match := string(regex.Find(g.gradlePropsOut))
   130  	groupID := strings.Split(match, ` `)[1]
   131  
   132  	return groupID, nil
   133  }
   134  
   135  // GetArtifactID returns the current ID of the artifact
   136  func (g *Gradle) GetArtifactID() (string, error) {
   137  	err := g.initGetArtifact()
   138  	if err != nil {
   139  		return "", err
   140  	}
   141  
   142  	regex := regexp.MustCompile(`(?m:^rootProject: root project '(.*)')`)
   143  	match := string(regex.Find(g.gradlePropsOut))
   144  	artifactID := strings.Split(match, `'`)[1]
   145  
   146  	return artifactID, nil
   147  }
   148  
   149  // GetVersion returns the current version of the artifact
   150  func (g *Gradle) GetVersion() (string, error) {
   151  	err := g.init()
   152  	if err != nil {
   153  		return "", err
   154  	}
   155  
   156  	return g.propertiesFile.GetVersion()
   157  }
   158  
   159  // SetVersion updates the version of the artifact
   160  func (g *Gradle) SetVersion(version string) error {
   161  	err := g.init()
   162  	if err != nil {
   163  		return err
   164  	}
   165  	return g.propertiesFile.SetVersion(version)
   166  }