Dart Class Analyzer is a Dart package that provides tools for analyzing Dart classes and methods in a project. It helps you understand the structure of your codebase by counting methods in classes and provides insights into your project's organization.
❗ In order to start using Dart Class Analyzer, you must have the Dart SDK installed on your machine.
Activate the package with:
dart pub global activate dart_class_analyzerInstall via dart pub add:
dart pub add dart_class_analyzerCount the methods in a project with:
dart pub global run dart_class_analyzer:method_counter [options] <project_lib_path>-
-v If provided, analyzer will output all files and their method counts.
-
-g If provided, analyzer will include generated
.g.dartfiles in the analysis. -
-h If provided, analyzer will display usage help.
- Import the
dart_class_analyzerpackage.
import 'package:dart_class_analyzer/dart_class_analyzer.dart';- Create an instance of
DartClassAnalyzer.
final analyzer = DartClassAnalyzer();Use the countMethodsInFolder method to analyze Dart files in a folder.
final pathToDartFiles = 'path/to/your/dart/files';
final classes = analyzer.countMethodsInFolder(pathToDartFiles);
for (final classModel in classes) {
print('${classModel.className} has ${classModel.methodCount} methods');
}This will print information about each class in the specified folder, including the class name and the number of methods it has.
Remember to replace 'path/to/your/dart/files' with the actual path to your Dart files, and adjust the code examples based on your project structure.
Use countMethodsInClass to analyze a single Dart class represented by code.
final dartCode = '''
class MyClass {
void method1() {}
void method2() {}
}
''';
final classModel = analyzer.countMethodsInClass(dartCode);
if (classModel != null) {
print('${classModel.className} has ${classModel.methodCount} methods');
}This will print information about the specified class.
If you have a Dart class and want to analyze its methods using reflection, you can use the analyzeClass method.
class MyClass {
void method1() {}
void method2() {}
}
final methodCount = analyzer.analyzeClass(MyClass);
print('MyClass has $methodCount methods');This will print the total number of methods in the specified class.
Dart Class Analyzer comes with a built-in GitHub Actions workflow powered by [Very Good Workflows][very_good_workflows_link]. You can also integrate it with your preferred CI/CD solution.
On each pull request and push, the CI pipeline performs code formatting, linting, and testing to ensure code consistency and correctness. The project adheres to Very Good Analysis for strict analysis options. Code coverage is monitored using Very Good Coverage.
To run all unit tests:
dart pub global activate coverage 1.2.0
dart test --coverage=coverage
dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.infoTo view the generated coverage report, you can use lcov.
# Generate Coverage Report
genhtml coverage/lcov.info -o coverage/
# Open Coverage Report
open coverage/index.htmlIf you would like to contribute to the project, follow these steps:
- Fork the project.
- Create your feature branch (
git checkout -b feature/YourFeature). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.