Class: Jets::Builders::HandlerGenerator

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ HandlerGenerator

Returns a new instance of HandlerGenerator.



12
13
14
# File 'lib/jets/builders/handler_generator.rb', line 12

def initialize(path)
  @path = path
end

Instance Method Details

#generateObject



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

def generate
  poly_shims
  ruby_node_shim
end

#get_source_path(original_path, task) ⇒ Object



42
43
44
45
46
47
# File 'lib/jets/builders/handler_generator.rb', line 42

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



50
51
52
53
54
55
56
# File 'lib/jets/builders/handler_generator.rb', line 50

def native_function(original_path, task)
  source_path = get_source_path(original_path, task)
  # Handler: handlers/controllers/posts_controller.handle
  dest_path = "#{tmp_app_root}/#{task.handler_path}"
  FileUtils.mkdir_p(File.dirname(dest_path))
  FileUtils.cp(source_path, dest_path)
end

#poly_shimsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets/builders/handler_generator.rb', line 21

def poly_shims
  missing = []

  deducer = Jets::Builders::Deducer.new(@path)
  poly_tasks = deducer.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

#ruby_node_shimObject

Generates one big node shim for a entire controller.



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

def ruby_node_shim
  deducer = Jets::Builders::Deducer.new(@path)

  js_path = "#{tmp_app_root}/#{deducer.js_path}"
  FileUtils.mkdir_p(File.dirname(js_path))

  template_path = File.expand_path('../node-shim.js', __FILE__)
  result = Jets::Erb.result(template_path, deducer: deducer)

  IO.write(js_path, result)
end

#tmp_app_rootObject

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



72
73
74
# File 'lib/jets/builders/handler_generator.rb', line 72

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