From cc677aac20015ac5163449da3c79277a1478d753 Mon Sep 17 00:00:00 2001 From: Jerome Lacoste Date: Sun, 7 Jan 2018 21:56:49 +0100 Subject: [PATCH] u3d/available: allow to match using regular expression --- lib/u3d/commands.rb | 7 +++++-- lib/u3d/commands_generator.rb | 3 ++- lib/u3d_core/core_ext/hash.rb | 30 ++++++++++++++++++++++++++++++ spec/u3d/commands_spec.rb | 9 +++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 lib/u3d_core/core_ext/hash.rb diff --git a/lib/u3d/commands.rb b/lib/u3d/commands.rb index f3f426fe..37e6c9f5 100644 --- a/lib/u3d/commands.rb +++ b/lib/u3d/commands.rb @@ -20,6 +20,7 @@ # SOFTWARE. ## --- END LICENSE BLOCK --- +require 'u3d_core/core_ext/hash' require 'u3d/compatibility' require 'u3d/unity_versions' require 'u3d/unity_version_definition' @@ -38,6 +39,8 @@ module U3d # API for U3d, redirecting calls to class they concern # rubocop:disable ClassLength class Commands + using ::CoreExtensions::Extractable + class << self def list_installed(options: {}) list = Installer.create.installed @@ -69,8 +72,8 @@ def list_available(options: {}) cache_versions = cache_versions(os, force_refresh: options[:force]) if ver - return UI.error "Version #{ver} is not in cache" if cache_versions[ver].nil? - cache_versions = { ver => cache_versions[ver] } + cache_versions = cache_versions.extract(*cache_versions.keys.select { |k| /#{Regexp.quote(ver)}/.match(k) }) + return UI.error "Version #{ver} doesn't match any in cache" if cache_versions.empty? end vcomparators = cache_versions.keys.map { |k| UnityVersionComparator.new(k) } diff --git a/lib/u3d/commands_generator.rb b/lib/u3d/commands_generator.rb index 496de4f5..a21ebddb 100644 --- a/lib/u3d/commands_generator.rb +++ b/lib/u3d/commands_generator.rb @@ -105,12 +105,13 @@ def run c.option '-f', '--force', 'Force refresh list of available versions' c.option '-r', '--release_level STRING', String, "Checks for availability on specific release level [#{levels.join(', ')}]" c.option '-o', '--operating_system STRING', String, "Checks for availability on specific OS [#{oses.join(', ')}]" - c.option '-u', '--unity_version STRING', String, 'Checks if specified version is available' + c.option '-u', '--unity_version STRING', String, 'Checks if specified version is available. Can be a regular expression' c.option '-p', '--packages', 'Lists available packages as well' c.example 'List all versions available, forcing a refresh of the available packages from Unity servers', 'u3d available -f' c.example 'List stable versions available', 'u3d available -r stable -p' c.example 'List all versions available for Linux platform', 'u3d available -o linux' c.example 'List packages available for Unity version 5.6.0f3', 'u3d available -u 5.6.0f3 -p' + c.example 'List packages available for Unity version containing the 5.6 string', 'u3d available -u \'5.6\' -p' c.description = 'List download-ready versions of Unity3d' c.action do |_args, options| options.default packages: false diff --git a/lib/u3d_core/core_ext/hash.rb b/lib/u3d_core/core_ext/hash.rb new file mode 100644 index 00000000..f66e9609 --- /dev/null +++ b/lib/u3d_core/core_ext/hash.rb @@ -0,0 +1,30 @@ +## --- BEGIN LICENSE BLOCK --- +# Copyright (c) 2017-present WeWantToKnow AS +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +## --- END LICENSE BLOCK --- +module CoreExtensions + module Extractable + refine Hash do + def extract(*keys) + select { |k, _v| keys.include?(k) } + end + end + end +end diff --git a/spec/u3d/commands_spec.rb b/spec/u3d/commands_spec.rb index 27c488f2..62779edb 100644 --- a/spec/u3d/commands_spec.rb +++ b/spec/u3d/commands_spec.rb @@ -126,6 +126,15 @@ U3d::Commands.list_available(options: { unity_version: '1.2.3f4' }) end + it 'only logs specified version found using regular expression' do + on_fake_os + with_fake_cache('fakeos' => { 'versions' => { '1.2.3f4' => 'fakeurl', '1.3.3f4' => 'fakeurl' } }) + + expect(U3d::UI).to receive(:message).with(/.*1.2.3f4.*fakeurl.*/) + + U3d::Commands.list_available(options: { unity_version: '1.2' }) + end + context 'when parsing user OS input' do it 'uses correct input' do fakeos = double('os')