Class: Bundler::Downloadfile

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/downloadfile.rb

Constant Summary collapse

HELP =
<<~MULTI_LINE_STRING
  Commands/Subcommands/Options:
    bundle download help                     # Provide help by printing usage instructions
    bundle download usage                    # (alias for help)
    bundle download start                    # Start downloads for current operating system
    bundle download                          # (alias for start)
    bundle download --all-operating-systems  # Download files for all operating systems
    bundle download --keep-existing          # Do not redownload already downloaded files
    bundle download clear                    # Clear downloads by deleting them under app/gems
    bundle download clean                    # (alias for clear)
    bundle download list                     # List downloads by printing Downloadfile content for app/gems
    bundle download show                     # Show downloaded files for app/gems
MULTI_LINE_STRING
SUPPORTED_OPERATING_SYSTEMS =
%w[mac windows linux]
PLATFORM_OS =
SUPPORTED_OPERATING_SYSTEMS.detect {|os| OS.send("#{os}?")}
SUBCOMMANDS =
%w[start clear clean help usage list show]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, gem_path:, keep_existing: nil, all_operating_systems: nil) ⇒ Downloadfile

Returns a new instance of Downloadfile.



44
45
46
47
48
49
50
51
52
# File 'lib/bundler/downloadfile.rb', line 44

def initialize(file, gem_path:, keep_existing: nil, all_operating_systems: nil)
  @file = file
  @file_content = File.read(@file)
  @gem_path = gem_path
  @keep_existing = keep_existing
  @all_operating_systems = all_operating_systems
  @downloads = []
  interpret
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



24
25
26
# File 'lib/bundler/downloadfile.rb', line 24

def file
  @file
end

#file_contentObject (readonly)

Returns the value of attribute file_content.



24
25
26
# File 'lib/bundler/downloadfile.rb', line 24

def file_content
  @file_content
end

#gem_pathObject (readonly)

Returns the value of attribute gem_path.



24
25
26
# File 'lib/bundler/downloadfile.rb', line 24

def gem_path
  @gem_path
end

#keep_existingObject (readonly)

Returns the value of attribute keep_existing.



24
25
26
# File 'lib/bundler/downloadfile.rb', line 24

def keep_existing
  @keep_existing
end

Instance Method Details

#clearObject Also known as: clean



76
77
78
79
# File 'lib/bundler/downloadfile.rb', line 76

def clear
  puts "Clearing #{file}"
  @downloads.each(&:delete)
end

#download(uri, to: '', os: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bundler/downloadfile.rb', line 54

def download(uri, to:'', os: nil)
  directory = File.join(@gem_path, to)
  FileUtils.mkdir_p(directory)
  os = SUPPORTED_OPERATING_SYSTEMS.detect {|system| os.to_s.downcase == system}
  options = {os: os}
  options['--keep_existing'] = nil if @keep_existing
  @downloads << ::Download::Object.new(
    url: uri,
    path: directory,
    options: options
  )
end

#helpObject Also known as: usage



82
83
84
# File 'lib/bundler/downloadfile.rb', line 82

def help
  puts HELP
end

#interpretObject



67
68
69
# File 'lib/bundler/downloadfile.rb', line 67

def interpret
  instance_eval(@file_content)
end

#listObject



87
88
89
90
# File 'lib/bundler/downloadfile.rb', line 87

def list
  puts "Listing #{file}"
  puts @file_content
end

#showObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bundler/downloadfile.rb', line 92

def show
  puts "Showing downloaded files for #{file}"
  puts '- - -'
  missing_downloads = false
  missing_downloads_for_current_platform_os = false
  @downloads.each do |download|
    download_file_entry = download.file_path
    if File.exist?(download_file_entry)
      puts "Downloaded: #{File.size(download_file_entry)} #{download_file_entry}"
    else
      missing_downloads = true
      missing_downloads_for_current_platform_os = true if download.os == PLATFORM_OS
      puts "Not downloaded: #{download_file_entry}"
    end
  end
  message = ''
  if missing_downloads_for_current_platform_os
    message += "Downloads are missing for the current operating system (#{PLATFORM_OS}). \nRun `bundle download` to download missing files for current operating system (#{PLATFORM_OS}).\n"
  else
    message += "All downloads are present for current operating system (#{PLATFORM_OS})."
    message += " Optionally, run `bundle download --all-operating-systems` to download files for all operating systems.\n" if missing_downloads
  end
  puts message
  puts
end

#startObject



71
72
73
74
# File 'lib/bundler/downloadfile.rb', line 71

def start
  puts "Downloading #{file}"
  @downloads.select {|download| @all_operating_systems || download.os.nil? || download.os == PLATFORM_OS }.each(&:start)
end