Skip to content

Commit

Permalink
Fix xcpretty#426: filter Xcode versions
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Jun 22, 2021
1 parent eeb8a07 commit 035bf5b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/xcode/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,15 @@ def list_annotated(xcodes_list)
end.join("\n")
end

def list
list_annotated(list_versions.sort { |first, second| compare_versions(first, second) })
def list(requirement = nil)
versions = list_versions

unless requirement.nil?
req = Gem::Requirement.new(requirement.split(','))
versions.select! { |ver| req =~ Gem::Version.new(ver.split(' ')[0])}
end

list_annotated(versions.sort { |first, second| compare_versions(first, second) })
end

def rm_list_cache
Expand Down
11 changes: 9 additions & 2 deletions lib/xcode/install/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ class List < Command
self.summary = 'List Xcodes available for download.'

def self.options
[['--all', 'Show all available versions. (Default, Deprecated)']].concat(super)
[['--all', 'Show all available versions. (Default, Deprecated)'],
['--filter', 'Filter by version requirement, e.g. "~> 12.0", or ">= 12.0, < 13.0"']].concat(super)
end

def initialize(argv)
@all = argv.flag?('all', true)
@filter = argv.option('filter')
super
end

def run
installer = XcodeInstall::Installer.new
puts installer.list
puts installer.list(@filter)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def fake_installed_xcodes(*names)
installer.list.should == "1\n2.3\n2.3.1\n2.3.2\n3 some\n4 beta\n10 beta"
end

it 'lists all versions filtered by requirements' do
fake_xcodes '1', '2.3', '2.3.1', '2.3.2', '3 some', '4 beta', '10 beta'
fake_installed_xcodes
installer.list('>= 2.0, < 10.0').should == "2.3\n2.3.1\n2.3.2\n3 some\n4 beta"
end

it 'lists all versions in the correct order' do
fake_xcodes(
'12 beta 4', '12 beta 3', '12 beta 2', '12 for macOS Universal Apps beta 2',
Expand Down

0 comments on commit 035bf5b

Please sign in to comment.