Class: Spektr::App
- Inherits:
-
Object
- Object
- Spektr::App
- Defined in:
- lib/spektr/app.rb
Instance Attribute Summary collapse
-
#checks ⇒ Object
Returns the value of attribute checks.
-
#controllers ⇒ Object
Returns the value of attribute controllers.
-
#gem_specs ⇒ Object
Returns the value of attribute gem_specs.
-
#initializers ⇒ Object
Returns the value of attribute initializers.
-
#lib_files ⇒ Object
Returns the value of attribute lib_files.
-
#models ⇒ Object
Returns the value of attribute models.
-
#production_config ⇒ Object
Returns the value of attribute production_config.
-
#rails_version ⇒ Object
Returns the value of attribute rails_version.
-
#root ⇒ Object
Returns the value of attribute root.
-
#routes ⇒ Object
Returns the value of attribute routes.
-
#ruby_version ⇒ Object
Returns the value of attribute ruby_version.
-
#views ⇒ Object
Returns the value of attribute views.
-
#warnings ⇒ Object
Returns the value of attribute warnings.
Instance Method Summary collapse
- #controller_paths ⇒ Object
- #find_files(path, extensions = 'rb') ⇒ Object
- #has_gem?(name) ⇒ Boolean
-
#initialize(checks:, ignore: [], root: './') ⇒ App
constructor
A new instance of App.
- #initializer_paths ⇒ Object
- #load ⇒ Object
- #model_paths ⇒ Object
- #report ⇒ Object
- #scan! ⇒ Object
- #view_paths ⇒ Object
Constructor Details
#initialize(checks:, ignore: [], root: './') ⇒ App
Returns a new instance of App.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/spektr/app.rb', line 6 def initialize(checks:, ignore: [], root: './') @root = root @checks = checks @controllers = [] @models = [] @warnings = [] @json_output = { app: {}, advisories: [] } @ignore = ignore @ruby_version = '2.7.1' version_file = File.join(root, '.ruby-version') @ruby_version = File.read(version_file).lines.first if File.exist?(version_file) end |
Instance Attribute Details
#checks ⇒ Object
Returns the value of attribute checks.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def checks @checks end |
#controllers ⇒ Object
Returns the value of attribute controllers.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def controllers @controllers end |
#gem_specs ⇒ Object
Returns the value of attribute gem_specs.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def gem_specs @gem_specs end |
#initializers ⇒ Object
Returns the value of attribute initializers.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def initializers @initializers end |
#lib_files ⇒ Object
Returns the value of attribute lib_files.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def lib_files @lib_files end |
#models ⇒ Object
Returns the value of attribute models.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def models @models end |
#production_config ⇒ Object
Returns the value of attribute production_config.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def production_config @production_config end |
#rails_version ⇒ Object
Returns the value of attribute rails_version.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def rails_version @rails_version end |
#root ⇒ Object
Returns the value of attribute root.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def root @root end |
#routes ⇒ Object
Returns the value of attribute routes.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def routes @routes end |
#ruby_version ⇒ Object
Returns the value of attribute ruby_version.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def ruby_version @ruby_version end |
#views ⇒ Object
Returns the value of attribute views.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def views @views end |
#warnings ⇒ Object
Returns the value of attribute warnings.
3 4 5 |
# File 'lib/spektr/app.rb', line 3 def warnings @warnings end |
Instance Method Details
#controller_paths ⇒ Object
137 138 139 |
# File 'lib/spektr/app.rb', line 137 def controller_paths @controller_paths ||= find_files('app/**/controllers') end |
#find_files(path, extensions = 'rb') ⇒ Object
149 150 151 |
# File 'lib/spektr/app.rb', line 149 def find_files(path, extensions = 'rb') Dir.glob(File.join(@root, path, '**', "*.#{extensions}")) end |
#has_gem?(name) ⇒ Boolean
159 160 161 162 163 |
# File 'lib/spektr/app.rb', line 159 def has_gem?(name) return false unless gem_specs gem_specs.any? { |spec| spec.name == name } end |
#initializer_paths ⇒ Object
133 134 135 |
# File 'lib/spektr/app.rb', line 133 def initializer_paths @initializer_paths ||= find_files('config/initializers') end |
#load ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/spektr/app.rb', line 22 def load loaded_files = [] config_path = File.join(@root, 'config', 'environments', 'production.rb') if File.exist?(config_path) @production_config = Targets::Config.new(config_path, File.read(config_path, encoding: 'utf-8')) end @initializers = initializer_paths.map do |path| loaded_files << path Targets::Base.new(path, File.read(path)) end @controllers = controller_paths.map do |path| loaded_files << path Targets::Controller.new(path, File.read(path, encoding: 'utf-8')) end @models = model_paths.map do |path| loaded_files << path Targets::Model.new(path, File.read(path, encoding: 'utf-8')) end @views = view_paths.map do |path| loaded_files << path Targets::View.new(path, File.read(path, encoding: 'utf-8')) end @routes = [File.join(@root, 'config', 'routes.rb')].map do |path| next unless File.exist? path loaded_files << path Targets::Routes.new(path, File.read(path, encoding: 'utf-8')) end.reject(&:nil?) # TODO: load non-app lib too @lib_files = find_files('lib').map do |path| next if loaded_files.include?(path) Targets::Base.new(path, File.read(path, encoding: 'utf-8')) end.reject(&:nil?) self end |
#model_paths ⇒ Object
141 142 143 |
# File 'lib/spektr/app.rb', line 141 def model_paths @model_paths ||= find_files('app/**/models') end |
#report ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/spektr/app.rb', line 99 def report @json_output[:app][:rails_version] = @rails_version @json_output[:app][:initializers] = @initializers.size @json_output[:app][:controllers] = @controllers.size @json_output[:app][:models] = @models.size @json_output[:app][:views] = @views.size @json_output[:app][:routes] = @routes.size @json_output[:app][:lib_files] = @lib_files.size @warnings.each do |warning| next if @ignore.include?(warning.fingerprint) @json_output[:advisories] << { name: warning.check.name, description: warning., path: warning.path, location: warning.location&.start_line, line: warning.line, check: warning.check.class.name, fingerprint: warning.fingerprint } end @json_output[:summary] = [] @json_output[:checks] = @checks.collect(&:name) @json_output[:advisories].group_by { |a| a[:name] }.each do |n, i| @json_output[:summary] << { n => i.size } end @json_output end |
#scan! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/spektr/app.rb', line 61 def scan! @checks.each do |check| if @controllers @controllers.each do |controller| check.new(self, controller).run end end if @views @views.each do |view| check.new(self, view).run end end if @models @models.each do |view| check.new(self, view).run end end if @routes @routes.each do |view| check.new(self, view).run end end if @initializers @initializers.each do |i| check.new(self, i).run end end if @lib_files @lib_files.each do |i| check.new(self, i).run end end check.new(self, @production_config).run if @production_config end self end |
#view_paths ⇒ Object
145 146 147 |
# File 'lib/spektr/app.rb', line 145 def view_paths @view_paths ||= find_files('app', "{#{%w[html.erb html.haml rhtml js.erb html.slim].join(',')}}") end |