From 44dd279f93443414f29eaefb35140c9467f0fb80 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 5 Feb 2022 15:17:13 -0500 Subject: [PATCH] wip --- lib/rscons.rb | 2 ++ lib/rscons/script.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/rscons.rb b/lib/rscons.rb index bfd14a1..28ea715 100644 --- a/lib/rscons.rb +++ b/lib/rscons.rb @@ -1,3 +1,5 @@ +require "net/http" +require "digest" require_relative "rscons/ansi" require_relative "rscons/application" require_relative "rscons/basic_environment" diff --git a/lib/rscons/script.rb b/lib/rscons/script.rb index 799a264..a25c5b9 100644 --- a/lib/rscons/script.rb +++ b/lib/rscons/script.rb @@ -37,6 +37,39 @@ module Rscons end.sort end + # Download a file. + # + # @param url [String] + # URL. + # @param dest [String] + # Path to where to save the file. + # @param options [Hash] + # Options. + # @option options [String] :sha256sum + # Expected file checksum. + # + # @return [void] + def download(url, dest, options = {}) + if File.exist?(dest) && options[:sha256sum] + if Digest::SHA2.hexdigest(File.binread(dest)) == options[:sha256sum] + # Destination file already exists and has the expected checksum. + return + end + end + uri = URI(url) + http = Net::HTTP.new(uri.host) + digest = Digest::SHA2.new + File.open(dest, "wb") do |fh| + http.get(uri.path) do |data| + fh.write(data) + digest << data + end + end + if options[:sha256sum] && options[:sha256sum] != digest.hexdigest + raise RsconsError.new("Unexpected checksum on #{dest}") + end + end + # Construct a task parameter. # # @param name [String]