lib/jets/builders/handler_generator.rb in jets-4.0.12 vs lib/jets/builders/handler_generator.rb in jets-5.0.0.beta1

- old
+ new

@@ -45,28 +45,54 @@ generate_handler(vars) end end end + def generate_handler(vars) + template = handler_template(vars) + result = evaluate_template(template, vars) + dest = "#{tmp_code}/#{vars.dest_path}" + + # Dont generate handles for all controllers if one_lambda_for_all_controllers + # Instead generate the same handler and use it. Writing to the same file + # multiple times but this is a simple way to implement this. + # It also keeps the logical in one spot. + if Jets.config.cfn.build.controllers == 'one_lambda_for_all_controllers' && + dest.include?('handlers/controllers') + dest = "handlers/controller.rb" + end + + FileUtils.mkdir_p(File.dirname(dest)) + IO.write(dest, result) + end + + def handler_template(vars) + if vars.process_type == "controller" + Jets.config.cfn.build.controllers + ".rb" # IE: templates/handlers/one_lambda_for_all_controllers.rb + else # job, rule, etc + "one_lambda_per_method.rb" + end + end + # source_path: app/functions/simple.rb def copy_simple_function(source_path) # Handler: handlers/controllers/posts_controller.handle dest_path = source_path.sub('app/functions', 'handlers/functions') FileUtils.mkdir_p(File.dirname(dest_path)) FileUtils.cp(source_path, dest_path) end def app_files - Jets::Commands::Build.app_files + Jets::Cfn::Builder.app_files end def poly_shims missing = [] app_files.each do |path| vars = Jets::Builders::ShimVars::App.new(path) - poly_tasks = vars.klass.tasks.select { |t| t.lang != :ruby } + poly_tasks = vars.klass.definitions.select { |d| d.lang != :ruby } poly_tasks.each do |task| source_path = get_source_path(path, task) if File.exist?(source_path) native_function(path, task) else @@ -98,21 +124,22 @@ jets_base_path if Jets.custom_domain? s3_bucket_config if Jets.s3_event? end def jets_base_path - copy_function_template("functions/jets/base_path.rb", stage_name: Jets::Resource::ApiGateway::Deployment.stage_name) + copy_function_template("functions/jets/base_path.rb", stage_name: Jets::Cfn::Resource::ApiGateway::Deployment.stage_name) + copy_function_template("functions/jets/base_path_mapping.rb") end def s3_bucket_config copy_function_template("shared/functions/jets/s3_bucket_config.rb") end # Copy code from internal folder to materialized app code def copy_function_template(path, vars={}) - internal = File.expand_path("../internal", File.dirname(__FILE__)) - src = "#{internal}/app/#{path}" + engines_internal_path = File.expand_path("../../../engines/internal", __dir__) + src = "#{engines_internal_path}/app/#{path}" result = Jets::Erb.result(src, vars) dest = "#{tmp_code}/handlers/#{path}" FileUtils.mkdir_p(File.dirname(dest)) IO.write(dest, result) end @@ -122,11 +149,11 @@ return if fun.internal? source_path = fun.source_file unless source_path attributes = fun.template.values.first - function_name = attributes['Properties']['FunctionName'] + function_name = attributes[:Properties][:FunctionName] puts "WARN: missing source file for: '#{function_name}' function".color(:yellow) return end dest_path = "#{tmp_code}/#{fun.handler_dest}" @@ -164,18 +191,11 @@ dest = "#{tmp_code}/handlers/shim.js" FileUtils.mkdir_p(File.dirname(dest)) IO.write(dest, result) end - def generate_handler(vars) - result = evaluate_template("handler.rb", vars) - dest = "#{tmp_code}/#{vars.dest_path}" - FileUtils.mkdir_p(File.dirname(dest)) - IO.write(dest, result) - end - def evaluate_template(template_file, vars) - template_path = File.expand_path("../templates/#{template_file}", __FILE__) + template_path = File.expand_path("../templates/handlers/#{template_file}", __FILE__) Jets::Erb.result(template_path, vars: vars) end # TODO: move CodeBuilder.tmp_code to a common level for HandlerGenerator and CodeBuilder def tmp_code