Skip to content

Commit

Permalink
Merge pull request #413 from revolter/fix/incorrect-order
Browse files Browse the repository at this point in the history
Fix incorrect order when using the list command
  • Loading branch information
Josh Holtz authored Oct 28, 2020
2 parents 7521678 + 3a4eab2 commit b6e1a06
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
23 changes: 22 additions & 1 deletion lib/xcode/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def list_annotated(xcodes_list)
end

def list
list_annotated(list_versions.sort_by(&:to_f))
list_annotated(list_versions.sort { |first, second| compare_versions(first, second) })
end

def rm_list_cache
Expand Down Expand Up @@ -464,6 +464,27 @@ def prereleases
links
end

def compare_versions(first, second)
# Sort by version number
numeric_comparation = first.to_f <=> second.to_f
return numeric_comparation if numeric_comparation != 0

# Return beta versions before others
is_first_beta = first.include?('beta')
is_second_beta = second.include?('beta')
return -1 if is_first_beta && !is_second_beta
return 1 if !is_first_beta && is_second_beta

# Return GM versions before others
is_first_gm = first.include?('GM')
is_second_gm = second.include?('GM')
return -1 if is_first_gm && !is_second_gm
return 1 if !is_first_gm && is_second_gm

# Sort alphabetically
first <=> second
end

def hdiutil(*args)
io = IO.popen(['hdiutil', *args])
result = io.read
Expand Down
12 changes: 6 additions & 6 deletions spec/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ module XcodeInstall
installer.stubs(:xcodes).returns(seedlist)

versions = [
'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',
'5.0.1', '5', '5.0.2', '5.1', '5.1.1',
'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',
'5', '5.0.1', '5.0.2', '5.1', '5.1.1',
'6.0.1', '6.1', '6.1.1', '6.2', '6.3', '6.3.1', '6.3.2', '6.4',
'7', '7.0.1', '7.1', '7.1.1', '7.2.1', '7.2', '7.3', '7.3.1',
'8', '8.1', '8.2', '8.2.1', '8.3.2', '8.3.3', '8.3',
'7', '7.0.1', '7.1', '7.1.1', '7.2', '7.2.1', '7.3', '7.3.1',
'8', '8.1', '8.2', '8.2.1', '8.3', '8.3.2', '8.3.3',
'9', '9.0.1', '9.1', '9.2', '9.3', '9.3.1', '9.4', '9.4.1',
'10', '10.1', '10.2.1', '10.2', '10.3',
'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'
'10', '10.1', '10.2', '10.2.1', '10.3',
'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'
]
installer.list.split("\n").should == versions
end
Expand Down
24 changes: 20 additions & 4 deletions spec/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,31 @@ def fake_installed_xcodes(*names)
it 'lists all versions' do
fake_xcodes '1', '2.3', '2.3.1', '2.3.2', '3 some', '4 beta', '10 beta'
fake_installed_xcodes
installer.list.should == "1\n2.3.2\n2.3.1\n2.3\n3 some\n4 beta\n10 beta"
installer.list.should == "1\n2.3\n2.3.1\n2.3.2\n3 some\n4 beta\n10 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',
'12 beta', '12 for macOS Universal Apps beta', '12.0.1', '12', '12 beta 6',
'12 beta 5', '12.1 GM seed', '12.2 beta 3', '12.2 beta', '12.2 beta 2'
)
fake_installed_xcodes

versions = [
'12 beta', '12 beta 2', '12 beta 3', '12 beta 4', '12 beta 5', '12 beta 6',
'12 for macOS Universal Apps beta', '12 for macOS Universal Apps beta 2',
'12', '12.0.1', '12.1 GM seed', '12.2 beta', '12.2 beta 2', '12.2 beta 3'
]
installer.list.split("\n").should == versions
end
end

describe '#list_annotated' do
it 'lists all versions with annotations' do
fake_xcodes '1', '2.3', '2.3.1', '2.3.2', '3 some', '4.3.1 for Lion', '9.4.1', '10 beta'
fake_installed_xcodes '2.3', '4.3.1 for Lion', '10 beta'
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)"
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)"
end

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

it 'distinguish each beta versions' do
fake_xcodes '11.4 beta', '11.4 beta 3'
fake_xcodes '11.4 beta 3', '11.4 beta'
fake_installed_xcodes '11.4 beta'
installer.list.should == "11.4 beta 3\n11.4 beta (installed)"
installer.list.should == "11.4 beta (installed)\n11.4 beta 3"
end
end
end
Expand Down

0 comments on commit b6e1a06

Please sign in to comment.