![]() |
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 |
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> |
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 |
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> |
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 |
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 |
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 |
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/> |
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.