github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/gradle/gradle.go (about)

     1  package gradle
     2  
     3  import (
     4  	"fmt"
     5  	gofrogcmd "github.com/jfrog/gofrog/io"
     6  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
     7  	"github.com/jfrog/jfrog-cli-go/utils/cliutils"
     8  	"github.com/jfrog/jfrog-cli-go/utils/config"
     9  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    10  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
    11  	"github.com/jfrog/jfrog-client-go/utils/log"
    12  	"io"
    13  	"io/ioutil"
    14  	"os"
    15  	"os/exec"
    16  	"path"
    17  	"path/filepath"
    18  	"strings"
    19  )
    20  
    21  const gradleExtractorDependencyVersion = "4.15.0"
    22  const gradleInitScriptTemplate = "gradle.init"
    23  
    24  const usePlugin = "useplugin"
    25  const useWrapper = "usewrapper"
    26  const gradleBuildInfoProperties = "BUILDINFO_PROPFILE"
    27  
    28  type GradleCommand struct {
    29  	tasks         string
    30  	configPath    string
    31  	configuration *utils.BuildConfiguration
    32  	rtDetails     *config.ArtifactoryDetails
    33  	threads       int
    34  }
    35  
    36  func NewGradleCommand() *GradleCommand {
    37  	return &GradleCommand{}
    38  }
    39  
    40  // Returns the ArtfiactoryDetails. The information returns from the config file provided.
    41  func (gc *GradleCommand) RtDetails() (*config.ArtifactoryDetails, error) {
    42  	// Get the rtDetails from the config file.
    43  	var err error
    44  	if gc.rtDetails == nil {
    45  		vConfig, err := utils.ReadConfigFile(gc.configPath, utils.YAML)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  		gc.rtDetails, err = utils.GetRtDetails(vConfig)
    50  	}
    51  	return gc.rtDetails, err
    52  }
    53  
    54  func (gc *GradleCommand) SetRtDetails(rtDetails *config.ArtifactoryDetails) *GradleCommand {
    55  	gc.rtDetails = rtDetails
    56  	return gc
    57  }
    58  
    59  func (gc *GradleCommand) Run() error {
    60  	gradleDependenciesDir, gradlePluginFilename, err := downloadGradleDependencies()
    61  	if err != nil {
    62  		return err
    63  	}
    64  	gradleRunConfig, err := createGradleRunConfig(gc.tasks, gc.configPath, gc.configuration, gc.threads, gradleDependenciesDir, gradlePluginFilename)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	defer os.Remove(gradleRunConfig.env[gradleBuildInfoProperties])
    69  	if err := gofrogcmd.RunCmd(gradleRunConfig); err != nil {
    70  		return err
    71  	}
    72  	return nil
    73  }
    74  
    75  func (gc *GradleCommand) CommandName() string {
    76  	return "rt_gradle"
    77  }
    78  
    79  func (gc *GradleCommand) SetConfiguration(configuration *utils.BuildConfiguration) *GradleCommand {
    80  	gc.configuration = configuration
    81  	return gc
    82  }
    83  
    84  func (gc *GradleCommand) SetConfigPath(configPath string) *GradleCommand {
    85  	gc.configPath = configPath
    86  	return gc
    87  }
    88  
    89  func (gc *GradleCommand) SetTasks(tasks string) *GradleCommand {
    90  	gc.tasks = tasks
    91  	return gc
    92  }
    93  
    94  func (gc *GradleCommand) SetThreads(threads int) *GradleCommand {
    95  	gc.threads = threads
    96  	return gc
    97  }
    98  
    99  func downloadGradleDependencies() (gradleDependenciesDir, gradlePluginFilename string, err error) {
   100  	dependenciesPath, err := config.GetJfrogDependenciesPath()
   101  	if err != nil {
   102  		return
   103  	}
   104  	gradleDependenciesDir = filepath.Join(dependenciesPath, "gradle", gradleExtractorDependencyVersion)
   105  	gradlePluginFilename = fmt.Sprintf("build-info-extractor-gradle-%s-uber.jar", gradleExtractorDependencyVersion)
   106  
   107  	filePath := fmt.Sprintf("org/jfrog/buildinfo/build-info-extractor-gradle/%s", gradleExtractorDependencyVersion)
   108  	downloadPath := path.Join(filePath, gradlePluginFilename)
   109  
   110  	filepath.Join(gradleDependenciesDir, gradlePluginFilename)
   111  	err = utils.DownloadExtractorIfNeeded(downloadPath, filepath.Join(gradleDependenciesDir, gradlePluginFilename))
   112  	return
   113  }
   114  
   115  func createGradleRunConfig(tasks, configPath string, configuration *utils.BuildConfiguration, threads int, gradleDependenciesDir, gradlePluginFilename string) (*gradleRunConfig, error) {
   116  	runConfig := &gradleRunConfig{env: map[string]string{}}
   117  	runConfig.tasks = tasks
   118  
   119  	vConfig, err := utils.ReadConfigFile(configPath, utils.YAML)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	runConfig.gradle, err = getGradleExecPath(vConfig.GetBool(useWrapper))
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	if threads > 0 {
   130  		vConfig.Set(utils.FORK_COUNT, threads)
   131  	}
   132  
   133  	runConfig.env[gradleBuildInfoProperties], err = utils.CreateBuildInfoPropertiesFile(configuration.BuildName, configuration.BuildNumber, vConfig, utils.Gradle)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	if !vConfig.GetBool(usePlugin) {
   139  		runConfig.initScript, err = getInitScript(gradleDependenciesDir, gradlePluginFilename)
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  	}
   144  
   145  	return runConfig, nil
   146  }
   147  
   148  func getInitScript(gradleDependenciesDir, gradlePluginFilename string) (string, error) {
   149  	gradleDependenciesDir, err := filepath.Abs(gradleDependenciesDir)
   150  	if err != nil {
   151  		return "", errorutils.CheckError(err)
   152  	}
   153  	initScriptPath := filepath.Join(gradleDependenciesDir, gradleInitScriptTemplate)
   154  
   155  	exists, err := fileutils.IsFileExists(initScriptPath, false)
   156  	if exists || err != nil {
   157  		return initScriptPath, err
   158  	}
   159  
   160  	gradlePluginPath := filepath.Join(gradleDependenciesDir, gradlePluginFilename)
   161  	gradlePluginPath = strings.Replace(gradlePluginPath, "\\", "\\\\", -1)
   162  	initScriptContent := strings.Replace(utils.GradleInitScript, "${pluginLibDir}", gradlePluginPath, -1)
   163  	if !fileutils.IsPathExists(gradleDependenciesDir, false) {
   164  		err = os.MkdirAll(gradleDependenciesDir, 0777)
   165  		if errorutils.CheckError(err) != nil {
   166  			return "", err
   167  		}
   168  	}
   169  
   170  	return initScriptPath, errorutils.CheckError(ioutil.WriteFile(initScriptPath, []byte(initScriptContent), 0644))
   171  }
   172  
   173  type gradleRunConfig struct {
   174  	gradle     string
   175  	tasks      string
   176  	initScript string
   177  	env        map[string]string
   178  }
   179  
   180  func (config *gradleRunConfig) GetCmd() *exec.Cmd {
   181  	var cmd []string
   182  	cmd = append(cmd, config.gradle)
   183  	if config.initScript != "" {
   184  		cmd = append(cmd, "--init-script", config.initScript)
   185  	}
   186  	cmd = append(cmd, strings.Split(config.tasks, " ")...)
   187  
   188  	log.Info("Running gradle command:", strings.Join(cmd, " "))
   189  	return exec.Command(cmd[0], cmd[1:]...)
   190  }
   191  
   192  func (config *gradleRunConfig) GetEnv() map[string]string {
   193  	return config.env
   194  }
   195  
   196  func (config *gradleRunConfig) GetStdWriter() io.WriteCloser {
   197  	return nil
   198  }
   199  
   200  func (config *gradleRunConfig) GetErrWriter() io.WriteCloser {
   201  	return nil
   202  }
   203  
   204  func getGradleExecPath(useWrapper bool) (string, error) {
   205  	if useWrapper {
   206  		if cliutils.IsWindows() {
   207  			return "gradlew.bat", nil
   208  		}
   209  		return "./gradlew", nil
   210  	}
   211  	gradleExec, err := exec.LookPath("gradle")
   212  	if err != nil {
   213  		return "", errorutils.CheckError(err)
   214  	}
   215  	return gradleExec, nil
   216  }