Skip to content

Instantly share code, notes, and snippets.

@sj26
Created November 19, 2018 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sj26/db20012bd2353800275e94c65a43d80b to your computer and use it in GitHub Desktop.
Save sj26/db20012bd2353800275e94c65a43d80b to your computer and use it in GitHub Desktop.
Setup a mirror to use with $HOMEBREW_ARTIFACT_DOMAIN
# Homebrew has $HOMEBREW_ARTIFACT_DOMAIN which allows using a mirror for downloads:
#
# https://docs.brew.sh/Manpage#environment
#
# But no easy way to create such a mirror. This command which creates a mirror from
# all core formulae downloads. Pop it in /usr/local/Homebrew/Library/Homebrew/cmd/
# with a `chmod +x` and run `brew mirror-artifacts`.
require "formula"
base_path = Pathname.new(ENV.fetch("HOMEBREW_ARTIFACT_PATH") { Dir.pwd })
ohai "Mirroring all formula downloads to: #{path}"
Formula.core_files.each do |fi|
begin
f = Formula[fi]
rescue Exception => e
opoo "#{fi}: something goes wrong."
puts e.message
puts e.backtrace.inspect
next
end
if f.downloader.is_a? CurlDownloadStrategy
ohai "Mirroring #{f}"
url = f.downloader.url
uri = URI.parse(url)
path = base_path.join(uri.host, uri.path.delete_prefix("/"))
path.parent.mkpath
curl_download url, to: path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment