Giter Club home page Giter Club logo

elvis's Introduction

elvis Build Status

Elvis Presley dancing

Command-line interface for Elvis, the Erlang style reviewer.

Contact Us

If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request :)).

Installation

  1. Clone the repo
  2. rebar3 compile

Usage

In any elvis-enabled product, elvis rock will trigger a rule check.

Script

elvis can be turned into a script by executing rebar3 escriptize. This will generate an elvis self-contained executable script, from which you can get help by typing elvis help. A list of available commands can be shown using the --commands option (i.e. elvis --commands).

To run elvis from the terminal use the rock command (i.e. elvis rock). There's no need to specify a configuration file path if you have an elvis.config file in the same location where you are executing the script, otherwise a configuration file can be specified through the use of the --config (or just -c) option.

elvis rock --config config/elvis.config

In 0.3.0 a new option was introduced in order to run elvis checks only on the source files that have changed since a particular branch of commit. Example usage would be elvis git-branch origin/HEAD.

Benefits

  • Enables consistency in style across all your code base.
  • Encourages the development team to sit down and talk about code conventions.
  • Allows continuous monitoring of code quality.
  • Helps developers avoid repeated mistakes that can be automatically detected.
  • Provides homogenisation among the different projects in a company, therefore facilitating project switching for developers and as well allowing easier code sharing between projects

Webhook

There's also a way to use elvis as a GitHub webhook for pull request (PR) events by calling the elvis_webhook:event/1 function. This will add a comment in each file and rule that is broken, analyzing only the files associated with the PR.

Running the webhook on your servers

Since GitHub's API needs a valid user and password to allow the creation of reviews on PRs, the parameters github_user and github_password need to be added to elvis's configuration and also the credentials used must be from an admin of the repo or someone with permissions for requesting changes on PRs.

The elvis_webhook:event/1 function takes a map containing the keys headers and body, whose values should be the map of headers and the body from the GitHub's event request.

Headers = #{<<"X-GitHub-Event">>, <<"pull_request">>},
Body = <<"{}">>, %% JSON data form GitHub's event.
Request = #{headers => Headers, body => Body},
elvis:webhook(Request).

Git hook

elvis can also be used as a git pre-commit hook using the git-hook command, just use something like the following as your pre-commit script:

#!/bin/sh
#
# Runs elvis rules to staged files where applicable.

elvis git-hook

As the comment states, elvis will search for files that match the filter of each rule group (see configuration) among the staged files, get their staged content and run the rules specified in the configuration. If any rule fails then elvis exits with a non-zero code, which signals git that the commit shouldn't be made.

Make sure your pre-commit hook script is executable (i.e. by running chmod +x pre-commit), otherwise git won't be able to run it.

Erlang Shell

If you only need to use elvis in the Erlang shell you might want to consider only including the elvis_core library as a dependency.

Configuration

To provide a default configuration for elvis you should either create an elvis.config file located in the root directory or set the following environment values in your configuration file:

[
 {
   elvis,
   [
    {config, [...]},
    {output_format, plain},

    %% Only necessary for the 'webhook' functionality
    {github_user, "user"},
    {github_password, "password"}
   ]
 }
].

The config and output_format are explained in elvis_core.

The GitHub configuration parameters github_user and github_password are required only when elvis is used as a webhook.

elvis.config

In your elvis.config file you can setup which rules should be applied, on what files and in which directories to do it.

The configuration is in Erlang format, it is not that hard to write but it is easier if you use the elvis.config file in this reposiotry as a template.

In the elvis.config file you create an elvis config where for a set of directories, you want to run a ruleset (or specific rules) on a set of files.

For example, configure to check all erlang files under the src directory using the ruleset erl_files:

[
 {
   elvis,
   [
    {config,
     [#{dirs => ["src"],
        filter => "*.erl",
        ruleset => erl_files
       }
     ]
    }
   ]
 }
].

You can use four different rulesets erl_files, makefiles, rebar_config or elvis_config.

Implemented Rules

