Skip to content

Commit

Permalink
chore(polyglot): update language weights latest-version-then-alphabet…
Browse files Browse the repository at this point in the history
…ical
  • Loading branch information
cysjonathan authored and adi-herwana-nus committed Jan 6, 2025
1 parent 7f9a167 commit 592cf29
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions lib/tasks/db/set_polyglot_language_weights.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ namespace :db do
# This rake updates the weight column in the polyglot_languages table,
# changing the order in which languages are displayed in drop-down menus.

LANGUAGE_ORDERING = [
'python',
'java',
'c++',
'r',
'javascript',
'c/c++'
].freeze

def comparable_polyglot_version(language)
language&.polyglot_version&.split('.')&.map(&:to_i)
end
Expand All @@ -27,25 +18,18 @@ namespace :db do
end

def language_compare(lang1, lang2) # rubocop:disable Metrics/CyclomaticComplexity
index1 = LANGUAGE_ORDERING.index { |l| lang1.polyglot_name == l }
index2 = LANGUAGE_ORDERING.index { |l| lang2.polyglot_name == l }

# Put more recent versions first
return -version_compare(lang1, lang2) if index1 == index2
return -version_compare(lang1, lang2) if lang1.polyglot_name == lang2.polyglot_name

# Put latest versions of each language before outdated versions of all languages
return -1 if latest?(lang1) && !latest?(lang2)
return 1 if !latest?(lang1) && latest?(lang2)

# Put other languages (not added to LANGUAGE_ORDERING array above) last
return 1 if index1.nil?
return -1 if index2.nil?

# At this point we know that:
# - both languages have not-nil index values that are different and
# - both languages are different and
# - the languages are either both latest? = true or both latest? = false.
# In any case, the index can be safely used to determine the ordering now.
index1 <=> index2
# Now polyglot_name can be safely used to determine the ordering now.
lang1.polyglot_name <=> lang2.polyglot_name
end

task set_polyglot_language_weights: :environment do
Expand Down

0 comments on commit 592cf29

Please sign in to comment.