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

u3d/available: allow to match using regular expression #216

Merged
merged 1 commit into from
Jan 8, 2018
Merged
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
7 changes: 5 additions & 2 deletions lib/u3d/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
3 changes: 2 additions & 1 deletion lib/u3d/commands_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions lib/u3d_core/core_ext/hash.rb
Original file line number Diff line number Diff line change
@@ -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) }
Copy link
Member

Choose a reason for hiding this comment

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

def extract(*args)
  self.keys & args
end

This may be cleaner?

Copy link
Member Author

Choose a reason for hiding this comment

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

does that work?

Copy link
Member

@niezbop niezbop Jan 8, 2018

Choose a reason for hiding this comment

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

Oops, maybe not. This should though:

def extract(*args)
  keys & args
end

Copy link
Member Author

Choose a reason for hiding this comment

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

image

Copy link
Member

Choose a reason for hiding this comment

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

Misunderstood the intent. Sorry

end
end
end
end
9 changes: 9 additions & 0 deletions spec/u3d/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down