/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ // // $Rev$ $Date$ // package buildharness.commands class iteration extends CommandSupport { /** The iteration index. */ int iteration /** The mapping of iteration index to replacement values. */ Map mapping def loadMapping(file) { assert file != null def props = new Properties() props.load(file.newInputStream()) // Sanity check mapping, make sure we have a complete set int max = props.keySet().size() for (i in 1 .. max) { assert props.containsKey(i as String) : "Missing mapping for iteration: ${i}" } return props } def applyMapping(name) { assert name != null // // TODO: Maybe apply the mapping to more than one property? // def value = context.getRequired(name) def parts = value.tokenize(' ') def list = [] def mappedValue = mapping[this.iteration as String] println "Applying mapping to ${name}=${parts}" parts.each { part -> part = part.replaceAll('%', { mappedValue }) list.add(part) } value = list.join(' ') println "Mapped ${name} to ${value}" context.set(name, value) } def execute() { this.iteration = context.getRequiredAsInt('iteration') assert this.iteration >= 1 : 'Iteration must be greater than 1' println "Iteration: ${this.iteration}" // The command we will execute to perform the iteration task def command = context.getRequired('iteration_command') println "Iteration Command: ${command}" // // TODO: Allow for groovy-based mapping support? // // Load mapping def mappingFile = context.getRequiredAsFile('mapping') assert mappingFile.exists() : "Missing iteration mapping file: ${mappingFile}" println "Using iteration mapping file: ${mappingFile}" this.mapping = loadMapping(mappingFile) println 'Iteration Map:' mapping.keySet().sort().each { println " ${it}=" + mapping[it] } // Make sure its a valid key in the map assert mapping.containsKey((String)this.iteration) : 'Invalid iteration value; not mapped: ' + this.iteration // Process arguments def apply_to = context.get('apply_to', 'args') println "Applying iteration mapping to: ${apply_to}" applyMapping(apply_to) // Forward to the handling command context.forward(command) } }