A reference of all rules implemented in Elvis can be found in this elvis_core's RULES.md.

User Defined Rules

If you have implemented an Elvis rule that's in your local repo or in one of your dependencies, you can add this rule to your elvis.config file and tell Elvis where to find the .beam that contains the compiled rule using the --code-path (-p) option.

For example if the rule is in one of your deps, you could run Elvis in the following way:

elvis rock -p deps/elvis_rules/ebin -c elvis.config

Dependencies

  • Erlang/OTP 24+
  • git

References

Inspired on HoundCI

elvis's People

Contributors

amilkr avatar bullno1 avatar define-null avatar elbrujohalcon avatar elfantasma avatar euen avatar f3c0 avatar fenek avatar ferigis avatar guilleiguaran avatar harenson avatar hernanrivasacosta avatar igaray avatar jfacorro avatar johanvikman avatar licenser avatar mzaini30 avatar nomorecoffee avatar onno-vos-dev avatar onnovos avatar paulo-ferraz-oliveira avatar sargun avatar srenatus avatar sztheory avatar technion avatar tjarvstrand avatar tyler-eon avatar waffle-iron avatar waisbrot avatar walrusvision avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elvis's Issues

master in deps

Rule

in your rebar.config or Erlang.mk, specify a tag or commit, but not master

Elvis crashes in `erl_parse:abstract/2`

Again with respect to https://github.com/lavrin/dcsp/ after I changed include directives to sidestep #75 Elvis crashes:

> elvis:rock(#{src_dirs => ["src", "include", "test"], rules => []}).
** exception error: no function clause matching erl_parse:abstract(undefined,{5,19}) (erl_parse.yrl, line 935)
     in function  erl_parse:record_fields/1 (erl_parse.yrl, line 792)
     in call from erl_parse:build_typed_attribute/2 (erl_parse.yrl, line 623)
     in call from erl_parse:yeccpars2_483/7 (erl_parse.yrl, line 80)
     in call from erl_parse:yeccpars0/5 (/net/isildur/ldisk/daily_build/17_prebuild_master-opu_o.2014-04-07_20/otp_src_17/bootstrap/lib/parsetools/include/yeccpre.hrl, line 56)
     in call from lists:map/2 (lists.erl, line 1237)
     in call from lists:map/2 (lists.erl, line 1237)
     in call from elvis_code:parse_tree/1 (src/elvis_code.erl, line 68)

Please see this gist for a dbg trace - unfortunately, the trace doesn't tell me much.

Elvis crashes when running on processone/ejabberd

Error message:

escript: exception error: no match of right hand side value
                 {error,{{49,36},
                         aleppo_parser,
                         ["syntax error before: ","..."]}}
  in function  elvis_code:parse_tree/2 (src/elvis_code.erl, line 67)
  in call from elvis_utils:parse_tree/2 (src/elvis_utils.erl, line 57)
  in call from elvis_utils:load_file_data/2 (src/elvis_utils.erl, line 69)
  in call from lists:map/2 (lists.erl, line 1237)
  in call from lists:map/2 (lists.erl, line 1237)
  in call from elvis:rock/1 (src/elvis.erl, line 53)
  in call from elvis:process_commands/2 (src/elvis.erl, line 152)
  in call from elvis:process_options/2 (src/elvis.erl, line 131)

Nesting Level

Rule

Nest your code no more than N levels.

Options
  • Max nesting level

DRY

Rule

don't repeat yourself

Implementation

Find exactly the same code repeated in the same module

Executable Path Length

Rule

Write functions with executable paths no longer than N.

Options
  • N: max length of executable paths.
  • Ignored modules

http://elvis.inakalabs.com doesn't advertise character encoding

My name's Radosław, but the site displays it as RadosÅ‚aw. There's no mention of "encoding" or "charset" in the page source, so the browser-default encoding is used, and that's ISO-8859-2 in my case (I'm from Poland), but the site is in fact in UTF-8. I guess <meta charset='utf-8'> in the head section would suffice.

Elvis can't find header files

