Managing Multi-Package Flutter Projects with Melos — A Leap Towards Efficient Development 🚀
Hello everyone; Today I’m here for a specific topic deep dive in Flutter for you. I hope you enjoy while reading.
In the realm of Flutter development, many projects start with a single package that houses all features and utilities. However, as complexity grows, dividing your project into multiple packages becomes an enticing proposition. This division fosters enhanced separation of concerns, enables more modular development, and even opens up avenues for open-sourcing packages. Imagine a project structured as a multi-layered cake 🍰, where each layer serves a specific purpose, resulting in a more organized and maintainable codebase.
The Multi-Layered Architecture 🏗️
Consider a multi-package project organized into layers:
- Root Project: This layer holds common configurations applicable to all packages within the project.
- Feature Packages: Independent feature packages reside in this layer, each standing on its own and not reliant on others.
- Utility Packages: The utility layer encompasses packages used by multiple feature packages.
However, as projects embrace this modular approach, new challenges emerge. Let’s explore a particular challenge and the innovative solution that addresses it.
The Challenge: Scaling Tasks 🛠️
In a single-package Flutter project, tasks like flutter pub get
, flutter test
, flutter analyze
, and generating code coverages are straightforward. However, within a multi-package setup, running these tasks across all packages and presenting a consolidated result becomes intricate. The challenge lies in executing tasks individually in every package and then compiling a summary post-task completion.
The Solution: Melos 🚀
To overcome this challenge, two solutions emerge. The first involves writing bash scripts, a functional yet not-so-smart solution. The second, and the more intelligent option, integrates Melos into your multi-package project. Developed by the renowned Flutter community team at Invertase, Melos acts as a CLI tool for managing Flutter/Dart projects with multiple packages.
Unlocking Melos 🔑
Melos presents a plethora of features designed to streamline the management of multi-package projects:
- Automatic Versioning & Changelog Generation: Melos simplifies versioning and changelog creation, ensuring a smooth release process.
- Automated Publishing: Publishing packages to pub.dev becomes a breeze with Melos, saving time and enhancing code availability.
- Local Package Linking and Installation: Easily test changes across packages without disrupting your entire project.
- Simultaneous Command Execution: Execute commands across multiple packages, ensuring uniformity and efficiency.
- Listing Local Packages & Dependencies: Gain visibility into package dependencies for informed decision-making.
Navigating Melos 🧭
To harness Melos’ power, installation is the first step. Assuming you’ve installed the Flutter SDK and set path variables, execute:
dart pub global activate melos
Next, open your starter project in your preferred IDE. The project structure should mirror the intended layering.
Run below command for adding melos library to root project.
flutter pub add melos --dev
Creating a melos.yaml
file in your root app allows you to configure Melos:
name: your_project_name
packages:
- packages/*
Lets add an example script to this file
name: your_project_name
packages:
- packages/*
scripts:
analyze:
exec: dart analyze .
To execute a script defined in melos.yaml
, use the following command:
melos run analyze
the output should be like below…
Here a few example of predefined melos methods
Melos Bootstrap execute flutter pub get
for all linked libraries:
melos bootstrap
Melos Clean execute flutter clean
for all linked libraries:
melos clean
Crafting specific commands for tasks is where Melos shines. Let’s explore the creation of commands for running test cases:
scripts:
test:selective_unit_test:
exec: flutter test
dir-exists: "test"
fail-fast: true
description: Run selective unit tests
test:all_unit_tests:
exec: flutter test
no-select: true
description: Run all unit testsAdditionally, you can incorporate commands for analysis and code coverage. Melos offers a GUI interface for seamless execution without terminal input.
and run the test
melos test:all_unit_tests
the output should be like this
Embrace the Future with Melos 🌟
Mastering Melos unlocks the potential of multi-package Flutter projects. Its organized approach, efficiency, and community support make it an ideal tool for managing complexity. As you venture further, explore advanced filtering options and commands provided by Melos. Don’t hesitate to connect on social platforms to share your experiences. In a world of rapid development, Melos is your ally in orchestrating streamlined and efficient Flutter projects.
Remember, progress and innovation are just a Melos command away! 🎉
The official document and the fame repos who use melos links below:
- Official Documentation
- FlutterFire Best melos example
- Flame-Engine/Flame
- aws-amplify/amplify-flutter
- fluttercommunity/plus_plugins
- GetStream/stream-chat-flutter
- 4itworks/opensource_qwkin_dart
- gql-dart/ferry
- cbl-dart/cbl-dart
- ema987/paddinger
- flutter-stripe/flutter_stripe
- kmartins/groveman
- ApptiveGrid/apptive_grid_flutter
- flutternetwork/WiFiFlutter
- iapicca/yak_packages
- atsign-foundation/at_app
- sub6resources/flutter_html
- ferraridamiano/ConverterNOW
- rrifafauzikomara/youtube_video
- NetGlade/auto_mappr