Skip to content

Instantly share code, notes, and snippets.

@sue445

sue445/latest_version.rb

Last active Aug 29, 2015
Embed
What would you like to do?
Example of naturalsorter
require "naturalsorter"
require "pp"
# get versions from `git tag` response
# ex) ["0.3.1", "0.3.2", "0.3.3", "0.3.4", "0.3.5", "0.3.6", "0.3.7", "0.3.8", "0.3.8.beta1", "0.3.9", "1.0.0", "1.0.0.beta1"]
def tag_versions
`git tag`.each_line.find_all{ |tag| tag.start_with?("v") }.map{ |tag| tag.gsub("v", "").strip }
end
tag_versions
=begin
["0.0.1",
"0.0.2",
"0.0.3",
"0.0.4",
"0.0.5",
"0.0.6",
"0.0.7",
"0.0.8",
"0.0.9",
"0.1.0",
"0.1.1",
"0.1.2",
"0.1.3",
"0.1.4",
"0.1.5",
"0.1.6",
"0.1.7",
"0.1.8",
"0.1.9",
"0.2.0",
"0.2.1",
"0.3.0",
"0.3.1",
"0.3.2",
"1.0.0",
"1.0.1",
"1.0.2",
"1.0.3",
"1.0.4",
"1.0.5",
"1.0.6",
"1.0.7",
"1.0.8",
"1.1.0",
"1.1.1",
"1.1.2",
"1.1.3",
"1.1.4",
"1.1.5",
"1.1.6",
"1.1.7",
"2.0.0",
"2.0.0.beta1",
"2.0.0.beta2",
"2.0.1",
"2.0.2",
"2.0.3",
"2.0.4",
"2.0.5",
"2.0.5.4",
"2.0.6",
"2.0.7",
"2.0.8",
"2.1.0",
"2.1.1",
"2.1.2",
"2.1.3",
"2.1.4",
"2.1.5",
"2.1.5.beta",
"2.1.6",
"2.1.7",
"2.1.8",
"2.1.9",
"2.2.0",
"2.2.1",
"2.2.2",
"2.2.3",
"2.2.4",
"2.2.5",
"2.2.6",
"2.2.7",
"2.2.8",
"2.2.9",
"2.3.0",
"2.3.1",
"2.3.10",
"2.3.2",
"2.3.3",
"2.3.3.beta1",
"2.3.3.beta2",
"2.3.3.beta3",
"2.3.4",
"2.3.4.beta1",
"2.3.5",
"2.3.6",
"2.3.7",
"2.3.8",
"2.3.9",
"2.3.9.beta1"]
=end
ascii_sorted_versions = tag_versions.sort
pp ascii_sorted_versions
=begin
["0.0.1",
"0.0.2",
"0.0.3",
"0.0.4",
"0.0.5",
"0.0.6",
"0.0.7",
"0.0.8",
"0.0.9",
"0.1.0",
"0.1.1",
"0.1.2",
"0.1.3",
"0.1.4",
"0.1.5",
"0.1.6",
"0.1.7",
"0.1.8",
"0.1.9",
"0.2.0",
"0.2.1",
"0.3.0",
"0.3.1",
"0.3.2",
"1.0.0",
"1.0.1",
"1.0.2",
"1.0.3",
"1.0.4",
"1.0.5",
"1.0.6",
"1.0.7",
"1.0.8",
"1.1.0",
"1.1.1",
"1.1.2",
"1.1.3",
"1.1.4",
"1.1.5",
"1.1.6",
"1.1.7",
"2.0.0",
"2.0.0.beta1",
"2.0.0.beta2",
"2.0.1",
"2.0.2",
"2.0.3",
"2.0.4",
"2.0.5",
"2.0.5.4",
"2.0.6",
"2.0.7",
"2.0.8",
"2.1.0",
"2.1.1",
"2.1.2",
"2.1.3",
"2.1.4",
"2.1.5",
"2.1.5.beta",
"2.1.6",
"2.1.7",
"2.1.8",
"2.1.9",
"2.2.0",
"2.2.1",
"2.2.2",
"2.2.3",
"2.2.4",
"2.2.5",
"2.2.6",
"2.2.7",
"2.2.8",
"2.2.9",
"2.3.0",
"2.3.1",
"2.3.10",
"2.3.2",
"2.3.3",
"2.3.3.beta1",
"2.3.3.beta2",
"2.3.3.beta3",
"2.3.4",
"2.3.4.beta1",
"2.3.5",
"2.3.6",
"2.3.7",
"2.3.8",
"2.3.9",
"2.3.9.beta1"]
=end
natural_sorted_versions = Naturalsorter::Sorter.sort_version(tag_versions)
pp natural_sorted_versions
=begin
["0.0.1",
"0.0.2",
"0.0.3",
"0.0.4",
"0.0.5",
"0.0.6",
"0.0.7",
"0.0.8",
"0.0.9",
"0.1.0",
"0.1.1",
"0.1.2",
"0.1.3",
"0.1.4",
"0.1.5",
"0.1.6",
"0.1.7",
"0.1.8",
"0.1.9",
"0.2.0",
"0.2.1",
"0.3.0",
"0.3.1",
"0.3.2",
"1.0.0",
"1.0.1",
"1.0.2",
"1.0.3",
"1.0.4",
"1.0.5",
"1.0.6",
"1.0.7",
"1.0.8",
"1.1.0",
"1.1.1",
"1.1.2",
"1.1.3",
"1.1.4",
"1.1.5",
"1.1.6",
"1.1.7",
"2.0.0.beta1",
"2.0.0.beta2",
"2.0.0",
"2.0.1",
"2.0.2",
"2.0.3",
"2.0.4",
"2.0.5",
"2.0.5.4",
"2.0.6",
"2.0.7",
"2.0.8",
"2.1.0",
"2.1.1",
"2.1.2",
"2.1.3",
"2.1.4",
"2.1.5.beta",
"2.1.5",
"2.1.6",
"2.1.7",
"2.1.8",
"2.1.9",
"2.2.0",
"2.2.1",
"2.2.2",
"2.2.3",
"2.2.4",
"2.2.5",
"2.2.6",
"2.2.7",
"2.2.8",
"2.2.9",
"2.3.0",
"2.3.1",
"2.3.2",
"2.3.3.beta1",
"2.3.3.beta2",
"2.3.3.beta3",
"2.3.3",
"2.3.4.beta1",
"2.3.4",
"2.3.5",
"2.3.6",
"2.3.7",
"2.3.8",
"2.3.9.beta1",
"2.3.9",
"2.3.10"]
=end
latest_version = natural_sorted_versions.last
# => 2.3.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.