happy_gemfile 0.0.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19258800aee6e7d78e880b8f016bbf1fe5546a61
4
- data.tar.gz: 49ac27434c683175c702a4e1b975786532bc0a47
3
+ metadata.gz: 842ccfc678031634380eae7674a29ae53bd57dc0
4
+ data.tar.gz: 029fa26f25c94fe0912900205352ccb3975f7acf
5
5
  SHA512:
6
- metadata.gz: afe8e96d86e56dc95b90aaabaa5ed9cf53d83aa19509a214d253f41622cd6a7df2b01cefbfb88a5264b310000a4cad7b070c84a62929e7352dfe10d278a582bb
7
- data.tar.gz: fe9c35ee48c2891bf485e812dc92fab5005ac28d47d1285d24c1f8b93bda30f87106d0be4a0f7719e983ba3d0133ff4c9ecdca610ac8f72856f700fb12590bd6
6
+ metadata.gz: ee5ca8fe9c199707be89c304a3daf17aeda1b4e805644c30d4e8b863e8e35ba10a371aa70eac416b9f7e8a80c4bf5cefe1c99fba885c07e454c1960364114d53
7
+ data.tar.gz: f74a25b5f1e79d77491e17f594b1850ca68862bcf2dd7664e1664cc85cfd10601e003ba97808f6dfecfa2841354cd46c1a2a6361b252efb7278f417e5df644c3
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.idea
11
+ .byebug_history
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in happy_gemfile.gemspec
4
3
  gemspec
4
+
5
+
data/README.md CHANGED
@@ -27,7 +27,7 @@ $ happy_gemfile
27
27
 
28
28
  Alternatively, you can call the alphabetize method directly in your app, say, in your config.rb.
29
29
  ```ruby
30
- HappyGems.alphabetize
30
+ HappyGemfile.alphabetize
31
31
  ```
32
32
 
33
33
 
data/exe/happy_gemfile CHANGED
@@ -2,4 +2,17 @@
2
2
 
3
3
  require 'happy_gemfile'
4
4
 
5
- HappyGemfile.alphabetize
5
+ if ARGV.empty?
6
+ puts "\nHey! I need some arguments here!\n\n"\
7
+ "You can run 'happy_gemfile all' for everything, or, if you don't trust me, you can pass any of these in:\n\n"\
8
+ "alphabetize - Organizes gems alphabetically\n"\
9
+ "wipe_comments - Removes all, non-inline comments\n"\
10
+ "organize_groups - Places everything tidily their specified group."
11
+ exit
12
+ end
13
+
14
+ gemfile = HappyGemfile.gemfile
15
+
16
+ %w(wipe_comments organize_groups alphabetize).each{|arg|gemfile = HappyGemfile.send(arg, gemfile) unless [arg, 'all'] - ARGV == [arg, 'all']}
17
+
18
+ HappyGemfile.replace_gemfile gemfile
@@ -1,3 +1,3 @@
1
1
  module HappyGemfile
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/happy_gemfile.rb CHANGED
@@ -1,21 +1,16 @@
1
1
  require "happy_gemfile/version"
2
2
 
3
3
  module HappyGemfile
4
+ def self.alphabetize lines=nil
4
5
 
5
- def self.alphabetize
6
- unless File.exists? "Gemfile"
7
- puts "There doesn't appear to be a Gemfile... not sure what to do."
8
- return false
9
- end
10
-
11
- lines = File.readlines "Gemfile"
6
+ lines ||= gemfile
12
7
  gem_groups = [[]]
13
8
  gem_indexes = [[]]
14
9
  group_count = 0
15
10
  in_group = false
16
11
 
17
12
  lines.each_with_index do |line, index|
18
- if line.include? 'gem ' and !(line.include? "source '")
13
+ if is?(line, 'gem')
19
14
  unless in_group
20
15
  gem_groups[0] << line
21
16
  gem_indexes[0] << index
@@ -23,12 +18,12 @@ module HappyGemfile
23
18
  gem_groups[group_count] << line
24
19
  gem_indexes[group_count] << index
25
20
  end
26
- elsif line.include? 'group'
21
+ elsif is?(line, 'group')
27
22
  in_group = true
28
23
  group_count += 1
29
24
  gem_groups << []
30
25
  gem_indexes << []
31
- elsif line.include? 'end'
26
+ elsif is? line, 'end'
32
27
  in_group = false
33
28
  end
34
29
  end
@@ -39,6 +34,84 @@ module HappyGemfile
39
34
  end
40
35
  end
41
36
 
37
+ lines
38
+ end
39
+
40
+ def self.wipe_comments lines=nil
41
+ lines ||= gemfile
42
+ lines.delete_if{|line| is_comment?(line)}
43
+ lines
44
+ end
45
+
46
+ def self.organize_groups lines=nil
47
+ lines ||= gemfile
48
+ groups = {general: []}
49
+ current_group = :general
50
+ lines.each do |line|
51
+ if is? line, 'gem'
52
+ groups[current_group] << line.gsub("\n", '')
53
+ elsif is? line, 'group'
54
+ current_group = group_name line
55
+ groups[current_group] ||= []
56
+ elsif is? line, 'end'
57
+ current_group = :general
58
+ else
59
+ groups[:not_gems] ||= []
60
+ groups[:not_gems] << line.gsub("\n", '')
61
+ end
62
+ end
63
+
64
+ groups.each {|key, lines| lines.delete_if {|line| ["\n", ''].include? line} }
65
+
66
+ organized = []
67
+
68
+ groups[:not_gems].each {|line| organized << line << "\n"}
69
+
70
+ organized << "\n"
71
+
72
+ groups[:general].each {|line| organized << line << "\n"}
73
+
74
+ organized << "\n"
75
+
76
+ (groups.keys - [:general, :not_gems]).each do |group|
77
+ organized << "group #{group_line(group)} do" << "\n"
78
+ groups[group].each {|line| organized << "#{line}" << "\n"}
79
+ organized << 'end' << "\n\n"
80
+ end
81
+ organized
82
+ end
83
+
84
+ # HELPERS
85
+
86
+ def self.is_comment? line
87
+ is? line, '#'
88
+ end
89
+
90
+ def self.is? line, type
91
+ line.split(' ').first == type
92
+ end
93
+
94
+ def self.group_name line
95
+ line.match(/group(.*)do/)
96
+ .to_a[1]
97
+ .strip.gsub(', ', '_')
98
+ .gsub(':', '')
99
+ .to_sym
100
+ end
101
+
102
+ def self.group_line group
103
+ group.to_s.split('_').map{|g| ":#{g}"}.join(', ')
104
+ end
105
+
106
+ def self.gemfile
107
+ unless File.exists? "Gemfile"
108
+ puts "There doesn't appear to be a Gemfile... not sure what to do."
109
+ return false
110
+ end
111
+ File.readlines "Gemfile"
112
+ end
113
+
114
+ def self.replace_gemfile lines
42
115
  File.open("Gemfile", 'w') { |file| file.write(lines.join('')) }
43
116
  end
44
117
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: happy_gemfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MainShayne233
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-08 00:00:00.000000000 Z
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler