How to find out how many lines of code there are in an Xcode project?
Asked Answered
W

15

199

Is there a way to determine how many lines of code an Xcode project contains? I promise not to use such information for managerial measurement or employee benchmarking purposes. ;)

Windowsill answered 5/1, 2010 at 1:18 Comment(2)
If you want lines, then you can use this answer: #5902258 But it includes spacesScorpius
Does this answer your question? Count total number of lines in an Xcode projectDeathblow
A
152

Check out CLOC.

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.

(Legacy builds are archived on SourceForge.)

Ancier answered 5/1, 2010 at 1:47 Comment(3)
CLOC is available via Homebrew. The ease of use in the command line was refreshing.Bystreet
I love the pun in the name there -- yes you can tell how productive you've been by checking out your CLOC.Boutte
Use above link to download './cloc-1.56.pl' perm version of cloc tool. Make sure that you enable execution permission on cloc-1.56.pl file using 'chmod u+x cloc-1.56.pl' command. If your source code is located in directory 'project_code' You just need to run following command.Lizbeth
W
276

I see this floating around and use it myself:

find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.swift" ")" -print0 | xargs -0 wc -l
Weighted answered 24/10, 2010 at 1:33 Comment(4)
To handle subdirs with spaces in the names you need: find . "(" -name ".m" -or -name ".mm" -or -name "*.cpp" ")" -print0 | xargs -0 wc -lThaothapa
it appears the .m and .mm tests are missing an * (edited: seems SO is not rendering those without a preceding slash) The above was not working for me until I added them as such: find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" ")" -print0 | xargs -0 wc -lPlayboy
How can I get total number of lines excluding commented and blank lines ?Theogony
Note for Swift users - replace or add another 'or' with the file extension .swiftTroika
A
152

Check out CLOC.

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.

(Legacy builds are archived on SourceForge.)

Ancier answered 5/1, 2010 at 1:47 Comment(3)
CLOC is available via Homebrew. The ease of use in the command line was refreshing.Bystreet
I love the pun in the name there -- yes you can tell how productive you've been by checking out your CLOC.Boutte
Use above link to download './cloc-1.56.pl' perm version of cloc tool. Make sure that you enable execution permission on cloc-1.56.pl file using 'chmod u+x cloc-1.56.pl' command. If your source code is located in directory 'project_code' You just need to run following command.Lizbeth
A
87

I have been using CLOC as mentioned by Nathan Kinsinger and it is fairly easy to use. It is a PERL script that you can add and run from your project directory.

PERL is already part of Mac OS and you can invoke the script this way to find out your number of lines you have written:

perl cloc-1.56.pl ./YourDirectoryWhereYourSourcesAre

This is an example of output i got from such command:

   176 text files.
   176 unique files.                                          
     4 files ignored.

http://cloc.sourceforge.net v 1.56  T=2.0 s (86.0 files/s, 10838.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Objective C                     80           3848           1876          11844
C/C++ Header                    92            980           1716           1412
-------------------------------------------------------------------------------
SUM:                           172           4828           3592          13256
-------------------------------------------------------------------------------
Albertoalberts answered 22/10, 2012 at 22:29 Comment(4)
Nice! I like this better than my own answer. :-)Weighted
You may need to download the script first. Then navigate to the directory where that script is and run that command with whatever file name you downloaded.Victorious
Should be cloc perl-1.56.pl ./YourDirectoryWhereYourSourcesAre and not as above, if you install perl from scratch make sure you enter the current perl version (mine was 5.30.1 so basically cloc perl-5.30.1.pl ./)Cousins
One last thing, I had to run span Moo as per error that appeared when running it for the 1st time so checkout for that one as wellCousins
K
75

Open up Terminal.app, go into your project's root directory, and run this command:

For Swift only:

find . \( -iname \*.swift \) -exec wc -l '{}' \+

For Obj-C only:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) -exec wc -l '{}' \+

For Obj-C + Swift:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -exec wc -l '{}' \+

For Obj-C + Swift + C + C++:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h -o -iname \*.hh -o -iname \*.hpp -o -iname \*.cpp -o -iname \*.swift \) -exec wc -l '{}' \+

Terminal quick tips:
ls: list directory contents
cd: change directory
Press tab to autocomplete
Remember to put "\" backslash before spaces
I suggest going one folder down from the main project so you get rid of code count from the frameworks

Kerrison answered 25/2, 2015 at 19:56 Comment(0)
M
45

In terminal, change into the project directory and run:

find . -type f -print0 | xargs -0 cat | wc -l

If you want only certain file types, try something like

