Giter Site home page Giter Site logo

lucazzp / dart_pre_commit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from skycoder42/dart_pre_commit

0.0 1.0 0.0 211 KB

A small collection of pre commit hooks to format and lint dart code

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%

dart_pre_commit's Introduction

dart_pre_commit

Continous Integration Pub Version

A small collection of pre commit hooks to format and lint dart code

Features

  • Provides three built in hooks to run on staged files
    • Fix up imports (package/relative) and sort them
    • Run dart format
    • Run dart analyze
  • Only processes staged files
    • Automatically stages modified files again
    • Fails if partially staged files had to be modified
  • Can be used as binary or as library
  • Integrates well with most git hook solutions

Installation

Simply add dart_pre_commit to your pubspec.yaml (preferebly as dev_depedency) and run dart pub get (or flutter pub get).

Usage

To make use of the hooks, you have to activate them first. This package only comes with the hook-code itself, not with a way to integrate it with git as actual hook. Here are a few examples on how to do so:

Using git_hooks

The first example uses the git_hooks package to activate the hook. Take the following steps to activate the hook:

  1. Add git_hooks as dev_dependency to your project
  2. Run dart pub run git_hooks create to initialize and activate git hooks for your project
  3. Modify git_hooks.dart to look like the following:
import "dart:io";

import "package:dart_pre_commit/dart_pre_commit.dart";
import "package:git_hooks/git_hooks.dart";

void main(List<String> arguments) {
  final params = {
    Git.preCommit: _preCommit
  };
  change(arguments, params);
}

Future<bool> _preCommit() async {
  hooks = await Hooks.create();  // adjust behaviour if neccessary
  final result = await hooks();  // run activated hooks on staged files
  return result.isSuccess;  // report the result
}

Using hanzo

The second example uses the hanzo package to activate the hook. Take the following steps to activate the hook:

  1. Add hanzo as dev_dependency to your project
  2. Run dart pub run hanzo install to initialize and activate git hooks for your project
  3. Create a file named ./tool/pre_commit.dart as follows:
import "package:dart_pre_commit/dart_pre_commit.dart";

void main() {
  hooks = await Hooks.create();  // adjust behaviour if neccessary
  final result = await hooks();  // run activated hooks on staged files
  exitCode = result.isSuccess ? 0 : 1;  // report the result
}
  1. Run dart pub run hanzo -i pre_commit

Without any 3rd party tools

The package also provides a script to run the hooks. You can check it out via dart pub run dart_pre_commit --help. To use it as git hook, without any other tool, you have to create a script called pre-commit in .git/hooks as follows:

#!/bin/bash

exec dart pub run dart_pre_commit  # Add extra options as needed

Documentation

The documentation is available at https://pub.dev/documentation/dart_pre_commit/latest/. A full example can be found at https://pub.dev/packages/dart_pre_commit/example.

dart_pre_commit's People

Contributors

lucazzp avatar skycoder42 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.