entruby 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9891022b30149b2a54971b292957b6c32374d4c40dd13b316afe3ad8f456ba0d
4
+ data.tar.gz: 5ff335f46aac2c12e77a2085d2609a75dc8d344b2f1a57f943ba1ef1483e0d82
5
+ SHA512:
6
+ metadata.gz: 226a60b530abe737bf4fe598ce6dad4c0d0a89a0a0af610c1747d9ded61b2fcfba5b5704d857d733ed9e3ddae8f6eec4d6a09173a6006cded4c64bd73e362a31
7
+ data.tar.gz: cb3a456af8a3e58b5a615c24aa44e3aaecc17c01923075b9056ac9fd8317ab89c4907dc55c008453c744870ce6a91660a285b8f9c8a7fab72316100002abe360
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at canmor.lam@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in entruby.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Canmor Lam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Entruby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/entruby`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'entruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install entruby
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/entruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Entruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/entruby/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "entruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "entruby/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "entruby"
8
+ spec.version = Entruby::VERSION
9
+ spec.authors = ["Canmor Lam"]
10
+ spec.email = ["canmor.lam@gmail.com"]
11
+
12
+ spec.summary = "Entropy in ruby"
13
+ spec.description = "Entropy in ruby"
14
+ spec.homepage = "https://canmor.lams.io"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end + Dir["vendor/**/*"].reject { |f| f.match(%r{(report\.json|\.map)$}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency "nokogiri", "~> 1.10"
27
+ spec.add_runtime_dependency "doxyparser", "~> 1.5"
28
+ spec.add_runtime_dependency "launchy", "~> 2.5"
29
+ spec.add_development_dependency "bundler", "~> 1.17"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ spec.add_development_dependency "pry"
33
+ end
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ require "entruby"
3
+ require "json"
4
+ require 'uri'
5
+ require "launchy"
6
+ require "fileutils"
7
+
8
+ app = Entruby::App.new(ARGV)
9
+ report = app.report
10
+
11
+ output_dir = File.expand_path(app.output_dir)
12
+ output_dir = File.join(output_dir, 'shower')
13
+
14
+ shower_dir = File.join(__dir__, '..', 'vendor', 'shower')
15
+ FileUtils.copy_entry shower_dir, output_dir
16
+
17
+ File.open(File.join(output_dir, 'report.json'), 'w') { |file| file.write(JSON.dump(report)) }
18
+ puts "Index is: #{report[:overview][:index]}"
19
+ report[:overview][:metrics].each { |metric| puts " - #{metric[:name]}: #{metric[:value]}" }
20
+
21
+ puts "
22
+ Report is generated to: #{output_dir}
23
+ Launching browser to reveal report..."
24
+
25
+ uri = URI.join('file:///', File.join(output_dir, 'index.html'))
26
+ Launchy.open(uri)
@@ -0,0 +1,167 @@
1
+ require "entruby/version"
2
+ require "doxyparser"
3
+ require "find"
4
+
5
+ class Reference
6
+ def initialize(element)
7
+ @id = element[:refid]
8
+ @content = element.content
9
+ end
10
+
11
+ # @return Doxyparser::Function
12
+ def reveal(collection)
13
+ collection.find { |member| member.basename == @content }
14
+ end
15
+ end
16
+
17
+ module Doxyparser
18
+ class Member
19
+ def col
20
+ aux = self.xpath("location")[0]
21
+ bodyEnd, bodyStart = aux["bodyend"], aux["bodystart"]
22
+ @col = bodyEnd && bodyStart ? Integer(bodyEnd) - Integer(bodyStart) : nil
23
+ end
24
+ end
25
+
26
+ class Param
27
+ def as_json(*)
28
+ {type: {name: type.name, basename: type.basename}, name: declname}
29
+ end
30
+ end
31
+
32
+ class Function
33
+ # @return Array[Reference]
34
+ def references
35
+ node.css('references').map { |ref| Reference.new(ref) }
36
+ end
37
+
38
+ def referenced_by
39
+ node.css('referencedby').map { |ref| Reference.new(ref) }
40
+ end
41
+
42
+ def efferent(all = nil)
43
+ all.to_a.empty? ? references : references.map { |ref| ref.reveal(all) }.compact
44
+ end
45
+
46
+ def afferent(all = nil)
47
+ all.to_a.empty? ? referenced_by : referenced_by.map { |ref| ref.reveal(all) }.compact
48
+ end
49
+
50
+ def fan_out(all = nil)
51
+ efferent(all).size
52
+ end
53
+
54
+ def fan_in(all = nil)
55
+ afferent(all).size
56
+ end
57
+
58
+ def coupling_metric(all = nil)
59
+ fan_out = fan_out(all).to_f
60
+ fan_in = fan_in(all).to_f
61
+ denominator = fan_out + fan_in
62
+ denominator == 0 ? 0 : fan_out / (denominator)
63
+ end
64
+
65
+ def global_name
66
+ "global::#{signature}"
67
+ end
68
+
69
+ def valid_params
70
+ params.reject { |p| p.type.name == 'void' }
71
+ end
72
+
73
+ def signature
74
+ "#{basename}(#{valid_params.map { |p| p.type.name }.join(', ')})"
75
+ end
76
+
77
+ # @return Report
78
+ def report(all)
79
+ {id: id, name: basename, signature: signature, location: location, metric: coupling_metric(all), col: col,
80
+ params: valid_params.map(&:as_json),
81
+ imports: efferent(all).map(&:signature)}
82
+ end
83
+ end
84
+ end
85
+
86
+ module Entruby
87
+ class Error < StandardError; end
88
+
89
+ class OverviewMetric
90
+ attr_reader :name, :description, :value, :weight
91
+
92
+ def initialize(name, description, value, weight)
93
+ @name, @description, @value, @weight = name, description, value, weight
94
+ end
95
+
96
+ def as_json(*args)
97
+ {name: name, description: description, value: value, weight: weight}
98
+ end
99
+ end
100
+
101
+ class Overview
102
+ attr_reader :metrics
103
+
104
+ COMPLEX_FUNCTION_PARAMS = 7
105
+ HIGH_COUPLING_METRIC = 0.7
106
+ HUGE_FUNCTION_COL = 50
107
+
108
+ def initialize(couplings)
109
+ huge_functions = couplings.count { |c| c[:col] >= HUGE_FUNCTION_COL }
110
+ complex_functions = couplings.count { |c| c[:params].size >= COMPLEX_FUNCTION_PARAMS }
111
+ couplers = couplings.count { |c| c[:metric] >= HIGH_COUPLING_METRIC }
112
+ @metrics = [
113
+ OverviewMetric.new("Excessive Couplers", "(Metric >= 0.7)", couplers, 3),
114
+ OverviewMetric.new("Complex Functions", "(Params >= 7)", complex_functions, 2),
115
+ OverviewMetric.new("Huge Functions", "(CoL >= 50)", huge_functions, 1),
116
+ ]
117
+ end
118
+
119
+ def report
120
+ {index: index, metrics: metrics.map(&:as_json)}
121
+ end
122
+
123
+ def index
124
+ metrics.reduce(0.0) { |memo, metric| memo + metric.value * metric.weight }
125
+ end
126
+ end
127
+
128
+ class App
129
+ attr_reader :output_dir
130
+
131
+ def initialize(argv)
132
+ if argv.size < 2
133
+ puts "USAGE: $ entruby <source directory> <doxygen xml directory>"
134
+ exit 1
135
+ end
136
+
137
+ @source_dir, @xml_dir, @output_dir = argv[0], argv[1], Dir.pwd
138
+ end
139
+
140
+ # @return [Array<Doxyparser::Function>]
141
+ def functions
142
+ source_file_paths = []
143
+ Find.find(@source_dir) do |path|
144
+ source_file_paths << File.basename(path).gsub('_', '__') if path =~ /.*\.c$/
145
+ end
146
+ source_file_paths.reduce([]) do |functions, file|
147
+ hfile = Doxyparser::parse_file(file, @xml_dir)
148
+ functions + hfile.functions
149
+ end
150
+ end
151
+
152
+ def functions_report
153
+ all_functions = functions
154
+ report = all_functions.map { |func| func.report(all_functions) }
155
+ report.sort_by! { |row| row[:metric] }.reverse!
156
+ end
157
+
158
+ def overview(couplings)
159
+ Overview.new(couplings).report
160
+ end
161
+
162
+ def report
163
+ couplings = functions_report
164
+ {overview: overview(couplings), functions: couplings}
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,3 @@
1
+ module Entruby
2
+ VERSION = "0.1.2"
3
+ end