diff --git a/lib/cldr/download.rb b/lib/cldr/download.rb index 926c8ff..6a80c77 100644 --- a/lib/cldr/download.rb +++ b/lib/cldr/download.rb @@ -11,7 +11,17 @@ module Download DEFAULT_VERSION = 41 class << self - def download(source = DEFAULT_SOURCE, target = DEFAULT_TARGET, version = DEFAULT_VERSION, &block) + def download(source = DEFAULT_SOURCE, target = DEFAULT_TARGET, version = DEFAULT_VERSION, pre_release: false, &block) + if pre_release + download_prerelease(source, target, version, &block) + else + download_release(source, target, version, &block) + end + end + + private + + def download_release(source, target, version, &block) source = format(source, version: version) target ||= File.expand_path(DEFAULT_TARGET) @@ -28,6 +38,27 @@ def download(source = DEFAULT_SOURCE, target = DEFAULT_TARGET, version = DEFAULT end end end + + def download_prerelease(source, target, version, &block) + source = format(source, version: version) + target ||= File.expand_path(DEFAULT_TARGET) + + URI.parse(source).open do |tempfile| + FileUtils.mkdir_p(target) + Zip.on_exists_proc = true + Zip::File.open(tempfile.path) do |file| + root_dir = file.first.name + production_dir = root_dir + "production" + file.each do |entry| + next unless entry.name.start_with?(production_dir) + + path = target + "/" + entry.name.gsub(production_dir, "") + FileUtils.mkdir_p(File.dirname(path)) + file.extract(entry, path) + end + end + end + end end end end diff --git a/lib/cldr/thor.rb b/lib/cldr/thor.rb index 55f4c37..66755d2 100644 --- a/lib/cldr/thor.rb +++ b/lib/cldr/thor.rb @@ -10,7 +10,7 @@ module Cldr class Thor < ::Thor namespace "cldr" - desc "download [--version=#{Cldr::Download::DEFAULT_VERSION}] [--target=#{Cldr::Download::DEFAULT_TARGET}] [--source=#{format(Cldr::Download::DEFAULT_SOURCE, version: Cldr::Download::DEFAULT_VERSION)}]", "Download and extract CLDR data" + desc "download [--version=#{Cldr::Download::DEFAULT_VERSION}] [--target=#{Cldr::Download::DEFAULT_TARGET}] [--source=#{format(Cldr::Download::DEFAULT_SOURCE, version: Cldr::Download::DEFAULT_VERSION)}] [--pre-release]", "Download and extract CLDR data" option :version, aliases: [:v], type: :numeric, default: Cldr::Download::DEFAULT_VERSION, banner: Cldr::Download::DEFAULT_VERSION, @@ -23,8 +23,11 @@ class Thor < ::Thor default: Cldr::Download::DEFAULT_SOURCE, banner: Cldr::Download::DEFAULT_SOURCE, desc: "Override the location of the CLDR zip to download. Overrides --version" + option :"pre-release", type: :boolean, + default: false, + desc: "Whether the source is a pre-release version" def download - Cldr::Download.download(options["source"], options["target"], options["version"]) { putc(".") } + Cldr::Download.download(options["source"], options["target"], options["version"], pre_release: options["pre-release"]) { putc(".") } end DEFAULT_MINIMUM_DRAFT_STATUS = Cldr::DraftStatus::CONTRIBUTED