A information to code era utilizing Mason.

Each group I’ve labored with was below stress to launch the product sooner, sending quicker updates and bug fixes. Though there are a lot of components that contribute to the event pace, one factor which all the time slowed me down is writing the boilerplate code. In a common venture, it could actually encompass as much as 30% of the entire code. This makes me surprise if there’s a method attainable to generate these codes. Which introduced me to Mason. So what’s Mason?
Mason permits builders to create and eat reusable templates known as bricks.
Principally, Mason is a template generator the place every template is named bricks. Mason supplies Mason CLI to create and eat these templates.
Though Mason is written in dart and it’s gaining recognition within the Flutter neighborhood however it may be used to generate templates for any language. Let’s see how we will use this in our venture to hurry up our improvement.
Mason will be put in both with the dart pub or brew.
# 🎯 Activate from https://pub.dev
dart pub international activate mason_cli# 🍺 Or set up from https://brew.sh
brew faucet felangel/mason
brew set up mason
Mason supplies fairly a couple of instructions to construct templates.

When you’ve put in mason_cli you’ll be able to go into the foundation folder of your venture and initialize mason.
mason init
This can generate the mason.yaml
file in your venture which accommodates metadata for all of your bricks.
# Register bricks that may be consumed through the Mason CLI.
# https://github.com/felangel/mason
bricks:
# Pattern Brick
# Run `mason make hiya` to attempt it out.
hiya: 0.1.0+1
# Bricks can be imported through git url.
# Uncomment the next strains to import
# a brick from a distant git url.
# widget:
# git:
# url: https://github.com/felangel/mason.git
# path: bricks/widget
Bricks merely are customizable reusable templates that may generate code with dynamic variables. These brick templates will be saved domestically within the venture, remotely in git, or will be revealed to the brickhub.
Mason makes use of mustache, a logic much less template syntax. To know extra about mustache test this documentation.
A brand new brick template will be created with the mason new
command.
mason new <BRICK_NAME> -d <DISCRIPTION>
mason new cubit -d "A brand new cubit template"
✓ Created new brick: cubit (0.0s)
✓ Generated 5 file(s):
/Customers/divyanshubhargava/bricks/cubit/brick.yaml (new)
/Customers/divyanshubhargava/bricks/cubit/README.md (new)
/Customers/divyanshubhargava/bricks/cubit/CHANGELOG.md (new)
/Customers/divyanshubhargava/bricks/cubit/LICENSE (new)
/Customers/divyanshubhargava/bricks/cubit/__brick__/HELLO.md (new)
Every brick template consists of the next information and folders.
__brick__
listing: All of your template information are added to this Listing.bricks.yaml
: A metadata file on your brick template.
identify: cubit
description: A brand new cubit template# The next defines the model and construct quantity on your brick.
# A model quantity is three numbers separated by dots, like 1.2.34
# adopted by an optionally available construct quantity (separated by a +).
model: 0.1.0+1# The next defines the setting for the present brick.
# It contains the model of mason that the brick requires.
setting:
mason: ">=0.1.0-dev <0.1.0"# Variables specify dynamic values that your brick relies on.
# Zero or extra variables will be specified for a given brick.
# Every variable has:
# * a sort (string, quantity, or boolean)
# * an optionally available quick description
# * an optionally available default worth
# * an optionally available immediate phrase used when asking for the variable.
vars:
identify:
sort: string
description: Cubit identify
default: Sprint
immediate: What is the cubit identify?
You’ll be able to see that we’ve outlined a variable identify
. Which would be the identify of the cubit class.
Let’s soar to the __brick__
listing and begin writing the cubit template. Let’s create 2 directories lib
and take a look at
with nested listing cubit
.
Now we have to create a listing with a cubit identify. For that, we will create a brand new listing with listing identify as #snakeCasename/snakeCase
.
This would be the folder construction of the __brick__
listing.
--__brick__
|--lib
| |--cubit
| | |--#snakeCasename/snakeCase
|
|--test
| |--cubit
| | |--#snakeCasename/snakeCase
|
|....
Within the --lib -> cubit -> #snakeCasename/snakeCase
we’ll create a state class and a cubit class.
#snakeCasename_state/snakeCase.dart
Within the --lib -> cubit -> #snakeCasename/snakeCase
we’ll create a state class and a cubit class.
#snakeCasename_state/snakeCase.dart
a part of '#snakeCasename_cubit/snakeCase.dart';class #pascalCasenameState/pascalCase extends Equatable
const #pascalCasenameState/pascalCase();@override
Record<Object?> get props => [];
#snakeCasename_cubit/snakeCase.dart
import 'bundle:equatable/equatable.dart';
import 'bundle:flutter_bloc/flutter_bloc.dart';half '#snakeCasename_state/snakeCase.dart';class #pascalCasenameCubit/pascalCase extends Cubit<#pascalCasenameState/pascalCase>
#pascalCasenameCubit/pascalCase() : tremendous(const #pascalCasenameState/pascalCase());
In --test -> cubit -> {#snakeCasename/snakeCase}
we’ll create a cubit take a look at class.
{#snakeCasename_cubit_test/snakeCase}.dart
import 'bundle:bloc_test/bloc_test.dart';
import 'bundle:mason_demo/cubit/#snakeCasename/snakeCase/#snakeCasename_cubit/snakeCase.dart';void foremost()
blocTest(
'emits [] when nothing is added',
construct: () => #pascalCasenameCubit/pascalCase(),
count on: () => [],
);
Now we’re carried out with our templates let’s add them to mason.yaml
.
bricks:
#Bricks
cubit:
path: bricks/cubit
To put in bricks merely run the mason get
command.
That is the place the magic occurs. Each time we have to create a brand new cubit we will merely run the mason makes cubit
command.
mason make cubit? What is the cubit identify? (Sprint) Auth
✓ Made brick cubit (0.1s)
✓ Generated 3 file(s):
/Customers/divyanshubhargava/StudioProjects/mason_demo/take a look at/cubit/auth/auth_cubit_test.dart (equivalent)
/Customers/divyanshubhargava/StudioProjects/mason_demo/lib/cubit/auth/auth_cubit.dart (new)
/Customers/divyanshubhargava/StudioProjects/mason_demo/lib/cubit/auth/auth_state.dart (new)
Voilà, inside a fraction of seconds we will generate all our cubit information.
auth_cubit.dart
import 'bundle:equatable/equatable.dart';
import 'bundle:flutter_bloc/flutter_bloc.dart';half 'auth_state.dart';class AuthCubit extends Cubit<AuthState>
AuthCubit() : tremendous(const AuthState());
auth_state.dart
a part of 'auth_cubit.dart';class AuthState extends Equatable
const AuthState();@override
Record<Object?> get props => [];
auth_cubit_test.dart
import 'bundle:bloc_test/bloc_test.dart';
import 'bundle:mason_demo/cubit/auth/auth_cubit.dart';void foremost()
blocTest(
'emits [] when nothing is added',
construct: () => AuthCubit(),
count on: () => [],
);
One factor which I discovered actually useful whereas producing code is to capable of have conditional code. For instance, if you wish to generate a brand new cubit and state class with out Equatable. To take action let’s add a brand new variable known asuseEquatable
in our brick.yaml
file.
useEquatable:
sort: boolean
description: Use Equatable
default: false
immediate: Do you need to use Equatable?
Now wrap the Equatable code in useEquatable
variable.
#useEquatable
// Your code
/useEquatable
Lastly, that is how our state’s class appears to be like like.
a part of '{#snakeCase}name_cubit{/snakeCase}.dart';class #pascalCase{identify}State/pascalCase #useEquatableextends Equatable/useEquatable {
const {#pascalCase}{{identify}}State{/pascalCase}();#useEquatable
@override
Record<Object?> get props => [];
/useEquatable
}
Record put in Bricks
mason listing//ormason ls
Add Bricks
mason add <BRICK_NAME>
Take away Bricks
mason take away <BRICK_NAME>
What we’ve carried out right here is simply the tip of the iceberg. With Mason, you’ll be able to obtain quite a bit.
Right here is the total working instance of this tutorial -> Mason_Demo
I’d extremely suggest you to take a look at the mason repo for a extra detailed view and the newest adjustments.
I hope you loved this text. You probably have any queries or ideas please let me know within the feedback down under.
Need to Join?You'll be able to join with me on Twitter, Github and LinkedIn.