A collection of useful scripts for dart or flutter projects

DFS – Dart Flutter Scripts

Warning: everything is WIP.

A collection of useful scripts for dart or flutter projects.

The scripts can be executed in the working directory of any dart or flutter project.

Installation:

Until the package is published to pub.dev, it can be installed as follow:

[~] dart pub global activate --source git https://github.com/osaxma/dfs.git

To run any script:

[~/path/to/project] dfs <script-command>

Note: each script will only work in the current working directory (i.e. one cannot pass a path to a different directory — at least for now).

Each <script-command> is shown below under the available scripts.

Available Scripts

  • Find Unused Packages (status: MVP)

    Area: project hygiene
    command: find-unused-packages
    alias: fup

    Description:
    with the help of dart’s analyzer and pubspec packages, the script inpset both the pubspec.yaml file and all dart files within lib to find any unused dependencies.

    Sample Output:

    [~/cwd] dfs find-unused-packages
        The following packages are not used anywhere in the lib directory. If they are used
        elsewhere (e.g. /test), consider moving them to `dev_dependencies` in `pubspec.yaml`
            - code_builder
            - dart_style
            - logging
    

  • Find Unused Top Level Declaration (status: MVP)

    Area: project hygiene
    command: find-unused-top-level
    alias: futl

    Finds unused top-level elements (classes, typedefs, getters, setters, functions and fields). This useful for dart/flutter projects that are not packages.

    Sample output

    [~/cwd]$ dfs find-unused-top-level
        finding unused top level declarations...  
        The following top level declarations are not used anywhere 
            - /cwd/lib/src/version.dart:7:7
            - /cwd/lib/src/common/ast_utils.dart:7:9

    To ignore specific files, you can pass globs to --ignore such as:

    dfs find-unused-top-level --ignore="lib/**.g.dart","lib/**.freezed.dart","**/genl10n/*.dart"
    

    For more info about the default ignored files, run the help command:

    dfs find-unused-top-level --help
    

  • Generate Data Classes (status: MVP)

    Area: utility
    command: generate-data-classes
    alias: gdc

    Generate the following for an existing class (in place):

    • copyWith method.
    • serialization methods (toMap/toJson) and deserialization factories (fromMap/fromJson).
    • Equality (operator ==) and hashcode.

    See how the data looks like before and after running the script at:

    The input data is read by parsing the Abstract Syntax Tree (AST) using the analyzer package, and then the data class is built using the code builder package.

Ideas for future scripts

  • Change Project Name (status: TBD)

    Area: pain
    command: TBD
    alias: TBD

    This is mainly for Flutter, and especially for multi-platform code where the name has to be changed in various places.


  • Run Script (status: TBD)

    Area: utility
    command: run
    alias: N/A

    [~/cwd] dfs run <script> 
    

    Where <script> is defined in pubspec.yaml

        script:
            build: flutter pub run build_runner build --delete-conflicting-outputs
            data: dfs generate-data-classes --endsWith="_data.dart" --directory="lib"

  • Get Packages For All (status: TBD)

    Area: utility
    command: TBD
    alias: TBD

    [~/cwd] dfs get packages --all
    

    Or

    [~/cwd] dfs get all
    

    Run dart pub get or flutter packages get for all packages within a monorepo or a repo with multiple packages.

GitHub

View Github