Class: AsProject::RemoteFileLoader

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/tasks/remote_file_loader.rb

Direct Known Subclasses

AbstractRemoteFileTask, RemoteLibraryTask

Instance Method Summary collapse

Instance Method Details

#fetch(location, limit = 10) ⇒ Object

Raises:



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
35
36
37
38
39
40
41
42
# File 'lib/tasks/remote_file_loader.rb', line 5

def fetch(location, limit = 10)
  uri = URI.parse(location)

  # Download the file now to the downloads dir
  # If the file is an archive (zip, gz, tar, tar.gz, dmg), extract to
  # AsProject/remote_files/@name/@location
  # Check the location again...
  raise UsageError.new("The RemoteFileTask can only handle HTTP requests at this time, it seems you were trying: #{uri.scheme}") if uri.scheme != 'http'
  raise UsageError.new('HTTP redirect too deep') if limit == 0
  
  response = nil
  t = Thread.new {
    response = Net::HTTP.get_response(uri.host, uri.path)
  }

  print(">> Downloading")
  # TODO: Add actual bytesLoaded vs bytesTotal
  # To the output...
  while t.alive?
    print(".")
    $stdout.flush
    sleep(0.2)
  end
  puts ""
  
  if(response.is_a? Net::HTTPSuccess)
    return response
  elsif(response.is_a? Net::HTTPRedirection)
    return fetch(response['location'], limit - 1)
  else
    if(response.nil?)
      raise UsageError.new("Network connection failed!")
    else
      puts response.to_s
      return response.error!
    end
  end
end

#is_dmg?(file) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/tasks/remote_file_loader.rb', line 140

def is_dmg?(file)
  return (file.split('.').pop == 'dmg')
end

#is_exe?(file) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/tasks/remote_file_loader.rb', line 119

def is_exe?(file)
  return (file.split('.').pop == 'exe')
end

#is_gzip?(file) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/tasks/remote_file_loader.rb', line 132

def is_gzip?(file)
  return (file.split('.').pop == 'gz')
end

#is_swc?(file) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/tasks/remote_file_loader.rb', line 136

def is_swc?(file)
  return (file.split('.').pop == 'swc')
end

#is_targz?(file) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/tasks/remote_file_loader.rb', line 127

def is_targz?(file)
  parts = file.split('.')
  return (parts.pop == 'gz' && parts.pop == 'tar')
end

#is_zip?(file) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/tasks/remote_file_loader.rb', line 123

def is_zip?(file)
  return (file.split('.').pop == 'zip')
end

#unpack_dmg(dmg_file, dir) ⇒ Object

This is actually not unpacking the FlashPlayer Binary file as expected… OSX is treated the player binary as if it is a regular text file, but if it is copied manually, the command works fine!?



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
115
116
117
# File 'lib/tasks/remote_file_loader.rb', line 81

def unpack_dmg(dmg_file, dir)
  # 1) Mount the dmg in place
  # 2) Recursively Copy it's contents to asproject_home
  # 3) Unmount the dmg
  if(mounted_path.nil?)
    raise StandardError.new('DMG file downloaded, but the RemoteFileTask needs a mounted_path in order to mount it')
  end

  if(!File.exists?(full_mounted_path))
    system("hdiutil mount #{dmg_file}")
  end
  
  begin
    mounted_target = File.join(full_mounted_path, extracted_file)
  
    # Copy the DMG contents using system copy rather than ruby utils
    # Because OS X does something special with .app files that the
    # Ruby FileUtils and File classes break...
    puts '=================='
    from = mounted_target
#        from = File.join(full_mounted_path, extracted_file)
    to = File.join(@user.downloads, @name.to_s, extracted_file)
    puts 'from: ' + from
    puts 'to: ' + to
    FileUtils.makedirs(File.dirname(to))
    
    if(File.exists?(from))
      `ditto '#{from}' '#{to}'`
    end
  rescue
    puts 'inside finally!'
    if(File.exists?(full_mounted_path))
      puts 'unmounting now!'
      system("hdiutil unmount -force \"#{full_mounted_path}\"")
    end
  end
end

#unpack_downloaded_file(file_name, expected_file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tasks/remote_file_loader.rb', line 44

def unpack_downloaded_file(file_name, expected_file)
  dir = File.dirname(file_name)
  if(!File.exists?(expected_file))
    if(is_zip?(file_name))
      unpack_zip(file_name, dir)
    elsif(is_targz?(file_name))
      unpack_targz(file_name, dir)
    elsif(is_dmg?(file_name))
      unpack_dmg(file_name, dir)
    elsif(is_swc?(file_name))
      # just copy the swc...
    elsif(!is_exe?(file_name))
      raise UsageError.new("RemoteFileTask does not know how to unpack files of type: #{file_name}")
    end
  end
end

#unpack_targz(tgz_file, dir) ⇒ Object



71
72
73
74
# File 'lib/tasks/remote_file_loader.rb', line 71

def unpack_targz(tgz_file, dir)
  tar = Zlib::GzipReader.new(File.open(tgz_file, 'rb'))
  Minitar.unpack(tar, dir)
end

#unpack_zip(zip_file, dir) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/tasks/remote_file_loader.rb', line 61

def unpack_zip(zip_file, dir)
  Zip::ZipFile::open(zip_file) do |zf|
     zf.each do |e|
       fpath = File.join(dir, e.name)
       FileUtils.mkdir_p(File.dirname(fpath))
       zf.extract(e, fpath)
     end
   end
end