Class: FaaStRuby::Local::OpalProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/faastruby/local/processors/opal.rb

Instance Attribute Summary

Attributes inherited from Processor

#queue, #thread

Instance Method Summary collapse

Methods inherited from Processor

#add_ignore, #add_thread, #get_thread, #initialize, #kill_thread, #present_in_ignore_list?, #remove_ignore, #remove_thread_record, #run, #should_ignore?, #start, #start_thread

Methods included from Logger

#debug, puts, #puts

Constructor Details

This class inherits a constructor from FaaStRuby::Local::Processor

Instance Method Details

#added(event) ⇒ Object



5
6
7
8
9
# File 'lib/faastruby/local/processors/opal.rb', line 5

def added(event)
  debug "added(#{event.inspect})"
  # does nothing
  return true
end

#compile(event) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/faastruby/local/processors/opal.rb', line 49

def compile(event)
  debug "compile"
  js_path = event.relative_path_dirname.sub(/^#{Local.opal_dir}\//, "")
  FileUtils.mkdir_p("#{Local.opal_js_destination}/#{js_path}")
  compile_cmd = [
    "opal",
    "-I",
    "#{event.dirname}",
    "--gem",
    'jquery',
    "--gem",
    'opal-jquery',
    "--compile",
    "#{event.dirname}/main.rb",
    ">",
    "#{Local.opal_js_destination}/#{js_path}/main.js"
  ]
  # run it:
  run(event.dirname, 'compile') {`#{compile_cmd.join(' ')}`; puts "Done"}
  puts "Running: #{compile_cmd.join(' ')}"
end

#delete_recursive_folder(entry) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/faastruby/local/processors/opal.rb', line 39

def delete_recursive_folder(entry)
  dirname = File.dirname(entry)
  return if dirname == Local.opal_js_destination
  glob = Dir.glob("#{dirname}/*")
  return if glob.any?
  puts "Deleting: #{dirname}"
  FileUtils.rmdir(dirname)
  delete_recursive_folder(dirname)
end

#first_parent_of(entry, event_type) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/faastruby/local/processors/opal.rb', line 71

def first_parent_of(entry, event_type)
  debug "first_parent_of(#{entry.inspect})"
  absolute_folder = get_parent_folder_for(entry)
  if event_type == :removed
    name = absolute_folder.dup
    name.slice!("#{Local.opal_dir}/")
  end
  absolute_folder
end

#get_parent_folder_for(entry) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/faastruby/local/processors/opal.rb', line 81

def get_parent_folder_for(entry)
  return File.dirname(entry) if File.basename(entry) == 'main.rb'
  debug "get_parent_folder_for(#{entry.inspect})"
  dirname = File.dirname(entry)
  raise MissingConfigurationFileError.new("ERROR: Could not determine the parent. Make sure your opal folders a main file 'main.rb'.") if dirname == SERVER_ROOT
  return dirname if File.file?("#{dirname}/main.rb")
  get_parent_folder_for(dirname)
end

#modified(event) ⇒ Object



11
12
13
14
15
16
# File 'lib/faastruby/local/processors/opal.rb', line 11

def modified(event)
  debug "modified(#{event.inspect})"
  # This should trigger
  # - compile to JS
  compile(event)
end

#remove_from_public(event) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/faastruby/local/processors/opal.rb', line 29

def remove_from_public(event)
  delete_path = event.relative_path_dirname.sub(/^#{Local.opal_dir}\//, "#{Local.public_dir}/")
  # return false unless delete_path.match(/^#{Local.public_dir}\//)
  return false if delete_path.nil? || delete_path.match(/^\s*$/)
  puts "Deleting: #{Local.opal_js_destination}/#{delete_path}"
  FileUtils.rm_rf("#{Local.opal_js_destination}/#{delete_path}")
  dir = "#{Local.opal_js_destination}/#{delete_path}"
  delete_recursive_folder(dir)
end

#removed(event) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/faastruby/local/processors/opal.rb', line 18

def removed(event)
  debug "removed(#{event.inspect})"
  # This should trigger
  # - Remove from workspace
  if event.file_is_opal_main?
    remove_from_public(event)
  else
    compile(event)
  end
end