24

What is the way to count total number of lines in an Xcode Project? I can see number of lines in an individual file but I need a sum up of all the lines in a project.

Tim
  • 8,932
  • 4
  • 43
  • 64

5 Answers5

55

A lightweight solution if you're using Homebrew (and a fan of the terminal) is the command-line program 'Cloc' (count lines of code). It breaks down the output for languages used in your project and gives you other useful information.

Cloc

$ brew install cloc 
$ cd path/to/project/ 
$ cloc .
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Tim
  • 8,932
  • 4
  • 43
  • 64
36

If you don't want to pay $4.99 for a one time use, and you don't want to bother with HomeBrew. While it does count the empty lines between your code, you can do this:

  1. Open Terminal
  2. cd to your Xcode project
  3. Execute the following when inside your target project:

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

If you want to exclude pods:

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

If your project has objective c and swift:

find . -type d \( -path ./Pods -o -path ./Vendor \) -prune -o \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -print0 | xargs -0 wc -l
ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
9

Check out: CLOC

ClOC counts blank lines, comment lines, and physical lines of source code.

To use CLOC (Count Lines Of Code) for count number of lines in a project. Download the the CLOC .pl file and write following line in terminal:

perl ./DirectoryWhereClockFileIS/cloc-1.56.pl ./YourDirectoryWhereYourSourcesAre

It will show you results like:

enter image description here

Chanchal Raj
  • 4,176
  • 4
  • 39
  • 46
7

There is an app on the App Store called Xcode Statistics. (Or something like that). It does what you want.

A word of warning though. The number of lines in a project has little to no relation to the quality or complexity of that project.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 2
    Thanks for that. And on your second point, I know I know. It is just for the fun of it ;) –  Feb 27 '15 at 11:56
  • 1
    Oh and for others the name is: "project statistics for xcode" –  Feb 27 '15 at 12:01
  • 2
    Somebody should invent a way to point to another resource without having to relate on the correct search term. /snark https://itunes.apple.com/us/app/project-statistics-for-xcode/id650764572?l=en&mt=12 – Matthias Bauch Feb 27 '15 at 12:17
  • @MatthiasBauch lol! I was on my phone at the time so wasn't able to find the app on the Mac App Store. Lol – Fogmeister Feb 27 '15 at 12:58
  • Comments on App Store indicate no Swift support. Last version update was 5 years ago. – Darrell Root Nov 27 '19 at 23:34
  • @DarrellRoot you’re commenting on a question and answer that is 5 years old... ‍♂️ – Fogmeister Nov 27 '19 at 23:54
  • 2
    Yes. Just had this problem and upvoted the answer in 4th place as most helpful. Did not downvote anyone. Just commented. We are building a library of questions and answers, but the rapid recent development of Swift hurts answer relevance. Might be a topic for meta. – Darrell Root Nov 28 '19 at 00:02
  • 1
    Xcode Statistics is out of date and no longer works – easiwriter Mar 04 '20 at 12:00
0

You can use this script to do that :)

#!/usr/bin/python

"""
    Count number of lines of code in a projects 
    directory.

    Credits to https://github.com/0RaMsY0
"""

import os
import argparse

def count_lines(path: str, code_extension: str) -> int:
    FOLDERS = []
    FILES = []
    PATHS = [path]
    TOTAL_LINES = 0
    STOP_LOOP = False

    while STOP_LOOP != True:
        for i in PATHS:
            for _ in os.listdir(i):
                if _.endswith(f".{code_extension}"):
                    FILES.append(f"{i}/{_}")
                else:
                    if os.path.isdir(f"{i}/{_}"):
                        PATHS.append(f"{i}/{_}")
        
        STOP_LOOP = True
    
    for FILE in FILES:
        TOTAL_LINES += len(open(FILE, "r").readlines())
    
    return TOTAL_LINES



if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-p", "--path", help="Path to count code numbers")
    parser.add_argument("-ex", "--extension", help="Code extenstion e.g py js ...")
    
    args = parser.parse_args()

    PATH = args.path
    CODE_EXTENSION = args.extension

    TOTAL_LINES = count_lines(PATH, code_extension=CODE_EXTENSION)

    print(TOTAL_LINES)

Usage:

you need to pass in two flags one for your project's path and the other is the extension of the source code for example it can be py for python, js for javascript etc...

python countlines.py -p "/path/to/project" -ex py 

Warning: Do not add a '.' to the start of the extension or it will not work

ramsy
  • 21
  • 3