codelines 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 787e27202ae5a1e7083ad43a7e776376181778d5
4
+ data.tar.gz: ef4fcc6d782e63ed203ec97d5958b94339cb8bf0
5
+ SHA512:
6
+ metadata.gz: 57b6ec1dd7f3b1b0f61f1f5eb92faa5d1962ea2998893995756dfa2ff1e9ae2d053e8f3f4395506501407d28eefcc2c797b77b5ac2b258c68d89ae6f98f399a0
7
+ data.tar.gz: bf928cc509c99bebbe83356e9787ffe3d5c8fcbbdbc653df8b9756c71c214b86e61a8db7a375506bb201908dc1cf16a026426623b137c98c97362ced8a37e337
data/bin/codelines ADDED
@@ -0,0 +1,61 @@
1
+ #! /usr/bin/env ruby
2
+ #--
3
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
4
+ # Version 2, December 2004
5
+ #
6
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
7
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
8
+ #
9
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
10
+ #++
11
+ require 'codelines'
12
+ require 'optparse'
13
+
14
+ options = {}
15
+
16
+ OptionParser.new do |o|
17
+ o.on '-l', '--list', 'Show available adapters' do
18
+ abort 'Available adapters:'.tap { |s|
19
+ CodeLines.adapters.sort { |a, b| a <=> b }.each { |p| s << "\n" + (' ' * 3) + p }
20
+ }
21
+ end
22
+
23
+ o.on '-a', '--adapter ADAPTER', 'The adapter to use' do |adapter|
24
+ options[:adapter ] = adapter.downcase
25
+ end
26
+
27
+ o.on '-p', '--profile PROFILE' do |profile|
28
+ options[:profile ] = profile
29
+ end
30
+
31
+ o.on '-r', '--repository REPOSITORY' do |repository|
32
+ options[:repository ] = repository
33
+ end
34
+
35
+ o.on '-b', '--branch BRANCH', 'Select a branch different from `master\'' do |branch|
36
+ options[:branch ] = branch
37
+ end
38
+
39
+ o.on '-c', '--no-comments', 'Ignore comments' do |branch|
40
+ options[:ignore_comments] = true
41
+ end
42
+
43
+ o.on '-s', '--authenticate USERNAME:PASSWORD', 'Perform the sign in' do |data|
44
+ data = data.split ?:
45
+ options[:username] = data.shift
46
+ options[:password] = data.pop
47
+ end
48
+ end.parse!
49
+
50
+ abort 'You must select an adapter, a profile and a repository.' if !options[:adapter] || !options[:profile] || !options[:repository]
51
+
52
+ require "codelines/adapters/#{options[:adapter]}"
53
+
54
+ adapter = CodeLines.constants.map { |p|
55
+ "CodeLines::#{p}".split('::').inject(Object) { |o, c| o.const_get c }
56
+ }.select { |c| c.to_s.split('::').last.downcase == options[:adapter] }.first
57
+
58
+ github = CodeLines::GitHub.new options[:profile]
59
+ github.authenticate options[:username], options[:password] if options[:username] && options[:password]
60
+
61
+ puts github.count repository: [ { name: options[:repository], branch: options[:branch] } ], ignore_comments: options[:ignore_comments]
@@ -0,0 +1,28 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ module CodeLines
12
+
13
+ class Adapter
14
+ def authenticate(*args, &block)
15
+ raise NotImplementedError, 'authenticate has been not implemented'
16
+ end
17
+
18
+ def count(*args, &block)
19
+ raise NotImplementedError, 'count has been not implemented'
20
+ end
21
+
22
+ protected
23
+ def comment?(what)
24
+ [ ?;, ?#, '//' ].include? what.gsub(/\s+/, '')[0]
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,64 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+ require 'github_api'
11
+ require 'base64'
12
+
13
+ module CodeLines
14
+
15
+ class GitHub < Adapter
16
+ def initialize(profile)
17
+ @profile = profile
18
+ @github = Github.new
19
+ @repositories = {}
20
+ end
21
+
22
+ def authenticate(username, password)
23
+ @github = Github.new basic_auth: "#{username}:#{password}"
24
+ end
25
+ alias_method :login, :authenticate
26
+
27
+ def count(options = {}, &block)
28
+ repos = options[:repository ] || []
29
+ ignore_comments = options[:ignore_comments] || false
30
+ reload = options[:reload] || false
31
+
32
+ lines = 0
33
+ repos.each { |name|
34
+ @repositories[name] = fetch(name) if reload || !@repositories.include?(name)
35
+
36
+ @repositories[name].each { |github|
37
+ source = Base64.decode64 github.content
38
+ source.gsub!(/\r\n?/m, "\n")
39
+ source.gsub!(/\/\*![^*]*\*+(?:[^*\/][^*]*\*+)*\//m, '') if ignore_comments
40
+
41
+ source.each_line { |line|
42
+ lines += 1 if ignore_comments == false || !comment?(line)
43
+ }
44
+
45
+ yield github if block_given?
46
+ }
47
+ }
48
+ lines
49
+ end
50
+
51
+ protected
52
+ def fetch(repository)
53
+ [].tap { |results|
54
+ @github.git_data.trees.get @profile, repository[:name], repository[:branch] || :master, recursive: true do |f|
55
+ next if f.type != 'blob'
56
+
57
+ file = @github.repos.contents.get user: @profile, repo: repository[:name], path: URI.escape(f.path)
58
+ results << file
59
+ end
60
+ }
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,26 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ module CodeLines
12
+
13
+ class << self
14
+ attr_reader :adapter
15
+
16
+ def setup(adapter, profile)
17
+ @adapter = adapter.new self, profile
18
+ end
19
+
20
+ def count(*args, &block)
21
+ raise LoadError, 'adapter not found' unless @adapter
22
+ @adapter.count *args, &block
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,15 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ module CodeLines
12
+
13
+ VERSION = '0.1'
14
+
15
+ end
data/lib/codelines.rb ADDED
@@ -0,0 +1,12 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+ require 'codelines/adapter'
11
+ require 'codelines/codelines'
12
+ require 'codelines/version'
@@ -0,0 +1,5 @@
1
+ #! /usr/bin/env ruby
2
+ require 'codelines'
3
+
4
+ describe CodeLines do
5
+ end
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env ruby
2
+ require 'codelines'
3
+ require 'codelines/adapters/github'
4
+
5
+ describe CodeLines::GitHub do
6
+ it 'counts the lines of code contained in a GitHub repository' do
7
+ github = CodeLines::GitHub.new 'RoxasShadow'
8
+
9
+ github.count(repository: [ { name: :codelines } ], ignore_comments: true ).should be > 100
10
+ github.count(repository: [ { name: :codelines } ], ignore_comments: false).should be > 100
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codelines
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Giovanni Capuano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: github_api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Count the number of lines of code contained in one or more repositories
56
+ hosted on GitHub, Bitbucket, etc.
57
+ email: webmaster@giovannicapuano.net
58
+ executables:
59
+ - codelines
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/codelines/adapter.rb
64
+ - lib/codelines/adapters/github.rb
65
+ - lib/codelines/codelines.rb
66
+ - lib/codelines/version.rb
67
+ - lib/codelines.rb
68
+ - test/codelines_spec.rb
69
+ - test/github_spec.rb
70
+ - bin/codelines
71
+ homepage: https://github.com/RoxasShadow
72
+ licenses:
73
+ - WTFPL
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Count how many lines of code are contained in your repositories
95
+ test_files:
96
+ - test/codelines_spec.rb
97
+ - test/github_spec.rb