diff --git a/Gemfile.lock b/Gemfile.lock index f106db58..8e1a90c5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,6 +9,7 @@ PATH inifile (>= 3.0.0, < 4.0.0) plist (>= 3.1.0, < 4.0.0) security (= 0.1.3) + win32-shortcut (>= 0.3.0) GEM remote: https://rubygems.org/ @@ -116,6 +117,7 @@ GEM tzinfo (1.2.3) thread_safe (~> 0.1) unicode-display_width (1.3.0) + win32-shortcut (0.3.0) PLATFORMS ruby diff --git a/lib/u3d/commands.rb b/lib/u3d/commands.rb index 63ded382..6a1be532 100644 --- a/lib/u3d/commands.rb +++ b/lib/u3d/commands.rb @@ -62,6 +62,27 @@ def list_installed(options: {}) end end + def create_shortcuts(options: {}) + installer = Installer.create + installer.sanitize_installs + list = installer.installed_sorted_by_versions + + if list.empty? + UI.important 'No Unity version installed' + return + end + + ver = options[:unity_version] + unless ver.nil? + UI.user_error! "Version #{ver} is not installed!" unless list.any? { |u| u.version == ver } + list = [ver] + end + + list.each do |u| + u.create_shortcut(options[:target_directory]) + end + end + # rubocop:disable Style/FormatStringToken def console require 'irb' diff --git a/lib/u3d/commands_generator.rb b/lib/u3d/commands_generator.rb index 524d8217..590b520c 100644 --- a/lib/u3d/commands_generator.rb +++ b/lib/u3d/commands_generator.rb @@ -110,6 +110,18 @@ def run end end + command :shortcuts do |c| + c.syntax = 'u3d shortcuts [-u | --unity_version ] [-t | --target ]' + c.option '-u', '--unity_version STRING', String, 'Creates a shortcut for this version of Unity only.' + c.option '-d', '--directory STRING', String, 'Specifies which directory to create the shortcuts in' + c.example 'Create a shortcut for all installed versions on the Desktop', 'u3d shortcuts -d ~/Desktop' + c.example 'Create a shortcut for Unity 5.6.0f3', 'u3d shortcuts -u 5.6.0f3' + c.summary = 'Create shortcuts for installed versions of Unity' + c.action do |_args, options| + Commands.create_shortcuts(options: convert_options(options)) + end + end + command :available do |c| oses = U3dCore::Helper.operating_systems c.syntax = 'u3d available [-r | --release_level ] [-o | --operating_system ] [-u | --unity_version ] [-p | --packages] [-f | --force]' diff --git a/lib/u3d/installation.rb b/lib/u3d/installation.rb index bc32e271..a8250569 100644 --- a/lib/u3d/installation.rb +++ b/lib/u3d/installation.rb @@ -305,6 +305,30 @@ def build_number(exe_path) nil end + def create_shortcut(path_to_unity_exe, unity_version, target_directory) + require 'win32-shortcut' + full_exe_path = File.expand_path(path_to_unity_exe) + lnk_parent = if target_directory.nil? + File.expand_path('..', full_exe_path) + else + File.expand_path(target_directory) + end + + lnk_name = "Unity_#{unity_version}.lnk" + lnk_path = File.join(lnk_parent, lnk_name) + + if File.exist? lnk_path + UI.message "Shortcut already exists at #{lnk_path}" + return Win32::Shortcut.open(lnk_path) + end + + shortcut = Win32::Shortcut.new(lnk_path) do |s| + s.description = "Shortcut to Unity.exe for Unity #{unity_version}. Automatically created by u3d." + s.path = full_exe_path + end + shortcut.tap { |s| UI.success "Created shortcut for #{s.path} at #{s.file}" } + end + private def string_file_info(info, path) @@ -408,5 +432,9 @@ def module_name_pattern(module_name) def clean_install? do_not_move? || !(root_path =~ UNITY_DIR_CHECK).nil? end + + def create_shortcut(target_directory) + WindowsInstallationHelper.new.create_shortcut(exe_path, version, target_directory) + end end end diff --git a/u3d.gemspec b/u3d.gemspec index c7269377..8df8d4f8 100644 --- a/u3d.gemspec +++ b/u3d.gemspec @@ -30,6 +30,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'inifile', '>= 3.0.0', '< 4.0.0' # Parses INI files spec.add_dependency 'plist', '>= 3.1.0', '< 4.0.0' # Generate the Xcode config plist file spec.add_dependency 'security', '= 0.1.3' # macOS Keychain manager, a dead project, no updates expected + spec.add_dependency 'win32-shortcut', '>= 0.3.0' # Development only spec.add_development_dependency "bundler", "~> 1.13" spec.add_development_dependency "coveralls"