Class: Spektr::App

Inherits:
Object
  • Object
show all
Defined in:
lib/spektr/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(checks:, ignore: [], root: './') ⇒ App

Returns a new instance of App.



10
11
12
13
14
15
16
17
18
19
20
21
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
60
61
# File 'lib/spektr/app.rb', line 10

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)
  case @ruby_version
  when /^2\.0\./
    require 'parser/ruby20'
    @@parser = Parser::Ruby20
  when /^2\.1\./
    require 'parser/ruby21'
    @@parser = Parser::Ruby21
  when /^2\.2\./
    require 'parser/ruby22'
    @@parser = Parser::Ruby22
  when /^2\.3/
    require 'parser/ruby23'
    @@parser = Parser::Ruby23
  when /^2\.4\./
    require 'parser/ruby24'
    @@parser = Parser::Ruby24
  when /^2\.5\./
    require 'parser/ruby25'
    @@parser = Parser::Ruby25
  when /^2\.6\./
    require 'parser/ruby26'
    @@parser = Parser::Ruby26
  when /^2\.7\./
    require 'parser/ruby27'
    @@parser = Parser::Ruby27
  when /^3\.0\./
    require 'parser/ruby30'
    @@parser = Parser::Ruby30
  when /^3\.1\./
    require 'parser/ruby31'
    @@parser = Parser::Ruby31
  when /^3\.2\./
    require 'parser/ruby32'
    @@parser = Parser::Ruby32
  else
    @@parser = Parser::CurrentRuby
  end
end

Instance Attribute Details

#checksObject

Returns the value of attribute checks.



3
4
5
# File 'lib/spektr/app.rb', line 3

def checks
  @checks
end

#controllersObject

Returns the value of attribute controllers.



3
4
5
# File 'lib/spektr/app.rb', line 3

def controllers
  @controllers
end

#gem_specsObject

Returns the value of attribute gem_specs.



3
4
5
# File 'lib/spektr/app.rb', line 3

def gem_specs
  @gem_specs
end

#initializersObject

Returns the value of attribute initializers.



3
4
5
# File 'lib/spektr/app.rb', line 3

def initializers
  @initializers
end

#lib_filesObject

Returns the value of attribute lib_files.



3
4
5
# File 'lib/spektr/app.rb', line 3

def lib_files
  @lib_files
end

#modelsObject

Returns the value of attribute models.



3
4
5
# File 'lib/spektr/app.rb', line 3

def models
  @models
end

#production_configObject

Returns the value of attribute production_config.



3
4
5
# File 'lib/spektr/app.rb', line 3

def production_config
  @production_config
end

#rails_versionObject

Returns the value of attribute rails_version.



3
4
5
# File 'lib/spektr/app.rb', line 3

def rails_version
  @rails_version
end

#rootObject

Returns the value of attribute root.



3
4
5
# File 'lib/spektr/app.rb', line 3

def root
  @root
end

#routesObject

Returns the value of attribute routes.



3
4
5
# File 'lib/spektr/app.rb', line 3

def routes
  @routes
end

#ruby_versionObject

Returns the value of attribute ruby_version.



3
4
5
# File 'lib/spektr/app.rb', line 3

def ruby_version
  @ruby_version
end

#viewsObject

Returns the value of attribute views.



3
4
5
# File 'lib/spektr/app.rb', line 3

def views
  @views
end

#warningsObject

Returns the value of attribute warnings.



3
4
5
# File 'lib/spektr/app.rb', line 3

def warnings
  @warnings
end

Class Method Details

.parserObject



6
7
8
# File 'lib/spektr/app.rb', line 6

def self.parser
  @@parser ||= Parser::CurrentRuby
end

Instance Method Details

#controller_pathsObject



183
184
185
# File 'lib/spektr/app.rb', line 183

def controller_paths
  @controller_paths ||= find_files('app/**/controllers')
end

#find_files(path, extensions = 'rb') ⇒ Object



195
196
197
# File 'lib/spektr/app.rb', line 195

def find_files(path, extensions = 'rb')
  Dir.glob(File.join(@root, path, '**', "*.#{extensions}"))
end

#has_gem?(name) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
208
209
# File 'lib/spektr/app.rb', line 205

def has_gem?(name)
  return false unless gem_specs

  gem_specs.any? { |spec| spec.name == name }
end

#initializer_pathsObject



179
180
181
# File 'lib/spektr/app.rb', line 179

def initializer_paths
  @initializer_paths ||= find_files('config/initializers')
end

#loadObject



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
98
99
100
101
102
103
104
105
# File 'lib/spektr/app.rb', line 63

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)
    begin
      Targets::Base.new(path, File.read(path, encoding: 'utf-8'))
    rescue Parser::SyntaxError => e
      ::Spektr.logger.debug "Couldn't parse #{path}: #{e.message}"
      nil
    end
  end.reject(&:nil?)
  self
end

#model_pathsObject



187
188
189
# File 'lib/spektr/app.rb', line 187

def model_paths
  @model_paths ||= find_files('app/**/models')
end

#reportObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/spektr/app.rb', line 145

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.message,
      path: warning.path,
      location: warning.location&.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



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
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/spektr/app.rb', line 107

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_pathsObject



191
192
193
# File 'lib/spektr/app.rb', line 191

def view_paths
  @view_paths ||= find_files('app', "{#{%w[html.erb html.haml rhtml js.erb html.slim].join(',')}}")
end