Module: Percy::Cli::Snapshot

Included in:
Percy::Cli
Defined in:
lib/percy/cli/snapshot.rb

Constant Summary collapse

STATIC_RESOURCE_EXTENSIONS =

Static resource types that an HTML file might load and that we want to upload for rendering.

[
  '.css', '.jpg', '.jpeg', '.gif', '.ico', '.png', '.bmp', '.pict', '.tif', '.tiff', '.ttf',
  '.eot', '.woff', '.otf', '.svg', '.svgz', '.webp', '.ps',
].freeze
DEFAULT_SNAPSHOTS_REGEX =
/\.(html|htm)$/

Instance Method Summary collapse

Instance Method Details

#run_snapshot(root_dir, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/percy/cli/snapshot.rb', line 20

def run_snapshot(root_dir, options = {})
  repo = options[:repo] || Percy.config.repo
  strip_prefix = File.absolute_path(options[:strip_prefix] || root_dir)
  num_threads = options[:threads] || 10
  snapshot_limit = options[:snapshot_limit]
  baseurl = options[:baseurl] || '/'
  raise ArgumentError.new('baseurl must start with /') if baseurl[0] != '/'

  base_resource_options = {strip_prefix: strip_prefix, baseurl: baseurl}

  # Find all the static files in the given root directory.
  root_paths = find_root_paths(root_dir, snapshots_regex: options[:snapshots_regex])
  resource_paths = find_resource_paths(root_dir)
  root_resources = build_resources(root_paths, base_resource_options.merge(is_root: true))
  related_resources = build_resources(resource_paths, base_resource_options)
  all_resources = root_resources + related_resources

  if root_resources.empty?
    say "No root resource files found. Are there HTML files in the given directory?"
    exit(-1)
  end

  build = rescue_connection_failures do
    say 'Creating build...'
    build = Percy.create_build(repo, resources: related_resources)

    say 'Uploading build resources...'
    upload_missing_resources(build, build, all_resources, {num_threads: num_threads})

    build
  end
  return if failed?

  # Upload a snapshot for every root resource, and associate the related_resources.
  output_lock = Mutex.new
  snapshot_thread_pool = Thread.pool(num_threads)
  total = snapshot_limit ? [root_resources.length, snapshot_limit].min : root_resources.length
  root_resources.each_with_index do |root_resource, i|
    break if snapshot_limit && i + 1 > snapshot_limit
    snapshot_thread_pool.process do
      output_lock.synchronize do
        say "Uploading snapshot (#{i+1}/#{total}): #{root_resource.resource_url}"
      end
      rescue_connection_failures do
        snapshot = Percy.create_snapshot(build['data']['id'], [root_resource])
        upload_missing_resources(build, snapshot, all_resources, {num_threads: num_threads})
        Percy.finalize_snapshot(snapshot['data']['id'])
      end
    end
  end
  snapshot_thread_pool.wait
  snapshot_thread_pool.shutdown

  # Finalize the build.
  say 'Finalizing build...'
  rescue_connection_failures { Percy.finalize_build(build['data']['id']) }
  return if failed?
  say "Done! Percy is now processing, you can view the visual diffs here:"
  say build['data']['attributes']['web-url']
end