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

[CLI] change the default recipe source URL #764

Merged
merged 12 commits into from
May 27, 2018
43 changes: 22 additions & 21 deletions spec/amber/cli/recipes/recipe_fetcher_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Amber::Recipes
Spec.before_each do
Dir.mkdir_p("./mydefault/app")
Dir.mkdir_p("./mydefault/controller")
Dir.mkdir_p("./mydefault/model")
Dir.mkdir_p("./mydefault/scaffold")
end

Expand All @@ -25,13 +26,26 @@ module Amber::Recipes
template.should_not be nil
template.should match(/.+mydefault\/controller$/)
end

it "should use a local model folder" do
template = RecipeFetcher.new("model", "mydefault").fetch
template.should_not be nil
template.should match(/.+mydefault\/model$/)
end

it "should use a local scaffold folder" do
template = RecipeFetcher.new("scaffold", "mydefault").fetch
template.should_not be nil
template.should match(/.+mydefault\/scaffold$/)
end
end
end

context "using the recipe cache in the current directory" do
context "using the recipe cache" do
Spec.before_each do
Dir.mkdir_p("#{AMBER_RECIPE_FOLDER}/mydefault/app")
Dir.mkdir_p("#{AMBER_RECIPE_FOLDER}/mydefault/controller")
Dir.mkdir_p("#{AMBER_RECIPE_FOLDER}/mydefault/model")
Dir.mkdir_p("#{AMBER_RECIPE_FOLDER}/mydefault/scaffold")
end

Expand All @@ -51,33 +65,20 @@ module Amber::Recipes
template.should_not be nil
template.should match(/.+mydefault\/controller$/)
end
end
end

context "using the recipe cache in the parent directory" do
Spec.before_each do
Dir.mkdir_p("../#{AMBER_RECIPE_FOLDER}/mydefault/app")
Dir.mkdir_p("../#{AMBER_RECIPE_FOLDER}/mydefault/controller")
Dir.mkdir_p("../#{AMBER_RECIPE_FOLDER}/mydefault/scaffold")
end

Spec.after_each do
FileUtils.rm_rf("../#{AMBER_RECIPE_FOLDER}/mydefault")
end

describe "RecipeFetcher" do
it "should use a cached app folder" do
template = RecipeFetcher.new("app", "mydefault").fetch
it "should use a cached model folder" do
template = RecipeFetcher.new("model", "mydefault").fetch
template.should_not be nil
template.should match(/.+mydefault\/app$/)
template.should match(/.+mydefault\/model$/)
end

it "should use a cached controller folder" do
template = RecipeFetcher.new("controller", "mydefault").fetch
it "should use a cached scaffold folder" do
template = RecipeFetcher.new("scaffold", "mydefault").fetch
template.should_not be nil
template.should match(/.+mydefault\/controller$/)
template.should match(/.+mydefault\/scaffold$/)
end
end
end

end
end
10 changes: 10 additions & 0 deletions spec/amber/cli/recipes/recipe_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Amber::Recipes
Spec.before_each do
Dir.mkdir_p("./mydefault/app")
Dir.mkdir_p("./mydefault/controller")
Dir.mkdir_p("./mydefault/model")
Dir.mkdir_p("./mydefault/scaffold")
end

Spec.after_each do
Expand All @@ -21,6 +23,14 @@ module Amber::Recipes
it "should return true for default controller" do
Recipe.can_generate?("controller", recipe).should eq true
end

it "should return true for default model" do
Recipe.can_generate?("model", recipe).should eq true
end

it "should return true for default scaffold" do
Recipe.can_generate?("scaffold", recipe).should eq true
end
end
end
end
17 changes: 13 additions & 4 deletions src/amber/cli/recipes/recipe_fetcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Amber::Recipes
end

def fetch

if Dir.exists?(@directory)
return @directory
end
Expand All @@ -26,18 +27,22 @@ module Amber::Recipes
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
end

return fetch_url template_path
end

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

def fetch_url(template_path)
def fetch_zip(url : String, template_path : String)
# download the recipe zip file from the github repository
HTTP::Client.get("#{recipe_source}/#{@name}.zip") do |response|
HTTP::Client.get(url) do |response|
if response.status_code != 200
CLI.logger.error "Could not find that recipe #{@name}", "Generate", :light_red
CLI.logger.error "Could not find the recipe #{@name} : #{response.status_code} #{response.status_message}", "Generate", :light_red
return nil
end

Expand All @@ -64,5 +69,9 @@ module Amber::Recipes
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
end
end
end