Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19785 > unrolled thread

ant task to compute lines of code

Started byLaura Schmidt <ls@mailinator.com>
First post2012-11-18 16:43 +0100
Last post2012-11-19 08:44 -0500
Articles 10 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  ant task to compute lines of code Laura Schmidt <ls@mailinator.com> - 2012-11-18 16:43 +0100
    Re: ant task to compute lines of code "John B. Matthews" <nospam@nospam.invalid> - 2012-11-18 11:19 -0500
      Re: ant task to compute lines of code Roedy Green <see_website@mindprod.com.invalid> - 2012-11-19 19:27 -0800
    Re: ant task to compute lines of code Robert Klemme <shortcutter@googlemail.com> - 2012-11-18 17:24 +0100
      Re: ant task to compute lines of code Laura Schmidt <ls@mailinator.com> - 2012-11-19 04:28 +0100
        Re: ant task to compute lines of code Lew <lewbloch@gmail.com> - 2012-11-18 20:35 -0800
          Re: ant task to compute lines of code Robert Klemme <shortcutter@googlemail.com> - 2012-11-19 05:07 -0800
    Re: ant task to compute lines of code Arne Vajhøj <arne@vajhoej.dk> - 2012-11-18 11:52 -0500
    Re: ant task to compute lines of code Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-11-19 00:05 -0600
    Re: ant task to compute lines of code Jeff Higgins <jeff@invalid.invalid> - 2012-11-19 08:44 -0500

#19785 — ant task to compute lines of code

FromLaura Schmidt <ls@mailinator.com>
Date2012-11-18 16:43 +0100
Subjectant task to compute lines of code
Message-ID<k8avks$nl8$1@news.m-online.net>
Hello,

I want to find out the total lines of code of a project using an ant task.

On the commandline I would do this like this:

find src -name *.java | wc -l

In ant, I only managed to output all the code:

  <target name="loc">
   <exec executable="find">
    <arg line="src -name *.java -exec cat {} ;"/>
   </exec> 	
  </target>

So I can get the loc like this:

ant -f project.xml loc | wc -l

But how can I integrate the "wc -l" in the ant task?

Thanks
Laura

[toc] | [next] | [standalone]


#19786

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-11-18 11:19 -0500
Message-ID<nospam-0216E1.11191818112012@news.aioe.org>
In reply to#19785
In article <k8avks$nl8$1@news.m-online.net>,
 Laura Schmidt <ls@mailinator.com> wrote:

> I want to find out the total lines of code of a project using an ant task.
> 
> On the commandline I would do this like this:
> 
> find src -name *.java | wc -l
> 
> In ant, I only managed to output all the code:
> 
>   <target name="loc">
>    <exec executable="find">
>     <arg line="src -name *.java -exec cat {} ;"/>
>    </exec> 	
>   </target>
> 
> So I can get the loc like this:
> 
> ant -f project.xml loc | wc -l
> 
> But how can I integrate the "wc -l" in the ant task?

The Exec task should work:

<http://ant.apache.org/manual/Tasks/exec.html>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#19824

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-19 19:27 -0800
Message-ID<n0ula81ihh5t8vigrv6hqkg6vfnh8fb0r2@4ax.com>
In reply to#19786
On Sun, 18 Nov 2012 11:19:18 -0500, "John B. Matthews"
<nospam@nospam.invalid> wrote, quoted or indirectly quoted someone who
said :

><http://ant.apache.org/manual/Tasks/exec.html>

e.g.
<!-- J E T -->
<!-- Requires Excelsior JET native Java compiler jc.exe on the path
-->
<!-- See http://mindprod.com/jgloss/jet.html for details -->
<target name="jet" depends="prejet" unless="jet.uptodate">
<echo message="    :::    bio    :::    jet compiling." />
  <exec executable="jc.exe" dir="com/mindprod/bio">
    <arg value="-DECOR=" />
    <arg value="bio.jar" />
  </exec>
  <!-- copy exe to E:/sys -->
  <copy file="com/mindprod/bio/bio.exe" todir="E:/sys" />
</target>
I would send the output to files one per module, then consolidate them
later.


-- 
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish 
as couch potatoes who hire others to go to the gym for them. 

[toc] | [prev] | [next] | [standalone]


#19787

FromRobert Klemme <shortcutter@googlemail.com>
Date2012-11-18 17:24 +0100
Message-ID<agsge8F5o27U1@mid.individual.net>
In reply to#19785
On 11/18/2012 04:43 PM, Laura Schmidt wrote:
> Hello,
>
> I want to find out the total lines of code of a project using an ant task.
>
> On the commandline I would do this like this:
>
> find src -name *.java | wc -l

No, you wouldn't.  That would give you the number of source files - at 
best.  Worst case is an error from "find" indicating invalid command 
line arguments.  Other variants are also possible, e.g. including 
directories or symlinks in the count.

More successful approaches

find src -type f -name \*.java -exec cat {} + | wc -l

{
   echo 0
   find -type f -name \*.java -exec wc -l {} + \
   | sed -n 's#^ *\([0-9]\+\) \+total$#\1+#p'
   echo n
} | dc

Btw, all these also count empty lines.  For non empty lines something 
like this could work

{
   echo 0
   find -type f -name \*.java -exec egrep -hc '[^[:space:]]' {} + \
   | sed 'a +'
   echo n
} | dc


Kind regards

	robert

[toc] | [prev] | [next] | [standalone]


#19803

FromLaura Schmidt <ls@mailinator.com>
Date2012-11-19 04:28 +0100
Message-ID<k8c902$rjs$1@news.m-online.net>
In reply to#19787
Hi Robert!

On 11/18/2012 05:24 PM, Robert Klemme wrote:
> On 11/18/2012 04:43 PM, Laura Schmidt wrote:

