Class: AndParcel::PackageRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/andparcel/parcel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ PackageRequest

Returns a new instance of PackageRequest.



92
93
94
95
# File 'lib/andparcel/parcel.rb', line 92

def initialize(opts)
  @opts=opts
  @tmps=[]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



90
91
92
# File 'lib/andparcel/parcel.rb', line 90

def config
  @config
end

#optsObject (readonly)

Returns the value of attribute opts.



90
91
92
# File 'lib/andparcel/parcel.rb', line 90

def opts
  @opts
end

#tmpsObject (readonly)

Returns the value of attribute tmps.



90
91
92
# File 'lib/andparcel/parcel.rb', line 90

def tmps
  @tmps
end

Instance Method Details

#assets(zf) ⇒ Object

For each asset to go into the ZIP file, add it



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/andparcel/parcel.rb', line 176

def assets(zf)
  if opts[:assets]
    files=AndParcel.glob(opts[:assets], opts[:project])
      
    files.each do |fr|
      zf.add(File.join('assets', fr.relative), fr.local) if File.exists?(fr.local)
    end
  end
  
  true
end

#docs(zf) ⇒ Object

For each documentation file to go into the ZIP file, add it



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/andparcel/parcel.rb', line 150

def docs(zf)
  if opts[:docs]
    files=AndParcel.glob(opts[:docs], opts[:project])
    
    files.each do |fr|
      zf.add(File.join('docs', fr.relative), fr.local)
    end
  end
  
  true
end

#libs(zf) ⇒ Object

For each JAR to go into the ZIP file, add it



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/andparcel/parcel.rb', line 137

def libs(zf)
  if opts[:jars]
    files=AndParcel.glob(opts[:jars], opts[:project])
    
    files.each do |fr|
      zf.add(File.join('libs', File.basename(fr.local)), fr.local)
    end
  end
  
  true
end

#manifest(zf) ⇒ Object

Scan the manifest template and grab all children of the <application> element



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/andparcel/parcel.rb', line 190

def manifest(zf)
  if opts[:manifest]
    doc=Nokogiri::XML(open(File.join(opts[:project], opts[:manifest])))
    out=Nokogiri::XML('<manifest-merge xmlns:android="http://schemas.android.com/apk/res/android"/>')
    
    sdk=doc.search('uses-sdk').first
    
    if sdk
      config['minSdkVersion']=sdk['minSdkVersion']
    end
  
    doc.search('application').children.each do |node|
      if node.element?
        out.root.add_child(node.to_s)
      end
    end
    
    store_stream(zf, 'ManifestMerge.xml', out.to_s)
  end
  
  true
end

#package(pkg_info) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/andparcel/parcel.rb', line 97

def package(pkg_info)
  @config=JSON.parse(open(File.join(opts[:project], pkg_info)) {|f| f.read})
  @config['parcel-version']=AndParcel.version

  dir=opts[:dir] || '.'
  
  FileUtils.mkdir_p(dir) if !File.exists?(dir)
  
  target=File.join(dir, config['name']+'_'+config['version']+'.zip')
  
  File.delete(target) if File.exists?(target)
  
  begin
    Zip::ZipFile.open(target, Zip::ZipFile::CREATE) do |zf|
      save_config(zf) if
        manifest(zf) if
          assets(zf) if
            resources(zf) if
              docs(zf) if
                libs(zf)
    end
    
    target
  rescue
    File.delete(target) if target && File.exists?(target)
    raise
  ensure
    tmps.each {|f| f.delete}
  end
end

#resources(zf) ⇒ Object

For each resource to go into the ZIP file, add it



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/andparcel/parcel.rb', line 163

def resources(zf)
  if opts[:res]
    files=AndParcel.glob(opts[:res], opts[:project])
      
    files.each do |fr|
      zf.add(File.join('res', fr.relative), fr.local)
    end
  end
  
  true
end

#save_config(zf) ⇒ Object

Take the configuration hash, convert to JSON, and put it in the ZIP file as config.json.



130
131
132
133
134
# File 'lib/andparcel/parcel.rb', line 130

def save_config(zf)
  store_stream(zf, 'parcel.json', config.to_json)
  
  true
end

#store_stream(zf, path, text) ⇒ Object

Helper method to take a hunk of text and a path and put in the ZIP file. The ZIP file gem being used does not appear to support putting in-memory text into arbitrary paths in the ZIP file. Hence, we write the text out to a temporary file, put that in the ZIP file, and make note to delete this temporary file sometime later.



220
221
222
223
224
225
226
# File 'lib/andparcel/parcel.rb', line 220

def store_stream(zf, path, text)
  tmp=Tempfile.new('parcel')
  tmp.puts text
  tmp.close
  zf.add(path, tmp.path)
  tmps << tmp
end