ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anthony Young-Garner <notadr...@yahoo.com>
Subject [SUBMIT] Interactive property-setting task
Date Mon, 30 Jul 2001 16:29:05 GMT
This task prompts build user for property values,
allowing interactive builds. It issomewhat tangential
to Ant's primary purpose but useful enough for
inclusion as anoptional task. I'd like to know if
people think its a good idea and how and whether it
can be improved.

----------------------------------------

Example build.xml file demonstrating use:

<?xml version="1.0"?>
<project name="PropertyPromptExample" default="main"
basedir=".">
	<property name="promptTimeout" value="0"/>
	<taskdef name="propertyprompt"
classname="com.ibm.samples.jakarta.ant.PropertyPrompt"/>
	<target name="main">
	<property name="propA" value="oldValA"/>
	<property name="propA" value="oldValA1"/>
	<echo>value of propA: ${propA}</echo>
	<echo>value of propB: ${propB}</echo>
	<propertyprompt propertyname="propA"
promptcharacter=":">Enter value for
propA</propertyprompt>
	<propertyprompt propertyname="propB"
defaultvalue="defvalB">What is the value for
propB</propertyprompt>
	<echo>value of propA: ${propA}</echo>
	<echo>value of propB: ${propB}</echo>
	</target>
</project>

---------------------------------------

Source code:

package org.apache.tools.ant.taskdefs.optional;

/**
 * Task that prompts user for property values to allow
interactive builds.
 * Admittedly, this task definitely falls way outside
the bounds of
 * Ant's core functionality but is useful enough to
warrant
 * inclusion amongst the optional tasks.
 * @author: <a
href=mailto:ajyoung@alum.mit.edu>Anthony J.
Young-Garner</a>
 * Set project property "promptTimeout" to control
behavior.
 * timeout = -1 --> Cancel prompting. Use default
property values.
 * timeout =  0 --> Wait indefinitely for user
response (default).
 * timeout =  x --> Wait x seconds for user reponse
before using default
 *                  property values (for x > 0).
IMPLEMENTATION POSTPONED UNTIL
 *                  JDK 1.4 provides non-blocking I/O.
 */

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Hashtable;
import org.apache.tools.ant.taskdefs.Property;
 
public class PropertyPrompt extends
org.apache.tools.ant.Task {
	private java.lang.String propertyname;
	private java.lang.String defaultvalue;
	private java.lang.String proposedValue;
	private java.lang.String prompttext;
	private java.lang.String promptcharacter;
	private int timeout;
/**
 * PropertyPrompt default constructor.
 */
public PropertyPrompt() {
	super();
}
/**
 * Sets the prompt text that will be presented to the
user.
 * @param prompt java.lang.String
 */
public void addText(String prompt) {
	setPrompttext(prompt);
}
/**
 * Run the PropertyPrompt task.
 * @exception org.apache.tools.ant.BuildException The
exception description.
 */
public void execute() throws
org.apache.tools.ant.BuildException {
	if (timeout > -1) {
		log("Prompting user for " + propertyname + ".
Default value is " + defaultvalue + ".");
		StringBuffer prompt = new StringBuffer();
		prompt.append("\n");
		prompt.append(prompttext);
		prompt.append(" [");
		prompt.append(defaultvalue);
		prompt.append("] ");
		prompt.append(promptcharacter);
		prompt.append(" ");
		System.out.print(prompt.toString());

		/** future version should have hooks for validation
of user input.*/
		BufferedReader reader = new BufferedReader(new
InputStreamReader (System.in));
		try {
			/**
			 * when we get to JDK 1.4 (yeah, right), this
should be non-blocking so that
			 * user-input can be time limited. (Threads could
be used as a work-around
			 * but I'm not comfortable introducing the
complexity of threads for such a
			 * small feature).
			 */
			proposedValue  = reader.readLine();
		} catch (IOException e) {
			log("Prompt failed. Using default.");
			proposedValue = defaultvalue;
		}
		if (!proposedValue.equals("")) {
			/**
			 * According to the mailing list, properties are
API mutable
			 * (as opposed to user-properties and the use of
multiple
			 * <property> tags to 'mutate' property values).
			 */
			project.setProperty(propertyname, proposedValue);
		}
	}	
}
/**
 * Returns defaultValue specified 
 * in this task for the Property
 * being set.
 * @return java.lang.String
 */
public java.lang.String getDefaultvalue() {
	return defaultvalue;
}
/**
 * Returns the terminating character used to 
 * punctuate the prompt text.
 */
public java.lang.String getPromptcharacter() {
	return promptcharacter;
}
/**
 * Returns text of the prompt.
 * @return java.lang.String
 */
public java.lang.String getPrompttext() {
	return prompttext;
}
/**
 * Returns name of the Ant Project Property
 * being set by this task.
 * @return java.lang.String
 */
public java.lang.String getPropertyname() {
	return propertyname;
}
/**
 * Initializes this task.
 */
public void init() {
	super.init();
	String timeoutProperty =
project.getProperty("promptTimeout");

	if (timeoutProperty == null) {
		timeout = 0;
	} else {
		try{
			timeout = Integer.parseInt(timeoutProperty);
		} catch(NumberFormatException e) {
			log("Invalid promptTimeout value: " +
timeoutProperty + ". Using default (wait
indefinitely).");
			timeout = 0;
		}
	}

	promptcharacter = "?";
}
/**
 * Sets defaultValue for the Property
 * being set by this task.
 * @param newDefaultvalue java.lang.String
 */
public void setDefaultvalue(java.lang.String
newDefaultvalue) {
	defaultvalue = newDefaultvalue;
}
/**
 * Sets the terminating character used to 
 * punctuate the prompt text (default is "?").
 * @param newPromptcharacter java.lang.String
 */
public void setPromptcharacter(java.lang.String
newPromptcharacter) {
	promptcharacter = newPromptcharacter;
}
/**
 * Sets text of the prompt.
 * @param newPrompttext java.lang.String
 */
public void setPrompttext(java.lang.String
newPrompttext) {
	prompttext = newPrompttext;
}
/**
 * Specifies the Ant Project Property
 * being set by this task.
 * @param newPropertyname java.lang.String
 */
public void setPropertyname(java.lang.String
newPropertyname) {
	propertyname = newPropertyname;
}
}


-----------------------------------


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Mime
View raw message