>> On the commandline I would do this like this:
>> find src -name *.java | wc -l
> No, you wouldn't.

Sorry, this was a copy/paste error.
My ant task approach cats all the files:

  <target name="loc">
   <exec executable="find">
    <arg line="src -name *.java -exec cat {} ;"/>
   </exec>
  </target>

My problem with this approach and also with your approaches is to fit 
the piped command line into the ant tag, e. g. to integrate the "| wc 
-l" into the target tag above.

Laura

[toc] | [prev] | [next] | [standalone]


#19806

FromLew <lewbloch@gmail.com>
Date2012-11-18 20:35 -0800
Message-ID<93022fca-0e68-4b8a-aceb-ea6042a7f4a5@googlegroups.com>
In reply to#19803
Laura Schmidt wrote:
> Sorry, this was a copy/paste error.
> My ant task approach cats all the files:

You could exec "wc -l $(find src -name *.java)".

Not sure what the attraction is of "cat" when "wc" 
takes files as arguments.

$ wc --help
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.  A word is a non-zero-length sequence of characters
delimited by white space.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#19811

FromRobert Klemme <shortcutter@googlemail.com>
Date2012-11-19 05:07 -0800
Message-ID<8b6c2a93-06e8-496c-92b9-4c3e776702fe@googlegroups.com>
In reply to#19806
On Monday, November 19, 2012 5:35:05 AM UTC+1, Lew wrote:

> You could exec "wc -l $(find src -name *.java)".

That's error prone in light of file and directory names with spaces and line breaks (not too common, I know).  In this case

find src -name \*.java -exec wc -l {} +

is better.

> Not sure what the attraction is of "cat" when "wc" 
> takes files as arguments.

One does not have to do the totaling manually.  Also "wc" will only output a single numeric value which is convenient if you want to further process it.

Kind regards

robert

[toc] | [prev] | [next] | [standalone]


#19788

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-11-18 11:52 -0500
Message-ID<50a9123d$0$293$14726298@news.sunsite.dk>
In reply to#19785
On 11/18/2012 10:43 AM, Laura Schmidt wrote:
> Hello,
>
> I want to find out the total lines of code of a project using an ant task.
>
> On the commandline I would do this like this:
>
> find src -name *.java | wc -l
>
> In ant, I only managed to output all the code:
>
>   <target name="loc">
>    <exec executable="find">
>     <arg line="src -name *.java -exec cat {} ;"/>
>    </exec>
>   </target>
>
> So I can get the loc like this:
>
> ant -f project.xml loc | wc -l
>
> But how can I integrate the "wc -l" in the ant task?

I would code an ant task to do it.

Actually I have.

:-)

See below.

Arne

================

code:

package dk.vajhoej.anttasks.codestats;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;

public class JavaCodeStatsTask extends Task {
     private String label;
     private boolean cf;
     private boolean cl;
     private List<FileSet> allfs = new ArrayList<FileSet>();
     @Override
     public void execute() {
         int nf = 0;
         int nl = 0;
         for(FileSet fs : allfs) {
             DirectoryScanner ds = fs.getDirectoryScanner();
             for(String fnm : ds.getIncludedFiles()) {
                 File f = new File(ds.getBasedir(), fnm);
                 nf++;
                 try {
                     BufferedReader br = new BufferedReader(new 
FileReader(f));
                     while(br.readLine() != null) {
                         nl++;
                     }
                 } catch (FileNotFoundException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
         System.out.println(label + ":");
         if(cf) {
             System.out.println("  Number of files: " + nf);
         }
         if(cl) {
             System.out.println("  Number of lines: " + nl);
         }
     }
     /**
      * Specify label to use.
      * @param label label
      */
     public void setLabel(String label) {
         this.label = label;
     }
     /**
      * Specify whether to count files.
      * @param cf true=count false=not count
      */
     public void setCountfiles(boolean cf) {
         this.cf = cf;
     }
     /**
      * Specify whether to count lines.
      * @param cl true=count false=not count
      */
     public void setCountlines(boolean cl) {
         this.cl = cl;
     }
     /**
      * Specify a file set to process.
      * @param fs file set
      */
     public void addFileSet(FileSet fs) {
         allfs.add(fs);
     }
}

example:

     <target name="count">
         <taskdef name="javacodestats" classpath="${anttaskslib}" 
classname="dk.vajhoej.anttasks.codestats.JavaCodeStatsTask"/>
         <javacodestats label="Library" countfiles="yes" countlines="yes">
             <fileset dir="src" includes="dk/vajhoej/record/*.java"/>
         </javacodestats>
         <javacodestats label="Unit test" countfiles="yes" countlines="yes">
             <fileset dir="src" includes="dk/vajhoej/record/test/*.java"/>
         </javacodestats>
     </target>

[toc] | [prev] | [next] | [standalone]


#19807

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-11-19 00:05 -0600
Message-ID<JtSdnW__l4aPUTTNnZ2dnUVZ8oSdnZ2d@giganews.com>
In reply to#19785
Laura Schmidt <ls@mailinator.com> wrote:
> Hello,
> 
> I want to find out the total lines of code of a project using an ant task.

I'd recommend the program sloccount for this rather than wc.

http://www.dwheeler.com/sloccount/

-- 
Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#19812

FromJeff Higgins <jeff@invalid.invalid>
Date2012-11-19 08:44 -0500
Message-ID<k8dcjg$gpr$1@dont-email.me>
In reply to#19785
On 11/18/2012 10:43 AM, Laura Schmidt wrote:
> Hello,
>
> I want to find out the total lines of code of a project using an ant task.
>
First hit for "ant task word count":
<http://antcount.sourceforge.net/>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web