phntmxyz / sidekick

Your personal CLI for your Flutter project
https://pub.dev/packages/sidekick
Apache License 2.0
56 stars 5 forks source link
dart flutter hacktoberfest

Sidekick

Dart CLI generator for Flutter and Dart apps - extend your project with custom tasks, add a sidekick to your app.

Write your automation scripts in Dart - a language all your coworkers are comfortable with - while fully supporting debugging and testing without losing the simplicity of executing shell scripts.

Awesome examples

Getting Started

Create your first CLI

Install the CLI generator sidekick. This is only required for generation, not for the execution of the CLI.

dart pub global activate sidekick

Initialize project

sidekick init <path-to-repo> 

Follow the instructions and you just created your first sidekick CLI. You can execute your CLI right away using its entrypoint and use any of the existing tasks.

Assuming your CLI is called flg (short for flutter gallery), execute the flg shell script in the root of your repository.

$ ./flg

A sidekick CLI to equip Dart/Flutter projects with custom tasks

Usage: flg <command> [arguments]

Global options:
-h, --help    Print this usage information.

Available commands:
  analyze          Dart analyzes the whole project
  clean            Cleans the project
  dart             Calls dart
  deps             Gets dependencies for all packages
  format           Formats all Dart files in the project
  flutter          Call the Flutter SDK associated with the project
  sidekick         Manages the sidekick CLI

Run "flg help <command>" for more information about a command.

Plugins

Our Favorite Plugins

Plugin Description
sidekick_vault Store project secrets encrypted in your repository
dockerize_sidekick_plugin Wrap your Flutter Web App in a docker container
flutterw_sidekick_plugin Pin a Flutter version to your project and share it with your team

See the full list of available plugins

To write your own plugin checkout the docs.

Install plugin

To install more command, you can use install plugins with

$ <cli> sidekick plugins install <pub-package>

Preinstalled commands

analyze

Runs the analyzer for all packages in the project.

dart

Runs the bundled dart runtime with any arguments. By calling flg dart you make sure to always use the correct dart version anyone else in your project is using.

clean

Deletes the build directory of the main application. This commands code is part of your CLI, intended to be modified to your needs.

deps

Gets all dependencies for all packages in your project. This will become your most used command in no time!

format

Formats all Dart files in the project. Default is 80. Change the line length for a specific package by adding the following to the pubspec.yaml of the package:

format:
  line-length: 120

flutter

Runs the bundled flutter runtime (provided via flutter-wrapper) with any arguments. By calling flg flutter you make sure to always use the correct flutter version, like anyone else of your team.

sidekick install-global

You can execute your CLI from anywhere. To do so, run the install-global command and follow the instructions.

$ ./flg sidekick install-global

Please add $HOME/.sidekick/bin to your PATH. 
Add this to your shell's config file (.zshrc, .bashrc, .bash_profile, ...)

  export PATH="$PATH":"$HOME/.sidekick/bin"

Then, restart your terminal

After adjusting your PATH, you can execute the CLI from anywhere.

$ flg

sidekick plugins create

A plugin template can be generated with sidekick plugins create --template <template type> --name <plugin name>.

This plugin was generated from the template $templateType.

The --template parameter must be given one of the following values:

sidekick plugins install

Installing a plugin from a pub server

<cli> sidekick plugins install <plugin name on pub server, e.g. sidekick_vault>

By default, pub.dev is used as pub server. A custom pub server can be used with the --hosted-url parameter.

Installing a plugin from a git repository

<cli> sidekick plugins install --source git <link to git repository>

Optional parameters:

Installing a plugin from a local path

<cli> sidekick plugins install --source path <path to plugin on local machine>

recompile

The entrypoint usually automatically detects when you changed the source code of your CLI. But for rare cases (like path dependencies) it is not possible to detect changes. In those scenarios use flg recompile to force recompile your CLI.

Writing custom tasks (Commands)

Writing your own commands is done in two steps.

  1. Create a class for your new command, give it a name.

    import 'package:sidekick_core/sidekick_core.dart';
    
    class YourCommand extends Command {
    @override
    String get description => 'does foo';
    
    @override
    String get name => 'foo';
    
    @override
    Future<void> run() async {
      // your custom code here
    }
    }
  2. Register your new command in the packages/flg_sidekick/lib/flg_sidekick.dart file by adding it to the runner

    // Generated by `sidekick init`
    Future<void> runFlg(List<String> args) async {
    final runner = initializeSidekick(mainProjectPath: '.');
    
    runner
      ..addCommand(FlutterCommand())
      //.. more commands
      ..addCommand(InstallGlobalCommand())
      ..addCommand(YourCommand()); // <-- Register your own command
    
    //...

Handling arguments

The sidekick CLI is based on package:args. Use the existing argParser of Command to define and parse the arguments of your command.

class EchoTextCommand extends Command {
  @override
  String get name => 'echo-text';

  @override
  String get description => 'Echos the text';

  EchoTextCommand() {
    argParser.addOption('text');
  }

  @override
  Future<void> run() async {
    final cliName = argResults!['text'] as String?;
    print('echo $cliName');
  }
}
$ flg echo-text --text="Hello World"
Hello World

Motivation

Once you start automating your development workflow, you rather soon hit the limits of Bash. Not only is it hard to learn for newcomers, but also hard to understand for experienced developers. The lack of dependency management and JSON parsing are only a few reasons that rule it out as a usable scripting language.

Build systems like Gradle allow you to write your tasks. But Dart and Flutter projects are not compatible with Gradle and don't offer an easy way to add custom tasks.

While you can place your dart scripts in /tool and add dev_dependencies to your pubspec.yaml you might rather soon run into version conflicts between your app and your scripts.

Let's face it, you need a standalone dart project for your custom CLI, and sidekick does the heavy lifting for you.

Principals

The sidekick CLI is self-executable

Full control of the source code

Being able to use all the benefits of a modern language

Development

Install cli locally during development

That's useful when you want to test the sidekick cli during development on your machine. Tests are great, but sometimes you want to see the beast in action.

cd sidekick
dart pub global activate -s path .

License

Copyright 2021 phntm GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.