2018-10-16 13:07:38 +02:00
|
|
|
import groovy.io.FileType;
|
|
|
|
import org.yaml.snakeyaml.Yaml
|
|
|
|
import org.codehaus.groovy.control.CompilerConfiguration
|
|
|
|
import com.sap.piper.DefaultValueCache
|
|
|
|
import java.util.regex.Matcher
|
|
|
|
|
|
|
|
//
|
2018-10-26 11:48:11 +02:00
|
|
|
// Collects helper functions for rendering the docu
|
2018-10-16 13:07:38 +02:00
|
|
|
//
|
2018-10-26 11:48:11 +02:00
|
|
|
class TemplateHelper {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static createParametersTable(Map parameters) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def t = ''
|
|
|
|
t += '| name | mandatory | default | possible values |\n'
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
parameters.keySet().toSorted().each {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def props = parameters.get(it)
|
|
|
|
t += "| `${it}` | ${props.required ? 'yes' : 'no'} | `${(props.defaultValue ?: 'n/a') }` | ${props.value ?: 'n/a'} |\n"
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
t
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static createParameterDescriptionSection(Map parameters) {
|
|
|
|
def t = ''
|
|
|
|
parameters.keySet().toSorted().each {
|
|
|
|
def props = parameters.get(it)
|
|
|
|
t += "* `${it}` - ${props.docu ?: 'n/a'}\n"
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
t
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
//
|
|
|
|
// Collects generic helper functions
|
|
|
|
//
|
|
|
|
class Helper {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static getConfigHelper(classLoader, roots) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
|
|
|
def compilerConfig = new CompilerConfiguration()
|
|
|
|
compilerConfig.setClasspathList( roots )
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
new GroovyClassLoader(classLoader, compilerConfig, true)
|
2018-10-16 13:07:38 +02:00
|
|
|
.parseClass(new File('src/com/sap/piper/ConfigurationHelper.groovy'))
|
|
|
|
.newInstance()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static getPrepareDefaultValuesStep(def gse) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def prepareDefaultValuesStep = gse.createScript('prepareDefaultValues.groovy', new Binding())
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
prepareDefaultValuesStep.metaClass.handlePipelineStepErrors {
|
|
|
|
m, c -> c()
|
|
|
|
}
|
|
|
|
prepareDefaultValuesStep.metaClass.libraryResource {
|
|
|
|
f -> new File("resources/${f}").text
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
prepareDefaultValuesStep.metaClass.readYaml {
|
|
|
|
m -> new Yaml().load(m.text)
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
2018-10-26 11:48:11 +02:00
|
|
|
|
|
|
|
prepareDefaultValuesStep
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static getDummyScript(def prepareDefaultValuesStep, def stepName) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def _prepareDefaultValuesStep = prepareDefaultValuesStep
|
|
|
|
def _stepName = stepName
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
return new Script() {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def STEP_NAME = _stepName
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def prepareDefaultValues() {
|
|
|
|
_prepareDefaultValuesStep()
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def run() {
|
|
|
|
throw new UnsupportedOperationException()
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
private static normalize(Set p) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def normalized = [] as Set
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def interim = [:]
|
|
|
|
p.each {
|
|
|
|
def parts = it.split('/') as List
|
|
|
|
_normalize(parts, interim)
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
interim.each { k, v -> flatten (normalized, k, v) }
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
normalized
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
private static void _normalize(List parts, Map interim) {
|
|
|
|
if( parts.size >= 1) {
|
|
|
|
if( ! interim[parts.head()]) interim[parts.head()] = [:]
|
|
|
|
_normalize(parts.tail(), interim[parts.head()])
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
private static flatten(Set flat, def key, Map interim) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
if( ! interim ) flat << (key as String)
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
interim.each { k, v ->
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def _key = "${key}/${k}"
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
if( v && v.size() > 0 )
|
|
|
|
flatten(flat, _key, v)
|
|
|
|
else {
|
|
|
|
flat << (_key as String)
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
2018-10-26 11:48:11 +02:00
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static void scanDocu(File f, Map params) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
|
|
|
boolean docu = false,
|
|
|
|
value = false,
|
2018-10-29 08:45:09 +02:00
|
|
|
docuEnd = false
|
2018-10-16 13:07:38 +02:00
|
|
|
|
|
|
|
def docuLines = [],
|
|
|
|
valueLines = []
|
|
|
|
|
|
|
|
f.eachLine {
|
|
|
|
line ->
|
|
|
|
|
2018-10-29 08:45:09 +02:00
|
|
|
if(docuEnd) {
|
|
|
|
docuEnd = false
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 12:32:25 +02:00
|
|
|
// first we check if we have a header comment for a step
|
|
|
|
Matcher headerMatcher = (line =~ /(def|void)\s*call\s*\(/ )
|
|
|
|
if(headerMatcher.size() == 1 && headerMatcher[0].size() == 2) {
|
|
|
|
}
|
2018-10-29 08:45:09 +02:00
|
|
|
|
2018-10-26 12:14:03 +02:00
|
|
|
Matcher m = (line =~ /.*'(.*)'.*/)
|
2018-10-16 13:07:38 +02:00
|
|
|
if(m.size() == 1 && m[0].size() == 2) {
|
|
|
|
def param = m[0][1]
|
2018-10-26 12:21:06 +02:00
|
|
|
|
|
|
|
if(params[param].docu ||
|
|
|
|
params[param].value)
|
|
|
|
System.err << "[WARNING] There is already some documentation for parameter '${param}. Is this parameter documented twice?'\n"
|
|
|
|
|
2018-10-16 13:07:38 +02:00
|
|
|
def _docu = [], _value = []
|
|
|
|
docuLines.each { _docu << it }
|
|
|
|
valueLines.each { _value << it}
|
|
|
|
params[param].docu = _docu*.trim().join(' ')
|
|
|
|
params[param].value = _value*.trim().join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
docuLines.clear()
|
|
|
|
valueLines.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
if( line.trim() ==~ /^\/\*\*/ ) {
|
|
|
|
docu = true
|
2018-10-26 11:48:11 +02:00
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
if(docu) {
|
2018-10-16 13:07:38 +02:00
|
|
|
def _line = line.replaceAll('^.*\\*/?', '').trim()
|
|
|
|
|
2018-10-25 10:01:44 +02:00
|
|
|
if(_line ==~ /@possibleValues.*/) {
|
2018-10-16 13:07:38 +02:00
|
|
|
value = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_line) {
|
|
|
|
if(value) {
|
2018-10-25 10:01:44 +02:00
|
|
|
if(_line ==~ /@possibleValues.*/)
|
|
|
|
_line = (_line =~ /@possibleValues\s*?(.*)/)[0][1]
|
2018-10-16 13:07:38 +02:00
|
|
|
valueLines << _line
|
|
|
|
} else {
|
|
|
|
docuLines << _line
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 11:48:11 +02:00
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
if(docu && line.trim() ==~ /^\*\//) {
|
2018-10-16 13:07:38 +02:00
|
|
|
docu = false
|
|
|
|
value = false
|
2018-10-29 08:45:09 +02:00
|
|
|
docuEnd = true
|
2018-10-26 11:48:11 +02:00
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static getScopedParameters(def script) {
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
def params = [:]
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
params.put('STEP_CONFIG', script.STEP_CONFIG_KEYS ?: [])
|
|
|
|
params.put('GENERAL_CONFIG', script.GENERAL_CONFIG_KEYS ?: [] )
|
|
|
|
params.put('PARAMS', script.PARAMETER_KEYS ?: [] )
|
2018-10-16 13:07:38 +02:00
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
static getRequiredParameters(File f) {
|
|
|
|
def params = [] as Set
|
|
|
|
f.eachLine {
|
|
|
|
line ->
|
|
|
|
if( line ==~ /.*withMandatoryProperty.*/ ) {
|
2018-10-16 13:07:38 +02:00
|
|
|
def param = (line =~ /.*withMandatoryProperty\('(.*)'/)[0][1]
|
|
|
|
params << param
|
2018-10-26 11:48:11 +02:00
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
2018-10-26 11:48:11 +02:00
|
|
|
return params
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
static getValue(Map config, def pPath) {
|
2018-10-16 13:07:38 +02:00
|
|
|
def p =config[pPath.head()]
|
|
|
|
if(pPath.size() == 1) return p // there is no tail
|
|
|
|
if(p in Map) getValue(p, pPath.tail())
|
|
|
|
else return p
|
2018-10-26 11:48:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
roots = [
|
|
|
|
'vars',
|
|
|
|
'src',
|
|
|
|
]
|
|
|
|
|
|
|
|
stepsDir = null
|
|
|
|
outDir = null
|
|
|
|
stepsDocuDir = null
|
|
|
|
|
2018-10-26 12:08:34 +02:00
|
|
|
steps = []
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
//
|
|
|
|
// assign parameters
|
|
|
|
|
|
|
|
if(args.length >= 1)
|
|
|
|
stepsDir = new File(args[0])
|
|
|
|
|
|
|
|
stepsDir = stepsDir ?: new File('vars')
|
|
|
|
|
|
|
|
if(args.length >= 2)
|
|
|
|
outDir = new File(args[1])
|
|
|
|
|
|
|
|
outDir = outDir ?: new File('out')
|
|
|
|
|
|
|
|
if(args.length >= 3)
|
|
|
|
stepsDocuDir = new File(args[2])
|
|
|
|
|
|
|
|
stepsDocuDir = stepsDocuDir ?: new File('documentation/docs/steps')
|
|
|
|
|
2018-10-26 12:08:34 +02:00
|
|
|
|
|
|
|
if(args.length >= 4)
|
|
|
|
steps << args[3]
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
// assign parameters
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// sanity checks
|
|
|
|
|
|
|
|
if( ! outDir.exists() ) {
|
|
|
|
if(! outDir.mkdirs()) {
|
|
|
|
System.err << "Cannot create output direcrory '${outDir}'.\n"
|
|
|
|
System.exit(1)
|
|
|
|
}
|
2018-10-16 13:07:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:48:11 +02:00
|
|
|
if( !stepsDocuDir.exists() ) {
|
|
|
|
System.err << "Steps docu dir '${stepsDocuDir}' does not exist.\n"
|
|
|
|
System.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !stepsDir.exists() ) {
|
|
|
|
System.err << "Steps dir '${stepsDir}' does not exist.\n"
|
|
|
|
System.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanity checks
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2018-10-26 12:08:34 +02:00
|
|
|
// find all the steps we have to document (if no step has been provided from outside)
|
|
|
|
if( ! steps) {
|
|
|
|
stepsDir.traverse(type: FileType.FILES, maxDepth: 0)
|
|
|
|
{ if(it.getName().endsWith('.groovy')) steps << (it =~ /vars\/(.*)\.groovy/)[0][1] }
|
|
|
|
} else {
|
|
|
|
System.err << "[INFO] Generating docu only for step ${steps.size > 1 ? 's' : ''} ${steps}.\n"
|
|
|
|
}
|
2018-10-26 11:48:11 +02:00
|
|
|
|
|
|
|
def gse = new GroovyScriptEngine( [ stepsDir.getName() ] as String[] , getClass().getClassLoader() )
|
|
|
|
|
|
|
|
def prepareDefaultValuesStep = Helper.getPrepareDefaultValuesStep(gse)
|
|
|
|
|
|
|
|
boolean exceptionCaught = false
|
|
|
|
|
|
|
|
for (step in steps) {
|
|
|
|
try {
|
|
|
|
handleStep(step, prepareDefaultValuesStep, gse)
|
|
|
|
} catch(Exception e) {
|
|
|
|
exceptionCaught = true
|
|
|
|
System.err << "${e.getClass().getName()} caught while handling step '${step}'."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(exceptionCaught) {
|
|
|
|
System.err << "[ERROR] Exception caught during generating documentation. Check earlier log for details.\n"
|
|
|
|
System.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void handleStep(step, prepareDefaultValuesStep, gse) {
|
|
|
|
File theStep = new File(stepsDir, "${step}.groovy")
|
|
|
|
File theStepDocuInput = new File(stepsDocuDir, "${step}.md")
|
|
|
|
File theGeneratedStepDocu = new File(outDir, "${step}.md")
|
|
|
|
|
|
|
|
if(!theStepDocuInput.exists()) {
|
|
|
|
System.err << "[WARNING] step docu input file for step '${step}' is missing.\n"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
System.err << "[INFO] Handling step '${step}'.\n"
|
|
|
|
|
|
|
|
def defaultConfig = Helper.getConfigHelper(getClass().getClassLoader(), roots).loadStepDefaults(Helper.getDummyScript(prepareDefaultValuesStep, step)).use()
|
|
|
|
|
|
|
|
def params = [] as Set
|
|
|
|
|
|
|
|
//
|
|
|
|
// scopedParameters is a map containing the scope as key and the parameters
|
|
|
|
// defined with that scope as a set of strings.
|
|
|
|
|
|
|
|
def scopedParameters
|
|
|
|
|
|
|
|
try {
|
|
|
|
scopedParameters = Helper.getScopedParameters(gse.createScript( "${step}.groovy", new Binding() ))
|
|
|
|
scopedParameters.each { k, v -> params.addAll(v) }
|
|
|
|
} catch(Exception e) {
|
|
|
|
System.err << "[ERROR] Step '${step}' violates naming convention for scoped parameters: ${e}.\n"
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
def requiredParameters = Helper.getRequiredParameters(theStep)
|
|
|
|
|
|
|
|
params.addAll(requiredParameters)
|
|
|
|
|
|
|
|
|
|
|
|
def parameters = [:]
|
|
|
|
|
|
|
|
Helper.normalize(params).toSorted().each {
|
|
|
|
|
|
|
|
it ->
|
|
|
|
|
|
|
|
def parameterProperties = [
|
|
|
|
defaultValue: Helper.getValue(defaultConfig, it.split('/')),
|
|
|
|
required: requiredParameters.contains((it as String))
|
|
|
|
]
|
|
|
|
|
|
|
|
parameters.put(it, parameterProperties)
|
|
|
|
|
|
|
|
// The scope is only defined for the first level of a hierarchical configuration.
|
|
|
|
// If the first part is found, all nested parameters are allowed with that scope.
|
|
|
|
def firstPart = it.split('/').head()
|
|
|
|
scopedParameters.each { key, val ->
|
|
|
|
parameterProperties.put(key, val.contains(firstPart))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Helper.scanDocu(theStep, parameters)
|
|
|
|
|
|
|
|
def text = theStepDocuInput.text
|
|
|
|
text = text.replace('__PARAMETER_TABLE__', TemplateHelper.createParametersTable(parameters))
|
|
|
|
text = text.replace('__PARAMETER_DESCRIPTION__', TemplateHelper.createParameterDescriptionSection(parameters))
|
|
|
|
|
|
|
|
theGeneratedStepDocu.withWriter { w -> w.write text }
|
|
|
|
}
|
|
|
|
|
|
|
|
System.err << "[INFO] done.\n"
|