Class: JSPreprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/JSPreprocessor.rb

Instance Method Summary collapse

Constructor Details

#initializeJSPreprocessor

Returns a new instance of JSPreprocessor.



3
4
5
# File 'lib/JSPreprocessor.rb', line 3

def initialize
  @defines = []
end

Instance Method Details

#process(cwd, main_file) ⇒ Object



7
8
9
10
11
# File 'lib/JSPreprocessor.rb', line 7

def process(cwd, main_file)
  main = File.open(cwd + main_file).read.split "\n"
  compiled_file = recurse_files(cwd, main).join "\n"
  replace_defines compiled_file
end

#recurse_files(cwd, file) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/JSPreprocessor.rb', line 13

def recurse_files(cwd, file)
  file.each_with_index do |line, index|
    if line.include? '#include'
      to_include = cwd + line[10..-2] + '.js'
      next_wd = Pathname.new(to_include).dirname.to_s + '/'
      file[index] = recurse_files next_wd, File.open(to_include).read.split("\n")
    elsif line.include? '#define'
      @defines << line[8..-1].split(' ')
      file.delete_at index
    end
  end
end

#replace_defines(file) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/JSPreprocessor.rb', line 26

def replace_defines(file)
  @defines.each do |definition|
    file.gsub! definition[0], definition[1]
  end

  file
end