class Frameit::FrameDownloader

Constants

HOST_URL

Public Class Methods

templates_path() click to toggle source
# File frameit/lib/frameit/frame_downloader.rb, line 41
def self.templates_path
  # Previously ~/.frameit/device_frames_2/x
  legacy_path = File.join(ENV['HOME'], ".frameit/devices_frames_2", Frameit.frames_version)
  return legacy_path if File.directory?(legacy_path)

  # New path, being ~/.fastlane/frameit/x
  return File.join(FastlaneCore.fastlane_user_dir, "frameit", Frameit.frames_version)
end

Public Instance Methods

download_frames() click to toggle source
# File frameit/lib/frameit/frame_downloader.rb, line 9
def download_frames
  print_disclaimer

  require 'json'
  require 'fileutils'

  UI.message("Downloading device frames to '#{templates_path}'")
  FileUtils.mkdir_p(templates_path)

  frames_version = download_file("version.txt")
  UI.important("Using frame version '#{frames_version}', you can optionally lock that version in your Framefile.json using `device_frame_version`")

  files = JSON.parse(download_file("files.json"))
  files.each_with_index do |current, index|
    content = download_file(current, txt: "#{index + 1} of #{files.count} files")
    File.binwrite(File.join(templates_path, current), content)
  end
  File.write(File.join(templates_path, "offsets.json"), download_file("offsets.json"))

  # Write the version.txt at the very end to properly resume downloads
  # if it's interrupted
  File.write(File.join(templates_path, "version.txt"), frames_version)

  UI.success("Successfully downloaded all required image assets")
end
frames_exist?(version: "latest") click to toggle source
# File frameit/lib/frameit/frame_downloader.rb, line 35
def frames_exist?(version: "latest")
  version_path = File.join(templates_path, "version.txt")
  version = File.read(version_path) if File.exist?(version_path)
  Dir["#{templates_path}/*.png"].count > 0 && version.to_i > 0
end
print_disclaimer() click to toggle source
templates_path() click to toggle source
# File frameit/lib/frameit/frame_downloader.rb, line 50
def templates_path
  self.class.templates_path
end

Private Instance Methods

download_file(path, txt: "file") click to toggle source
# File frameit/lib/frameit/frame_downloader.rb, line 71
def download_file(path, txt: "file")
  require 'uri'
  require 'excon'
  require 'addressable/uri'

  url = File.join(HOST_URL, Frameit.frames_version, Addressable::URI.encode(path))
  UI.message("Downloading #{txt} from '#{url}' ...")
  body = Excon.get(url).body
  raise body if body.include?("<Error>")
  return body
rescue => ex
  UI.error(ex)
  UI.user_error!("Error accessing URL '#{url}'")
end