|
| 1 | +desc 'Update the vendored version of penthouse' |
| 2 | +task :update_penthouse do # rubocop:disable Metrics/BlockLength |
| 3 | + require 'net/http' |
| 4 | + require 'uri' |
| 5 | + fetch = lambda do |url| |
| 6 | + uri = URI.parse(url) |
| 7 | + response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| |
| 8 | + http.request(Net::HTTP::Get.new(uri.path, 'User-Agent' => 'Ruby')) |
| 9 | + end |
| 10 | + case response |
| 11 | + when Net::HTTPSuccess |
| 12 | + response.body |
| 13 | + when Net::HTTPRedirection |
| 14 | + # unpkg.com returns a relative URL in the `location` field. |
| 15 | + location = uri.clone |
| 16 | + location.path = URI.parse(response['location']).path |
| 17 | + location |
| 18 | + else |
| 19 | + response.error! |
| 20 | + end |
| 21 | + end |
| 22 | + local_root = 'lib/penthouse' |
| 23 | + remote_root = "#{fetch['https://unpkg.com/penthouse']}/lib" |
| 24 | + |
| 25 | + File.write File.join(local_root, 'timeago.js'), |
| 26 | + fetch["#{remote_root}/dist/timeago.js"] |
| 27 | + File.write File.join(local_root, 'timeago.locales.js'), |
| 28 | + fetch["#{remote_root}/dist/timeago.locales.min.js"] |
| 29 | + src = fetch.call.body |
| 30 | + # Remove the source mapping comment as this gem does not bundle source maps: |
| 31 | + src.sub!(%r{^//# sourceMappingURL=.*\n\z}, '') |
| 32 | + File.write(File.join('lib/penthouse/penthouse.js'), src) |
| 33 | + |
| 34 | + version_path = File.join('lib/popper_js/version.rb') |
| 35 | + File.write version_path, |
| 36 | + File.read(version_path) |
| 37 | + .sub(/VERSION = '.*?'/, |
| 38 | + "VERSION = '#{uri.path.split('@')[-1]}'") |
| 39 | + |
| 40 | + STDERR.puts "Updated from #{uri}" |
| 41 | +end |
0 commit comments