Class: AsProject::AbstractRemoteFileTask

Inherits:
RemoteFileLoader show all
Includes:
Archive::Tar
Defined in:
lib/tasks/remote_file_task.rb

Overview

Concrete instances returned from User objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RemoteFileLoader

#fetch, #is_dmg?, #is_exe?, #is_gzip?, #is_swc?, #is_targz?, #is_zip?, #unpack_dmg, #unpack_downloaded_file, #unpack_targz, #unpack_zip

Constructor Details

#initialize(name, user) {|_self| ... } ⇒ AbstractRemoteFileTask

Returns a new instance of AbstractRemoteFileTask.

Yields:

  • (_self)

Yield Parameters:



81
82
83
84
85
86
# File 'lib/tasks/remote_file_task.rb', line 81

def initialize(name, user)
  @name = name
  @user = user
  yield self if block_given?
  define
end

Instance Attribute Details

#extracted_fileObject

Returns the value of attribute extracted_file.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def extracted_file
  @extracted_file
end

#mounted_pathObject

Returns the value of attribute mounted_path.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def mounted_path
  @mounted_path
end

#nameObject

Returns the value of attribute name.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def name
  @name
end

#urlObject

Returns the value of attribute url.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def url
  @url
end

Instance Method Details

#clean_path(path) ⇒ Object



153
154
155
156
157
158
# File 'lib/tasks/remote_file_task.rb', line 153

def clean_path(path)
  if(path.index(' '))
    return %{'#{path}'}
  end
  return path
end

#defineObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tasks/remote_file_task.rb', line 88

def define
  @uri = URI.parse(url)
  downloaded_file = downloaded_file_path
  file downloaded_file do |f|
    get_remote_file(@uri, downloaded_file)
    if(extracted_file_path == downloaded_file)
      File.chmod(0755, extracted_file_path)
    end
  end
  
  if(extracted_file_path != downloaded_file)
    if(!Rake::Task.task_defined?(extracted_file_path))
      file extracted_file_path => downloaded_file do |f|
        if(!File.exists?(extracted_file_path))
          unpack_downloaded_file(downloaded_file, extracted_file_path)
          File.chmod(0755, extracted_file_path)
        end
      end
    end
 end

  task @name => [extracted_file_path]
end

#downloaded_file_pathObject



116
117
118
119
120
# File 'lib/tasks/remote_file_task.rb', line 116

def downloaded_file_path
  parts = @uri.path.split('/')
  file = parts.pop
  return File.join(@user.downloads, @name.to_s, file)
end

#execute(params) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tasks/remote_file_task.rb', line 160

def execute(params)
 # Execute the system call with params
 # Sometimes DOS wants a 'run' prefix
 # Sometimes OSX wants a 'open' prefix
 # Sometimes Cygwin wants a './' prefix
 # This method should be overridden in
 # subclasses
 target = File.basename(extracted_file_path)
 if(!Logger.debug)
   puts("#{target} #{params}")
 end
 system("#{clean_path(extracted_file_path)} #{params}")
end

#extracted_file_pathObject



112
113
114
# File 'lib/tasks/remote_file_task.rb', line 112

def extracted_file_path
  return File.join(@user.downloads, @name.to_s, extracted_file)
end

#get_remote_file(uri, target) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/tasks/remote_file_task.rb', line 135

def get_remote_file(uri, target)
  if(!File.exists?(target))
    puts "Fetching #{uri.to_s} into #{target} now!"
    response = fetch(uri.to_s)
  
    File.makedirs(File.dirname(target))
    # Store the downloaded file on disk
    File.open(target, 'wb') do |f|
      f.write(response.body)
    end
  
    puts ""
    puts "Downloaded #{(response.body.size / 1024).to_s} KB"
    puts "to: #{target}"
    puts ""
  end
end

#make_downloaded_dir(file) ⇒ Object



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

def make_downloaded_dir(file)
  dir = File.dirname(file)
  File.makedirs(dir)
end

#remote_file_dirObject



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

def remote_file_dir
  return File.join(@user.downloads, @name.to_s)
end

#remote_locationObject



131
132
133
# File 'lib/tasks/remote_file_task.rb', line 131

def remote_location
  return File.join(remote_file_dir, extracted_file)
end