Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Jul 3, 2022
0 parents commit 8e26503
Show file tree
Hide file tree
Showing 40 changed files with 1,512 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint

on:
push:
branches:
- main

pull_request:

jobs:
lint:
runs-on: ubuntu-latest
name: RuboCop Linter
env:
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- name: Run RuboCop
run: bundle exec rubocop
34 changes: 34 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build

on:
push:
branches:
- main

pull_request:

jobs:
main:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
env:
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
strategy:
matrix:
ruby:
- "3.1"
- "3.0"
- "2.7"
- "2.6"
- "jruby"
- "truffleruby"
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run RSpec
run: bundle exec rspec
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status

Gemfile.lock
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
36 changes: 36 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require:
- rubocop-performance
- rubocop-rspec
- rubocop-rake

AllCops:
TargetRubyVersion: 2.6
SuggestExtensions: false
NewCops: enable
Exclude:
- 'spec/fixtures/**/*'
- 'vendor/**/*'
Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'

Naming/FileName:
Exclude:
- 'lib/rubocop-gradual.rb'

RSpec/ExpectOutput:
Enabled: false

RSpec/MultipleExpectations:
Enabled: false
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

## [Unreleased]

## [0.1.0] - 2022-07-03

### Added

- Initial implementation. ([@skryukov])

[@skryukov]: https://github.com/skryukov

[Unreleased]: https://github.com/skryukov/rubocop-gradual/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/skryukov/rubocop-gradual/commits/v0.1.0

[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in rubocop-gradual.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "rubocop-performance"
gem "rubocop-rake"
gem "rubocop-rspec"
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Svyatoslav Kryukov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# RuboCop Gradual

[![Gem Version](https://badge.fury.io/rb/rubocop-gradual.svg)](https://rubygems.org/gems/rubocop-gradual)
[![Build](https://github.com/skryukov/rubocop-gradual/workflows/Build/badge.svg)](https://github.com/skryukov/rubocop-gradual/actions)

RuboCop Gradual is a tool that helps track down and fix RuboCop offenses in your code gradually. It's a more flexible alternative to RuboCop's `--auto-gen-config` option.

<a href="https://evilmartians.com/?utm_source=rubocop-gradual&utm_campaign=project_page">
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
</a>

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add rubocop-gradual

Run RuboCop Gradual to create a lock file (defaults to `.rubocop_gradual.lock`):

$ rubocop-gradual

Commit the lock file to the project repository to keep track of all non-fixed offenses.

Run `rubocop-gradual` before commiting changes to update the lock file. RuboCop Gradual will keep updating the lock file to keep track of all non-fixed offenses, but it will throw an error if there are any new offenses.

## Usage

Proposed workflow:

- Run `rubocop-gradual` to generate a lock file and commit it to the project repository.

- Add `rubocop-gradual --CI` to your CI pipeline instead of `rubocop`/`standard`. It will throw an error if the lock file is out of date.

- Optionally, add `rubocop-gradual` as a pre-commit hook to your repository (using [lefthook], for example).

- RuboCop Gradual will throw an error on any new offense, but if you really want to force update the lock file, run `rubocop-gradual --update`.

## Available options

```
--ci Run Gradual in the CI mode.
-u, --update Force update Gradual lock file.
--gradual-file FILE Specify Gradual lock file.
--no-gradual Disable Gradual.
-v, --version Display version.
-V, --verbose-version Display verbose version.
-h, --help Display help message.
```

## Alternatives

- [RuboCop TODO file]. Comes out of the box with RuboCop. Provides a way to ignore offenses on the file level, which is problematic since it is possible to introduce new offenses without any signal from linter.
- [Pronto]. Checks for offenses only on changed files. Does not provide a way to temporarily ignore offenses.
- [Betterer]. Universal test runner that helps make incremental improvements witten in JavaScript. RuboCop Gradual is highly inspired by Betterer.

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/skryukov/rubocop-gradual

## License

The gem is available as open source under the terms of the [MIT License].

[lefthook]: https://github.com/evilmartians/lefthook
[RuboCop TODO file]: https://docs.rubocop.org/rubocop/configuration.html#automatically-generated-configuration
[Pronto]: https://github.com/prontolabs/pronto-rubocop
[Betterer]: https://github.com/phenomnomnominal/betterer
[MIT License]: https://opensource.org/licenses/MIT
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[rubocop spec]
8 changes: 8 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "rubocop/gradual"

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
16 changes: 16 additions & 0 deletions exe/rubocop-gradual
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift("#{__dir__}/../lib")

require "rubocop-gradual"
require "benchmark"

exit_status = 0
cli = RuboCop::Gradual::CLI.new

time = Benchmark.realtime { exit_status = cli.run }

puts "Finished in #{time} seconds" if cli.options[:debug] || cli.options[:display_time]

exit exit_status
3 changes: 3 additions & 0 deletions lib/rubocop-gradual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative "rubocop/gradual"
26 changes: 26 additions & 0 deletions lib/rubocop/gradual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "rubocop"

require_relative "gradual/version"
require_relative "gradual/cli"

module RuboCop
# RuboCop Gradual project namespace
module Gradual
class Error < StandardError; end

class << self
attr_accessor :debug, :exit_code, :mode, :path

def set_defaults!
self.debug = false
self.exit_code = 0
self.mode = :base
self.path = ".rubocop_gradual.lock"
end
end

set_defaults!
end
end
33 changes: 33 additions & 0 deletions lib/rubocop/gradual/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative "options"
require_relative "formatter"

module RuboCop
module Gradual
# CLI is a wrapper around RuboCop::CLI.
class CLI < RuboCop::CLI
def run(args = ARGV)
Gradual.mode = :base
rubocop_args = Options.new.parse(args)
super(rubocop_args)
end

private

def apply_default_formatter
return super if Gradual.mode == :disabled
raise OptionArgumentError, "-f, --format cannot be used in gradual mode." if @options[:formatters]

@options[:formatters] = [[Formatter, nil]]
end

def execute_runners
raise OptionArgumentError, "--auto-gen-config cannot be used in gradual mode." if @options[:auto_gen_config]

result = super
Gradual.mode == :disabled ? result : Gradual.exit_code
end
end
end
end
Loading

0 comments on commit 8e26503

Please sign in to comment.