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

前端 未结 15 1538
闹比i
闹比i 2020-11-30 16:32

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 purp

相关标签:
15条回答
  • 2020-11-30 16:34

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

    0 讨论(0)
  • 2020-11-30 16:39

    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

    0 讨论(0)
  • 2020-11-30 16:39

    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.

    0 讨论(0)
  • 2020-11-30 16:40

    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
    -------------------------------------------------------------------------------
    
    0 讨论(0)
  • 2020-11-30 16:45

    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 ;).

    0 讨论(0)
  • 2020-11-30 16:46

    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.

    0 讨论(0)
提交回复
热议问题