Class: Omnibus::Compressor::DMG

Inherits:
Base show all
Defined in:
lib/omnibus/compressors/dmg.rb

Constant Summary

Constants included from Util

Util::SHELLOUT_OPTIONS

Constants included from NullArgumentable

NullArgumentable::NULL

Instance Attribute Summary

Attributes inherited from Base

#packager, #project

Attributes inherited from Packager::Base

#project

DSL methods collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods inherited from Packager::Base

build, #exclusions, id, #id, #initialize, #install_dir, #package_path, #resource_path, #resources_path, #run!, setup, #staging_dir

Methods included from Util

#copy_file, #create_directory, #create_file, #create_link, included, #remove_directory, #remove_file, #shellout, #shellout!, #windows_safe_path

Methods included from Templating

included, #render_template

Methods included from NullArgumentable

included, #null?

Methods included from Logging

included

Methods included from Digestable

#digest, #digest_directory

Constructor Details

This class inherits a constructor from Omnibus::Compressor::Base

Instance Method Details

#attach_dmgString

Attach the dmg, storing a reference to the device for later use.


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/omnibus/compressors/dmg.rb', line 157

def attach_dmg
  @device ||= Dir.chdir(staging_dir) do
    log.info(log_key) { "Attaching dmg as disk" }

    cmd = shellout! "hdiutil attach \\\\\n-readwrite \\\\\n-noverify \\\\\n-noautoopen \\\\\n\"\#{writable_dmg}\" | egrep '^/dev/' | sed 1q | awk '{print $1}'\n".gsub(/^ {10}/, '')

    cmd.stdout.strip
  end
end

#clean_disksvoid

This method returns an undefined value.

Cleans any previously left over mounted disks.

We are trying to detach disks that look like:

/dev/disk1s1 on /Volumes/chef (hfs, local, nodev, nosuid, read-only, noowners, quarantine, mounted by serdar)
/dev/disk2s1 on /Volumes/chef 1 (hfs, local, nodev, nosuid, read-only, noowners, quarantine, mounted by serdar)

118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/omnibus/compressors/dmg.rb', line 118

def clean_disks
  log.info(log_key) { "Cleaning previously mounted disks" }

  existing_disks = shellout!("mount | grep /Volumes/#{volume_name} | awk '{print $1}'")
  existing_disks.stdout.lines.each do |existing_disk|
    existing_disk.chomp!

    Omnibus.logger.debug(log_key) do
      "Detaching disk `#{existing_disk}' before starting dmg packaging."
    end

    shellout!("hdiutil detach '#{existing_disk}'")
  end
end

#compress_dmgvoid

This method returns an undefined value.

Compress the dmg using hdiutil and zlib.


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/omnibus/compressors/dmg.rb', line 238

def compress_dmg
  log.info(log_key) { "Compressing dmg" }

  Dir.chdir(staging_dir) do
    shellout! "chmod -Rf go-w /Volumes/\#{volume_name}\nsync\nhdiutil detach \"\#{@device}\"\nhdiutil convert \\\\\n\"\#{writable_dmg}\" \\\\\n-format UDZO \\\\\n-imagekey \\\\\nzlib-level=9 \\\\\n-o \"\#{package_path}\"\nrm -rf \"\#{writable_dmg}\"\n".gsub(/^ {10}/, '')
  end
end

#create_writable_dmgObject

Create a writable dmg we can put assets on.


136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/omnibus/compressors/dmg.rb', line 136

def create_writable_dmg
  log.info(log_key) { "Creating writable dmg" }

  shellout! "hdiutil create \\\\\n-srcfolder \"\#{resources_dir}\" \\\\\n-volname \"\#{volume_name}\" \\\\\n-fs HFS+ \\\\\n-fsargs \"-c c=64,a=16,e=16\" \\\\\n-format UDRW \\\\\n-size 512000k \\\\\n\"\#{writable_dmg}\"\n".gsub(/^ {8}/, '')
end

#package_nameObject


283
284
285
286
# File 'lib/omnibus/compressors/dmg.rb', line 283

def package_name
  extname = File.extname(packager.package_name)
  packager.package_name.sub(extname, '.dmg')
end

#pkg_position(val = NULL) ⇒ String

Set or return the starting x,y position where the .pkg file should live in the DMG window.

Examples:

pkg_position "535, 50"

