Class: Jets::Builders::HandlerGenerator
- Inherits:
-
Object
- Object
- Jets::Builders::HandlerGenerator
- Defined in:
- lib/jets/builders/handler_generator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #app_files ⇒ Object
- #app_ruby_shims ⇒ Object
- #build ⇒ Object
- #common_base_shim ⇒ Object
-
#copy_function_template(path, vars = {}) ⇒ Object
Copy code from internal folder to materialized app code.
-
#copy_simple_function(source_path) ⇒ Object
source_path: app/functions/simple.rb.
-
#copy_source_as_handler(fun) ⇒ Object
app/shared/functions/kevin.py => /tmp/jets/demo/app_root/handlers/shared/functions/kevin.py.
- #evaluate_template(template_file, vars) ⇒ Object
-
#generate_data_yaml ⇒ Object
The handlers/data.yml is used by the shims.
- #generate_handler(vars) ⇒ Object
- #get_source_path(original_path, task) ⇒ Object
- #handler_template(vars) ⇒ Object
- #internal_shims ⇒ Object
- #jets_base_path ⇒ Object
-
#native_function(original_path, task) ⇒ Object
Builds and copies over the native source code: python or node.
- #poly_shims ⇒ Object
- #s3_bucket_config ⇒ Object
- #shared_ruby_shim(fun) ⇒ Object
- #shared_shims ⇒ Object
-
#tmp_code ⇒ Object
TODO: move CodeBuilder.tmp_code to a common level for HandlerGenerator and CodeBuilder.
Class Method Details
.build! ⇒ Object
12 13 14 |
# File 'lib/jets/builders/handler_generator.rb', line 12 def self.build! new.build end |
Instance Method Details
#app_files ⇒ Object
92 93 94 |
# File 'lib/jets/builders/handler_generator.rb', line 92 def app_files Jets::Cfn::Builder.app_files end |
#app_ruby_shims ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jets/builders/handler_generator.rb', line 38 def app_ruby_shims app_files.each do |path| # Generates one shim for each app class: controller, job, etc vars = Jets::Builders::ShimVars::App.new(path) if path.include?('app/functions') copy_simple_function(path) else generate_handler(vars) end end if app_files.empty? && Jets.gem_layer? # When there are no app files, still need to generate a shim for JetsController lib = File.('../../..', __dir__) path = "#{lib}/engines/internal/app/controllers/jets/public_controller.rb" vars = Jets::Builders::ShimVars::App.new(path) generate_handler(vars) end end |
#build ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/jets/builders/handler_generator.rb', line 16 def build generate_data_yaml app_ruby_shims poly_shims shared_shims internal_shims end |
#common_base_shim ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/jets/builders/handler_generator.rb', line 196 def common_base_shim vars = Jets::Builders::ShimVars::Base.new result = evaluate_template("shim.js", vars) dest = "#{tmp_code}/handlers/shim.js" FileUtils.mkdir_p(File.dirname(dest)) IO.write(dest, result) end |
#copy_function_template(path, vars = {}) ⇒ Object
Copy code from internal folder to materialized app code
146 147 148 149 150 151 152 153 |
# File 'lib/jets/builders/handler_generator.rb', line 146 def copy_function_template(path, vars={}) engines_internal_path = File.("../../../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 |
#copy_simple_function(source_path) ⇒ Object
source_path: app/functions/simple.rb
85 86 87 88 89 90 |
# File 'lib/jets/builders/handler_generator.rb', line 85 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 |
#copy_source_as_handler(fun) ⇒ Object
app/shared/functions/kevin.py => /tmp/jets/demo/app_root/handlers/shared/functions/kevin.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/jets/builders/handler_generator.rb', line 156 def copy_source_as_handler(fun) return if fun.internal? source_path = fun.source_file unless source_path attributes = fun.template.values.first 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}" FileUtils.mkdir_p(File.dirname(dest_path)) FileUtils.cp(source_path, dest_path) end |
#evaluate_template(template_file, vars) ⇒ Object
204 205 206 207 |
# File 'lib/jets/builders/handler_generator.rb', line 204 def evaluate_template(template_file, vars) template_path = File.("../templates/handlers/#{template_file}", __FILE__) Jets::Erb.result(template_path, vars: vars) end |
#generate_data_yaml ⇒ Object
The handlers/data.yml is used by the shims
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jets/builders/handler_generator.rb', line 25 def generate_data_yaml vars = Jets::Builders::ShimVars::Base.new data = { "s3_bucket" => vars.s3_bucket } data["rack_zip"] = vars.rack_zip if vars.rack_zip content = YAML.dump(data) path = "#{tmp_code}/handlers/data.yml" FileUtils.mkdir_p(File.dirname(path)) IO.write(path, content) end |
#generate_handler(vars) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jets/builders/handler_generator.rb', line 58 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 |
#get_source_path(original_path, task) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/jets/builders/handler_generator.rb', line 172 def get_source_path(original_path, task) folder = original_path.sub(/\.rb$/,'') lang_folder = "#{folder}/#{task.lang}" root = Jets.root unless original_path.include?("lib/jets/internal") "#{root}/#{lang_folder}/#{task.meth}#{task.lang_ext}" end |
#handler_template(vars) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/jets/builders/handler_generator.rb', line 76 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 |
#internal_shims ⇒ Object
131 132 133 134 |
# File 'lib/jets/builders/handler_generator.rb', line 131 def internal_shims jets_base_path if Jets.custom_domain? s3_bucket_config if Jets.s3_events? end |
#jets_base_path ⇒ Object
136 137 138 139 |
# File 'lib/jets/builders/handler_generator.rb', line 136 def jets_base_path 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 |
#native_function(original_path, task) ⇒ Object
Builds and copies over the native source code: python or node
180 181 182 183 184 185 186 |
# File 'lib/jets/builders/handler_generator.rb', line 180 def native_function(original_path, task) source_path = get_source_path(original_path, task) # Handler: handlers/controllers/posts_controller.handle dest_path = "#{tmp_code}/#{task.handler_path}" FileUtils.mkdir_p(File.dirname(dest_path)) FileUtils.cp(source_path, dest_path) end |
#poly_shims ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/jets/builders/handler_generator.rb', line 96 def poly_shims missing = [] app_files.each do |path| vars = Jets::Builders::ShimVars::App.new(path) 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 missing << source_path end end unless missing.empty? puts "ERROR: Missing source files. Please make sure these source files exist or remove their declarations".color(:red) puts missing exit 1 end end end |
#s3_bucket_config ⇒ Object
141 142 143 |
# File 'lib/jets/builders/handler_generator.rb', line 141 def s3_bucket_config copy_function_template("shared/functions/jets/s3_bucket_config.rb") end |
#shared_ruby_shim(fun) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/jets/builders/handler_generator.rb', line 188 def shared_ruby_shim(fun) # Cant use native_function because that requires task. Just re-implement dest_path = fun.handler_dest source_path = dest_path.sub(/^handlers/,'app') FileUtils.mkdir_p(File.dirname(dest_path)) FileUtils.cp(source_path, dest_path) end |
#shared_shims ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/jets/builders/handler_generator.rb', line 119 def shared_shims Jets::Stack.subclasses.each do |subclass| subclass.functions.each do |fun| if fun.lang.to_s == "ruby" shared_ruby_shim(fun) else copy_source_as_handler(fun) end end end end |
#tmp_code ⇒ Object
TODO: move CodeBuilder.tmp_code to a common level for HandlerGenerator and CodeBuilder
210 211 212 |
# File 'lib/jets/builders/handler_generator.rb', line 210 def tmp_code "#{Jets.build_root}/#{CodeBuilder.tmp_code}" end |