From 8aa01cb3126442a1147c1be21be2dd9b8572781a Mon Sep 17 00:00:00 2001 From: Paul Niezborala Date: Wed, 5 Dec 2018 15:02:39 +0100 Subject: [PATCH 1/7] Install win32-shortcut gem --- Gemfile.lock | 2 ++ u3d.gemspec | 1 + 2 files changed, 3 insertions(+) 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/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" From 681622df2ff46bb5f42243d5b5a1cb592f2e9dba Mon Sep 17 00:00:00 2001 From: Paul Niezborala Date: Wed, 5 Dec 2018 15:59:26 +0100 Subject: [PATCH 2/7] Add ability to create shortcut on windows --- lib/u3d/commands.rb | 22 ++++++++++++++++++++++ lib/u3d/commands_generator.rb | 12 ++++++++++++ lib/u3d/installation.rb | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/lib/u3d/commands.rb b/lib/u3d/commands.rb index 63ded382..d9a56b35 100644 --- a/lib/u3d/commands.rb +++ b/lib/u3d/commands.rb @@ -62,6 +62,28 @@ 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| + s = u.create_shortcut(options[:target_directory]) + UI.success "Created shortcut for #{s.path} at #{s.file}" + 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..882faf77 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 '-t', '--target_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 -t ~/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..df365a10 100644 --- a/lib/u3d/installation.rb +++ b/lib/u3d/installation.rb @@ -304,6 +304,24 @@ def build_number(exe_path) end 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? then + 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) + + Win32::Shortcut.new(lnk_path) do |shortcut| + shortcut.description = "Shortcut to Unity.exe for Unity #{unity_version}. Automatically created by u3d." + shortcut.path = full_exe_path + end + end private @@ -408,5 +426,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 From e5a1e3e3c9fa3ada14adddd1a87d0235c8cfcf8f Mon Sep 17 00:00:00 2001 From: Paul Niezborala Date: Wed, 5 Dec 2018 16:00:27 +0100 Subject: [PATCH 3/7] Rubocop compliance --- lib/u3d/commands.rb | 2 +- lib/u3d/installation.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/u3d/commands.rb b/lib/u3d/commands.rb index d9a56b35..84aa7c53 100644 --- a/lib/u3d/commands.rb +++ b/lib/u3d/commands.rb @@ -66,7 +66,7 @@ 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 diff --git a/lib/u3d/installation.rb b/lib/u3d/installation.rb index df365a10..57ec3a54 100644 --- a/lib/u3d/installation.rb +++ b/lib/u3d/installation.rb @@ -304,14 +304,14 @@ def build_number(exe_path) end 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? then - File.expand_path('..', full_exe_path) - else - File.expand_path(target_directory) + 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" From b21064f97c159a84013bcccde6c501cf6a79fc30 Mon Sep 17 00:00:00 2001 From: Paul Niezborala Date: Wed, 5 Dec 2018 16:03:29 +0100 Subject: [PATCH 4/7] Change target_directory option to -d/--directory as -t was already taken by --trace --- lib/u3d/commands_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/u3d/commands_generator.rb b/lib/u3d/commands_generator.rb index 882faf77..590b520c 100644 --- a/lib/u3d/commands_generator.rb +++ b/lib/u3d/commands_generator.rb @@ -113,8 +113,8 @@ def run 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 '-t', '--target_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 -t ~/Desktop' + 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| From ed3dce4ef8f5604965431521ac094dc5fa535144 Mon Sep 17 00:00:00 2001 From: Paul Niezborala Date: Wed, 5 Dec 2018 16:10:06 +0100 Subject: [PATCH 5/7] Do not create shortcut if it already exists --- lib/u3d/installation.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/u3d/installation.rb b/lib/u3d/installation.rb index 57ec3a54..30b35781 100644 --- a/lib/u3d/installation.rb +++ b/lib/u3d/installation.rb @@ -317,6 +317,11 @@ def create_shortcut(path_to_unity_exe, unity_version, target_directory) 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 + Win32::Shortcut.new(lnk_path) do |shortcut| shortcut.description = "Shortcut to Unity.exe for Unity #{unity_version}. Automatically created by u3d." shortcut.path = full_exe_path From b360ae884f5a12bf7c4d466816dbf2dd2aad086d Mon Sep 17 00:00:00 2001 From: Paul Niezborala Date: Wed, 5 Dec 2018 16:18:27 +0100 Subject: [PATCH 6/7] Move success message to shortcut creation --- lib/u3d/commands.rb | 3 +-- lib/u3d/installation.rb | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/u3d/commands.rb b/lib/u3d/commands.rb index 84aa7c53..6a1be532 100644 --- a/lib/u3d/commands.rb +++ b/lib/u3d/commands.rb @@ -79,8 +79,7 @@ def create_shortcuts(options: {}) end list.each do |u| - s = u.create_shortcut(options[:target_directory]) - UI.success "Created shortcut for #{s.path} at #{s.file}" + u.create_shortcut(options[:target_directory]) end end diff --git a/lib/u3d/installation.rb b/lib/u3d/installation.rb index 30b35781..cd285495 100644 --- a/lib/u3d/installation.rb +++ b/lib/u3d/installation.rb @@ -322,10 +322,11 @@ def create_shortcut(path_to_unity_exe, unity_version, target_directory) return Win32::Shortcut.open(lnk_path) end - Win32::Shortcut.new(lnk_path) do |shortcut| - shortcut.description = "Shortcut to Unity.exe for Unity #{unity_version}. Automatically created by u3d." - shortcut.path = full_exe_path + 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 From ea304c2fed6df16754f017a81d5276295508ef1b Mon Sep 17 00:00:00 2001 From: Paul Niezborala Date: Wed, 5 Dec 2018 18:04:40 +0100 Subject: [PATCH 7/7] More rubocop compliance --- lib/u3d/installation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/u3d/installation.rb b/lib/u3d/installation.rb index cd285495..a8250569 100644 --- a/lib/u3d/installation.rb +++ b/lib/u3d/installation.rb @@ -312,7 +312,7 @@ def create_shortcut(path_to_unity_exe, unity_version, target_directory) File.expand_path('..', full_exe_path) else File.expand_path(target_directory) - end + end lnk_name = "Unity_#{unity_version}.lnk" lnk_path = File.join(lnk_parent, lnk_name)