-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from nikolalsvk/add-check-option
Add --check option
- Loading branch information
Showing
11 changed files
with
384 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.gem | ||
*.ordinare | ||
/coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,5 @@ require "ordinare" | |
if ARGV.size > 0 | ||
Ordinare.parse_args | ||
else | ||
Ordinare.sort | ||
Ordinare::Sort.sort_gemfile | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Ordinare | ||
class Check | ||
def self.gemfile_sorted?(path = "Gemfile") | ||
new(path).gemfile_sorted? | ||
end | ||
|
||
def initialize(path) | ||
@path = path | ||
end | ||
|
||
def gemfile_sorted? | ||
unless File.file?(@path) | ||
abort "No Gemfile found in the current directory, is this a Rails project with Gemfile?" | ||
end | ||
|
||
ordered_content = Ordinare::Sort.sort_content(@path, File.readlines(@path)) | ||
|
||
read_content = File.readlines(@path) | ||
|
||
if read_content == ordered_content | ||
puts "Gemfile is sorted properly" | ||
true | ||
else | ||
abort "Gemfile is not sorted" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
module Ordinare | ||
class Sort | ||
def self.sort_gemfile(overwrite = true, path = "Gemfile") | ||
new(overwrite, path).sort_gemfile | ||
end | ||
|
||
def self.sort_content(path = "Gemfile", content) | ||
new(false, path).sort_content(content) | ||
end | ||
|
||
def initialize(overwrite, path) | ||
@overwrite = overwrite | ||
@read_path = path | ||
@write_path = overwrite ? @read_path : "#{path}.ordinare" | ||
end | ||
|
||
def sort_gemfile | ||
unless File.file?(@read_path) | ||
abort "No Gemfile found in the current directory, is this a Rails project with Gemfile?" | ||
end | ||
|
||
read_content = File.readlines(@read_path) | ||
|
||
ordered_content = sort_content(read_content) | ||
|
||
write_to_a_file(ordered_content) | ||
end | ||
|
||
def sort_content(content) | ||
ranges_to_sort = find_ranges_of_gems(content) | ||
|
||
ranges_to_sort.each do |range| | ||
content[range[:start_index]..range[:end_index]] = | ||
content[range[:start_index]..range[:end_index]].sort | ||
end | ||
|
||
content | ||
end | ||
|
||
private | ||
|
||
def find_ranges_of_gems(content) | ||
gems = content.each_with_index.map do |line, index| | ||
if line.strip.start_with?("gem ") | ||
index | ||
end | ||
end | ||
|
||
ranges_to_sort = [] | ||
gems.each_with_index do |gem, index| | ||
current_range = if ranges_to_sort.last && !ranges_to_sort.last[:end_index] | ||
ranges_to_sort.last | ||
else | ||
{ :start_index => nil, :end_index => nil } | ||
end | ||
start_index = current_range[:start_index] | ||
end_index = current_range[:end_index] | ||
|
||
if gem && !gems[index - 1] && gems[index + 1] | ||
current_range[:start_index] = index | ||
current_range[:end_index] = index if index == gems.length - 1 | ||
|
||
ranges_to_sort << current_range unless ranges_to_sort.any? { |range| range[:start_index] == index } | ||
elsif gem && gems[index - 1] && !gems[index + 1] | ||
ranges_to_sort.map do |range| | ||
if range[:start_index] == start_index | ||
range[:end_index] = index | ||
end | ||
|
||
range | ||
end | ||
end | ||
end | ||
ranges_to_sort | ||
end | ||
|
||
def write_to_a_file(content) | ||
File.open(@write_path, "w+") do |file| | ||
content.each { |line| file.puts(line) } | ||
end | ||
|
||
puts "Your sorted Gemfile can be found at #{@write_path} path" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Ordinare | ||
VERSION = "0.3.0" | ||
VERSION = "0.4.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
source 'http://rubygems.org' | ||
|
||
ruby '2.3.1' | ||
|
||
gem 'rails', '4.2.7.1' | ||
|
||
gem 'coffee-rails', "~> 4.0.1" | ||
gem 'uglifier', "~> 2.7.2" | ||
|
||
gem 'compass-rails', '~> 2.0' | ||
gem 'sass-rails', '~> 4.0.0' | ||
|
||
group :test do | ||
gem "rspec-rails", "~>3.0" | ||
gem 'capybara', '~> 2.6.2' | ||
|
||
gem 'cucumber-rails', '~> 1.4.0', require: false # require: false is needed for cucumber-rails | ||
|
||
gem 'selenium-webdriver', '~> 2.45' | ||
|
||
# Launchy is needed by Capybara, e.g. save_and_open command needs Launchy to open a browser | ||
gem 'connection_pool', "~> 2.1" | ||
gem 'database_cleaner', '~> 1.1' | ||
gem 'email_spec', '~> 1.6.0' | ||
gem 'launchy', '~> 2.4', '>= 2.4.3' | ||
gem 'rack-test', "~> 0.6.2" | ||
gem 'timecop', '~> 0.6.3' | ||
|
||
# required for CircleCI automatic test balancing | ||
gem 'rspec_junit_formatter' | ||
end | ||
|
||
group :development, :test do | ||
gem 'pry-byebug' | ||
end | ||
|
||
gem 'js-routes', '~> 1.2.5' | ||
gem 'therubyracer', '~> 0.12.2', platforms: :ruby | ||
|
||
# Color utilities needed for landing page | ||
gem 'color', '~> 1.8' | ||
|
||
gem 'transit-ruby', '~> 0.8' | ||
gem 'uuidtools', '~> 2.1' | ||
|
||
# Markdown parser | ||
gem 'redcarpet', '~> 3.3', '>= 3.3.4' | ||
|
||
gem 'twilio-ruby', '~> 4.11.1' | ||
|
||
gem 'intercom', '~> 3.5.10' | ||
|
||
gem 'simple_token_authentication', '~> 1.0' | ||
|
||
gem 'daemons' | ||
|
||
gem "select2-rails" | ||
|
||
gem 'client_side_validations' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "spec_helper" | ||
|
||
describe Ordinare::Check do | ||
describe "#gemfile_sorted?" do | ||
context "no Gemfile found" do | ||
it "aborts with message" do | ||
expect { | ||
described_class.gemfile_sorted?("spec/fixtures/no_gemfile") | ||
}.to raise_error(SystemExit) | ||
end | ||
end | ||
|
||
context "Gemfile is sorted properly" do | ||
it "returns true" do | ||
gemfile_sorted = described_class.gemfile_sorted?("spec/fixtures/ordered_gemfile/Gemfile") | ||
|
||
expect(gemfile_sorted).to be_truthy | ||
end | ||
end | ||
|
||
context "Gemfile is not sorted properly" do | ||
it "raises an exception" do | ||
expect { | ||
described_class.gemfile_sorted?("spec/fixtures/complex_gemfile/Gemfile") | ||
}.to raise_error(SystemExit) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "spec_helper" | ||
require "fileutils" | ||
|
||
describe Ordinare::Sort do | ||
describe "#sort_gemfile" do | ||
context "no Gemfile found" do | ||
it "aborts with message" do | ||
expect { | ||
Ordinare::Sort.sort_gemfile(false, "spec/fixtures/no_gemfile") | ||
}.to raise_error(SystemExit) | ||
end | ||
end | ||
|
||
context "Gemfile found" do | ||
it "sorts basic Gemfile" do | ||
basic_gemfile = "spec/fixtures/basic_gemfile/Gemfile" | ||
Ordinare::Sort.sort_gemfile(false, basic_gemfile) | ||
|
||
same_files = FileUtils.identical?("#{basic_gemfile}.ordered", "#{basic_gemfile}.ordinare") | ||
expect(same_files).to be_truthy | ||
end | ||
|
||
it "sorts Gemfile with groups" do | ||
group_gemfile = "spec/fixtures/group_gemfile/Gemfile" | ||
Ordinare::Sort.sort_gemfile(false, group_gemfile) | ||
|
||
same_files = FileUtils.identical?("#{group_gemfile}.ordered", "#{group_gemfile}.ordinare") | ||
expect(same_files).to be_truthy | ||
end | ||
|
||
it "sorts complicated Gemfile" do | ||
complex_gemfile = "spec/fixtures/complex_gemfile/Gemfile" | ||
Ordinare::Sort.sort_gemfile(false, complex_gemfile) | ||
|
||
same_files = FileUtils.identical?("#{complex_gemfile}.ordered", "#{complex_gemfile}.ordinare") | ||
expect(same_files).to be_truthy | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.