find . -type f -name \*.[ch]* -print0 | xargs -0 cat | wc -l
Malave answered 5/1, 2010 at 1:30 Comment(1)
Nice, but includes comments and blank lines and thus is not exactly what was asked for. A LOC measure should be as independent of code formatting style as possible and thus a bunch of blank lines or comments between to logical parts in a file shouldn't count towards the sum.Payroll
U
39
  1. open terminal
  2. navigate to your project
  3. execute following command inside your project:

    find . -path ./Pods -prune -o -name "*.swift" -print0 ! -name "/Pods" | xargs -0 wc -l
    

    Or:

    find . -path ./Pods -prune -o -name "*[hm]" -print0 ! -name "/Pods" | xargs -0 wc -l
    

(*Excluding pod files count from total count)

Urina answered 16/11, 2016 at 13:8 Comment(0)
B
34

Check out Xcode Statistician, it does exactly what you want. It also provides other interesting statistics so is worth a run for fun now and then.

Note that it will not look inside real folders, though it will look in groups. Odds are you aren't using real folders so it'll work great. If you are using folders then you just have to do the count in each folder and add them together.

Note: As of June, 2012, it seems this does not work properly with the latest versions of Xcode.

Bemoan answered 15/12, 2010 at 14:6 Comment(8)
Just a heads up for those who mix Objective-C and C/C++: It doesn't count *.c or *.cpp files.Decal
Note that Xcode Statisician doesn't deal with subdirs in your project at this point.Thaothapa
@Thaothapa Aye, hence "Note that it will not look inside real folders". Quite a bit more manual work, unfortunately (and recursive folder searching is easy to code), but it's workable.Bemoan
Seems it doesn't work at all with X-Code 4.2 projects on Lion (at least it didn't give any statistics for my project)Gaptoothed
Ah, bummer, it may be outdated now.Bemoan
It still works as of xcode 4.5.1. It just has a bug where it doesn't count lines in subfolders. If you navigate into each folder, it will tell you the number of lines in that folder. A bit tedious to add them up, but you probably won't have more than a dozen or so folders anyway (hopefully)Former
Sorry, but this program fails to recurse down sub-directories, and is also not code-signed by any identified developer - so may be malware. So for most reasonably sized projects, it is unusableCarvey
@MottiShneor This answer was from 8 years ago. Things have changed a great deal since then. In fact, the edited answer (made 3 years ago) specifically notes that it no longer works.Bemoan
J
16

If you go to your project's directory in terminal and enter:

find . "(" -name "*.h" -or -name "*.m" -or -name "*.mm" -or -name "*.hpp" -or -name "*.cpp"  -or  -name "*.c" -or -name "*.cc" -or -name "*.swift" ")" -print0 | xargs -0 wc -l

That will give you a project breakdown, as well as the line total for each file and the project as a whole.

Jung answered 25/9, 2013 at 5:47 Comment(4)
For swift just removed everything and added .swift find . "(" -name "*.swift" ")" -print0 | xargs -0 wc -lAdolphadolphe
@Adolphadolphe Alternatively, we can just add .swift to the original commandJung
Indeed. Since most libraries and borrowed code are still in .h & .m it helped to trim the count down to just what I had written.Adolphadolphe
Absolutely. Everyone has their own solution. And if they have their own prefix (eg: "RE"), they could even do find . "(" -name "RE*.swift" ")" -print0 | xargs -0 wc -lJung
J
14

Steps to implement CLOC library in Mac as below:

  1. Open Terminal.
  2. Install Homebrew by copying and pasting the below command in the Terminal (including double quotes).

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Enter system password if asked.

You will see the terminal screen as below.

Installing Homebrew

System will popup so many permissions, allow all the permissions

If everything goes fine, you will see terminal screen as below,

Homebrew installation successful

  1. Now its time to install CLOC using below command.

brew install cloc

  1. Navigate to the project directory and run either of the following commands.

cloc . --exclude-dir=Pods (to exclude pod files)

cloc . (including pod files)

If everything goes fine, it will display the number of lines of code as below,

CLOC Result

Juback answered 26/8, 2020 at 12:8 Comment(0)
K
9

Nozzi's version doesn't work for me, but this one:

find . -type f -print0 | xargs -0 cat | wc -l
Khedive answered 4/7, 2012 at 14:0 Comment(1)
short and concise - but it counts lines also on non-source-code files. So actually the filename-extension filtering is crucial here. The OP wanted to count "source code lines". not any text lines.Carvey
P
7

A quick & easy way:

Use a regex search (Find Navigator, choose Find > Regular Expression).

.\n

