如何查看Xcode项目中有多少行代码?
有没有办法确定 Xcode 项目包含多少行代码?我承诺不会将此类信息用于管理衡量或员工基准测试目的。 ;)
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. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我看到这个漂浮在周围并自己使用它:
I see this floating around and use it myself:
查看 CLOC。
(旧版本存档在 SourceForge 上。)
Check out CLOC.
(Legacy builds are archived on SourceForge.)
我一直在使用 CLOC 作为
Nathan Kinsinger
提到过,它相当容易使用。它是一个 PERL 脚本,您可以从项目目录添加并运行它。PERL 已经是 Mac OS 的一部分,您可以通过这种方式调用脚本来找出您已编写的行数:
这是我从此类命令获得的输出示例:
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:
This is an example of output i got from such command:
打开 Terminal.app,进入项目的根目录,然后运行以下命令:
仅适用于 Swift:
仅适用于 Obj-C:
适用于 Obj-C + Swift:
适用于 Obj-C + Swift + C + C++:
Open up Terminal.app, go into your project's root directory, and run this command:
For Swift only:
For Obj-C only:
For Obj-C + Swift:
For Obj-C + Swift + C + C++:
在终端中,切换到项目目录并运行:
如果您只需要某些文件类型,请尝试类似的操作
In terminal, change into the project directory and run:
If you want only certain file types, try something like
在项目中执行以下命令:
<前><代码>查找 . -path ./Pods -prune -o -name "*.swift" -print0 ! -名称“/Pods”| xargs -0 wc -l
或者:
<前><代码>查找 . -path ./Pods -prune -o -name "*[hm]" -print0 ! -名称“/Pods”| xargs -0 wc -l
(*从总计数中排除 pod 文件计数)
execute following command inside your project:
Or:
(*Excluding pod files count from total count)
查看 Xcode Statistician,它完全可以满足您的需求。它还提供了其他有趣的统计数据,因此值得时不时地运行一下。
请注意,它不会在实际文件夹中查找,但会成组查找。很可能您没有使用真正的文件夹,所以它会很好用。如果您使用文件夹,那么您只需在每个文件夹中进行计数并将它们添加在一起。
注意:截至 2012 年 6 月,这似乎无法在最新版本的 Xcode 上正常工作。
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.
如果您在终端中转到项目目录并输入:
这将为您提供项目细分,以及每个文件和整个项目的行总数。
If you go to your project's directory in terminal and enter:
That will give you a project breakdown, as well as the line total for each file and the project as a whole.
在 Mac 中实现 CLOC 库的步骤如下:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果询问,请输入系统密码。
您将看到如下所示的终端屏幕。
系统会弹出这么多权限,允许所有权限
如果一切顺利,您将看到如下终端屏幕,
brew install cloc
<代码>时钟。 --exclude-dir=Pods (排除 pod 文件)
cloc .
(包括 pod 文件)如果一切顺利,它将显示代码行数
Steps to implement CLOC library in Mac as below:
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.
System will popup so many permissions, allow all the permissions
If everything goes fine, you will see terminal screen as below,
brew install cloc
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,
Nozzi 的版本不适合我,但是这个:
Nozzi's version doesn't work for me, but this one:
快速&简单的方法:
使用正则表达式搜索(“查找导航器”,选择“查找”>“正则表达式”)。
可以方便地与 Xcode 搜索范围配合使用,您可以轻松地将其自定义为您想要计算的任何类型的行;)。
A quick & easy way:
Use a regex search (Find Navigator, choose Find > Regular Expression).
Works conveniently with Xcode search scopes and you can easily customize it to whatever type of line you'd like to count ;).
我不熟悉 xcode,但如果您需要的只是计算目录树中所有这些特定文件的行数,您可以使用以下命令:
遵循 Joshua Nozzi 的回答,在 GNU find 中,此类文件的正则表达式如下:
或者甚至
使用正则表达式来匹配以 < 结尾的所有文件代码>.m、
.mm
、.cpp
或.swift
。您可以在如何在文件查找中使用正则表达式中查看有关这些表达式的更多信息。如果您使用Mac OS find,那么您需要稍微不同的方法,如 Motti 所解释的注释中的Shneor:
两者都会提供以下形式的输出:
因此您可以像这样保留它,或者只是通过管道传输到
tail -1
(即find .. . | tail -1
) 这样你就可以得到最后一行的总数。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:
Following Joshua Nozzi's answer, in GNU find the regular expression for such files would be like:
or even
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:
Both will provide an output on the form of:
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.您可以通过 MacPorts 安装 SLOCCount。或者,更粗略地说,您可以使用 wc -l。
You can install SLOCCount through MacPorts. Or, more crudely, you can use wc -l.
line-counter
是一个不错的选择。它比 CLOC 更轻,并且比其他命令更强大且更易于使用。快速概述
这是获取该工具的方式
使用
line
命令获取当前目录下的文件数和行数(递归)如果您想要更多详细信息,只需使用
line -d.
这个工具最好的部分是,您可以向其中添加 .gitignore 之类的配置文件。您可以设置规则来选择或忽略要计数的文件类型,就像在“.gitignore”中所做的那样。是的,这个工具只是为了让我更容易知道我有多少行而发明的。
更多描述和用法在这里:https://github.com/MorganZhang100/line-counter
我是这个简单工具的作者。希望它可以帮助别人。
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
Use
line
command to get the file count and line count under current directory (recursively)If you want more detail, just use
line -d
.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.
简单方法 - 100% 经过测试
当我在处理一个 swift 项目时,有时我会很想知道我实际编写了多少行代码。看看会很有趣。然而,我不会只是坐在那里数每一行,我们毕竟是程序员,我们可以做得更好!
使用终端
打开终端并导航到您的项目:
接下来,输入以下代码行之一以获取项目中的总行数。它的工作原理是读取目录中的所有 swift 文件(对不起 objc 用户!)并计算行数。
不包括 Pod
Pod
包括使用 Cloc 的
如果您的 Mac 上有自制程序,则有一种更简单的方法来显示行数。使用终端导航到您的项目:
接下来,安装 Cloc
最后,输入以下命令之一
不包括 Pods
包括 Pods
结果:
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:
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
Including Pods
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:
Next, install Cloc
Finally, type in one of the following commands
Not Including Pods
Including Pods
Results: