sloc 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b46653f18fd1c3a549b1a044ab715b906dbbd7c3
4
- data.tar.gz: 2a7de9e2d0b267a0236adb70af09c0ea65cae417
3
+ metadata.gz: e259712548f8143e04fdc705826079d117c2bca8
4
+ data.tar.gz: 04a88b96d2cee31d9e4e3973efbd97c759f35f40
5
5
  SHA512:
6
- metadata.gz: 9ebe9dbcd0bce080ce86ac42be5a9f51ac33aab53fa0ead3ad510f5ebd6668c05a6069ef471f1f45d2ba9138447ccd1fce6bb0b4ecdc6b94e56126e68d68da81
7
- data.tar.gz: faea9c4ae2e41215832d0ecacb22d0493f208e9b8b2a53b513b3dc3b30d1b2452999aa99fb61b1e838fe73663874e2abd4e8debf792f41d7e4bfc57d9e809bec
6
+ metadata.gz: 1ca49dea0ba4c645f0760cb1f2955825bb5fc1e03228346bc9a5fe8b8d7a11f5bc7ec5d29f64f2d62e9d5629bfa62c65491da5c967a6a67782f5e40f4192926c
7
+ data.tar.gz: 98f377612932aab8f82fd8b0bbefa781bf42efbe990ac20bfdf4a30c5b324928f63b9c433e0457832a42827c8013611ecb4b9dda6d12365be0c1cd420f355459
@@ -0,0 +1,12 @@
1
+ # Configuration parameters: AllowURI, URISchemes.
2
+ Metrics/LineLength:
3
+ Max: 104
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Style/RegexpLiteral:
9
+ MaxSlashes: 0
10
+
11
+ Style/TrailingComma:
12
+ EnforcedStyleForMultiline: comma
data/README.md CHANGED
@@ -1,4 +1,9 @@
1
1
  # Sloc
2
+ [![Gem Version](https://badge.fury.io/rb/sloc.svg)](http://badge.fury.io/rb/sloc)
3
+ [![Circle CI](https://img.shields.io/circleci/project/meganemura/sloc/master.svg)](https://circleci.com/gh/meganemura/sloc)
4
+ [![Coverage Status](https://coveralls.io/repos/meganemura/sloc/badge.svg)](https://coveralls.io/r/meganemura/sloc)
5
+ [![Code Climate](https://codeclimate.com/github/meganemura/sloc/badges/gpa.svg)](https://codeclimate.com/github/meganemura/sloc)
6
+ [![Dependency Status](https://gemnasium.com/meganemura/sloc.svg)](https://gemnasium.com/meganemura/sloc)
2
7
 
3
8
  ## Usage
4
9
 
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rubocop/rake_task'
7
+ RuboCop::RakeTask.new(:style)
8
+
9
+ task default: :spec
10
+ task ci: [:spec, :style]
@@ -1,14 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "sloc"
3
+ require 'bundler/setup'
4
+ require 'sloc'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
9
+ require 'pry'
10
+ Pry.start
data/bin/setup CHANGED
File without changes
@@ -0,0 +1,9 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.1.4
4
+ dependencies:
5
+ pre:
6
+ - gem install bundler
7
+ test:
8
+ override:
9
+ - bundle exec rake ci
@@ -1,5 +1,3 @@
1
- require "sloc/version"
2
-
3
- module Sloc
4
- # Your code goes here...
5
- end
1
+ require 'sloc/analyzer'
2
+ require 'sloc/runner'
3
+ require 'sloc/version'
@@ -1,19 +1,47 @@
1
1
  module Sloc
2
2
  class Analyzer
3
- def initialize(options)
3
+ def initialize(options = {})
4
4
  @options = options
5
5
  end
6
6
 
7
+ REPORT_KEYS = [:total, :empty_lines, :single_comment]
8
+
7
9
  def analyze(code, extension)
8
10
  result = {}
9
11
 
10
- code.gsub!(/\r\n|\r/, "\n")
12
+ code.scrub!.gsub!(/\r\n|\r/, "\n")
13
+
14
+ result[:total] = code.scan("\n").size
15
+ result[:empty_lines] = code.scan(/^\s*$/).size
11
16
 
12
- result[:total] = code.scan("\n").size
13
- result[:empty_lines] = code.scan(/^\s*$/).size
14
- result[:comments] = 'N/A' # FIXME
17
+ regexp = single_comment_expression(extension)
18
+ result[:single_comment] = regexp ? code.scan(regexp).size : 0
15
19
 
16
20
  result
17
21
  end
22
+
23
+ def comment_expressions(extension)
24
+ start, stop = block_comment_expression(extension)
25
+
26
+ {
27
+ single: single_comment_expression(extension),
28
+ start: start,
29
+ stop: stop,
30
+ }
31
+ end
32
+
33
+ def single_comment_expression(extension)
34
+ case extension
35
+ when '.rb' then /#/
36
+ when '.vim' then /"/
37
+ end
38
+ end
39
+
40
+ def block_comment_expression(extension)
41
+ case extension
42
+ when '.rb' then [/=begin/, /=end/]
43
+ else [nil, nil]
44
+ end
45
+ end
18
46
  end
19
47
  end
@@ -10,9 +10,7 @@ module Sloc
10
10
  def run(args = ARGV)
11
11
  @options, paths = parse_options(args)
12
12
 
13
- if paths.empty?
14
- return help
15
- end
13
+ return help if paths.empty? || @options[:help]
16
14
 
17
15
  runner = Runner.new(@options)
18
16
 
@@ -20,8 +18,11 @@ module Sloc
20
18
  end
21
19
 
22
20
  def parse_options(args)
23
- opts = Slop.parse! do |o|
21
+ opts = Slop.parse!(args) do |o|
24
22
  o.on('-h', '--help', 'Display this help message.')
23
+ o.on('-o', '--order=', 'Specify key to order by.')
24
+ o.on('--desc', 'Reverse order if specified')
25
+ o.on('-l', '--limit=', 'Specify key to set limitation of displaying file number.')
25
26
  end
26
27
 
27
28
  [opts, args]
@@ -2,41 +2,74 @@ require 'sloc/analyzer'
2
2
 
3
3
  module Sloc
4
4
  class Runner
5
- def initialize(options)
5
+ def initialize(options = {})
6
6
  @options = options
7
+ @analyzer = Analyzer.new(@options)
7
8
  end
8
9
 
9
10
  def run(paths)
11
+ # TODO: formatted output
12
+ require 'pp'
13
+ pp report(paths)
14
+
15
+ nil
16
+ end
17
+
18
+ def report(paths)
10
19
  target_files = find_target_files(paths)
11
20
 
12
21
  # TODO: count sloc
13
- analyzer = Analyzer.new(@options)
14
22
  report = target_files.each_with_object({}) do |path, h|
15
23
  code = File.read(path)
16
24
  extension = File.extname(path)
17
25
 
18
- h[path] = analyzer.analyze(code, extension)
26
+ h[path] = @analyzer.analyze(code, extension)
19
27
  end
20
28
 
21
- # TODO: formatted output
22
- require 'pp'
23
- pp report
24
-
25
- nil
29
+ process_options(report)
26
30
  end
27
31
 
28
32
  private
29
33
 
34
+ def process_options(report)
35
+ r = report
36
+ .sort_by { |_key, value| value[order] } # --order=
37
+ .tap { |hash| hash.reverse! if desc? } # --desc
38
+
39
+ if limit?
40
+ Hash[r.first(limitation)]
41
+ else
42
+ Hash[r]
43
+ end
44
+ end
45
+
46
+ def order
47
+ key = @options[:order] ? @options[:order].to_sym : nil
48
+ Analyzer::REPORT_KEYS.include?(key) ? key : Analyzer::REPORT_KEYS.first
49
+ end
50
+
51
+ def desc?
52
+ @options[:desc]
53
+ end
54
+
55
+ def limit?
56
+ !limitation.nil?
57
+ end
58
+
59
+ def limitation
60
+ @options[:limit] ? [@options[:limit].to_i, 0].max : nil
61
+ end
62
+
30
63
  def find_target_files(paths)
31
64
  files = paths.uniq.flat_map do |path|
32
65
  if File.directory?(path)
33
- Dir.glob("#{path}/**/*").select {|f| File.file?(f) }
66
+ Dir.glob("#{path}/**/*").select { |f| File.file?(f) }
34
67
  else
35
- path
68
+ File.exist?(path) ? path : nil
36
69
  end
37
70
  end
38
71
 
39
- files.map {|f| File.expand_path(f) }.uniq
72
+ files.compact.map { |f| File.expand_path(f) }.uniq
40
73
  end
41
74
  end
42
75
  end
@@ -1,3 +1,3 @@
1
1
  module Sloc
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
@@ -4,24 +4,27 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'sloc/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "sloc"
7
+ spec.name = 'sloc'
8
8
  spec.version = Sloc::VERSION
9
- spec.authors = ["meganemura"]
10
- spec.email = ["meganemura@users.noreply.github.com"]
9
+ spec.authors = ['meganemura']
10
+ spec.email = ['meganemura@users.noreply.github.com']
11
11
 
12
- spec.summary = "A tool for counting source lines of code"
12
+ spec.summary = 'A tool for counting source lines of code'
13
13
  spec.description = spec.summary
14
- spec.homepage = "https://github.com/meganemura/sloc"
15
- spec.license = "MIT"
14
+ spec.homepage = 'https://github.com/meganemura/sloc'
15
+ spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
18
+ spec.bindir = 'exe'
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
21
21
 
22
- spec.add_dependency "slop", "~> 3.0"
22
+ spec.add_dependency 'slop', '~> 3.0'
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.8"
25
- spec.add_development_dependency "pry"
26
- spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'bundler', '>= 1.7'
25
+ spec.add_development_dependency 'coveralls'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'rubocop'
27
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sloc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - meganemura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop
@@ -28,16 +28,30 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.8'
33
+ version: '1.7'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '1.8'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: pry
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,34 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
69
111
  description: A tool for counting source lines of code
70
112
  email:
71
113
  - meganemura@users.noreply.github.com
@@ -76,13 +118,14 @@ extra_rdoc_files: []
76
118
  files:
77
119
  - ".gitignore"
78
120
  - ".rspec"
79
- - ".travis.yml"
121
+ - ".rubocop.yml"
80
122
  - Gemfile
81
123
  - LICENSE.txt
82
124
  - README.md
83
125
  - Rakefile
84
126
  - bin/console
85
127
  - bin/setup
128
+ - circle.yml
86
129
  - exe/sloc
87
130
  - lib/sloc.rb
88
131
  - lib/sloc/analyzer.rb
@@ -1,3 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.1.4