lib/u3d/commands.rb in u3d-0.9.3 vs lib/u3d/commands.rb in u3d-0.9.4

- old
+ new

@@ -19,22 +19,25 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. ## --- END LICENSE BLOCK --- require 'u3d/unity_versions' +require 'u3d/unity_version_definition' require 'u3d/downloader' require 'u3d/installer' +require 'u3d/unity_project' require 'u3d/cache' require 'u3d/utils' require 'u3d/log_analyzer' require 'u3d/unity_runner' require 'u3d_core/command_executor' require 'u3d_core/credentials' require 'fileutils' module U3d # API for U3d, redirecting calls to class they concern + # rubocop:disable ClassLength class Commands class << self def list_installed(options: {}) list = Installer.create.installed if list.empty? @@ -47,31 +50,23 @@ # sorted versions vcomparators = map.keys.map { |k| UnityVersionComparator.new(k) } sorted_keys = vcomparators.sort.map { |v| v.version.to_s } sorted_keys.each do |k| u = map[k] - UI.message "%-*s%s" % [30, "Version #{u.version}", " (#{u.path})"] + UI.message "Version #{u.version.ljust(30)}(#{u.path})" packages = u.packages next unless options[:packages] && packages && !packages.empty? UI.message 'Packages:' packages.each { |pack| UI.message " - #{pack}" } end end def list_available(options: {}) ver = options[:unity_version] - os = options[:operating_system] + os = valid_os_or_current(options[:operating_system]) rl = options[:release_level] - if os - os = os.to_sym - oses = U3dCore::Helper.operating_systems - raise "Specified OS (#{os}) isn't valid [#{oses.join(', ')}]" unless oses.include?(os) - else - os = U3dCore::Helper.operating_system - end cache = Cache.new(force_os: os, force_refresh: options[:force]) - versions = {} return UI.error "Version #{ver} is not in cache" if ver && cache[os.id2name]['versions'][ver].nil? versions = if ver { ver => cache[os.id2name]['versions'][ver] } @@ -101,118 +96,70 @@ inif.keys.each { |pack| UI.message " - #{pack}" } end end end - def download(args: [], options: {}) - version = args[0] - UI.user_error!('Please specify a Unity version to download') unless version + def install(args: [], options: {}) + version = specified_or_current_project_version(args[0]) - packages = packages_with_unity_first(options) - os = U3dCore::Helper.operating_system + + packages = packages_with_unity_first(os, options) + cache = Cache.new(force_os: os) - versions = cache[os.id2name]['versions'] - version = interpret_latest(version, versions) + cache_versions = cache[os.id2name]['versions'] + version = interpret_latest(version, cache_versions) + unless cache_versions[version] + UI.error "No version '#{version}' was found in cache. Either it doesn't exist or u3d doesn't know about it yet. Try refreshing with 'u3d available -f'" + return + end + definition = UnityVersionDefinition.new(version, os, cache_versions) unity = check_unity_presence(version: version) - return unless enforce_setup_coherence(packages, options, unity) + return unless enforce_setup_coherence(packages, options, unity, definition) - U3d::Globals.use_keychain = true if options[:keychain] && Helper.mac? - - unless options[:no_install] + if options[:install] + U3d::Globals.use_keychain = true if options[:keychain] && Helper.mac? UI.important 'Root privileges are required' raise 'Could not get administrative privileges' unless U3dCore::CommandExecutor.has_admin_privileges? end - files = [] - if os == :linux - UI.important 'Option -a | --all not available for Linux' if options[:all] - UI.important 'Option -p | --packages not available for Linux' if options[:packages] - downloader = Downloader::LinuxDownloader - files << ["Unity #{version}", downloader.download(version, versions), {}] - else - downloader = Downloader::MacDownloader if os == :mac - downloader = Downloader::WindowsDownloader if os == :win - if options[:all] - files = downloader.download_all(version, versions) - else - packages.each do |package| - result = downloader.download_specific(package, version, versions) - files << [package, result[0], result[1]] unless result.nil? - end - end - end + files = Downloader.fetch_modules(definition, packages: packages, download: options[:download]) - return if options[:no_install] - Installer.install_modules(files, version, installation_path: options[:installation_path]) + return unless options[:install] + Installer.install_modules(files, definition.version, installation_path: options[:installation_path]) end - def local_install(args: [], options: {}) - UI.user_error!('Please specify a version') if args.empty? - version = args[0] - - packages = packages_with_unity_first(options) - - os = U3dCore::Helper.operating_system - - unity = check_unity_presence(version: version) - return unless enforce_setup_coherence(packages, options, unity) - - U3d::Globals.use_keychain = true if options[:keychain] && Helper.mac? - - UI.important 'Root privileges are required' - raise 'Could not get administrative privileges' unless U3dCore::CommandExecutor.has_admin_privileges? - - files = [] - if os == :linux - UI.important 'Option -a | --all not available for Linux' if options[:all] - UI.important 'Option -p | --packages not available for Linux' if options[:packages] - downloader = Downloader::LinuxDownloader - files << ["Unity #{version}", downloader.local_file(version), {}] - else - downloader = Downloader::MacDownloader if os == :mac - downloader = Downloader::WindowsDownloader if os == :win - if options[:all] - files = downloader.all_local_files(version) - else - packages.each do |package| - result = downloader.local_file(package, version) - files << [package, result[0], result[1]] unless result.nil? - end - end - end - - Installer.install_modules(files, version, installation_path: options[:installation_path]) - end - def run(options: {}, run_args: []) version = options[:unity_version] runner = Runner.new - pp = runner.find_projectpath_in_args(run_args) + args_pp = Runner.find_projectpath_in_args(run_args) + pp = args_pp pp = Dir.pwd unless pp up = UnityProject.new(pp) unless version # fall back in project default if we are on a Unity project version = up.editor_version if up.exist? unless version UI.user_error!('Not sure which version of Unity to run. Are you in a project?') end end - run_args = ['-projectpath', up.path] if run_args.empty? && up.exist? + if up.exist? && args_pp.nil? + extra_run_args = ['-projectpath', up.path] + run_args = [extra_run_args, run_args].flatten + end - # we could - # * support matching 5.3.6p3 if passed 5.3.6 + # we could support matching 5.3.6p3 if passed 5.3.6 unity = Installer.create.installed.find { |u| u.version == version } UI.user_error! "Unity version '#{version}' not found" unless unity runner.run(unity, run_args, raw_logs: options[:raw_logs]) end def credentials_actions - %w(add remove check) + %w[add remove check] end def credentials(args: [], _options: {}) action = args[0] raise "Please specify an action to perform, one of #{credentials_actions.join(',')}" unless action @@ -224,24 +171,11 @@ UI.error 'Invalid credentials' unless U3dCore::CommandExecutor.has_admin_privileges? elsif action == 'remove' U3dCore::Globals.use_keychain = true U3dCore::Credentials.new(user: ENV['USER']).forget_credentials(force: true) else - U3dCore::Globals.use_keychain = true - credentials = U3dCore::Credentials.new(user: ENV['USER']) - U3dCore::Globals.with_do_not_login(true) do - if credentials.password.to_s.empty? - UI.message "No credentials stored" - else - if U3dCore::CommandExecutor.has_admin_privileges? - UI.success "Stored credentials are valid" - else - UI.error "Stored credentials are not valid" - end - end - end - # FIXME: return value + credentials_check end end def local_analyze(args: []) raise ArgumentError, 'No files given' if args.empty? @@ -252,11 +186,11 @@ f.readlines.each { |l| analyzer.parse_line l } end end def release_levels - [:stable, :beta, :patch] + %i[stable beta patch] end def release_letter_mapping { latest: 'f', @@ -266,10 +200,49 @@ } end private + def specified_or_current_project_version(version) + unless version # no version specified, use the one from the current unity project if any + UI.message "No unity version specified. If the current directory is a Unity project, we try to install the one it requires" + up = UnityProject.new(Dir.pwd) + version = up.editor_version if up.exist? + end + UI.user_error!('Please specify a Unity version to download') unless version + version + end + + def credentials_check + U3dCore::Globals.use_keychain = true + credentials = U3dCore::Credentials.new(user: ENV['USER']) + U3dCore::Globals.with_do_not_login(true) do + if credentials.password.to_s.empty? + UI.message "No credentials stored" + elsif U3dCore::CommandExecutor.has_admin_privileges? + UI.success "Stored credentials are valid" + else + UI.error "Stored credentials are not valid" + end + end + # FIXME: return value + end + + # if the specified string representatio of `os` is non nil + # convert the it to a symbol and checks it against the valid ones + # or return the current OS + def valid_os_or_current(os) + if os + os = os.to_sym + oses = U3dCore::Helper.operating_systems + raise "Specified OS (#{os}) isn't valid [#{oses.join(', ')}]" unless oses.include?(os) + else + os = U3dCore::Helper.operating_system + end + os + end + def interpret_latest(version, versions) return version unless release_letter_mapping.keys.include? version.to_sym letter = release_letter_mapping[version.to_sym] @@ -280,11 +253,15 @@ .version.to_s UI.message "Version '#{version}' is #{iversion}." iversion end - def packages_with_unity_first(options) + def packages_with_unity_first(os, options) + if os == :linux + UI.important 'Option -a | --all not available for Linux' if options[:all] + UI.important 'Option -p | --packages not available for Linux' if options[:packages] + end temp = options[:packages] || ['Unity'] temp.insert(0, 'Unity') if temp.delete('Unity') temp end @@ -298,30 +275,36 @@ return unity end nil end - def enforce_setup_coherence(packages, options, unity) + def enforce_setup_coherence(packages, options, unity, definition) + packages = definition.available_packages if options[:all] if unity UI.important "Unity #{unity.version} is already installed" - return false if Helper.linux? + # Not needed since Linux custom u3d files contain only one entry wich is Unity + # return false if definition.os == :linux if packages.include?('Unity') UI.important 'Ignoring Unity module, it is already installed' packages.delete('Unity') - options[:installation_path] ||= unity.path if Helper.windows? + + # FIXME: Move me to the WindowsInstaller + options[:installation_path] ||= unity.path if definition.os == :win end if unity.packages unity.packages.each do |pack| UI.important "Ignoring #{pack} module, it is already installed" if packages.delete(pack) end end + return false if packages.empty? else unless packages.include?('Unity') - UI.error "Please install Unity #{unity.version} before any of its packages" + UI.error 'Please install Unity before any of its packages' return false end end true end end end + # rubocop:enable ClassLength end