Class: Percy::Cli::SnapshotRunner
- Inherits:
-
Object
- Object
- Percy::Cli::SnapshotRunner
- Defined in:
- lib/percy/cli/snapshot_runner.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', '.js', '.jpg', '.jpeg', '.gif', '.ico', '.png', '.bmp', '.pict', '.tif', '.tiff', '.ttf', '.eot', '.woff', '.otf', '.svg', '.svgz', '.webp', '.ps', ].freeze
- DEFAULT_SNAPSHOTS_REGEX =
/\.(html|htm)$/- MAX_FILESIZE_BYTES =
15 MB.
15 * 1024**2
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #_failed? ⇒ Boolean
-
#_find_files(*paths) ⇒ Object
A file find method that follows directory and file symlinks.
- #_find_resource_paths(dir_path, options = {}) ⇒ Object
- #_find_root_paths(dir_path, options = {}) ⇒ Object
- #_ignore_resource_path?(path, options) ⇒ Boolean
- #_include_resource_path?(path, options) ⇒ Boolean
- #_include_root_path?(path, options) ⇒ Boolean
- #_list_resources(paths, options = {}) ⇒ Object
- #_rescue_connection_failures ⇒ Object
-
#_upload_missing_resources(build, obj, potential_resources, options = {}) ⇒ Object
Uploads missing resources either for a build or snapshot.
-
#initialize ⇒ SnapshotRunner
constructor
A new instance of SnapshotRunner.
- #run(root_dir, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ SnapshotRunner
Returns a new instance of SnapshotRunner.
23 24 25 |
# File 'lib/percy/cli/snapshot_runner.rb', line 23 def initialize @client = Percy::Client.new(client_info: "percy-cli/#{VERSION}", environment_info: '') end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
12 13 14 |
# File 'lib/percy/cli/snapshot_runner.rb', line 12 def client @client end |
Instance Method Details
#_failed? ⇒ Boolean
116 117 118 |
# File 'lib/percy/cli/snapshot_runner.rb', line 116 def _failed? !!@failed end |
#_find_files(*paths) ⇒ Object
A file find method that follows directory and file symlinks.
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/percy/cli/snapshot_runner.rb', line 205 def _find_files(*paths) paths.flatten! paths.map! { |p| Pathname.new(p) } files = paths.select(&:file?) (paths - files).each do |dir| files << _find_files(dir.children) end files.flatten.map(&:to_s) end |
#_find_resource_paths(dir_path, options = {}) ⇒ Object
143 144 145 |
# File 'lib/percy/cli/snapshot_runner.rb', line 143 def _find_resource_paths(dir_path, = {}) _find_files(dir_path).select { |path| _include_resource_path?(path, ) } end |
#_find_root_paths(dir_path, options = {}) ⇒ Object
137 138 139 140 141 |
# File 'lib/percy/cli/snapshot_runner.rb', line 137 def _find_root_paths(dir_path, = {}) _find_files(dir_path) .select { |path| _include_root_path?(path, ) } .reject { |path| _ignore_resource_path?(path, ) } end |
#_ignore_resource_path?(path, options) ⇒ Boolean
225 226 227 228 229 230 231 232 233 |
# File 'lib/percy/cli/snapshot_runner.rb', line 225 def _ignore_resource_path?(path, ) return false unless [:ignore_regex] begin path.match([:ignore_regex]) rescue StandardError false end end |
#_include_resource_path?(path, options) ⇒ Boolean
217 218 219 220 221 222 223 |
# File 'lib/percy/cli/snapshot_runner.rb', line 217 def _include_resource_path?(path, ) # Skip git files. return false if path =~ /\/\.git\// return true if [:include_all] STATIC_RESOURCE_EXTENSIONS.include?(File.extname(path)) end |
#_include_root_path?(path, options) ⇒ Boolean
235 236 237 238 239 240 241 242 |
# File 'lib/percy/cli/snapshot_runner.rb', line 235 def _include_root_path?(path, ) # Skip git files. return false if path =~ /\/\.git\// # Skip files that don't match the snapshots_regex. snapshots_regex = [:snapshots_regex] || DEFAULT_SNAPSHOTS_REGEX path.match(snapshots_regex) end |
#_list_resources(paths, options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/percy/cli/snapshot_runner.rb', line 147 def _list_resources(paths, = {}) strip_prefix = File.([:strip_prefix]) baseurl = [:baseurl] resources = [] # Strip trailing slash from strip_prefix. strip_prefix = strip_prefix[0..-2] if strip_prefix[-1] == '/' paths.each do |path| sha = Digest::SHA256.hexdigest(File.read(path)) next if File.size(path) > MAX_FILESIZE_BYTES resource_url = Addressable::URI.escape(File.join(baseurl, path.sub(strip_prefix, ''))) resources << Percy::Client::Resource.new( resource_url, sha: sha, is_root: [:is_root], path: path, ) end resources end |
#_rescue_connection_failures ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/percy/cli/snapshot_runner.rb', line 120 def _rescue_connection_failures raise ArgumentError, 'block is requried' unless block_given? begin yield rescue Percy::Client::ServerError, # Rescue server errors. Percy::Client::UnauthorizedError, # Rescue unauthorized errors (no auth creds setup). Percy::Client::PaymentRequiredError, # Rescue quota exceeded errors. Percy::Client::ConflictError, # Rescue project disabled errors and others. Percy::Client::ConnectionFailed, # Rescue some networking errors. Percy::Client::TimeoutError => e Percy.logger.error(e) @failed = true nil end end |
#_upload_missing_resources(build, obj, potential_resources, options = {}) ⇒ Object
Uploads missing resources either for a build or snapshot.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/percy/cli/snapshot_runner.rb', line 168 def _upload_missing_resources(build, obj, potential_resources, = {}) # Upload the content for any missing resources. missing_resources = obj['data']['relationships']['missing-resources']['data'] = Commander::UI::ProgressBar.new( missing_resources.length, title: 'Uploading resources...', format: ':title |:progress_bar| :percent_complete% complete - :resource_url', width: 20, complete_message: nil, ) output_lock = Mutex.new uploader_thread_pool = Thread.pool([:num_threads] || 10) missing_resources.each do |missing_resource| uploader_thread_pool.process do missing_resource_sha = missing_resource['id'] resource = potential_resources.find { |r| r.sha == missing_resource_sha } output_lock.synchronize do .increment resource_url: resource.resource_url end # Remote resources are stored in 'content', local resources are # read from the filesystem. content = resource.content || File.read(resource.path.to_s) client.upload_resource(build['data']['id'], content) end end uploader_thread_pool.wait uploader_thread_pool.shutdown end |
#run(root_dir, options = {}) ⇒ Object
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 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 106 107 108 109 110 111 112 113 114 |
# File 'lib/percy/cli/snapshot_runner.rb', line 27 def run(root_dir, = {}) root_dir = File.(File.absolute_path(root_dir)) strip_prefix = File.(File.absolute_path([:strip_prefix] || root_dir)) # CLI Options enable_javascript = !![:enable_javascript] include_all = !![:include_all] snapshot_limit = [:snapshot_limit] snapshots_regex = [:snapshots_regex] ignore_regex = [:ignore_regex] widths = [:widths].map { |w| Integer(w) } num_threads = [:threads] || 10 baseurl = [:baseurl] || '/' raise ArgumentError, 'baseurl must start with /' if baseurl[0] != '/' = {strip_prefix: strip_prefix, baseurl: baseurl} # Find all the static files in the given root directory. opts = { include_all: include_all, ignore_regex: ignore_regex, snapshots_regex: snapshots_regex, } root_paths = _find_root_paths(root_dir, opts) resource_paths = _find_resource_paths(root_dir, opts) root_resources = _list_resources(root_paths, .merge(is_root: true)) build_resources = _list_resources(resource_paths, ) all_resources = root_resources + build_resources if root_resources.empty? say 'No root resource files found. Are there HTML files in the given directory?' exit(-1) end build_resources.each do |resource| Percy.logger.debug { "Found build resource: #{resource.resource_url}" } end build = _rescue_connection_failures do say 'Creating build...' build = client.create_build(resources: build_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 build_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 = client.create_snapshot( build['data']['id'], [root_resource], enable_javascript: enable_javascript, widths: widths, ) _upload_missing_resources(build, snapshot, all_resources, num_threads: num_threads) client.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 { client.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 |