ordinare 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/ordinare +1 -1
- data/lib/ordinare.rb +13 -57
- data/lib/ordinare/check.rb +28 -0
- data/lib/ordinare/sort.rb +85 -0
- data/lib/ordinare/version.rb +1 -1
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14a96df1b928e752b8e7e4f58dec45684cf7eeca
|
4
|
+
data.tar.gz: 82c1d5904834601be09a5b01559268f08e2a7791
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e174e6998ad43ae8c473edf8203c59e4527c1bb547059e67579735d591e071f8f105fa7538e4ff887086ac53b78884f2328a7be7ef266cbf618211d62ea1f2f
|
7
|
+
data.tar.gz: 163973763fce1b3e09c04a92e7c64c06f177e9e224e2b773083773cb50ba26723d3a52aa98a499740d7e8d0dbdef092507c7abec7d5e6477e386dcff65bce937
|
data/bin/ordinare
CHANGED
data/lib/ordinare.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require "ordinare/version"
|
2
|
+
require "ordinare/sort"
|
3
|
+
require "ordinare/check"
|
2
4
|
require "optparse"
|
3
5
|
|
4
6
|
module Ordinare
|
@@ -13,6 +15,7 @@ module Ordinare
|
|
13
15
|
overwrite = true
|
14
16
|
version = nil
|
15
17
|
help = nil
|
18
|
+
check = nil
|
16
19
|
|
17
20
|
OptionParser.new do |opts|
|
18
21
|
opts.banner = "Usage: ordinare inside your Rails project"
|
@@ -27,6 +30,10 @@ module Ordinare
|
|
27
30
|
overwrite = false
|
28
31
|
end
|
29
32
|
|
33
|
+
opts.on("-c", "--check", "Check if Gemfile is sorted properly") do
|
34
|
+
check = true
|
35
|
+
end
|
36
|
+
|
30
37
|
opts.on("-v", "--version", "Check gem version") do
|
31
38
|
puts Ordinare::VERSION
|
32
39
|
version = true
|
@@ -38,64 +45,13 @@ module Ordinare
|
|
38
45
|
end
|
39
46
|
end.parse!
|
40
47
|
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
def sort(overwrite = true, path = "Gemfile")
|
45
|
-
unless File.file?(path)
|
46
|
-
abort("No Gemfile found in the current directory, is this a Rails project with Gemfile?")
|
47
|
-
end
|
48
|
-
|
49
|
-
content = File.readlines(path)
|
48
|
+
return if version || help
|
50
49
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
path = "#{path}.ordinare" unless overwrite
|
59
|
-
|
60
|
-
File.open(path, "w+") do |file|
|
61
|
-
content.each { |line| file.puts(line) }
|
62
|
-
end
|
63
|
-
|
64
|
-
puts "Your sorted Gemfile can be found at #{path} path"
|
65
|
-
end
|
66
|
-
|
67
|
-
def find_ranges_of_gems(content)
|
68
|
-
gems = content.each_with_index.map do |line, index|
|
69
|
-
if line.strip.start_with?("gem ")
|
70
|
-
index
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
ranges_to_sort = []
|
75
|
-
gems.each_with_index do |gem, index|
|
76
|
-
current_range = if ranges_to_sort.last && !ranges_to_sort.last[:end_index]
|
77
|
-
ranges_to_sort.last
|
78
|
-
else
|
79
|
-
{ :start_index => nil, :end_index => nil }
|
80
|
-
end
|
81
|
-
start_index = current_range[:start_index]
|
82
|
-
end_index = current_range[:end_index]
|
83
|
-
|
84
|
-
if gem && !gems[index - 1] && gems[index + 1]
|
85
|
-
current_range[:start_index] = index
|
86
|
-
current_range[:end_index] = index if index == gems.length - 1
|
87
|
-
|
88
|
-
ranges_to_sort << current_range unless ranges_to_sort.any? { |range| range[:start_index] == index }
|
89
|
-
elsif gem && gems[index - 1] && !gems[index + 1]
|
90
|
-
ranges_to_sort.map do |range|
|
91
|
-
if range[:start_index] == start_index
|
92
|
-
range[:end_index] = index
|
93
|
-
end
|
94
|
-
|
95
|
-
range
|
96
|
-
end
|
97
|
-
end
|
50
|
+
if check
|
51
|
+
Ordinare::Check.gemfile_sorted?(path)
|
52
|
+
else
|
53
|
+
puts path
|
54
|
+
Ordinare::Sort.sort_gemfile(overwrite, path)
|
98
55
|
end
|
99
|
-
ranges_to_sort
|
100
56
|
end
|
101
57
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Ordinare
|
2
|
+
class Check
|
3
|
+
def self.gemfile_sorted?(path = "Gemfile")
|
4
|
+
new(path).gemfile_sorted?
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(path)
|
8
|
+
@path = path
|
9
|
+
end
|
10
|
+
|
11
|
+
def gemfile_sorted?
|
12
|
+
unless File.file?(@path)
|
13
|
+
abort "No Gemfile found in the current directory, is this a Rails project with Gemfile?"
|
14
|
+
end
|
15
|
+
|
16
|
+
ordered_content = Ordinare::Sort.sort_content(@path, File.readlines(@path))
|
17
|
+
|
18
|
+
read_content = File.readlines(@path)
|
19
|
+
|
20
|
+
if read_content == ordered_content
|
21
|
+
puts "Gemfile is sorted properly"
|
22
|
+
true
|
23
|
+
else
|
24
|
+
abort "Gemfile is not sorted"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Ordinare
|
2
|
+
class Sort
|
3
|
+
def self.sort_gemfile(overwrite = true, path = "Gemfile")
|
4
|
+
new(overwrite, path).sort_gemfile
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.sort_content(path = "Gemfile", content)
|
8
|
+
new(false, path).sort_content(content)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(overwrite, path)
|
12
|
+
@overwrite = overwrite
|
13
|
+
@read_path = path
|
14
|
+
@write_path = overwrite ? @read_path : "#{path}.ordinare"
|
15
|
+
end
|
16
|
+
|
17
|
+
def sort_gemfile
|
18
|
+
unless File.file?(@read_path)
|
19
|
+
abort "No Gemfile found in the current directory, is this a Rails project with Gemfile?"
|
20
|
+
end
|
21
|
+
|
22
|
+
read_content = File.readlines(@read_path)
|
23
|
+
|
24
|
+
ordered_content = sort_content(read_content)
|
25
|
+
|
26
|
+
write_to_a_file(ordered_content)
|
27
|
+
end
|
28
|
+
|
29
|
+
def sort_content(content)
|
30
|
+
ranges_to_sort = find_ranges_of_gems(content)
|
31
|
+
|
32
|
+
ranges_to_sort.each do |range|
|
33
|
+
content[range[:start_index]..range[:end_index]] =
|
34
|
+
content[range[:start_index]..range[:end_index]].sort
|
35
|
+
end
|
36
|
+
|
37
|
+
content
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def find_ranges_of_gems(content)
|
43
|
+
gems = content.each_with_index.map do |line, index|
|
44
|
+
if line.strip.start_with?("gem ")
|
45
|
+
index
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
ranges_to_sort = []
|
50
|
+
gems.each_with_index do |gem, index|
|
51
|
+
current_range = if ranges_to_sort.last && !ranges_to_sort.last[:end_index]
|
52
|
+
ranges_to_sort.last
|
53
|
+
else
|
54
|
+
{ :start_index => nil, :end_index => nil }
|
55
|
+
end
|
56
|
+
start_index = current_range[:start_index]
|
57
|
+
end_index = current_range[:end_index]
|
58
|
+
|
59
|
+
if gem && !gems[index - 1] && gems[index + 1]
|
60
|
+
current_range[:start_index] = index
|
61
|
+
current_range[:end_index] = index if index == gems.length - 1
|
62
|
+
|
63
|
+
ranges_to_sort << current_range unless ranges_to_sort.any? { |range| range[:start_index] == index }
|
64
|
+
elsif gem && gems[index - 1] && !gems[index + 1]
|
65
|
+
ranges_to_sort.map do |range|
|
66
|
+
if range[:start_index] == start_index
|
67
|
+
range[:end_index] = index
|
68
|
+
end
|
69
|
+
|
70
|
+
range
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
ranges_to_sort
|
75
|
+
end
|
76
|
+
|
77
|
+
def write_to_a_file(content)
|
78
|
+
File.open(@write_path, "w+") do |file|
|
79
|
+
content.each { |line| file.puts(line) }
|
80
|
+
end
|
81
|
+
|
82
|
+
puts "Your sorted Gemfile can be found at #{@write_path} path"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/ordinare/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ordinare
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikola Đuza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: codecov
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,6 +53,8 @@ extra_rdoc_files: []
|
|
39
53
|
files:
|
40
54
|
- bin/ordinare
|
41
55
|
- lib/ordinare.rb
|
56
|
+
- lib/ordinare/check.rb
|
57
|
+
- lib/ordinare/sort.rb
|
42
58
|
- lib/ordinare/version.rb
|
43
59
|
homepage: https://github.com/nikolalsvk/ordinare
|
44
60
|
licenses:
|