Class: U3d::MacInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/u3d/installer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install_pkg(file_path, version: nil, target_path: nil) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/u3d/installer.rb', line 313

def self.install_pkg(file_path, version: nil, target_path: nil)
  target_path ||= DEFAULT_MAC_INSTALL
  command = "installer -pkg #{file_path.shellescape} -target #{target_path.shellescape}"
  unity = Installer.create.installed.find { |u| u.version == version }
  if unity.nil?
    UI.verbose "No Unity install for version #{version} was found"
    U3dCore::CommandExecutor.execute(command: command, admin: true)
  else
    begin
      path = File.expand_path('..', unity.path)
      temp_path = File.join(target_path, 'Applications', 'Unity')
      move_to_temp = (temp_path != path)
      if move_to_temp
        UI.verbose "Temporary switching location of #{path} to #{temp_path} for installation purpose"
        FileUtils.mv path, temp_path
      end
      U3dCore::CommandExecutor.execute(command: command, admin: true)
    ensure
      FileUtils.mv temp_path, path if move_to_temp
    end
  end
rescue => e
  UI.error "Failed to install pkg at #{file_path}: #{e}"
else
  UI.success "Successfully installed package from #{file_path}"
end

Instance Method Details

#installedObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/u3d/installer.rb', line 295

def installed
  unless (`mdutil -s /` =~ /disabled/).nil?
    $stderr.puts 'Please enable Spotlight indexing for /Applications.'
    exit(1)
  end

  bundle_identifiers = ['com.unity3d.UnityEditor4.x', 'com.unity3d.UnityEditor5.x']

  mdfind_args = bundle_identifiers.map { |bi| "kMDItemCFBundleIdentifier == '#{bi}'" }.join(' || ')

  cmd = "mdfind \"#{mdfind_args}\" 2>/dev/null"
  UI.verbose cmd
  versions = `#{cmd}`.split("\n").map { |path| MacInstallation.new(path: path) }

  # sorting should take into account stable/patch etc
  versions.sort! { |x, y| x.version <=> y.version }
end

#sanitize_install(unity) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/u3d/installer.rb', line 281

def sanitize_install(unity)
  source_path = File.expand_path('..', unity.path)
  parent = File.expand_path('..', source_path)
  new_path = File.join(parent, UNITY_DIR % unity.version)
  UI.important "Moving #{source_path} to #{new_path}..."
  source_path = "\"#{source_path}\"" if source_path =~ / /
  new_path = "\"#{new_path}\"" if new_path =~ / /
  U3dCore::CommandExecutor.execute(command: "mv #{source_path} #{new_path}", admin: true)
rescue => e
  UI.error "Unable to move #{source_path} to #{new_path}: #{e}"
else
  UI.success "Successfully moved #{source_path} to #{new_path}"
end