Class: Jets::Builders::HandlerGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/builders/handler_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

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_filesObject



31
32
33
# File 'lib/jets/builders/handler_generator.rb', line 31

def app_files
  Jets::Commands::Build.app_files
end

#app_ruby_shimsObject



23
24
25
26
27
28
29
# File 'lib/jets/builders/handler_generator.rb', line 23

def app_ruby_shims
  app_files.each do |path|
    # Generates one big node shim for a entire controller.
    vars = Jets::Builders::ShimVars::App.new(path)
    generate_handler(vars)
  end
end

#buildObject



16
17
18
19
20
21
# File 'lib/jets/builders/handler_generator.rb', line 16

def build
  common_base_shim
  app_ruby_shims
  poly_shims
  shared_shims
end

#common_base_shimObject



106
107
108
109
110
111
112
# File 'lib/jets/builders/handler_generator.rb', line 106

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_source_as_handler(fun) ⇒ Object

app/shared/functions/kevin.py => /tmp/jets/demo/app_root/handlers/shared/functions/kevin.py



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jets/builders/handler_generator.rb', line 71

def copy_source_as_handler(fun)
  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".colorize(: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



121
122
123
124
# File 'lib/jets/builders/handler_generator.rb', line 121

def evaluate_template(template_file, vars)
  template_path = File.expand_path("../templates/#{template_file}", __FILE__)
  Jets::Erb.result(template_path, vars: vars)
end

#generate_handler(vars) ⇒ Object



114
115
116
117
118
119
# File 'lib/jets/builders/handler_generator.rb', line 114

def generate_handler(vars)
  result = evaluate_template("handler.js", vars)
  dest = "#{tmp_code}/#{vars.js_path}"
  FileUtils.mkdir_p(File.dirname(dest))
  IO.write(dest, result)
end

#get_source_path(original_path, task) ⇒ Object



85
86
87
88
89
90
# File 'lib/jets/builders/handler_generator.rb', line 85

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

#native_function(original_path, task) ⇒ Object

Builds and copies over the native source code: python or node



93
94
95
96
97
98
99
# File 'lib/jets/builders/handler_generator.rb', line 93

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_shimsObject



35
36
37
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 35

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.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".colorize(:red)
      puts missing
      exit 1
    end
  end
end

#shared_ruby_shim(fun) ⇒ Object



101
102
103
104
# File 'lib/jets/builders/handler_generator.rb', line 101

def shared_ruby_shim(fun)
  vars = Jets::Builders::ShimVars::Shared.new(fun)
  generate_handler(vars)
end

#shared_shimsObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jets/builders/handler_generator.rb', line 58

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_codeObject

TODO: move CodeBuilder.tmp_code to a common level for HandlerGenerator and CodeBuilder



127
128
129
# File 'lib/jets/builders/handler_generator.rb', line 127

def tmp_code
  "#{Jets.build_root}/#{CodeBuilder.tmp_code}"
end