With the following repo layout:

.
├── Makefile
├── README.md
├── include
│   └── dcsp.hrl
├── src
│   ├── dcsp.app.src
│   ├── dcsp.erl
│   ├── dcsp_agent.erl
│   ├── dcsp_problem.erl
│   ├── dcsp_problem_logic.erl
│   ├── dcsp_solver.erl
│   ├── dcsp_sup.erl
│   ├── ex1.erl
│   └── nqueens.erl
└── test
    └── dcsp_SUITE.erl

and dcsp.erl (and others) including dcsp.hrl like this (note no leading include/):

-include("dcsp.hrl").

Elvis (commit 647fd5a) can't find dcsp.hrl:

> elvis:rock(#{src_dirs => ["src", "include", "test"], rules => []}).
** exception error: no match of right hand side value {error,{not_found,"dcsp.hrl"}}
     in function  elvis_code:parse_tree/1 (src/elvis_code.erl, line 65)
     in call from elvis_utils:parse_tree/1 (src/elvis_utils.erl, line 56)
     in call from elvis_utils:load_file_data/1 (src/elvis_utils.erl, line 68)
     in call from lists:map/2 (lists.erl, line 1237)
     in call from elvis:rock/1 (src/elvis.erl, line 52)

The repo in question is https://github.com/lavrin/dcsp/.

#state records

Rule

name your state records #state and use -type state():: #state{} in all your OTP modules.

Improve feedback when using Elvis on the command line

For big projects (e.g. erlang/otp, ejabberd), the initial processing of files takes too long and the user is left waiting without any feedback. It would be nice to display information related to what file is being processed before showing the actual output.

Macros in module/function names

Rule

Don't use macros for module or function names

Implementation

check for ?MACROS that translate to an atom and are used in expressions like ?MACRO:something or something:?MACRO

-callback

Rule

use -callback instead of behavior_info/1 for your behavior definitions

Options
  • None

Naming convention

Rule

stick to one convention when naming modules (i.e: prefix_module1 vs module2 vs anotherprefix_module3)

Options
  • Regex with which to validate module names
  • Excluded modules

if

Rule

Don't use if

Missing link in README

There's a link missing in the README.md.

There's also a way to use elvis as a GitHub [webhook][webhooks] for pull request (PR) events by calling the webhook/1 function. This will add a comment for each rule that is broken by a line in the files associated wiith the PR.

Dynamic Functions

Rule

Don't use dynamic function calling outside of behaviour modules with callbacks

Options
  • Ignore modules

CLI tool

Create an escript that basically runs elvis:rock(…). Use the tool for CLI getopt.

God Modules

Rule

Don't design your system using god modules

Options
  • Max number of exported functions
  • Max number of expressions in the generated code
  • Max number of lines of code
Implementation

The number of exported functions and expressions in generated code can be obtained using beam_lib-related functions
The number of lines of code, maybe too

Macro Names

Rule

Macros should be named in ALL_UPPER_CASE

Options

No options

Indentation Format

Rule

Indent using {N spaces|tabs}

Options
  • Indentation character
  • (if using spaces) number of spaces
Implementation

Check in each line, how much more indented it is against the previous one.

Erlang console tool

Make the system run with

elvis:rock(…)

or something similar (i.e. let it be ready to run from an Erlang console), following these rules:

  • The code to be analysed should be src/*.erl or given as a param
  • The list of rules to validate should be configurable by app.config and only one or two very simple rules (e.g. 80 chars a line) should be implemented
  • The results should be in the simplest possible form
  • Add Tests
  • Write Documentation

Rules wiki page

Create a wiki page listing the rules with their options and samples.

Exported vs. Non-exported

Rule

Try to always separate NON-EXPORTED and EXPORTED functions in groups, with the exported ones first, unless it helps readability and code discovery.

Checkstyle compatible output

How about making the output compatible with the CheckStyle file format? This will allow to run the tool from jenkins and publish the results of the build without any translations

Code width

Rule

Stick to N chars per line

Options
  • Max line length

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.