Skip to content

Commit b6e1a06

Browse files
author
Josh Holtz
authored
Merge pull request #413 from revolter/fix/incorrect-order
Fix incorrect order when using the list command
2 parents 7521678 + 3a4eab2 commit b6e1a06

3 files changed

Lines changed: 48 additions & 11 deletions

File tree

lib/xcode/install.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def list_annotated(xcodes_list)
317317
end
318318

319319
def list
320-
list_annotated(list_versions.sort_by(&:to_f))
320+
list_annotated(list_versions.sort { |first, second| compare_versions(first, second) })
321321
end
322322

323323
def rm_list_cache
@@ -464,6 +464,27 @@ def prereleases
464464
links
465465
end
466466

467+
def compare_versions(first, second)
468+
# Sort by version number
469+
numeric_comparation = first.to_f <=> second.to_f
470+
return numeric_comparation if numeric_comparation != 0
471+
472+
# Return beta versions before others
473+
is_first_beta = first.include?('beta')
474+
is_second_beta = second.include?('beta')
475+
return -1 if is_first_beta && !is_second_beta
476+
return 1 if !is_first_beta && is_second_beta
477+
478+
# Return GM versions before others
479+
is_first_gm = first.include?('GM')
480+
is_second_gm = second.include?('GM')
481+
return -1 if is_first_gm && !is_second_gm
482+
return 1 if !is_first_gm && is_second_gm
483+
484+
# Sort alphabetically
485+
first <=> second
486+
end
487+
467488
def hdiutil(*args)
468489
io = IO.popen(['hdiutil', *args])
469490
result = io.read

spec/json_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ module XcodeInstall
2020
installer.stubs(:xcodes).returns(seedlist)
2121

2222
versions = [
23-
'4.3 for Lion', '4.3.1 for Lion', '4.3.2 for Lion', '4.3.3 for Lion', '4.4.1', '4.5', '4.6.2', '4.6', '4.6.1', '4.6.3',
24-
'5.0.1', '5', '5.0.2', '5.1', '5.1.1',
23+
'4.3 for Lion', '4.3.1 for Lion', '4.3.2 for Lion', '4.3.3 for Lion', '4.4.1', '4.5', '4.6', '4.6.1', '4.6.2', '4.6.3',
24+
'5', '5.0.1', '5.0.2', '5.1', '5.1.1',
2525
'6.0.1', '6.1', '6.1.1', '6.2', '6.3', '6.3.1', '6.3.2', '6.4',
26-
'7', '7.0.1', '7.1', '7.1.1', '7.2.1', '7.2', '7.3', '7.3.1',
27-
'8', '8.1', '8.2', '8.2.1', '8.3.2', '8.3.3', '8.3',
26+
'7', '7.0.1', '7.1', '7.1.1', '7.2', '7.2.1', '7.3', '7.3.1',
27+
'8', '8.1', '8.2', '8.2.1', '8.3', '8.3.2', '8.3.3',
2828
'9', '9.0.1', '9.1', '9.2', '9.3', '9.3.1', '9.4', '9.4.1',
29-
'10', '10.1', '10.2.1', '10.2', '10.3',
30-
'11', '11.1', '11.2', '11.2.1', '11.3 beta', '11.3', '11.3.1', '11.4 beta', '11.4', '11.4 beta 3', '11.4 beta 2', '11.4.1', '11.5 beta 2', '11.5', '11.5 GM Seed', '11.5 beta'
29+
'10', '10.1', '10.2', '10.2.1', '10.3',
30+
'11', '11.1', '11.2', '11.2.1', '11.3 beta', '11.3', '11.3.1', '11.4 beta', '11.4 beta 2', '11.4 beta 3', '11.4', '11.4.1', '11.5 beta', '11.5 beta 2', '11.5 GM Seed', '11.5'
3131
]
3232
installer.list.split("\n").should == versions
3333
end

spec/list_spec.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,31 @@ def fake_installed_xcodes(*names)
4343
it 'lists all versions' do
4444
fake_xcodes '1', '2.3', '2.3.1', '2.3.2', '3 some', '4 beta', '10 beta'
4545
fake_installed_xcodes
46-
installer.list.should == "1\n2.3.2\n2.3.1\n2.3\n3 some\n4 beta\n10 beta"
46+
installer.list.should == "1\n2.3\n2.3.1\n2.3.2\n3 some\n4 beta\n10 beta"
47+
end
48+
49+
it 'lists all versions in the correct order' do
50+
fake_xcodes(
51+
'12 beta 4', '12 beta 3', '12 beta 2', '12 for macOS Universal Apps beta 2',
52+
'12 beta', '12 for macOS Universal Apps beta', '12.0.1', '12', '12 beta 6',
53+
'12 beta 5', '12.1 GM seed', '12.2 beta 3', '12.2 beta', '12.2 beta 2'
54+
)
55+
fake_installed_xcodes
56+
57+
versions = [
58+
'12 beta', '12 beta 2', '12 beta 3', '12 beta 4', '12 beta 5', '12 beta 6',
59+
'12 for macOS Universal Apps beta', '12 for macOS Universal Apps beta 2',
60+
'12', '12.0.1', '12.1 GM seed', '12.2 beta', '12.2 beta 2', '12.2 beta 3'
61+
]
62+
installer.list.split("\n").should == versions
4763
end
4864
end
4965

5066
describe '#list_annotated' do
5167
it 'lists all versions with annotations' do
5268
fake_xcodes '1', '2.3', '2.3.1', '2.3.2', '3 some', '4.3.1 for Lion', '9.4.1', '10 beta'
5369
fake_installed_xcodes '2.3', '4.3.1 for Lion', '10 beta'
54-
installer.list.should == "1\n2.3.2\n2.3.1\n2.3 (installed)\n3 some\n4.3.1 for Lion (installed)\n9.4.1\n10 beta (installed)"
70+
installer.list.should == "1\n2.3 (installed)\n2.3.1\n2.3.2\n3 some\n4.3.1 for Lion (installed)\n9.4.1\n10 beta (installed)"
5571
end
5672

5773
it 'distinguish between beta and official_version' do
@@ -61,9 +77,9 @@ def fake_installed_xcodes(*names)
6177
end
6278

6379
it 'distinguish each beta versions' do
64-
fake_xcodes '11.4 beta', '11.4 beta 3'
80+
fake_xcodes '11.4 beta 3', '11.4 beta'
6581
fake_installed_xcodes '11.4 beta'
66-
installer.list.should == "11.4 beta 3\n11.4 beta (installed)"
82+
installer.list.should == "11.4 beta (installed)\n11.4 beta 3"
6783
end
6884
end
6985
end

0 commit comments

Comments
 (0)