Works conveniently with Xcode search scopes and you can easily customize it to whatever type of line you'd like to count ;).

Penthouse answered 20/11, 2013 at 6:35 Comment(2)
Now that's a nice one! not the fastest, but without leaving the dev environment. I like it. Probably a better regular expression can avoid counting white-space lines and things like that.Carvey
Keep it simple. I like it.Farny
S
7

I am not familiar with xcode, but if all you need is to count the number of lines from all those specific files within a directory tree, you may use the following command:

find .... match of all those files ... -exec wc -l {} +

Following Joshua Nozzi's answer, in GNU find the regular expression for such files would be like:

find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.swift" ")" -exec wc -l {} +

or even

find -regex ".*\.\(m\|mm\|cpp\|swift\)$" -exec wc -l {} +

this uses a regular expression to match all files ending in either .m, .mm, .cpp or .swift. You can see more information about those expressions in How to use regex in file find.

If you are working with Mac OS find, then you need a slightly different approach, as explained by Motti Shneor in comments:

find -E . -regex ".*\.([hmc]|mm|cp+|swift|pch)$" -exec wc -l {} +

Both will provide an output on the form of:

234 ./file1
456 ./file2
690 total

So you can either keep it like this or just pipe to tail -1 (that is, find ... | tail -1) so that you just get the last line being the total.

Stiver answered 7/11, 2015 at 18:26 Comment(5)
well --- the find -regex ".*\. etc doesn't work for me on MacOS - it claims an illegal option --r what kind of find are you using?Carvey
@MottiShneor you probably want to check this question where they talk about \| and others in MacOS. --regex is valid in both GNU find (the one I use) and MacOS.Stiver
well --- no. I found out in MacOS you must find -E for extended regular expressions. Do copy the suggested command above onto whatever OS-X you can get, and see for yourself. man find on MacOS-X tells you you must -E for that.Carvey
Finally, you must also provide a path where to search on OSX. so the variation that does work, looks like this: find -E . -regex ".*\.([hmc]|mm|cp+|swift|pch)$" -exec wc -l {} +Carvey
@MottiShneor thanks for providing the approach in MacOS. I have updated the answer with that.Stiver
M
6

You can install SLOCCount through MacPorts. Or, more crudely, you can use wc -l.

Macur answered 5/1, 2010 at 1:27 Comment(2)
CLOC is based on SCLOCount so i guess it is still a better approach to use CLOCAlbertoalberts
"Total Estimated Cost to Develop = $ 1,934,715". This made my day!Drennan
I
4

line-counter is a good alternative. It's lighter than CLOC and much more powerful and easier to use than other commands.

A quick overview

This is how you get the tool

$ pip install line-counter

Use line command to get the file count and line count under current directory (recursively)

$ line
Search in /Users/Morgan/Documents/Example/
file count: 4
line count: 839

If you want more detail, just use line -d.

$ line -d
Search in /Users/Morgan/Documents/Example/
Dir A/file C.c                                             72
Dir A/file D.py                                           268
file A.py                                                 467
file B.c                                                   32
file count: 4
line count: 839

And the best part of this tool is, you can add .gitignore like configure file to it. You can set up rules to select or ignore what kind of files to count just like what you do in '.gitignore'. Yes, this tool is just invented to make knowing how many lines I have easier.

More description and usage is here: https://github.com/MorganZhang100/line-counter

I'm the author of this simple tool. Hope it can help somebody.

Ingest answered 10/8, 2016 at 23:59 Comment(1)
Good Job! It helps me a lot.Ruder
A
3

Easy way - 100% working Tested

When I’m working on a swift project, I sometime’s get the urge to know how many lines of code I have actually written. It would be interesting to see. However, I’m not just going to sit there counting every single line, we’re programmers after all, we can do better!

Using Terminal Open up terminal and navigate to your project:

cd path/to/your/project/

Next, type in one of the following lines of code to get the total number of lines in your project. It works by reading all the swift files (sorry objc users!) in your directory and counting up the lines.

Not Including Pods

find . -path ./Pods -prune -o -name '*.swift' -print0 ! -name '/Pods' | xargs -0 wc -l

Including Pods


find . -name '*.swift' -print0 | xargs -0 wc -l

Using Cloc If you have homebrew on your mac, there is a much easier way to show the number of lines. Navigate to your project using terminal:

cd path/to/your/project/

Next, install Cloc

brew install cloc

Finally, type in one of the following commands

Not Including Pods

cloc . --exclude-dir=Pods

Including Pods

cloc .

Results:

enter image description here

Archilochus answered 12/5, 2023 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.