Class: Brakeman::AppTree

Inherits:
Object show all
Defined in:
lib/brakeman/app_tree.rb

Constant Summary collapse

VIEW_EXTENSIONS =
%w[html.erb html.haml rhtml js.erb html.slim].join(",")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, init_options = {}) ⇒ AppTree

Returns a new instance of AppTree.


22
23
24
25
26
27
# File 'lib/brakeman/app_tree.rb', line 22

def initialize(root, init_options = {})
  @root = root
  @skip_files = init_options[:skip_files]
  @only_files = init_options[:only_files]
  @additional_libs_path = init_options[:additional_libs_path] || []
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root


5
6
7
# File 'lib/brakeman/app_tree.rb', line 5

def root
  @root
end

Class Method Details

.from_options(options) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/brakeman/app_tree.rb', line 7

def self.from_options(options)
  root = File.expand_path options[:app_path]

  # Convert files into Regexp for matching
  init_options = {}
  if options[:skip_files]
    init_options[:skip_files] = Regexp.new("(?:" << options[:skip_files].map { |f| Regexp.escape f }.join("|") << ")$")
  end
  if options[:only_files]
    init_options[:only_files] = Regexp.new("(?:" << options[:only_files].map { |f| Regexp.escape f }.join("|") << ")")
  end
  init_options[:additional_libs_path] = options[:additional_libs_path]
  new(root, init_options)
end

Instance Method Details

#controller_pathsObject


58
59
60
# File 'lib/brakeman/app_tree.rb', line 58

def controller_paths
  @controller_paths ||= find_paths("app/**/controllers")
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/brakeman/app_tree.rb', line 45

def exists?(path)
  File.exists?(File.join(@root, path))
end

#expand_path(path) ⇒ Object


29
30
31
# File 'lib/brakeman/app_tree.rb', line 29

def expand_path(path)
  File.expand_path(path, @root)
end

#initializer_pathsObject


54
55
56
# File 'lib/brakeman/app_tree.rb', line 54

def initializer_paths
  @initializer_paths ||= find_paths("config/initializers")
end

#layout_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
73
# File 'lib/brakeman/app_tree.rb', line 70

def layout_exists?(name)
  pattern = "#{@root}/{engines/*/,}app/views/layouts/#{name}.html.{erb,haml,slim}"
  !Dir.glob(pattern).empty?
end

#lib_pathsObject


75
76
77
78
# File 'lib/brakeman/app_tree.rb', line 75

def lib_paths
  @lib_files ||= find_paths("lib").reject { |path| path.include? "/generators/" or path.include? "lib/tasks/" } +
                 find_additional_lib_paths
end

#model_pathsObject


62
63
64
# File 'lib/brakeman/app_tree.rb', line 62

def model_paths
  @model_paths ||= find_paths("app/**/models")
end

#path_exists?(path) ⇒ Boolean

This is a pair for #read_path. Again, would like to kill these

Returns:

  • (Boolean)

50
51
52
# File 'lib/brakeman/app_tree.rb', line 50

def path_exists?(path)
  File.exists?(path)
end

#read(path) ⇒ Object


33
34
35
# File 'lib/brakeman/app_tree.rb', line 33

def read(path)
  File.read(File.join(@root, path))
end

#read_path(path) ⇒ Object

This variation requires full paths instead of paths based off the project root. I'd prefer to get all the code outside of AppTree using project-root based paths (e.g. app/models/user.rb) instead of full paths, but I suspect it's an incompatible change.


41
42
43
# File 'lib/brakeman/app_tree.rb', line 41

def read_path(path)
  File.read(path)
end

#template_pathsObject


66
67
68
# File 'lib/brakeman/app_tree.rb', line 66

def template_paths
  @template_paths ||= find_paths("app/**/views", "*.{#{VIEW_EXTENSIONS}}")
end