Sunday, October 26, 2014

Apache Ant: Pushing your Ant script to a whole new level...

If you have been using Apache Ant quite a bit, you will soon realize that it can be pretty annoying at times. You couldn't open up and read a file, you couldn't create Arrays, etc... Therefore there's actually a way to work around it. You can actually use Javascript + Java inside the Ant script environment. And on top of that you can actually manipulate simple basic Ant variables in the Javascript + Java environment.

 <macrodef name="getIndexHtmlContents">
  <sequential>      
   <script language="javascript">
    <![CDATA[
     //Import the necessary Java classes
     importClass(java.io.File);
     importClass(java.io.FileReader);
     importClass(java.io.BufferedReader);

     //We will be reading the value from the Ant Project Property 'filenameProp'
     var filenameProp = GWR.getProperty("filenameProp");

     //Then we will be using BufferedReader and FileReader to read the contents of the file
     var reader = new BufferedReader(new FileReader(new File(filenameProp)));

     //Next we will loop through all the contents of the file and store it in a temporary variable
     var sCurrentLine;
     var strEverything = "";
     while ((sCurrentLine = reader.readLine()) != null) {
      strEverything += sCurrentLine;
     }

     //We will printout the contents of the file/temporary variable on the ant console output
     println(strEverything);

     //Writing the value of the temporary variable and saing it in the Ant Project Property 'fileContents'
     GWR.setProperty("fileContents", strEverything);
    ]]>
   </script>
  </sequential>
 </macrodef>
Though you have specified that you will be writing some script in 'Javascript' syntax, it actually allows Java codes to be written inside that <script> block too.

* Click here to find out more about 'Apache Ant'.

Friday, October 17, 2014

Linux: Mass find and replace a folder of files

In the Linux environment, it is pretty easy to loop through a particular type of files to find and replace a particular string using shell script. I hope this helps.

The following will give you an idea how to get it done.
You need to replace all the {value} with the corresponding values.
#!/bin/bash

varReplace={string_to_replace}
find ./{the_folder}/ -name \*.{file_extension} -type f -print0 | while read -d $'\0' file; do
    echo "Processing $file"
    sed -i 's/{string_to_look_for}/'$varReplace'/g' $file
done
The following example will go through all the files with extension (.js)
in the folder 'output', find the string '{my_version}' and replace it with
the string 'v1.0.122'.
#!/bin/bash

varReplace=v1.0.122
find ./output/ -name \*.js -type f -print0 | while read -d $'\0' file; do
    echo "Processing $file"
    sed -i 's/{my_version}/'$varReplace'/g' $file
done

Friday, October 10, 2014

Simple Javascript Timer Implementation

Well basically this is a post meant for myself. I keep forgetting how to get this done. Basically this is a simple implementation of a Timer in Javascript.


* Click here to access the demo that I have created on 'JSFiddle'.
^ Click here to test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code
  editor.

Friday, October 3, 2014

AngularJS: Input type number and maxlength fix

Well, there might be situations that you need to limit the number of characters that you are allowing the user to enter in a numerical text field. Therefore, here's a fix for angularJS.


* Click here to access the demo that I have created on 'JSFiddle'.
^ Click here to test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code
  editor.