Skip to content
Snippets Groups Projects
Commit 013f33be authored by Markus Reiter's avatar Markus Reiter
Browse files

Change language DSL to only allow strings.

parent b5531e8e
No related branches found
No related tags found
No related merge requests found
require "set"
require "locale"
require "hbc/dsl/appcast"
require "hbc/dsl/base"
......@@ -99,9 +100,10 @@ module Hbc
@homepage ||= homepage
end
def language(*args, &block)
def language(*args, default: false, &block)
if !args.empty? && block_given?
@language_blocks ||= {}
@language_blocks.default = block if default
@language_blocks[args] = block
else
language_eval
......@@ -114,29 +116,15 @@ module Hbc
return unless instance_variable_defined?(:@language_blocks)
default_key = @language_blocks.keys.detect { |key| key.include?(:default) }
MacOS.languages.map(&Locale.method(:parse)).any? { |locale|
key = @language_blocks.keys.detect { |strings|
strings.any? { |string| locale.include?(string) }
}
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
return @language = @language_blocks[key].call unless key.nil?
}
regexes_or_strings.each do |regex_or_string|
if regex_or_string.class == language.class
next unless regex_or_string == language
else
next unless regex_or_string =~ language
end
@language = block.call
return
end
end
end
# fallback to :default
@language = default_key.nil? ? nil : @language_blocks[default_key].call
@language = @language_blocks.default.call
end
def url(*args, &block)
......
......@@ -124,36 +124,51 @@ describe Hbc::DSL do
describe "language stanza" do
it "allows multilingual casks" do
cask = lambda {
cask = lambda do
Hbc::Cask.new("cask-with-apps") do
language "FIRST_LANGUAGE" do
:first
language "zh" do
sha256 "abc123"
"zh-CN"
end
language %r{SECOND_LANGUAGE} do
:second
language "en-US", default: true do
sha256 "xyz789"
"en-US"
end
language :default do
:default
end
url "https://example.org/#{language}.zip"
end
}
MacOS.stubs(languages: ["FIRST_LANGUAGE"])
cask.call.language.must_equal :first
MacOS.stubs(languages: ["SECOND_LANGUAGE"])
cask.call.language.must_equal :second
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
end
MacOS.stubs(languages: ["THIRD_LANGUAGE", "FIRST_LANGUAGE", "SECOND_LANGUAGE"])
cask.call.language.must_equal :first
MacOS.stubs(languages: ["zh"])
cask.call.language.must_equal "zh-CN"
cask.call.sha256.must_equal "abc123"
cask.call.url.to_s.must_equal "https://example.org/zh-CN.zip"
MacOS.stubs(languages: ["zh-XX"])
cask.call.language.must_equal "zh-CN"
cask.call.sha256.must_equal "abc123"
cask.call.url.to_s.must_equal "https://example.org/zh-CN.zip"
MacOS.stubs(languages: ["en"])
cask.call.language.must_equal "en-US"
cask.call.sha256.must_equal "xyz789"
cask.call.url.to_s.must_equal "https://example.org/en-US.zip"
MacOS.stubs(languages: ["xx-XX"])
cask.call.language.must_equal "en-US"
cask.call.sha256.must_equal "xyz789"
cask.call.url.to_s.must_equal "https://example.org/en-US.zip"
MacOS.stubs(languages: ["xx-XX", "zh", "en"])
cask.call.language.must_equal "zh-CN"
cask.call.sha256.must_equal "abc123"
cask.call.url.to_s.must_equal "https://example.org/zh-CN.zip"
MacOS.stubs(languages: ["xx-XX", "en-US", "zh"])
cask.call.language.must_equal "en-US"
cask.call.sha256.must_equal "xyz789"
cask.call.url.to_s.must_equal "https://example.org/en-US.zip"
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment