LayerLens
Keep your code well structured.
What is LayerLens?
LayerLens is a tool that:
-
Automatically generates a dependency diagram as a Mermaid
flowchart
within every directory of your Dart or Flutter package. -
Identifies and alerts you to any cyclic dependencies.
NOTE: LayerLens shows inside-package dependencies. For cross-package dependencies use flutter pub deps
.
How does it work?
Unlike other dependency visualization and cycle detection tools that focus on language-specific concepts, LayerLens innovates by operating on file system concepts. It assumes that organizing your code by files and directories accurately reflects your mental model of the project.
Specifically, LayerLens:
-
Restricts each diagram to the content of a single directory, so that every directory has its own diagram.
-
For each directory treats immediate sub-directories and files as equal elements, and shows dependencies between these elements as a directed graph.
As result:
-
Each directory diagram is simple. It does not contain (1) any internal details of directories or files, and (2) any details of code outside the directory.
-
All diagrams together are enough to detect cycles in application.

Configure layerlens
Globally
-
Run
dart pub global activate layerlens
-
Verify you can run
layerlens
. If you getcommand not found
, make sure your path contains pub cache.
As a dependency
-
Add
layerlens: <version>
to the sectiondev_dependencies
in the package's pubspec.yaml. -
Run
dart pub get
orflutter pub get
for the package. -
Verify you can run
dart run layerlens
.
Configure IDE
To see the diagrams in your IDE:
-
VSCode: install
Markdown Preview Mermaid Support
extension -
Android Studio: enable the "Mermaid" extension in the Markdown language settings
Generate diagrams
-
Run command:
-
With global configuration:
layerlens
in the root of the package or in other place with--path <your package root>
-
With package configuration:
dart run layerlens
in the package root
-
-
Find the generated file DEPS.md in each source folder, where libraries or folders depend on each other.
-
In VSCode, right click DEPS.md and select 'Open Preview'
Continuous integration
Fail if issues
Make your pre-submit bots failing in case of issues, using flags:
--fail-on-cycles
: fail if there are dependency cycles--fail-if-changed
: fail if the generated diagrams has changed
Re-generate on every GitHub push
Copy the content of run-layerlens.yaml
to .github/workflows
.
It will work if your repo policy allows bots to update files.
Filter
If you want to generate the DEPS.md
only for a specific folders, use --only
and --except
options,
formatted as glob syntax.
For example, to generate the diagrams:
- only for the root
lib/
folder:dart run layerlens --only "lib"
- only for the root
lib/
folder:dart run layerlens --only "lib"
- for all folders except
l10n/
:dart run layerlens --except "l10n"
- only for root
lib/
and it's subfolder: runlayerlens --only "lib" --only "lib/subfolder1"
- for the entire subtree for a given subfolder:
layerlens --only "lib/subfolder1" --only "lib/subfolder1/**"
Supported languages
While layerlens concepts are language agnostic, for now only dart
is supported.
Please submit an issue, if you want other language to be added.
Contribute to layerlens
See CONTRIBUTING.md
for details.
License
Apache 2.0; see LICENSE
for details.
Disclaimer
This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.