Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] u3d/installation: add feature to create shortcuts #336

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/u3d/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 12 additions & 0 deletions lib/u3d/commands_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ def run
end
end

command :shortcuts do |c|
c.syntax = 'u3d shortcuts [-u | --unity_version <version>] [-t | --target <folder>]'
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems optional. It should document where it creates the screenshot when it isn't passed.

c.example 'Create a shortcut for all installed versions on the Desktop', 'u3d shortcuts -d ~/Desktop'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it risky to create shortcuts for all versions without specifying --all?

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 <level>] [-o | --operating_system <OS>] [-u | --unity_version <version>] [-p | --packages] [-f | --force]'
Expand Down
28 changes: 28 additions & 0 deletions lib/u3d/installation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird implementation of a default no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be the desktop? In which case, should it be passed to the function (and let the caller define it).

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we open it here?

end

shortcut = Win32::Shortcut.new(lnk_path) do |s|
s.description = "Shortcut to Unity.exe for Unity #{unity_version}. Automatically created by u3d."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to remove shortcuts when uninstalling?

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)
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions u3d.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see overall comment related to platform specific dependency.

# Development only
spec.add_development_dependency "bundler", "~> 1.13"
spec.add_development_dependency "coveralls"
Expand Down