Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   ant task to compute lines of code (http://www.velocityreviews.com/forums/t954599-ant-task-to-compute-lines-of-code.html)

Laura Schmidt 11-18-2012 03:43 PM

ant task to compute lines of code
 
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

John B. Matthews 11-18-2012 04:19 PM

Re: ant task to compute lines of code
 
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>

Robert Klemme 11-18-2012 04:24 PM

Re: ant task to compute lines of code
 
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

Arne Vajhøj 11-18-2012 04:52 PM

Re: ant task to compute lines of code
 
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.JavaCodeS tatsTask"/>
<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>



Laura Schmidt 11-19-2012 03:28 AM

Re: ant task to compute lines of code
 
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

Lew 11-19-2012 04:35 AM

Re: ant task to compute lines of code
 
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

Robert Klemme 11-19-2012 01:07 PM

Re: ant task to compute lines of code
 
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

Jeff Higgins 11-19-2012 01:44 PM

Re: ant task to compute lines of code
 
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/>

Roedy Green 11-20-2012 03:27 AM

Re: ant task to compute lines of code
 
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.


All times are GMT. The time now is 03:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57