Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MacOS.language less opinionated and add language stanza. #906

Merged
merged 15 commits into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor logic to always choose first matched language.
  • Loading branch information
reitermarkus committed Oct 3, 2016
commit bc2d676b6f590d0ddd2987699624f6718384b641
36 changes: 23 additions & 13 deletions Library/Homebrew/cask/lib/hbc/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,37 @@ def homepage(homepage = nil)

def language(*args, &block)
if !args.empty? && block_given?
args.each do |arg|
MacOS.languages.each_with_index do |l, index|
next unless arg == :default || l.match(arg)
next unless @language.nil? || @language[:level].nil? || @language[:level] > index
@language = {
block: block,
level: index,
}
end
end
@language_blocks ||= {}
@language_blocks[args] = block
else
language_eval
@language
end
end


def language_eval
if @language.is_a?(Hash) && @language.key?(:block)
@language = @language[:block].call
return if instance_variable_defined?(:@language)

return unless instance_variable_defined?(:@language_blocks)

default_key = @language_blocks.keys.detect { |key| key.include?(:default) }

MacOS.languages.each do |language|
@language_blocks.each do |regexes_or_strings, block|
if regexes_or_strings.include?(:default)
regexes_or_strings = regexes_or_strings - [:default] + [%r{^en}]
end

case language
when *regexes_or_strings
@language = block.call
return
end
end
end

# fallback to :default
@language = default_key.nil? ? nil : @language_blocks[default_key].call
end

def url(*args, &block)
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/cask/test/cask/dsl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@

MacOS.stubs(languages: ["THIRD_LANGUAGE"])
cask.call.language.must_equal :default

MacOS.stubs(languages: ["THIRD_LANGUAGE", "SECOND_LANGUAGE", "FIRST_LANGUAGE"])
cask.call.language.must_equal :second

MacOS.stubs(languages: ["THIRD_LANGUAGE", "FIRST_LANGUAGE", "SECOND_LANGUAGE"])
cask.call.language.must_equal :first
end
end

Expand Down