@0b1.org/loci 中文文档教程
Loci - Lines of Code Inventorizer
Key Features
- Command Line Mode or NPM Library
- Command Line can take environment variables or switches
- Multiple output formats: Text, JSON, XML, HTML, CSV
- Over 200 languages supported
- Modern stacks like Node, Angular, Vue, C#, Terraform
- Automatic disambiguation where file could be more than one language
- Override disambiguation for speed with command line switches
- Multiple threads and automatic single/multi-thread detetion based on file count
- Faster than Cloc in most scenarios
NPM Location
https://www.npmjs.com/package/@0b1.org/loci
History
我一直是 Cloc 的忠实粉丝年。 事实上,我在新项目中经常使用它,以至于其他代码计数器根本没有给我信心:任何其他代码计数器和我都会“比较苹果和橘子”——它们的结果并不完全相同。
不幸的是,许多环境不允许自定义二进制文件,排除了 cloc 作为 Perl 脚本和 cloc.exe。
然而,Node 更为流行,所以我决定看看我是否可以创建一个用 node 编写的 cloc 的近亲。
我想我成功了,但开源社区将是最终的评判者。
两者之间仍然存在一些细微差别。 Loci 默认排除文件夹 bin、obj 和 .vscode,而 Cloc 则不。 Cloc 还有一个 Loci 没有的非常成熟的功能列表:git repo querying、diffs、sql generation。 如果您需要这些功能,Cloc 仍然是更好的选择。
Cloc 在较小的文件组上速度更快,但速度只有几分之一秒。 对于批量扫描,Loci 调用工作线程,以便可以继续计数而无需等待文件 i/o。
在 Cloc 中,定义分布在整个 Perl 函数中,但在 Loci 中,定义被存储为 YAML 定义文件。 这意味着可以快速轻松地添加新语言。 在单个脚本中,大约 7,500 行 SOLID 代码 = 数据到 CLOC 的 17,000 行,它更易于维护,imo。
Performance
性能比较显示 Loci 在几乎所有代码大小上都优于 Cloc
Command-Line Usage
安装命令行用法,输入命令
>npm install -g @0b1.org/loci
要删除 CLI,输入命令
npm remove -g @0b1.org/loci
Basic帮助:
- Show help on basic usage and switches:
loci --help
- Show help on environment variables:
loci --show:environmentals
- Show the default options:
loci --show:defaults
- Show the current options:
loci --show:options
(includes any command line switches and selected folders) - Show the language definitions:
loci --show:languages
基本扫描:
- Scan a folder with default options
loci c:\_dev\OLD_CODE\uisdk
- Scan unique files in the folder
loci --unique c:\_dev\OLD_CODE\uisdk
- Scan, but exclude folders named 'backup'
loci --unique --xd:+backup c:\_dev\OLD_CODE\uisdk
- Scan, but exclude ONLY folders named 'backup'
loci --unique --xd:backup c:\_dev\OLD_CODE\uisdk
- (note the + symbol means, add these to the default excludes, but a missing + symbol means replace the defaults)
开关中用分号分隔的列表。 用空格分隔要扫描的文件夹列表。 (如果您的文件夹名称有空格,请用双引号
引起来)完整帮助链接: https://github.com/0bOne/loci/blob/main/lib/data/help/basic-usage.txt
环境变量信息: https://github.com/0bOne/loci/blob/main/lib/data/help/environmentals.txt
Library Usage
您可以在自己的项目中包含 npm 包。
在你当前的项目中输入命令 npm install @0b1.org/loci
为了最简单的使用,它只是包装了 CLI,使用:
const Loci = require ("@0b1.org/loci/lib/Loci");
Loci.CLIScan();
你可以像运行 Loci 一样运行你自己的代码:
node index.js --unique .
这个当然用处不大。 一个更完整的程序看起来像这样:
const Loci = require ("@0b1.org/loci/lib/Loci");
const options = Loci.LoadDefaultOptions();
options.sources.push("scan_this_folder);
options.xf.push("not_this_subfolder");
options.threads = 5;
//the default logger logs to the console.
//Or pass in your own logger implementing the same methods
const logger = Loci.GetLogger(options);
//instantiate and scan
const scanner = new Loci(logger);
const output = scanner.Scan(options);
//format and display the output.
//Alternatively, process the json object elsewhere in your project.
const formatter = Loci.GetFormatter(options);
const textResult = formatter.Format(output);
Loci.Output(textResult, options);
就是这样! 编码愉快!
Reporting Issues or feature requests
请包含命令行开关、环境变量或编程选项。 如果可能,还包含 loci 未按预期处理的代码文件。
请在提交前检查现有的错误列表
如果您想添加一种语言,您可以通过编辑 languages.yaml 自己进行试验或向我们发送一些示例文件。
您可以通过 sourcecoder0b1@gmail.com 与我联系
Contributing
即使像这样的小型开源项目也需要相当多的时间。 如果您想做出贡献,请告诉我!
许可证是麻省理工学院,所以请随意分支和试验。 (但请记住,如果一个项目得到改进,社区将比存在多个派生项目更有好处)
Compatibility
- Tested on node v16.0.0
Known Issues
- Only tested on windows and Linux
- Only runs in single-threaded mode on Node versions lower than 15.0.0. No intent to fix.
- Cucumber test fails (for Robot language only) in node 14.0.0 (passes in higher versions)
Release Notes
Version 1.0.7
- Added scale factor option
- Added --to: switch for output to file
- Added --supress switch for aSuppression of output components (times, files, code counts, totals)
- Added raster count support for XML, XSD
- Fixed - files being opened rw in certain circumstances, causing exceptions in certain circumstances
- Fixed - not ignoring node_modules folders and some others
- Fixed - zero value in indicidual code counts regression introduced in 1.0.6
Version 1.0.6
- Added static methods to Loci to get logger, formatter, and default options, so that only Loci module need be imported
- Added --tf switch to control single/multi threaded mode file count cut-off point.
- Defaulted -tf switch to 3500 based on 1.0.5 benchmark tests for likely best performance.
- Remvove several obsolete files from source
- Changed Loci instance scan method from 'Begin' to 'Scan' for improved readability
- Added disambiguate by Shebang scoring rule
- Added disambiguation rules for Ant/XML, Maven/XML, Verilog-SystemVerilog/Coq, and D/dtrace
- Added CSV output format
Version 1.0.5
- Addex experimental Raster scan for JavaScript only (approx 10-20% performance gain over RegEx)
- Refactored File Processing & regognition for readability and performance gain
- Refactored Unique file determination algorithm for significant performance gains (>75%)
- Added more granular benchmark graph to readme.
Version 1.0.4
- Fixed: works with Node 14.0.0
- Fixed: The filter processing time exceeds the total processing time in multi-threaded mode
- String tests migrated to Cucumber
- Tested on Ubuntu Server 20.04.03 LTS running Node 16.13.1
Version 1.0.3
- Fixed: RTE scanning empty folder
- Fixed: Cucumber tests all fail
- Fixed: Verious ESLint styling issues
- Tested working on Node version 15.0.0 or higher
- Added version check to generate error and exit on node versions below 15.0.0
Roadmap
- Raster scan support for all languages (ongoing effort)
- Custom XSLT, JSON Transforms, and/or HTML CSS for results formatting
- Use .gitignore to filter out files and folders
- Custom definitions for disambiguation and languages
- Output (scanned, unique, ignored, etc) to activity list
- Output as SQL inserts
- Scan inside archives
- Additional languages (on request)
- Scan remote git repos
Loci - Lines of Code Inventorizer
Key Features
- Command Line Mode or NPM Library
- Command Line can take environment variables or switches
- Multiple output formats: Text, JSON, XML, HTML, CSV
- Over 200 languages supported
- Modern stacks like Node, Angular, Vue, C#, Terraform
- Automatic disambiguation where file could be more than one language
- Override disambiguation for speed with command line switches
- Multiple threads and automatic single/multi-thread detetion based on file count
- Faster than Cloc in most scenarios
NPM Location
https://www.npmjs.com/package/@0b1.org/loci
History
I have been a huge fan of Cloc for many years. In fact, I have used it so often on new projects that other code counters just don't give me the confidence: Any other code counter and I would be "comparing apples to oranges" - they don't all give the same results.
Unfortunately many environments don't allow custom binaries, ruling out cloc as a Perl script AND cloc.exe.
Node, however, is more prevalent, so I decided to see if I could create a close cousin of cloc written in node.
I think I succeeeded, but the Open Source community will be the ultimate judge.
There is still a few minor differences between the two. Loci excludes the folders bin, obj and .vscode by default, whereas Cloc does not. Cloc also has a very mature feature list that Loci does not: git repo querying, diffs, sql generation. If you need those features, Cloc remains the better choice.
Cloc is faster on smaller groups of files but only by fractions of a second. For bulk scanning Loci invokes worker threads so that counting can continue without waiting for file i/o.
In Cloc definitions are spread throughout the Perl functions, but in Loci definitions fare stored as YAML definition files. This means new languages can be added quickly and easily. And at ~7,500 lines of SOLID code = data to CLOC's 17,000 lines in a single script, its a little more maintainable, imo.
Performance
Performance comparisons show Loci outperforming Cloc at almost all code sizes
Command-Line Usage
To install for command line usage, enter the command
>npm install -g @0b1.org/loci
To remove the CLI, enter the command
npm remove -g @0b1.org/loci
Basic help:
- Show help on basic usage and switches:
loci --help
- Show help on environment variables:
loci --show:environmentals
- Show the default options:
loci --show:defaults
- Show the current options:
loci --show:options
(includes any command line switches and selected folders) - Show the language definitions:
loci --show:languages
Basic scanning:
- Scan a folder with default options
loci c:\_dev\OLD_CODE\uisdk
- Scan unique files in the folder
loci --unique c:\_dev\OLD_CODE\uisdk
- Scan, but exclude folders named 'backup'
loci --unique --xd:+backup c:\_dev\OLD_CODE\uisdk
- Scan, but exclude ONLY folders named 'backup'
loci --unique --xd:backup c:\_dev\OLD_CODE\uisdk
- (note the + symbol means, add these to the default excludes, but a missing + symbol means replace the defaults)
Separated lists in switches with semicolons. Separate lists of folders to scan with spaces. (if your folder names have spaces, wrap them in double quotes)
Link to full help: https://github.com/0bOne/loci/blob/main/lib/data/help/basic-usage.txt
Environment Variable information: https://github.com/0bOne/loci/blob/main/lib/data/help/environmentals.txt
Library Usage
You can include the npm package in your own project.
In your current project enter the command npm install @0b1.org/loci
For the simplest use, which just wraps the CLI, use:
const Loci = require ("@0b1.org/loci/lib/Loci");
Loci.CLIScan();
And you can run your own code as though it were Loci:
node index.js --unique .
This, of course, is not much use. A more complete program would look like this:
const Loci = require ("@0b1.org/loci/lib/Loci");
const options = Loci.LoadDefaultOptions();
options.sources.push("scan_this_folder);
options.xf.push("not_this_subfolder");
options.threads = 5;
//the default logger logs to the console.
//Or pass in your own logger implementing the same methods
const logger = Loci.GetLogger(options);
//instantiate and scan
const scanner = new Loci(logger);
const output = scanner.Scan(options);
//format and display the output.
//Alternatively, process the json object elsewhere in your project.
const formatter = Loci.GetFormatter(options);
const textResult = formatter.Format(output);
Loci.Output(textResult, options);
That's it! Happy coding!
Reporting Issues or feature requests
Please inlude the command line switches, environment variables, or programmatic options. If possible, also include the code file that loci did not process as expected.
PLEASE check the existing bug list before submitting
If you would like a language added, you can experiment yourself by editing languages.yaml or send us some sample files.
You can contact me at sourcecoder0b1@gmail.com
Contributing
Even a small open source project like this takes a fair amount of time. If you would like to contribute, please let me know!
The license is MIT, so feel free to branch and experiment as you please. (But bear in mind it is benefits the community more if one project is improved than if multiple derivations exist)
Compatibility
- Tested on node v16.0.0
Known Issues
- Only tested on windows and Linux
- Only runs in single-threaded mode on Node versions lower than 15.0.0. No intent to fix.
- Cucumber test fails (for Robot language only) in node 14.0.0 (passes in higher versions)
Release Notes
Version 1.0.7
- Added scale factor option
- Added --to: switch for output to file
- Added --supress switch for aSuppression of output components (times, files, code counts, totals)
- Added raster count support for XML, XSD
- Fixed - files being opened rw in certain circumstances, causing exceptions in certain circumstances
- Fixed - not ignoring node_modules folders and some others
- Fixed - zero value in indicidual code counts regression introduced in 1.0.6
Version 1.0.6
- Added static methods to Loci to get logger, formatter, and default options, so that only Loci module need be imported
- Added --tf switch to control single/multi threaded mode file count cut-off point.
- Defaulted -tf switch to 3500 based on 1.0.5 benchmark tests for likely best performance.
- Remvove several obsolete files from source
- Changed Loci instance scan method from 'Begin' to 'Scan' for improved readability
- Added disambiguate by Shebang scoring rule
- Added disambiguation rules for Ant/XML, Maven/XML, Verilog-SystemVerilog/Coq, and D/dtrace
- Added CSV output format
Version 1.0.5
- Addex experimental Raster scan for JavaScript only (approx 10-20% performance gain over RegEx)
- Refactored File Processing & regognition for readability and performance gain
- Refactored Unique file determination algorithm for significant performance gains (>75%)
- Added more granular benchmark graph to readme.
Version 1.0.4
- Fixed: works with Node 14.0.0
- Fixed: The filter processing time exceeds the total processing time in multi-threaded mode
- String tests migrated to Cucumber
- Tested on Ubuntu Server 20.04.03 LTS running Node 16.13.1
Version 1.0.3
- Fixed: RTE scanning empty folder
- Fixed: Cucumber tests all fail
- Fixed: Verious ESLint styling issues
- Tested working on Node version 15.0.0 or higher
- Added version check to generate error and exit on node versions below 15.0.0
Roadmap
- Raster scan support for all languages (ongoing effort)
- Custom XSLT, JSON Transforms, and/or HTML CSS for results formatting
- Use .gitignore to filter out files and folders
- Custom definitions for disambiguation and languages
- Output (scanned, unique, ignored, etc) to activity list
- Output as SQL inserts
- Scan inside archives
- Additional languages (on request)
- Scan remote git repos