github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/MtaUtils.groovy (about)

     1  package com.sap.piper
     2  
     3  import java.util.Map
     4  
     5  import hudson.AbortException
     6  
     7  class MtaUtils {
     8  
     9      final protected script
    10  
    11      protected MtaUtils(script) {
    12          this.script = script
    13      }
    14  
    15      def generateMtaDescriptorFromPackageJson (String srcPackageJson, String targetMtaDescriptor, String applicationName)  throws Exception{
    16          if (!srcPackageJson) throw new IllegalArgumentException("The parameter 'srcPackageJson' can not be null or empty.")
    17          if (!targetMtaDescriptor) throw new IllegalArgumentException("The parameter 'targetMtaDescriptor' can not be null or empty.")
    18          if (!applicationName) throw new IllegalArgumentException("The parameter 'applicationName' can not be null or empty.")
    19  
    20          if (!script.fileExists(srcPackageJson)) throw new AbortException("'${srcPackageJson}' does not exist.")
    21  
    22          def dataFromJson = script.readJSON file: srcPackageJson
    23  
    24          def mtaData  = script.readYaml text: script.libraryResource('template_mta.yaml')
    25  
    26          if(!dataFromJson.name) throw new AbortException("'name' not set in the given package.json.")
    27          mtaData['ID'] = dataFromJson.name
    28  
    29          if(!dataFromJson.version) throw new AbortException("'version' not set in the given package.json.")
    30          mtaData['version'] = dataFromJson.version
    31          mtaData['modules'][0]['parameters']['version'] = "${dataFromJson.version}-\${timestamp}"
    32          mtaData['modules'][0]['parameters']['name'] = applicationName
    33  
    34          mtaData['modules'][0]['name'] = applicationName
    35  
    36          script.writeYaml file: targetMtaDescriptor, data: mtaData
    37  
    38          if (!script.fileExists(targetMtaDescriptor)) throw new AbortException("'${targetMtaDescriptor}' has not been generated.")
    39      }
    40  }