class U3d::MacInstaller

Public Instance Methods

install(file_path, version, installation_path: nil, info: nil) click to toggle source
# File lib/u3d/installer.rb, line 191
def install(file_path, version, installation_path: nil, info: nil)
  # rubocop:enable UnusedMethodArgument
  extension = File.extname(file_path)
  raise "Installation of #{extension} files is not supported on Mac" unless %w[.zip .po .pkg .dmg].include? extension
  path = installation_path || DEFAULT_MAC_INSTALL
  if extension == '.po'
    install_po(file_path, version, info: info)
  elsif extension == '.zip'
    install_zip(file_path, version, info: info)
  elsif extension == '.dmg'
    UI.important "Skipping installation of #{file_path} for now"
  else
    install_pkg(file_path, version: version, target_path: path)
  end
end
install_pkg(file_path, version: nil, target_path: nil) click to toggle source
# File lib/u3d/installer.rb, line 207
def 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 = installed.find { |u| u.version == version }
  temp_path = File.join(target_path, 'Applications', 'Unity')
  if unity.nil?
    UI.verbose "No Unity install for version #{version} was found"
    U3dCore::CommandExecutor.execute(command: command, admin: true)
    destination_path = File.join(target_path, 'Applications', format(UNITY_DIR, version: version))
    FileUtils.mv temp_path, destination_path
  else
    UI.verbose "Unity install for version #{version} found under #{unity.root_path}"
    begin
      path = unity.root_path
      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 StandardError => e
  UI.error "Failed to install pkg at #{file_path}: #{e}"
else
  UI.success "Successfully installed package from #{file_path}"
end
installed() click to toggle source
# File lib/u3d/installer.rb, line 186
def installed
  paths = (list_installed_paths + spotlight_installed_paths).uniq
  paths.map { |path| MacInstallation.new(root_path: path) }
end
sanitize_install(unity, long: false, dry_run: false) click to toggle source
# File lib/u3d/installer.rb, line 175
def sanitize_install(unity, long: false, dry_run: false)
  source_path = unity.root_path
  parent = File.expand_path('..', source_path)
  dir_name = format(long ? UNITY_DIR_LONG : UNITY_DIR,
                    version: unity.version, build_number: unity.build_number)
  new_path = File.join(parent, dir_name)

  moved = U3dCore::AdminTools.move_os_file(:mac, source_path, new_path, dry_run: dry_run)
  unity.root_path = new_path if moved && !dry_run
end
uninstall(unity: nil) click to toggle source
# File lib/u3d/installer.rb, line 237
def uninstall(unity: nil)
  UI.verbose("Uninstalling Unity at '#{unity.root_path}'...")
  command = "rm -r #{unity.root_path.argescape}"
  U3dCore::CommandExecutor.execute(command: command, admin: true)
rescue StandardError => e
  UI.error "Failed to uninstall unity at #{unity.path}: #{e}"
else
  UI.success "Successfully uninstalled '#{unity.root_path}'"
end

Private Instance Methods

list_installed_paths() click to toggle source
# File lib/u3d/installer.rb, line 249
def list_installed_paths
  paths = find_installations_with_path(
    default_root_path: DEFAULT_MAC_INSTALL,
    postfix: %w[
      Applications
      Unity*
      Unity.app
    ]
  ) { |u| Pathname.new(u).parent.to_s }
  UI.verbose "Found list_installed_paths: #{paths}"
  paths
end
spotlight_installed_paths() click to toggle source
# File lib/u3d/installer.rb, line 262
def spotlight_installed_paths
  unless (`mdutil -s /` =~ /disabled/).nil?
    UI.important 'Please enable Spotlight indexing for /Applications.'
    return []
  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
  paths = `#{cmd}`.split("\n")
  paths = paths.map { |u| Pathname.new(u).parent.to_s }
  UI.verbose "Found spotlight_installed_paths: #{paths}"
  paths
end