Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch redirected urls #900

Merged
merged 3 commits into from
Oct 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions src/amber/cli/recipes/recipe_fetcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module Amber::Recipes
getter kind : String # one of the supported kinds [app, controller, scaffold]
getter name : String | Nil
getter directory : String
getter template_path : String

def initialize(@kind : String, @name : String | Nil)
@directory = "#{Dir.current}/#{@name}/#{@kind}"
@template_path = "#{AMBER_RECIPE_FOLDER}/#{@name}"
end

def fetch
Expand All @@ -20,57 +22,66 @@ module Amber::Recipes
return "#{@name}/#{@kind}"
end

template_path = "#{AMBER_RECIPE_FOLDER}/#{@name}"

if Dir.exists?("#{template_path}/#{@kind}")
return "#{template_path}/#{@kind}"
if Dir.exists?("#{@template_path}/#{@kind}")
return "#{@template_path}/#{@kind}"
end

if @name.not_nil!.downcase.starts_with?("http") && @name.not_nil!.downcase.ends_with?(".zip")
return fetch_zip @name.as(String), template_path
if (name = @name) && name.downcase.starts_with?("http") && name.downcase.ends_with?(".zip")
return fetch_zip name
end

return fetch_url template_path
return fetch_url
end

def recipe_source
CLI.config.recipe_source || "https://github.com/amberframework/recipes/releases/download/dist/"
end

def fetch_zip(url : String, template_path : String)
def fetch_zip(url : String)
# download the recipe zip file from the github repository
HTTP::Client.get(url) do |response|
if response.status_code != 200
if response.status_code == 302
# download the recipe zip frile from redirected url
if redirection_url = response.headers["Location"]?
HTTP::Client.get(redirection_url) do |redirected_response|
save_zip(redirected_response)
end
end
elsif response.status_code != 200
CLI.logger.error "Could not find the recipe #{@name} : #{response.status_code} #{response.status_message}", "Generate", :light_red
return nil
end

# make a temp directory and expand the zip into the temp directory
Dir.mkdir_p(template_path)
save_zip(response)
end
end

Zip::Reader.open(response.body_io) do |zip|
zip.each_entry do |entry|
path = "#{template_path}/#{entry.filename}"
if entry.dir?
Dir.mkdir_p(path)
else
File.write(path, entry.io.gets_to_end)
end
def save_zip(response : HTTP::Client::Response)
# make a temp directory and expand the zip into the temp directory
Dir.mkdir_p(@template_path)

Zip::Reader.open(response.body_io) do |zip|
zip.each_entry do |entry|
path = "#{@template_path}/#{entry.filename}"
if entry.dir?
Dir.mkdir_p(path)
else
File.write(path, entry.io.gets_to_end)
end
end
end

# return the path of the template directory
if Dir.exists?("#{template_path}/#{@kind}")
return "#{template_path}/#{@kind}"
if Dir.exists?("#{@template_path}/#{@kind}")
return "#{@template_path}/#{@kind}"
end

CLI.logger.error "Cannot generate #{@kind} from #{@name} recipe", "Generate", :light_red
return nil
end

def fetch_url(template_path : String)
return fetch_zip "#{recipe_source}/#{@name}.zip", template_path
def fetch_url
return fetch_zip "#{recipe_source}/#{@name}.zip"
end
end
end