Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asking user for yes|no input

Tags:

versioning

ant

I am working on an Ant build process for an application that uses a versioning in the following format: major.minor.buildcount. So currently the application is around 2.1.52, where we are on version 2.1 and there have been 35 builds.

I am now adding in an ant target to ask the user if they would like to advance the major version and/or the minor version.

When I run my target from the command line I would like to follow the following:

@@ ant version
Versioning application...
Would you like to advance the major version to 3? (Y|n)
@@ n
Not Advancing major version
Would you like to advance the minor version to 2? (y|N)
@@ y
Advancing minor version

The lines prepended with @@ is the user input that I would like to take. My major and minor versions are stored in a build.properties file.

Here is my code so far

<?xml version="1.0"?>
<project name="StudentMS" default="zip" basedir=".">
    <propertyfile file="./.ant/build.properties">
        <entry key="version.buildnumber" type="int" default="0" operation="+" pattern="00" />
    </propertyfile>

    <property file="./.ant/build.properties" />
    <property name="sourceDir" location="/Users/dave/Workspace/ColdFusion/StudentMs" />
    <property name="buildDir" location="${sourceDir}/builds" />

    <target name="version" description="Adds a major and minor version to the build.">
        <input message="Advance major version? ${version.major}" addproperty="updatemajor" validargs="y,n" defaultvalue="n" />
        <propertyfile file="./.ant/build.properties">
            <entry key="version.major" type="int" default="0" operation="+" pattern="00" />
        </propertyfile>

        <input message="Advance minor version? ${version.minor}" addproperty="updateminor" validargs="y,n" defaultvalue="y" />
        <propertyfile file="./.ant/build.properties">
            <entry key="version.minor" type="int" default="0" operation="+" pattern="00" />
        </propertyfile>
    </target>
</project>

And my build.properties

#Tue, 29 Mar 2011 11:46:30 -0400
version.buildnumber=35
version.major=2
version.minor=1

I am still very new to Ant so I am sorry that I can't post more advanced code. So the first thing I need to do is add some kind of conditional around my property file edits.

like image 928
Dave Long Avatar asked Mar 29 '11 15:03

Dave Long


People also ask

How do you request user input?

The simplest way to obtain user input is by using the input() function. This function prompts the user for an input from the keyboard. Once the user has give the input and pressed Enter, the program flow continues.

How do I prompt a user in Linux?

You can use the built-in read command ; Use the -p option to prompt the user with a question. It should be noted that FILEPATH is the variable name you have chosen, and is set with the answer to the command prompt. So if you were to then run vlc "$FILEPATH" , for example, vlc would open that file.


1 Answers

what you want can be achieved by combining the condition and antcall task and by adding a couple of extra targets.

I think something like this should work:

<property file="./.ant/build.properties" />
<property name="sourceDir" location="/Users/dave/Workspace/ColdFusion/StudentMs" />
<property name="buildDir" location="${sourceDir}/builds" />

<target name="version" description="Adds a major and minor version to the build.">
    <input message="Advance major version? ${version.major}" addproperty="updatemajor" validargs="y,n" defaultvalue="n" />

     <condition property="executeMajor">
        <and>
           <isset property="updatemajor" />
           <equals arg1="${updatemajor}" arg2="y" />
        </and>
     </condition>

    <antcall target="update_major" />

    <input message="Advance minor version? ${version.minor}" addproperty="updateminor" validargs="y,n" defaultvalue="y" />

     <condition property="executeMinor">
        <and>
           <isset property="updateminor" />
           <equals arg1="${updateminor}" arg2="y" />
        </and>
     </condition>

   <antcall target="update_minor" />

</target>

<target name="update_major" if="executeMajor">
    <!-- Code to update major here -->
</target>

<target name="update_minor" if="executeMinor">
    <!-- Code to update minor here -->
</target>

Basically, what it does is set the executeMajor and executeMinor properties just in the case that the updatemajor/updateminor are set to "y". Then, ant will run the update targets just if the executeMajor/Minor variables are set, and it will skip them otherwise.

like image 106
jbalsas Avatar answered Sep 27 '22 17:09

jbalsas