Skip to content

Instantly share code, notes, and snippets.

@Sauraus
Last active November 3, 2015 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sauraus/6b699f7726957bb8f577 to your computer and use it in GitHub Desktop.
Save Sauraus/6b699f7726957bb8f577 to your computer and use it in GitHub Desktop.
Unity cookbook
#
# Cookbook Name:: unity
# Library:: editor
#
# Copyright (C) 2015 Disney Consumer Products Interactive
#
# All rights reserved - Do Not Redistribute
#
require 'chef/resource'
require 'chef/provider'
module Unity
module Resources
module UnityEditor
# A 'unity_editor' resource to install and uninstall Unity Editor.
class Resource < Chef::Resource::LWRPBase
self.resource_name = :unity_editor
provides(:unity_editor)
actions(:install, :uninstall)
default_action(:install)
attribute(:version, kind_of: String, name_attribute: true)
attribute(:url, kind_of: String, required: true)
attribute(:checksum, kind_of: String, required: true)
attribute(:app, kind_of: String, required: true) if Chef.node.platform_family?('mac_os_x')
attribute(:install_root, kind_of: String, required: true, default: (Chef.node.platform_family?('mac_os_x')) ? '/Applications' : 'c:\unity')
attribute(:force, kind_of: [TrueClass, FalseClass], default: false)
end
class Provider < Chef::Provider::LWRPBase
provides(:unity_editor)
use_inline_resources
def action_install
return if exists?
case Chef.node.platform_family
when 'mac_os_x'
install_mac_os_x
when 'windows'
install_windows
else
raise "Unity Editor LWRP only support OSX & Windows"
end
end
def action_uninstall
if exists?
directory install_dir do
recursive true
action :delete
end
end
end
def install_mac_os_x
# Remove folder if it exists and we force the install.
directory install_dir do
recursive true
action :delete
end if new_resource.force && exists?
# Conditional to test whether Unity version is 5.x.x or greater
# If not, use "dmg" cookbook to handle the .dmg file
# Else use "remote_file" to download PKG file and "execute" to install
if new_resource.url.end_with?('dmg')
dmg_package "Unity3D" do
app new_resource.app
source new_resource.url
checksum new_resource.checksum
volumes_dir 'Unity Installer'
type 'pkg'
action :install
end
elsif new_resource.url.end_with?('pkg')
pkg_file = ::File.join(Chef::Config[:file_cache_path], "#{new_resource.app}.pkg")
remote_file pkg_file do
source new_resource.url
checksum new_resource.checksum
end
execute "installer -pkg '#{pkg_file}' -target /" do
user 'root'
end
end
# Default installs /Applications/Unity, so move it in case we want multiple installs
execute "mv -f /Applications/Unity #{install_dir}" do
only_if { Dir.exist?('/Applications/Unity') }
end
end
def install_windows
# Remove folder if it exists and we force the install.
directory install_dir do
recursive true
action :delete
end if new_resource.force && exists?
registry_key 'HKCU\Software\Unity Technologies' do
recursive true
action :delete_key
end
directory "\"c:\\Program Files (x86)\\Unity\"" do
recursive true
action :delete
end
directory "\"c:\\Program Files\\Unity\"" do
recursive true
action :delete
end
# Unity installer will ignore the /D option if Unity was already installed on the system before
windows_package 'Unity' do
source new_resource.url
checksum new_resource.checksum
version new_resource.version
options '/S /AllUsers'
installer_type :custom
action :install
end
directory install_dir do
recursive true
action :create
end
# NOTE: Unity side2side installs are complicated
if new_resource.version[1].eql?('4')
execute "Moving Unity #{new_resource.version} to #{install_dir}" do
command "move \"c:\\Program Files (x86)\\Unity\\Editor\" #{install_dir}"
end
elsif new_resource.version[1].eql?('5')
execute "Moving Unity #{new_resource.version} to #{install_dir}" do
command "move \"c:\\Program Files\\Unity\\Editor\" #{install_dir}"
end
end
end
def install_dir
::File.join(new_resource.install_root, "Unity_#{new_resource.version}")
end
def exists?
new_resource.force ? false : Dir.exists?(Chef.node.platform_family?('mac_os_x') ? install_dir : ::File.join(install_dir , 'Editor'))
end
end
end
end
end
platform = node['platform_family']
(%w(windows mac_os_x).include?(platform)) ?
unity_versions = data_bag(node['unity']['editor'][platform]['databag']) :
(raise "Unsupported platform [#{platform}] for Unity Editor install")
unity_versions.each do |version|
unity = data_bag_item(node['unity']['editor'][platform]['databag'], version)
unity_editor unity['id'] do
url unity['url']
checksum unity['checksum']
app unity['app'] if platform.eql?('mac_os_x')
install_root node['unity']['editor'][platform]['install_root']
action unity['action']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment