This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Download and compile external assets during deployment
- Loading branch information
Showing
7 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
namespace :custom_assets do | ||
# Downloads a tar from a URL, extracts its contents into ./tmp, | ||
# and returns a colon separated list of paths belonging to the first level directories | ||
# inside the original tar file. | ||
# | ||
# Params: | ||
# * url: any http[s] url. Optionally it can include a tag <branch> that will be replaced with | ||
# the contents of the branch param | ||
# * credentials: http basic auth credentials. The format should be user:password | ||
# * branch: Mainly for Github URLs usage. The content of this param will replace the <branch> tag | ||
# in the url param | ||
desc "Download external assets" | ||
task :download, [:url, :credentials, :branch] do |t, args| | ||
Bundler.require :default, :development | ||
|
||
url = args[:url] | ||
credentials = args[:credentials] | ||
branch = args[:branch] | ||
tar_file_path = "./tmp/assets.tar" | ||
|
||
raise ArgumentError.new("usage: rake custom_assets:download[<url>,<credentials>,<branch>]") if url.blank? | ||
|
||
if branch.present? | ||
url = url.gsub("<branch>", branch) | ||
end | ||
|
||
# Download tar file -------------------------- | ||
http_options = {} | ||
if credentials.present? | ||
u, p = credentials.split(":") | ||
http_options[:basic_auth] = { username: u, password: p } | ||
end | ||
response = HTTParty.get url, http_options | ||
|
||
if !response.success? | ||
raise "HTTP while trying to download assets #{response.inspect}" | ||
end | ||
|
||
|
||
# Extract tar file -------------------------- | ||
File.open(tar_file_path, "w+b") do |file| | ||
file.write response.body | ||
end | ||
|
||
untar_cmd = "tar -xf #{tar_file_path} -C ./tmp" | ||
result = system(untar_cmd) | ||
raise "Error running `#{untar_cmd}`" unless result | ||
|
||
# Print dir where assets were extracted ----- | ||
base_assets_dir = `tar -tf #{tar_file_path} | head -n 1` | ||
base_assets_dir.strip! | ||
base_assets_path = Pathname.new("./tmp").join(base_assets_dir) | ||
|
||
assets_dirs = Dir.glob(base_assets_path.join("*")).select { |f| File.directory? f } | ||
puts assets_dirs.join(":") | ||
end | ||
end |