86
87
88
89
90
91
92
# File 'lib/omnibus/compressors/dmg.rb', line 86

def pkg_position(val = NULL)
  if null?(val)
    @pkg_position || '535, 50'
  else
    @pkg_position = val
  end
end

#prettify_dmgvoid

This method returns an undefined value.

Use Applescript to setup the DMG with pretty logos and colors.


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/omnibus/compressors/dmg.rb', line 213

def prettify_dmg
  log.info(log_key) { "Making the dmg all pretty and stuff" }

  render_template(resource_path('create_dmg.osascript.erb'),
    destination: "#{staging_dir}/create_dmg.osascript",
    variables: {
      volume_name:   volume_name,
      pkg_name:      packager.package_name,
      window_bounds: window_bounds,
      pkg_position:  pkg_position,
    }
  )

  Dir.chdir(staging_dir) do
    shellout! "osascript \"\#{staging_dir}/create_dmg.osascript\"\n".gsub(/^ {10}/, '')
  end
end

#resources_dirString

The path where the MSI resources will live.


104
105
106
# File 'lib/omnibus/compressors/dmg.rb', line 104

def resources_dir
  File.expand_path("#{staging_dir}/Resources")
end

#set_dmg_iconvoid

This method returns an undefined value.

Set the dmg icon to our custom icon.


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/omnibus/compressors/dmg.rb', line 262

def set_dmg_icon
  log.info(log_key) { "Setting dmg icon" }

  Dir.chdir(staging_dir) do
    shellout! "# Convert the png to an icon\nsips -i \"\#{resource_path('icon.png')}\"\n\n# Extract the icon into its own resource\nDeRez -only icns \"\#{resource_path('icon.png')}\" > tmp.rsrc\n\n# Append the icon reosurce to the DMG\nRez -append tmp.rsrc -o \"\#{package_path}\"\n\n# Source the icon\nSetFile -a C \"\#{package_path}\"\n".gsub(/^ {10}/, '')
  end
end

#set_volume_iconvoid

This method returns an undefined value.

Create the icon for the volume using sips.


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/omnibus/compressors/dmg.rb', line 178

def set_volume_icon
  log.info(log_key) { "Setting volume icon" }

  icon = resource_path('icon.png')

  Dir.chdir(staging_dir) do
    shellout! "# Generate the icns\nmkdir tmp.iconset\nsips -z 16 16     \#{icon} --out tmp.iconset/icon_16x16.png\nsips -z 32 32     \#{icon} --out tmp.iconset/[email protected]\nsips -z 32 32     \#{icon} --out tmp.iconset/icon_32x32.png\nsips -z 64 64     \#{icon} --out tmp.iconset/[email protected]\nsips -z 128 128   \#{icon} --out tmp.iconset/icon_128x128.png\nsips -z 256 256   \#{icon} --out tmp.iconset/[email protected]\nsips -z 256 256   \#{icon} --out tmp.iconset/icon_256x256.png\nsips -z 512 512   \#{icon} --out tmp.iconset/[email protected]\nsips -z 512 512   \#{icon} --out tmp.iconset/icon_512x512.png\nsips -z 1024 1024 \#{icon} --out tmp.iconset/[email protected]\niconutil -c icns tmp.iconset\n\n# Copy it over\ncp tmp.icns \"/Volumes/\#{volume_name}/.VolumeIcon.icns\"\n\n# Source the icon\nSetFile -a C \"/Volumes/\#{volume_name}\"\n".gsub(/^ {10}/, '')
  end
end

#volume_nameString

The name of the volume to create. By defauly, this is the project's friendly name.


301
302
303
# File 'lib/omnibus/compressors/dmg.rb', line 301

def volume_name
  project.friendly_name
end

#window_bounds(val = NULL) ⇒ String

Set or return the starting x,y and ending x,y positions for the created DMG window.

Examples:

window_bounds "100, 100, 750, 600"

64
65
66
67
68
69
70
# File 'lib/omnibus/compressors/dmg.rb', line 64

def window_bounds(val = NULL)
  if null?(val)
    @window_bounds || '100, 100, 750, 600'
  else
    @window_bounds = val
  end
end

#writable_dmgString

The path to the writable dmg on disk.


291
292
293
# File 'lib/omnibus/compressors/dmg.rb', line 291

def writable_dmg
  File.expand_path("#{staging_dir}/#{project.name}-writable.dmg")
end