Class: Percy::Capybara::Bedrock::Loader

Inherits:
Loaders::BaseLoader
  • Object
show all
Defined in:
lib/percy/capybara/bedrock/loader.rb

Constant Summary collapse

RESOURCE_PATHS =
[
  '/app/mu-plugins',
  '/app/plugins',
  '/app/themes',
  '/app/uploads',
  '/wp',
].freeze
SKIP_RESOURCE_EXTENSIONS =
[
  '.gz',
  '.htm',
  '.html',
  '.json',
  '.lock',
  '.log',
  '.map',
  '.md',
  '.php',
  '.phtml',
  '.rar',
  '.rb',
  '.sql',
  '.txt',
  '.xml',
  '.zip',
  '.gemspec',
].freeze
SKIP_RESOURCE_BASENAMES =
[
  '.DS_Store',
  '.editorconfig',
  '.eslintrc.js',
  '.gitignore',
  'Gemfile',
  'LICENSE',
  'Rakefile',
].freeze
SKIP_RESOURCE_PATHS =
[
  '/.cache-loader/',
  '/.cache/',
  '/.caches/',
  '/.circleci/',
  '/.git/',
  '/.github/',
  '/build/',
  '/cache/',
  '/caches/',
  '/doc/',
  '/docs/',
  '/log/',
  '/logs/',
  '/node_modules/',
  '/tmp/',
  '/vendor/',
  '/wp/wp-content/themes/',
  'resources/assets/',
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loader

Returns a new instance of Loader.

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/percy/capybara/bedrock/loader.rb', line 70

def initialize(options = {})
  # @web_root should point to <bedrock>/web
  @web_root = options[:web_root]
  @base_url = options[:base_url] || '/'

  raise ArgumentError, '@web_root is required' if @web_root.nil? || @web_root == ''
  unless Pathname.new(@web_root).absolute?
    raise ArgumentError, "@web_root needs to be an absolute path. Received: #{@web_root}"
  end
  unless Dir.exist?(@web_root)
    raise ArgumentError, "@web_root provided was not found. Received: #{@web_root}"
  end

  super
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



68
69
70
# File 'lib/percy/capybara/bedrock/loader.rb', line 68

def base_url
  @base_url
end

#web_rootObject (readonly)

Returns the value of attribute web_root.



68
69
70
# File 'lib/percy/capybara/bedrock/loader.rb', line 68

def web_root
  @web_root
end

Instance Method Details

#_resources_from_dir(root_dir, base_url: '/') ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/percy/capybara/bedrock/loader.rb', line 95

def _resources_from_dir(root_dir, base_url: '/')
  resources = []

  resource_dirs = RESOURCE_PATHS.map do |resource_path|
    root_dir + resource_path
  end

  _find_files(resource_dirs).each do |path|
    next if skip?(path: path)

    # Replace the @web_root with the base_url to generate the resource_url
    resource_url = _uri_join(base_url, path.sub(root_dir.to_s, ''))
    sha = Digest::SHA256.hexdigest(File.read(path))
    resources << Percy::Client::Resource.new(resource_url, sha: sha, path: path)
  end

  resources
end

#build_resourcesObject



90
91
92
# File 'lib/percy/capybara/bedrock/loader.rb', line 90

def build_resources
  _resources_from_dir(@web_root, base_url: @base_url)
end

#skip?(path:) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
# File 'lib/percy/capybara/bedrock/loader.rb', line 114

def skip?(path:)
  skip_extension?(path: path) ||
  skip_basename?(path: path) ||
  skip_path?(path: path) ||
  skip_file_size?(path: path)
end

#skip_basename?(path:) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/percy/capybara/bedrock/loader.rb', line 125

def skip_basename?(path:)
  SKIP_RESOURCE_BASENAMES.include?(File.basename(path))
end

#skip_extension?(path:) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/percy/capybara/bedrock/loader.rb', line 121

def skip_extension?(path:)
  SKIP_RESOURCE_EXTENSIONS.include?(File.extname(path))
end

#skip_file_size?(path:) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/percy/capybara/bedrock/loader.rb', line 133

def skip_file_size?(path:)
  File.size(path) > MAX_FILESIZE_BYTES
end

#skip_path?(path:) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/percy/capybara/bedrock/loader.rb', line 129

def skip_path?(path:)
  SKIP_RESOURCE_PATHS.any? { |skip| path.include?(skip) }
end

#snapshot_resourcesObject



86
87
88
# File 'lib/percy/capybara/bedrock/loader.rb', line 86

def snapshot_resources
  [root_html_resource]
end