Skip to content

onepub-dev/barrel_create

Repository files navigation

CREATE BARREL

Create Barrel is a CLI tool that creates a barrel file for the passed directory(s).

A barrel file is a dart library that exports a list of files so that you can import the barrel file rather than each individual file.

For example if you had a directory call dao with the following files:

/dao
dao_job.dart
dao_customer.dart
dao_contact.dart

Create Barrel would create a barrel file in that directory called dao.g.dart:

dao.g.dart

//
// Generated file. Do not modify.
// Created by `barrel_create`
//
export 'dao_job.dart';
export 'dao_customer.dart';
export 'dao_contact.dart';

So now you can import the barrel file rather than each individual file:

import 'dao_job.dart';
import 'dao_customer.dart';
import 'dao_contact.dart';

becomes:

import 'dao/dao.g.dart';

To create a barrel file:

dart pub global activate barrel_create
barrel_create [--threshold=n] [--recursion] [--[no-]quiet] <path to directory> [path to directory]...

Examples

To avoid having to retype the arguments you can create a settings file for each project that controls where barrel_create creates barrel files.

recursively create barrel files for every directory under the lib directory that contains at least 3 Dart files.

cd my/project/root
barrel_create 

Barrel Create also has a short cut brl

cd my/project/root
brl

create a barrel file for a specific directory

cd my/project/root
brl lib/src/dao

create a barrel file for multiple directory

cd my/project/root
brl lib/src/dao lib/src/entity

create a barrel file for any directory in my project with at least 10 dart libraries

cd my/project/root
brl -t 10 

create a barrel file for all directory under lib/src/ui with at least 4 libraries.

cd my/project/root
brl -t 4 -r lib/src/ui

Advanced options

directories

By default barrel_create only scans libraries that exists under the project lib directory.

You can pass one or more directories to tell barrel_create to only scan specific directories or to scan directories outside the project lib directory.

Directories MUST be relative to the current project's root directory.

You can pass a list of directories to barrel_create and it will process each directory in turn:

brl tool test lib

recursion

By passing in the --recursion (-r) flag, barrel create will recursively process all sub-directories under each of the passed directories.

brl -r  tool  test

threshold

By default, when recursing, barrel_create will only create a barrel file for directories which contain at least three dart libraries.

If you pass a directory, but don't specify the --recursive option, then a barrel file will be created even if only a two dart files exist in the directory (i.e. threshold is set to 2).

If there is only a single library in a directory it makes no sense to have a barrel file.

You can change the threshold by passing the --threshold (-t) flag

brl -t 10 tool test

quiet

When recursing barrel_create reports any directory that it inspected but didn't have enough .dart files to trigger the creation of a Barrel file.

By default these messages are suppressed. You can enable the messages by passing --no-quiet (-no-q) flag.

generated directories

If barrel_create detects a directory that has generated files (*.g.dart) in it (other than its own file) then it will not generate a barrel file in that directory.

Most generated directories already have a barrel file created by the generation tool.

If you want barrel_create to create a barrel file then use the touch command (or an editor) to create an empty barrel file in that directory and from then on barrel_create will create a barrel file in that directory.

So if you have directory:

generated
generate\customer.json.g.dart

Create an empty file: generate\generate.g.dart

generated
generate\generate.g.dart
generate\customer.json.g.dart

On the next run barrel_create will overwrite generate.g.dart with a barrel file.

Settings File

To save you from having to type the same args for barrel_create each time you run it, you can instead create a settings file in your dart projects tool directory.

Whenever you run barrel_create without any arguments and a settings file exist, then the settings will be used.

The settings file is placed in: <my project>/tool/barrel_create.yaml

Example

quiet: true
threshold: 10
recursive: true
directories:
  - dao
  - entity

To use the settings file:

cd myproject
brl

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages