Class: Supply::Setup

Inherits:
Object
  • Object
show all
Defined in:
supply/lib/supply/setup.rb

Instance Method Summary collapse

Instance Method Details

#download_images(listing) ⇒ Object



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
# File 'supply/lib/supply/setup.rb', line 49

def download_images(listing)
  UI.message("🖼️  Downloading images (#{listing.language})")

  require 'net/http'

  allowed_imagetypes = [Supply::IMAGES_TYPES, Supply::SCREENSHOT_TYPES].flatten

  allowed_imagetypes.each do |image_type|
    begin
      path = File.join(, listing.language, IMAGES_FOLDER_NAME, image_type)

      p = Pathname.new(path)
      if IMAGES_TYPES.include?(image_type) # IMAGE_TYPES are stored in locale/images location
        FileUtils.mkdir_p(p.dirname.to_s)
      else # SCREENSHOT_TYPES go under their respective folders.
        FileUtils.mkdir_p(p.to_s)
      end

      UI.message("Downloading `#{image_type}` for #{listing.language}...")

      urls = client.fetch_images(image_type: image_type, language: listing.language)
      next if urls.nil? || urls.empty?

      image_counter = 1 # Used to prefix the downloaded files, so order is preserved.
      urls.each do |url|
        if IMAGES_TYPES.include?(image_type) # IMAGE_TYPES are stored in locale/images
          file_path = "#{path}.#{FastImage.type(url)}"
        else # SCREENSHOT_TYPES are stored in locale/images/<screensho_types>
          file_path = File.join(path, "#{image_counter}_#{listing.language}.#{FastImage.type(url)}")
        end

        File.binwrite(file_path, Net::HTTP.get(URI.parse(url)))

        UI.message("\tDownloaded - #{file_path}")

        image_counter += 1
      end
    rescue => ex
      UI.error(ex.to_s)
      UI.error("Error downloading '#{image_type}' for #{listing.language}...")
    end
  end
end

#perform_downloadObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'supply/lib/supply/setup.rb', line 3

def perform_download
  UI.message("🕗  Downloading metadata, images, screenshots...")

  if File.exist?()
    UI.important("Metadata already exists at path '#{}'")
    return
  end

  client.begin_edit(package_name: Supply.config[:package_name])

  client.listings.each do |listing|
    (listing)
    download_images(listing)
  end

  if Supply.config[:version_name].to_s == ""
    latest_version = client.latest_version(Supply.config[:track])
    if latest_version
      Supply.config[:version_name] = latest_version.name
    else
      UI.user_error!("Could not find the latest version to download metadata, images, and screenshots from")
    end
  end

  client.release_listings(Supply.config[:version_name]).each do |release_listing|
    store_release_listing(release_listing)
  end

  client.abort_current_edit

  UI.success("✅  Successfully stored metadata in '#{}'")
end

#store_metadata(listing) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'supply/lib/supply/setup.rb', line 36

def (listing)
  UI.message("📝  Downloading metadata (#{listing.language})")

  containing = File.join(, listing.language)
  FileUtils.mkdir_p(containing)

  Supply::AVAILABLE_METADATA_FIELDS.each do |key|
    path = File.join(containing, "#{key}.txt")
    UI.message("Writing to #{path}...")
    File.open(path, 'w:UTF-8') { |file| file.write(listing.send(key)) }
  end
end

#store_release_listing(release_listing) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'supply/lib/supply/setup.rb', line 93

def store_release_listing(release_listing)
  UI.message("🔨  Downloading changelogs (#{release_listing.language}, #{release_listing.version})")

  containing = File.join(, release_listing.language, CHANGELOGS_FOLDER_NAME)
  unless File.exist?(containing)
    FileUtils.mkdir_p(containing)
  end

  path = File.join(containing, "#{release_listing.version}.txt")
  UI.message("Writing to #{path}...")
  File.write(path, release_listing.release_notes)
end